Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(packages/sui-studio): Add new param to use explicit import #1460

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/sui-studio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ Use `-C path` or `--context path` parameter to generate the component with a giv
$ npx sui-studio generate house window -C ./myCustomContext.js
```

#### Explicit import

Use `-E` or `--explicit` parameter to generate a component in a file with its same name and an index re-export.

```sh
$ npx sui-studio generate house window -E
```

### Use new compiler

We're migrating to `swc` so you could generate the new component with the expected `prepare` field by using the flag `--swc` or `-W`.
Expand Down
39 changes: 25 additions & 14 deletions packages/sui-studio/bin/sui-studio-generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ program
.option('-P, --prefix <prefix>', 'add prefix for this component')
.option('-S, --scope <scope>', 'add scope for this component')
.option('-W, --swc', 'Use the new SWC compiler', false)
.option('-E, --explicit', 'Use explicit import', false)
.on('--help', () => {
console.log(' Examples:')
console.log('')
Expand Down Expand Up @@ -52,6 +53,7 @@ const componentInPascal = toPascalCase(
const COMPONENT_DIR = `/components/${category}/${component}/`
const COMPONENT_PATH = `${BASE_DIR}${COMPONENT_DIR}`
const COMPONENT_ENTRY_JS_POINT_FILE = `${COMPONENT_PATH}src/index.js`
const EXPLICIT_COMPONENT_JS_POINT_FILE = `${COMPONENT_PATH}src/${componentInPascal}.js`
const COMPONENT_PACKAGE_JSON_FILE = `${COMPONENT_PATH}package.json`
const COMPONENT_PACKAGE_GITIGNORE_FILE = `${COMPONENT_PATH}.gitignore`
const COMPONENT_PACKAGE_NPMIGNORE_FILE = `${COMPONENT_PATH}.npmignore`
Expand All @@ -66,7 +68,7 @@ const COMPONENT_CONTEXT_FILE = `${DEMO_DIR}context.js`
const TEST_DIR = `${COMPONENT_PATH}/test/`
const COMPONENT_TEST_FILE = `${TEST_DIR}index.test.js`

const {context, scope, prefix = 'sui', swc} = program.opts()
const {context, scope, prefix = 'sui', swc, explicit} = program.opts()
const packageScope = scope ? `@${scope}/` : ''
const packageCategory = category ? `${toKebabCase(category)}-` : ''
const packageName = `${packageScope}${prefix}-${packageCategory}${toKebabCase(
Expand Down Expand Up @@ -139,6 +141,25 @@ describe${context ? '.context.default' : ''}('${componentInPascal}', ${
})
`

const componentTemplate = `// import PropTypes from 'prop-types'

export default function ${componentInPascal}() {
return (
<div className="${prefix}-${componentInPascal}">
<h1>${componentInPascal}</h1>
</div>
)
}

${componentInPascal}.displayName = '${componentInPascal}'
${componentInPascal}.propTypes = {}
`

const explicitImportTemplate = `import ${componentInPascal} from './${componentInPascal}.js'

export default ${componentInPascal}
`
Comment on lines +158 to +161
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can go with the shorter version that mixes the rexport of default and named exports.

Suggested change
const explicitImportTemplate = `import ${componentInPascal} from './${componentInPascal}.js'
export default ${componentInPascal}
`
const explicitImportTemplate = `export {default} from './${componentInPascal}.js'
`

Then if we want to export some named (for example: foo and bar) things inside the component would be much shorter:

const explicitImportTemplate = export {default, foo, bar} from './${componentInPascal}.js'`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a proposal in stage 1, I checked and we use @babel/plugin-proposal-export-default-from to support it. If we're sure our infra will keep it, I'm ok with the change, but just want to double check 🙂


const defaultContext = `module.exports = {
default: {
i18n: {
Expand Down Expand Up @@ -225,21 +246,11 @@ test

writeFile(
COMPONENT_ENTRY_JS_POINT_FILE,
`// import PropTypes from 'prop-types'

export default function ${componentInPascal}() {
return (
<div className="${prefix}-${componentInPascal}">
<h1>${componentInPascal}</h1>
</div>
)
}

${componentInPascal}.displayName = '${componentInPascal}'
${componentInPascal}.propTypes = {}
`
explicit ? explicitImportTemplate : componentTemplate
),

explicit && writeFile(EXPLICIT_COMPONENT_JS_POINT_FILE, componentTemplate),

writeFile(
COMPONENT_ENTRY_SCSS_POINT_FILE,
`@import '~@s-ui/theme/lib/index';
Expand Down