rsnext/packages/next/build/webpack/plugins/shared-runtime-plugin.ts
Joe Haddad 0249b331c2
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
2019-03-26 12:30:31 -04:00

35 lines
1.1 KiB
TypeScript

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
},
}
},
})
})
}
}