1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

[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.
This commit is contained in:
Arunoda Susiripala 2018-02-19 16:19:41 +05:30 committed by Tim Neutkens
parent 695c8836d4
commit 9cfea69ab1
2 changed files with 8 additions and 11 deletions

View file

@ -1,22 +1,19 @@
import { join } from 'path'
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 {
constructor ({ dir, dist }) {
this.dir = dir
this.dist = dist
}
apply (compiler) {
compiler.plugin('compilation', (compilation) => {
compilation.mainTemplate.plugin('require-ensure', (code) => {
compilation.mainTemplate.plugin('require-ensure', (code, chunk) => {
// Update to load chunks from our custom chunks directory
const chunksDirPath = join(this.dir, this.dist, 'dist')
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 chunksDirPathNormalized = chunksDirPath.replace(/\\/g, '/')
let updatedCode = code.replace('require("./"', `require("${chunksDirPathNormalized}/"`)
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

View file

@ -255,7 +255,7 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
!dev && new webpack.optimize.ModuleConcatenationPlugin(),
!isServer && new PagesPlugin(),
!isServer && new DynamicChunksPlugin(),
isServer && new NextJsSsrImportPlugin({ dir, dist: config.distDir }),
isServer && new NextJsSsrImportPlugin(),
!isServer && new webpack.optimize.CommonsChunkPlugin({
name: `commons`,
filename: `commons.js`,