2018-08-30 14:02:18 +02:00
|
|
|
/**
|
|
|
|
COPYRIGHT (c) 2017-present James Kyle <me@thejameskyle.com>
|
|
|
|
MIT License
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWAR
|
|
|
|
*/
|
2018-07-24 11:24:40 +02:00
|
|
|
// Implementation of this PR: https://github.com/jamiebuilds/react-loadable/pull/132
|
|
|
|
// Modified to strip out unneeded results for Next's specific use case
|
2018-08-30 14:02:18 +02:00
|
|
|
|
2020-08-03 14:26:23 +02:00
|
|
|
import webpack, {
|
2019-11-11 04:24:53 +01:00
|
|
|
Compiler,
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2020-06-01 23:00:22 +02:00
|
|
|
compilation as CompilationType,
|
2019-11-11 04:24:53 +01:00
|
|
|
} from 'webpack'
|
2020-08-20 12:38:39 +02:00
|
|
|
import sources from 'webpack-sources'
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2020-08-22 14:16:13 +02:00
|
|
|
// @ts-ignore: TODO: remove ignore when webpack 5 is stable
|
|
|
|
const { RawSource } = webpack.sources || sources
|
|
|
|
|
2020-08-03 14:26:23 +02:00
|
|
|
const isWebpack5 = parseInt(webpack.version!) === 5
|
|
|
|
|
|
|
|
function getModulesIterable(compilation: any, chunk: any) {
|
|
|
|
if (isWebpack5) {
|
|
|
|
return compilation.chunkGraph.getChunkModulesIterable(chunk)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunk.modulesIterable
|
|
|
|
}
|
|
|
|
|
|
|
|
function getModuleId(compilation: any, module: any) {
|
|
|
|
if (isWebpack5) {
|
|
|
|
return compilation.chunkGraph.getModuleId(module)
|
|
|
|
}
|
|
|
|
|
|
|
|
return module.id
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
function buildManifest(
|
2020-06-07 01:00:03 +02:00
|
|
|
_compiler: Compiler,
|
2020-06-01 23:00:22 +02:00
|
|
|
compilation: CompilationType.Compilation
|
2019-05-29 13:57:26 +02:00
|
|
|
) {
|
2019-04-23 23:12:33 +02:00
|
|
|
let manifest: { [k: string]: any[] } = {}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2020-05-18 21:24:37 +02:00
|
|
|
compilation.chunkGroups.forEach((chunkGroup) => {
|
2019-04-10 18:06:16 +02:00
|
|
|
if (chunkGroup.isInitial()) {
|
|
|
|
return
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2019-04-23 23:12:33 +02:00
|
|
|
chunkGroup.origins.forEach((chunkGroupOrigin: any) => {
|
2019-04-10 18:06:16 +02:00
|
|
|
const { request } = chunkGroupOrigin
|
2019-03-17 19:19:26 +01:00
|
|
|
|
2019-04-23 23:12:33 +02:00
|
|
|
chunkGroup.chunks.forEach((chunk: any) => {
|
|
|
|
chunk.files.forEach((file: string) => {
|
2019-04-10 18:06:16 +02:00
|
|
|
if (!file.match(/\.js$/) || !file.match(/^static\/chunks\//)) {
|
|
|
|
return
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2020-08-03 14:26:23 +02:00
|
|
|
for (const module of getModulesIterable(compilation, chunk)) {
|
|
|
|
let id = getModuleId(compilation, module)
|
2019-03-17 19:19:26 +01:00
|
|
|
|
2019-04-10 18:06:16 +02:00
|
|
|
if (!manifest[request]) {
|
|
|
|
manifest[request] = []
|
2019-03-17 19:19:26 +01:00
|
|
|
}
|
2019-04-10 18:06:16 +02:00
|
|
|
|
2019-06-29 02:48:28 +02:00
|
|
|
// Avoid duplicate files
|
|
|
|
if (
|
|
|
|
manifest[request].some(
|
2020-05-18 21:24:37 +02:00
|
|
|
(item) => item.id === id && item.file === file
|
2019-06-29 02:48:28 +02:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:06:16 +02:00
|
|
|
manifest[request].push({
|
|
|
|
id,
|
|
|
|
file,
|
|
|
|
})
|
2019-03-17 19:19:26 +01:00
|
|
|
}
|
2019-04-10 18:06:16 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-07-24 11:24:40 +02:00
|
|
|
})
|
|
|
|
|
2019-03-17 19:19:26 +01:00
|
|
|
manifest = Object.keys(manifest)
|
|
|
|
.sort()
|
2019-11-11 04:24:53 +01:00
|
|
|
// eslint-disable-next-line no-sequences
|
2019-04-23 23:12:33 +02:00
|
|
|
.reduce((a, c) => ((a[c] = manifest[c]), a), {} as any)
|
2019-03-17 19:19:26 +01:00
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
return manifest
|
|
|
|
}
|
|
|
|
|
2018-07-25 13:45:42 +02:00
|
|
|
export class ReactLoadablePlugin {
|
2019-04-23 23:12:33 +02:00
|
|
|
private filename: string
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
constructor(opts: { filename: string }) {
|
2018-07-24 11:24:40 +02:00
|
|
|
this.filename = opts.filename
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:26:23 +02:00
|
|
|
createAssets(compiler: any, compilation: any, assets: any) {
|
|
|
|
const manifest = buildManifest(compiler, compilation)
|
2020-08-20 12:38:39 +02:00
|
|
|
// @ts-ignore: TODO: remove when webpack 5 is stable
|
2020-08-22 14:16:13 +02:00
|
|
|
assets[this.filename] = new RawSource(JSON.stringify(manifest, null, 2))
|
2020-08-03 14:26:23 +02:00
|
|
|
return assets
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
apply(compiler: Compiler) {
|
2020-08-03 14:26:23 +02:00
|
|
|
if (isWebpack5) {
|
|
|
|
compiler.hooks.make.tap('ReactLoadableManifest', (compilation) => {
|
|
|
|
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
|
|
|
|
compilation.hooks.processAssets.tap(
|
|
|
|
{
|
|
|
|
name: 'ReactLoadableManifest',
|
|
|
|
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
|
|
|
|
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
|
2019-05-29 13:57:26 +02:00
|
|
|
},
|
2020-08-03 14:26:23 +02:00
|
|
|
(assets: any) => {
|
|
|
|
this.createAssets(compiler, compilation, assets)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
compiler.hooks.emit.tap('ReactLoadableManifest', (compilation: any) => {
|
|
|
|
this.createAssets(compiler, compilation, compilation.assets)
|
|
|
|
})
|
2018-07-24 11:24:40 +02:00
|
|
|
}
|
|
|
|
}
|