mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
4b143fc232
* Make sure dynamic imports works on Windows * Fix an issue with the load test firmware. * Fix symlink creation on Unix
33 lines
1.2 KiB
JavaScript
33 lines
1.2 KiB
JavaScript
import { join } from 'path'
|
|
|
|
// This plugin modifies the require-ensure code generated by Webpack
|
|
// to work with Next.js SSR
|
|
export default class NextJsSsrImportPlugin {
|
|
constructor ({ dir, dist }) {
|
|
this.dir = dir
|
|
this.dist = dist
|
|
}
|
|
|
|
apply (compiler) {
|
|
compiler.plugin('compilation', (compilation) => {
|
|
compilation.mainTemplate.plugin('require-ensure', (code) => {
|
|
// Update to load chunks from our custom chunks directory
|
|
const chunksDirPath = join(this.dir, this.dist, 'dist')
|
|
// Make sure even in windows, the path looks like in unix
|
|
// Node.js require system will convert it accordingly
|
|
const chunksDirPathNormalized = chunksDirPath.replace(/\\/g, '/')
|
|
let updatedCode = code.replace('require("./"', `require("${chunksDirPathNormalized}/"`)
|
|
|
|
// Replace a promise equivalent which runs in the same loop
|
|
// If we didn't do this webpack's module loading process block us from
|
|
// doing SSR for chunks
|
|
updatedCode = updatedCode.replace(
|
|
'return Promise.resolve();',
|
|
`return require('next/dynamic').SameLoopPromise.resolve();`
|
|
)
|
|
return updatedCode
|
|
})
|
|
})
|
|
}
|
|
}
|