rsnext/packages/next/build/webpack/plugins/nextjs-ssr-module-cache.ts
Tim Neutkens bef9b56109
Update filename generation for client-side compilation (#14279)
Updates the way filenames are generated for browser compilation.
Notably:
- All entry bundles now have hashes in production, this includes pages (previously pages used a buildId in the path)
- The AmpFiles no longer depends on hardcoded bundle names, it uses the buildManifest instead (internals)
- All cases where we match the page name from the chunk/entrypoint name now use the same function `getRouteFromEntrypoint` (internals)
- In development we no longer include the "faked" `buildId` set to `development` for page files, instead we just use the `/_next/static/pages` path (was `/_next/static/development/pages`). This was changed as it caused unneeded complexity and makes generating the bundles easier (internals)
- Updated tons of tests to be more resilient to these changes by relying on the buildManifest instead of hardcoded paths (internals)

Follow up of these PRs:
https://github.com/vercel/next.js/pull/13759
https://github.com/vercel/next.js/pull/13870
https://github.com/vercel/next.js/pull/13937
https://github.com/vercel/next.js/pull/14130
https://github.com/vercel/next.js/pull/14176
https://github.com/vercel/next.js/pull/14268


Fixes #6303
Fixes #12087 
Fixes #1948
Fixes #4368
Fixes #4255
Fixes #2548
2020-06-20 19:59:47 +00:00

75 lines
3.1 KiB
TypeScript

import webpack from 'webpack'
import { RawSource } from 'webpack-sources'
import { join, relative, dirname } from 'path'
import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-entrypoint'
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 {
private options: { outputPath: string }
constructor(options: { outputPath: string }) {
this.options = options
}
apply(compiler: webpack.Compiler) {
const { outputPath } = this.options
compiler.hooks.emit.tapAsync(
'NextJsSSRModuleCache',
(compilation, callback) => {
compilation.assets[SSR_MODULE_CACHE_FILENAME] = new RawSource(`
/* This cache is used by webpack for instantiated modules */
module.exports = {}
`)
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
if (!getRouteFromEntrypoint(chunk.name)) {
return originalFn(source, chunk)
}
const pagePath = join(outputPath, dirname(chunk.name))
let relativePathToBaseDir = relative(
pagePath,
join(outputPath, SSR_MODULE_CACHE_FILENAME)
)
// 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}');`,
])
}
}
return tapInfo
},
})
}
)
}
}