rsnext/packages/next/build/webpack/plugins/next-esm-plugin.ts
Alex Castle 3e8b36e879 Experimental: Granular build chunking (#7696)
* Refactor SplitChunksPlugin configs and add experimental chunking strategy

* Use typeDefs for SplitChunksConfig

* Modify build manifest plugin to create runtime build manifest

* Add support for granular chunks to page-loader

* Ensure normal behavior if experimental granularChunks flag is false

* Update client build manifest to remove iife & implicit global

* Factor out '/_next/' prepending into getDependencies

* Update packages/next/build/webpack-config.ts filepath regex

Co-Authored-By: Jason Miller <developit@users.noreply.github.com>

* Simplify dependency load ordering in page-loader.js

* Use SHA1 hash to shorten filenames for dependency modules

* Add scheduler to framework cacheGroup in webpack-config

* Update page loader to not duplicate script tags with query parameters

* Ensure no slashes end up in the file hashes

* Add prop-types to framework chunk

* Fix issue with mis-attributed events

* Increase modern build size budget--possibly decrement after consulting with @janicklasralph

* Use module.rawRequest for lib chunks

Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com>

* Dasherize lib chunk names

Co-Authored-By: Daniel Stockman <daniel.stockman@gmail.com>

* Fix typescript errors, reorganize lib name logic

* Dasherize rawRequest, short circuit name logic when rawRequest found

* Add `scheduler` package to test regex

* Fix a nit

* Adjust build manifest plugin

* Shorten key name

* Extract createPreloadLink helper

* Extract getDependencies helper

* Move method

* Minimize diff

* Minimize diff x2

* Fix Array.from polyfill

* Simplify page loader code

* Remove async=false for script tags

* Code golf `getDependencies` implementation

* Require lib chunks be in node_modules

* Update packages/next/build/webpack-config.ts

Co-Authored-By: Joe Haddad <timer150@gmail.com>

* Replace remaining missed windows compat regex

* Trim client manifest

* Prevent duplicate link preload tags

* Revert size test changes

* Squash manifest size even further

* Add comment for clarity

* Code golfing 🏌️‍♂️

* Correctly select modern dependencies

* Ship separate modern client manifest when module/module enabled

* Update packages/next/build/webpack/plugins/build-manifest-plugin.ts

Co-Authored-By: Joe Haddad <timer150@gmail.com>

* Remove unneccessary filter from page-loader

* Add lookbehind to file extension regex in page-loader

* v9.0.3

* Update examples for Apollo with AppTree (#8180)

* Update examples for Apollo with AppTree

* Fix apolloClient being overwritten when rendering AppTree

* Golf page-loader (#8190)

* Remove lookbehind for module replacement

* Wait for build manifest promise before page load or prefetch

* Updating modern-only chunks inside the right entry point

* Fixing ts errors

* Rename variable

* Revert "Wait for build manifest promise before page load or prefetch"

This reverts commit c370528c6888ba7fa71162a0854534ed280224ef.

* Use proper typedef for webpack chunk

* Re-enable promisified client build manifest

* Fix bug in getDependencies map

* Insert check for granularChunks in page-loader

* Increase size limit temporarily for granular chunks

* Add 50ms delay to flaky test

* Set env.__NEXT_GRANULAR_CHUNKS in webpack config

* Reset size limit to 187

* Set process.env.__NEXT_GRANULAR_CHUNKS to false if selectivePageBuilding

* Update test/integration/production/test/index.test.js

Co-Authored-By: Joe Haddad <timer150@gmail.com>

* Do not create promise if not using chunking PR
2019-08-08 13:14:33 -04:00

277 lines
8.3 KiB
TypeScript

/**
* MIT License
*
* Copyright (c) 2018 Prateek Bhatnagar
*
* 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
* SOFTWARE.
*/
/**
* Webpack plugin for NextJs that runs a child compiler to generate a second (modern) JS bundle
*
* @author: Janicklas Ralph (https://github.com/janickals-ralph)
*
* Original source from which this was built upon - https://github.com/prateekbh/babel-esm-plugin
*/
import {
Compiler,
compilation,
Plugin,
RuleSetRule,
RuleSetLoader,
Output,
} from 'webpack'
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin')
const MultiEntryPlugin = require('webpack/lib/MultiEntryPlugin')
const JsonpTemplatePlugin = require('webpack/lib/web/JsonpTemplatePlugin')
const SplitChunksPlugin = require('webpack/lib/optimize/SplitChunksPlugin')
const RuntimeChunkPlugin = require('webpack/lib/optimize/RuntimeChunkPlugin')
const PLUGIN_NAME = 'NextEsmPlugin'
export default class NextEsmPlugin implements Plugin {
options: {
filename: any
chunkFilename: any
excludedPlugins: string[]
additionalPlugins: Plugin[]
}
constructor(options: {
filename: any
chunkFilename: any
excludedPlugins?: string[]
additionalPlugins?: any
}) {
this.options = Object.assign(
{
excludedPlugins: [PLUGIN_NAME],
additionalPlugins: [],
},
options
)
}
apply(compiler: Compiler) {
compiler.hooks.make.tapAsync(
PLUGIN_NAME,
(compilation: compilation.Compilation, callback) => {
this.runBuild(compiler, compilation).then(callback)
}
)
}
getBabelLoader(rules: RuleSetRule[]) {
for (let rule of rules) {
if (!rule.use) continue
if (Array.isArray(rule.use)) {
return (rule.use as RuleSetLoader[]).find(
r => r.loader && r.loader.includes('next-babel-loader')
)
}
const ruleUse = rule.use as RuleSetLoader
const ruleLoader = rule.loader as string
if (
(ruleUse.loader && ruleUse.loader.includes('next-babel-loader')) ||
(ruleLoader && ruleLoader.includes('next-babel-loader'))
) {
return ruleUse || rule
}
}
}
updateOptions(childCompiler: Compiler) {
if (!childCompiler.options.module) {
throw new Error('Webpack.options.module not found!')
}
let babelLoader = this.getBabelLoader(childCompiler.options.module.rules)
if (!babelLoader) {
throw new Error('Babel-loader config not found!')
}
babelLoader.options = Object.assign({}, babelLoader.options, {
isModern: true,
})
}
updateAssets(
compilation: compilation.Compilation,
childCompilation: compilation.Compilation
) {
compilation.assets = Object.assign(
childCompilation.assets,
compilation.assets
)
compilation.namedChunkGroups = Object.assign(
childCompilation.namedChunkGroups,
compilation.namedChunkGroups
)
const childChunkFileMap = childCompilation.chunks.reduce(
(
chunkMap: { [key: string]: compilation.Chunk },
chunk: compilation.Chunk
) => {
chunkMap[chunk.name] = chunk
return chunkMap
},
{}
)
// Merge files from similar chunks
compilation.chunks.forEach((chunk: compilation.Chunk) => {
const childChunk = childChunkFileMap[chunk.name]
if (childChunk && childChunk.files) {
delete childChunkFileMap[chunk.name]
chunk.files.push(
...childChunk.files.filter((v: any) => !chunk.files.includes(v))
)
}
})
// Add modern only chunks
compilation.chunks.push(...Object.values(childChunkFileMap))
// Place modern only chunk inside the right entry point
compilation.entrypoints.forEach((entryPoint, entryPointName) => {
const childEntryPoint = childCompilation.entrypoints.get(entryPointName)
childEntryPoint.chunks.forEach((chunk: compilation.Chunk) => {
if (childChunkFileMap.hasOwnProperty(chunk.name)) {
entryPoint.chunks.push(chunk)
}
})
})
}
async runBuild(compiler: Compiler, compilation: compilation.Compilation) {
const outputOptions: Output = { ...compiler.options.output }
if (typeof this.options.filename === 'function') {
outputOptions.filename = this.options.filename(outputOptions.filename)
} else {
outputOptions.filename = this.options.filename
}
if (typeof this.options.chunkFilename === 'function') {
outputOptions.chunkFilename = this.options.chunkFilename(
outputOptions.chunkFilename
)
} else {
outputOptions.chunkFilename = this.options.chunkFilename
}
let plugins = (compiler.options.plugins || []).filter(
c => !this.options.excludedPlugins.includes(c.constructor.name)
)
// Add the additionalPlugins
plugins = plugins.concat(this.options.additionalPlugins)
/**
* We are deliberatly not passing plugins in createChildCompiler.
* All webpack does with plugins is to call `apply` method on them
* with the childCompiler.
* But by then we haven't given childCompiler a fileSystem or other options
* which a few plugins might expect while execution the apply method.
* We do call the `apply` method of all plugins by ourselves later in the code
*/
const childCompiler = compilation.createChildCompiler(
PLUGIN_NAME,
outputOptions
)
childCompiler.context = compiler.context
childCompiler.inputFileSystem = compiler.inputFileSystem
childCompiler.outputFileSystem = compiler.outputFileSystem
// Call the `apply` method of all plugins by ourselves.
if (Array.isArray(plugins)) {
for (const plugin of plugins) {
plugin.apply(childCompiler)
}
}
let compilerEntries: any = compiler.options.entry
if (typeof compilerEntries === 'function') {
compilerEntries = await compilerEntries()
}
if (typeof compilerEntries === 'string') {
compilerEntries = { index: compilerEntries }
}
Object.keys(compilerEntries).forEach(entry => {
const entryFiles = compilerEntries[entry]
if (Array.isArray(entryFiles)) {
new MultiEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler
)
} else {
new SingleEntryPlugin(compiler.context, entryFiles, entry).apply(
childCompiler
)
}
})
// Convert entry chunk to entry file
new JsonpTemplatePlugin().apply(childCompiler)
const optimization = compiler.options.optimization
if (optimization) {
if (optimization.splitChunks) {
new SplitChunksPlugin(
Object.assign({}, optimization.splitChunks)
).apply(childCompiler)
}
if (optimization.runtimeChunk) {
new RuntimeChunkPlugin(
Object.assign({}, optimization.runtimeChunk)
).apply(childCompiler)
}
}
compilation.hooks.additionalAssets.tapAsync(
PLUGIN_NAME,
childProcessDone => {
this.updateOptions(childCompiler)
childCompiler.runAsChild((err, entries, childCompilation) => {
if (err) {
return childProcessDone(err)
}
if (childCompilation.errors.length > 0) {
return childProcessDone(childCompilation.errors[0])
}
this.updateAssets(compilation, childCompilation)
childProcessDone()
})
}
)
}
}