Unverified Commit 0249b331 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Use a shared module cache (#6791)

* [WIP] Use a shared module cache

* ID modules in development

* Revert "ID modules in development"

This reverts commit 0613d92fa2c8c7fa11a5ff5b7770d784af1cec63.

* Remove context replacement

* Only enable shared runtime in prod

* Sort settings

* Add shared runtime experimental setting

* only enable shared runtime in serverless
parent dbd41725
......@@ -27,8 +27,9 @@ const defaultConfig = {
(Number(process.env.CIRCLE_NODE_TOTAL) ||
(os.cpus() || { length: 1 }).length) - 1
),
exportTrailingSlash: true,
profiling: false,
exportTrailingSlash: true
sharedRuntime: false
}
}
......
......@@ -12,6 +12,7 @@ import { NEXT_PROJECT_ROOT, NEXT_PROJECT_ROOT_NODE_MODULES, NEXT_PROJECT_ROOT_DI
import {TerserPlugin} from './webpack/plugins/terser-webpack-plugin/src/index'
import { ServerlessPlugin } from './webpack/plugins/serverless-plugin'
import { AllModulesIdentifiedPlugin } from './webpack/plugins/all-modules-identified-plugin'
import { SharedRuntimePlugin } from './webpack/plugins/shared-runtime-plugin'
import { HashedChunkIdsPlugin } from './webpack/plugins/hashed-chunk-ids-plugin'
import { WebpackEntrypoints } from './entries'
type ExcludesFalse = <T>(x: T | false) => x is T
......@@ -73,6 +74,8 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
cpus: config.experimental.cpus,
}
const sharedRuntime = config.experimental.sharedRuntime && target === 'serverless'
let webpackConfig: webpack.Configuration = {
mode: webpackMode,
devtool: dev ? 'cheap-module-source-map' : false,
......@@ -140,9 +143,9 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
})
] : undefined
} : {
runtimeChunk: {
runtimeChunk: (!sharedRuntime || dev) ? {
name: CLIENT_STATIC_FILES_RUNTIME_WEBPACK
},
} : false,
splitChunks: dev ? {
cacheGroups: {
default: false,
......@@ -295,6 +298,8 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, isServe
// This sets chunk ids to be hashed versions of their names to reduce
// bundle churn
!dev && new HashedChunkIdsPlugin(buildId),
// On the client we want to share the same runtime cache
!dev && !isServer && sharedRuntime && new SharedRuntimePlugin(),
!dev && new webpack.IgnorePlugin({
checkResource: (resource: string) => {
return /react-is/.test(resource)
......
......@@ -14,11 +14,7 @@ export class AllModulesIdentifiedPlugin implements Plugin {
return
}
const identifier = m
.identifier()
// Ensure the context isn't included in the hash (this normally
// isn't present)
.replace(m.context, '')
const identifier = m.identifier()
// This hashing algorithm is consistent with how the rest of
// webpack does it (n.b. HashedModuleIdsPlugin)
......
......@@ -19,7 +19,16 @@ export class HashedChunkIdsPlugin implements Plugin {
.update(id)
.digest('hex')
.substr(0, 4)
continue
}
const ids = [...chunk.modulesIterable]
.map(m => m.id)
.sort() as string[]
const h = createHash('md4')
ids.forEach(id => h.update(id))
chunk.id = h.digest('hex').substr(0, 4)
}
})
})
......
import { Compiler, Plugin } from 'webpack'
export class SharedRuntimePlugin implements Plugin {
apply(compiler: Compiler) {
const installedModules = [
'// The module cache',
'var installedModules = {};',
].join('\n')
const nextInstalledModules = [
'// The Next.js shared module cache',
'var installedModules = window.__next_installed_modules__ || (window.__next_installed_modules__ = {});',
].join('\n')
compiler.hooks.compilation.tap('SharedRuntimePlugin', compilation => {
;(compilation.mainTemplate as any).hooks.localVars.intercept({
register: (tapInfo: any) => {
if (!(tapInfo.name === 'MainTemplate' && tapInfo.type === 'sync')) {
return tapInfo
}
const { fn } = tapInfo
return {
...tapInfo,
fn: function() {
const ret = fn.apply(this, arguments)
return typeof ret === 'string'
? ret.replace(installedModules, nextInstalledModules)
: ret
},
}
},
})
})
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment