1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/server/build/plugins/nextjs-ssr-import.js
Arunoda Susiripala 9cfea69ab1 [SSR] Use relative paths in dynamic import output code (#3844)
* [SSR] Use relative paths in dynamic import output code
Now we use relative paths in the output code of dynamic import
in the server side.

* Remove unwanted constructor.
2018-02-19 11:49:41 +01:00

30 lines
1.3 KiB
JavaScript

import { join, resolve, relative, dirname } from 'path'
// This plugin modifies the require-ensure code generated by Webpack
// to work with Next.js SSR
export default class NextJsSsrImportPlugin {
apply (compiler) {
compiler.plugin('compilation', (compilation) => {
compilation.mainTemplate.plugin('require-ensure', (code, chunk) => {
// Update to load chunks from our custom chunks directory
const outputPath = resolve('/')
const pagePath = join('/', dirname(chunk.name))
const relativePathToBaseDir = relative(pagePath, outputPath)
// Make sure even in windows, the path looks like in unix
// Node.js require system will convert it accordingly
const relativePathToBaseDirNormalized = relativePathToBaseDir.replace(/\\/g, '/')
let updatedCode = code.replace('require("./"', `require("${relativePathToBaseDirNormalized}/"`)
// 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
})
})
}
}