Skip to content

Commit

Permalink
feat: support webpack magic comments (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
NozomuIkuta committed Jun 2, 2021
1 parent 74b343d commit 4e88e8f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export default {
</script>
```

If you want to prefetch or preload the components with `Lazy` prefix, you can configure it by [prefetch/preload options](#prefetch/preload).

### Nested Components

If you have components in nested directories:
Expand Down Expand Up @@ -304,6 +306,31 @@ export default {

Components having the same name in `~/components` will overwrite the one in `my-theme/components`, learn more in [Overwriting Components](#overwriting-components). The lowest value will overwrite.

#### prefetch/preload

- Type: `Boolean/Number`
- Default: `false`

These properties are used in production to configure how [components with `Lazy` prefix](#Lazy-Imports) are handled by Wepack via its magic comments, learn more in [Wepack's official documentation](https://webpack.js.org/api/module-methods/#magic-comments).

```js
export default {
components: [
{ path: 'my-theme/components', prefetch: true }
]
}
```

yields:

```js
// plugin.js
const componets = {
MyComponentA: import(/* webpackPrefetch: true */ ...),
MyComponentB: import(/* webpackPrefetch: true */ ...)
}
```

## Migration guide

## `v1` to `v2`
Expand Down
6 changes: 4 additions & 2 deletions src/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise<
const filePaths = new Set<string>()
const scannedPaths: string[] = []

for (const { path, pattern, ignore = [], prefix, extendComponent, pathPrefix, level } of dirs.sort(sortDirsByPathLength)) {
for (const { path, pattern, ignore = [], prefix, extendComponent, pathPrefix, level, prefetch = false, preload = false } of dirs.sort(sortDirsByPathLength)) {
const resolvedNames = new Map<string, string>()

for (const _file of await globby(pattern!, { cwd: path, ignore })) {
Expand Down Expand Up @@ -76,7 +76,9 @@ export async function scanComponents (dirs: ScanDir[], srcDir: string): Promise<
asyncImport: '',
export: 'default',
global: Boolean(global),
level: Number(level)
level: Number(level),
prefetch: Boolean(prefetch),
preload: Boolean(preload)
}

if (typeof extendComponent === 'function') {
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface Component {
/** @deprecated */
global: boolean
level: number
prefetch: boolean
preload: boolean
}

export interface ScanDir {
Expand All @@ -22,6 +24,8 @@ export interface ScanDir {
global?: boolean | 'dev'
pathPrefix?: boolean
level?: number
prefetch: boolean
preload: boolean
extendComponent?: (component: Component) => Promise<Component | void> | (Component | void)
}

Expand Down
8 changes: 7 additions & 1 deletion templates/components/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { wrapFunctional } from './utils'
const components = {
<%= components.map(c => {
const exp = c.export === 'default' ? `c.default || c` : `c['${c.export}']`
return ` ${c.pascalName.replace(/^Lazy/, '')}: () => import('../${relativeToBuild(c.filePath)}' /* webpackChunkName: "${c.chunkName}" */).then(c => wrapFunctional(${exp}))`
const magicComments = [
`webpackChunkName: "${c.chunkName}"`,
c.prefetch === true || typeof c.prefetch === 'number' ? `webpackPrefetch: ${c.prefetch}` : false,
c.preload === true || typeof c.preload === 'number' ? `webpackPreload: ${c.preload}` : false,
].filter(Boolean).join(', ')

return ` ${c.pascalName.replace(/^Lazy/, '')}: () => import('../${relativeToBuild(c.filePath)}' /* ${magicComments} */).then(c => wrapFunctional(${exp}))`
}).join(',\n') %>
}

Expand Down

0 comments on commit 4e88e8f

Please sign in to comment.