Skip to content

Commit

Permalink
feat: add includeDynamicImportedAssets option
Browse files Browse the repository at this point in the history
  • Loading branch information
ztoben committed Dec 4, 2020
1 parent c87d6fe commit e15f40b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
18 changes: 17 additions & 1 deletion index.js
Expand Up @@ -5,6 +5,7 @@ const _ = require('lodash')
const getAssetKind = require('./lib/getAssetKind')
const isHMRUpdate = require('./lib/isHMRUpdate')
const isSourceMap = require('./lib/isSourceMap')
const getDynamicImportedChildAssets = require('./lib/getDynamicImportedChildAssets')

const createQueuedWriter = require('./lib/output/createQueuedWriter')
const createOutputWriter = require('./lib/output/createOutputWriter')
Expand All @@ -21,6 +22,7 @@ function AssetsWebpackPlugin (options) {
includeAllFileTypes: true,
includeFilesWithoutChunk: false,
includeAuxiliaryAssets: false,
includeDynamicImportedAssets: false,
keepInMemory: false,
integrity: false,
removeFullPathAutoPrefix: false
Expand Down Expand Up @@ -97,6 +99,11 @@ AssetsWebpackPlugin.prototype = {
assets = [...assets, ...stats.entrypoints[chunkName].auxiliaryAssets]
}

if (self.options.includeDynamicImportedAssets && chunkName && stats.entrypoints[chunkName].children) {
const dynamicImportedChildAssets = getDynamicImportedChildAssets(options, stats.entrypoints[chunkName].children)
assets = [...assets, ...dynamicImportedChildAssets]
}

if (!Array.isArray(assets)) {
assets = [assets]
}
Expand All @@ -114,6 +121,7 @@ AssetsWebpackPlugin.prototype = {
const type = typeof typeMap[typeName]
const compilationAsset = compilation.assets[asset]
const integrity = compilationAsset && compilationAsset.integrity
const loadingBehaviour = obj.loadingBehaviour

if (type === 'undefined') {
typeMap[typeName] = combinedPath
Expand All @@ -125,7 +133,15 @@ AssetsWebpackPlugin.prototype = {
if (type === 'string') {
typeMap[typeName] = [typeMap[typeName]]
}
typeMap[typeName].push(combinedPath)

if (self.options.includeDynamicImportedAssets && loadingBehaviour) {
const typeNameWithLoadingBehaviour = typeName + ':' + loadingBehaviour

typeMap[typeNameWithLoadingBehaviour] = typeMap[typeNameWithLoadingBehaviour] || []
typeMap[typeNameWithLoadingBehaviour].push(combinedPath)
} else {
typeMap[typeName].push(combinedPath)
}
}

added = true
Expand Down
34 changes: 34 additions & 0 deletions lib/getDynamicImportedChildAssets.js
@@ -0,0 +1,34 @@
const getAssetsFromChildChunk = (options, chunk, loadingBehavior) => {
const assets = []

if (chunk.assets) {
chunk.assets.forEach(asset => {
asset.loadingBehavior = loadingBehavior
assets.push(asset)
})
}

if (options.includeAuxiliaryAssets && chunk.auxiliaryAssets) {
chunk.auxiliaryAssets.forEach(asset => {
asset.loadingBehavior = loadingBehavior
assets.push(asset)
})
}

return assets
}

module.exports = function getDynamicImportedChildAssets (options, children) {
const loadingBehaviors = ['preload', 'prefetch']
let assets = []

loadingBehaviors.forEach(loadingBehavior => {
if (children[loadingBehavior]) {
children[loadingBehavior].forEach(childChunk => {
assets = [...assets, ...getAssetsFromChildChunk(options, childChunk, loadingBehavior)]
})
}
})

return assets
}
11 changes: 11 additions & 0 deletions readme.md
Expand Up @@ -365,6 +365,17 @@ When set, will output any files that are part of the chunk and marked as auxilia
new AssetsPlugin({includeAuxiliaryAssets: true})
```

#### `includeDynamicImportedAssets`

Optional. `false` by default.

When set, will output any files that are part of the chunk and marked as preloadable or prefechtable child assets via a dynamic import.
See: https://webpack.js.org/guides/code-splitting/#prefetchingpreloading-modules

```js
new AssetsPlugin({includeDynamicImportedAssets: true})
```

### Using in multi-compiler mode

If you use webpack multi-compiler mode and want your assets written to a single file,
Expand Down

0 comments on commit e15f40b

Please sign in to comment.