2021-01-14 02:59:08 +01:00
|
|
|
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
|
2018-07-24 11:24:40 +02:00
|
|
|
import { join, relative, dirname } from 'path'
|
2021-06-30 13:44:40 +02:00
|
|
|
import getRouteFromEntrypoint from '../../../server/get-route-from-entrypoint'
|
2018-07-24 11:24:40 +02:00
|
|
|
const SSR_MODULE_CACHE_FILENAME = 'ssr-module-cache.js'
|
|
|
|
|
|
|
|
// By default webpack keeps initialized modules per-module.
|
|
|
|
// This means that if you have 2 entrypoints loaded into the same app
|
|
|
|
// they will *not* share the same instance
|
|
|
|
// This creates many issues when developers / libraries rely on the singleton pattern
|
|
|
|
// As this pattern assumes every module will have 1 instance
|
|
|
|
// This plugin overrides webpack's code generation step to replace `installedModules`
|
|
|
|
// The replacement is a require for a file that's also generated here that only exports an empty object
|
|
|
|
// Because of Node.js's single instance modules this makes webpack share all initialized instances
|
|
|
|
// Do note that this module is only geared towards the `node` compilation target.
|
|
|
|
// For the client side compilation we use `runtimeChunk: 'single'`
|
|
|
|
export default class NextJsSsrImportPlugin {
|
2019-04-23 23:12:33 +02:00
|
|
|
private options: { outputPath: string }
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
constructor(options: { outputPath: string }) {
|
2018-07-24 11:24:40 +02:00
|
|
|
this.options = options
|
|
|
|
}
|
2019-05-29 13:57:26 +02:00
|
|
|
apply(compiler: webpack.Compiler) {
|
2019-02-19 22:45:07 +01:00
|
|
|
const { outputPath } = this.options
|
2019-05-29 13:57:26 +02:00
|
|
|
compiler.hooks.emit.tapAsync(
|
|
|
|
'NextJsSSRModuleCache',
|
|
|
|
(compilation, callback) => {
|
2021-01-14 02:59:08 +01:00
|
|
|
compilation.assets[SSR_MODULE_CACHE_FILENAME] = new sources.RawSource(`
|
2018-07-24 11:24:40 +02:00
|
|
|
/* This cache is used by webpack for instantiated modules */
|
|
|
|
module.exports = {}
|
|
|
|
`)
|
2019-05-29 13:57:26 +02:00
|
|
|
callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
compiler.hooks.compilation.tap(
|
|
|
|
'NextJsSSRModuleCache',
|
|
|
|
(compilation: any) => {
|
|
|
|
compilation.mainTemplate.hooks.localVars.intercept({
|
|
|
|
register(tapInfo: any) {
|
|
|
|
if (tapInfo.name === 'MainTemplate') {
|
|
|
|
const originalFn = tapInfo.fn
|
|
|
|
tapInfo.fn = (source: any, chunk: any) => {
|
|
|
|
// If the chunk is not part of the pages directory we have to keep the original behavior,
|
|
|
|
// otherwise webpack will error out when the file is used before the compilation finishes
|
|
|
|
// this is the case with mini-css-extract-plugin
|
2020-06-20 21:59:47 +02:00
|
|
|
|
|
|
|
if (!getRouteFromEntrypoint(chunk.name)) {
|
2019-05-29 13:57:26 +02:00
|
|
|
return originalFn(source, chunk)
|
|
|
|
}
|
|
|
|
const pagePath = join(outputPath, dirname(chunk.name))
|
|
|
|
let relativePathToBaseDir = relative(
|
|
|
|
pagePath,
|
|
|
|
join(outputPath, SSR_MODULE_CACHE_FILENAME)
|
|
|
|
)
|
2018-11-28 15:03:02 +01:00
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
// Make sure even in windows, the path looks like in unix
|
|
|
|
// Node.js require system will convert it accordingly
|
|
|
|
const relativePathToBaseDirNormalized = relativePathToBaseDir.replace(
|
|
|
|
/\\/g,
|
|
|
|
'/'
|
|
|
|
)
|
|
|
|
return (webpack as any).Template.asString([
|
|
|
|
source,
|
|
|
|
'// The module cache',
|
|
|
|
`var installedModules = require('${relativePathToBaseDirNormalized}');`,
|
|
|
|
])
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
}
|
2019-05-29 13:57:26 +02:00
|
|
|
return tapInfo
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2018-07-24 11:24:40 +02:00
|
|
|
}
|
|
|
|
}
|