mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
15bb1c5e79
- Replaces taskr-babel with taskr-typescript for the `next` package - Makes sure Node 8+ is used, no unneeded transpilation - Compile Next.js client side files through babel the same way pages are - Compile Next.js client side files to esmodules, not commonjs, so that tree shaking works. - Move error-debug.js out of next-server as it's only used/require in development - Drop ansi-html as dependency from next-server - Make next/link esmodule (for tree-shaking) - Make next/router esmodule (for tree-shaking) - add typescript compilation to next-server - Remove last remains of Flow - Move hoist-non-react-statics to next, out of next-server - Move htmlescape to next, out of next-server - Remove runtime-corejs2 from next-server
65 lines
3 KiB
JavaScript
65 lines
3 KiB
JavaScript
import webpack from 'webpack'
|
|
import { RawSource } from 'webpack-sources'
|
|
import { join, relative, dirname } from 'path'
|
|
import {IS_BUNDLED_PAGE_REGEX} from 'next-server/constants'
|
|
|
|
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 {
|
|
constructor (options) {
|
|
this.options = options
|
|
}
|
|
apply (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) => {
|
|
compilation.mainTemplate.hooks.localVars.intercept({
|
|
register (tapInfo) {
|
|
if (tapInfo.name === 'MainTemplate') {
|
|
const originalFn = tapInfo.fn
|
|
tapInfo.fn = (source, chunk) => {
|
|
// 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 (!IS_BUNDLED_PAGE_REGEX.exec(chunk.name) && chunk.name !== 'error-debug.js') {
|
|
return originalFn(source, chunk)
|
|
}
|
|
const pagePath = join(outputPath, dirname(chunk.name))
|
|
let relativePathToBaseDir = relative(pagePath, join(outputPath, SSR_MODULE_CACHE_FILENAME))
|
|
if (chunk.name === 'error-debug.js') {
|
|
relativePathToBaseDir = `./${relativePathToBaseDir}`
|
|
}
|
|
|
|
// 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.Template.asString([
|
|
source,
|
|
'// The module cache',
|
|
`var installedModules = require('${relativePathToBaseDirNormalized}');`
|
|
])
|
|
}
|
|
}
|
|
return tapInfo
|
|
}
|
|
})
|
|
})
|
|
}
|
|
}
|