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:
parent
695c8836d4
commit
9cfea69ab1
|
@ -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
|
// This plugin modifies the require-ensure code generated by Webpack
|
||||||
// to work with Next.js SSR
|
// to work with Next.js SSR
|
||||||
export default class NextJsSsrImportPlugin {
|
export default class NextJsSsrImportPlugin {
|
||||||
constructor ({ dir, dist }) {
|
|
||||||
this.dir = dir
|
|
||||||
this.dist = dist
|
|
||||||
}
|
|
||||||
|
|
||||||
apply (compiler) {
|
apply (compiler) {
|
||||||
compiler.plugin('compilation', (compilation) => {
|
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
|
// 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
|
// Make sure even in windows, the path looks like in unix
|
||||||
// Node.js require system will convert it accordingly
|
// Node.js require system will convert it accordingly
|
||||||
const chunksDirPathNormalized = chunksDirPath.replace(/\\/g, '/')
|
const relativePathToBaseDirNormalized = relativePathToBaseDir.replace(/\\/g, '/')
|
||||||
let updatedCode = code.replace('require("./"', `require("${chunksDirPathNormalized}/"`)
|
let updatedCode = code.replace('require("./"', `require("${relativePathToBaseDirNormalized}/"`)
|
||||||
|
|
||||||
// Replace a promise equivalent which runs in the same loop
|
// Replace a promise equivalent which runs in the same loop
|
||||||
// If we didn't do this webpack's module loading process block us from
|
// If we didn't do this webpack's module loading process block us from
|
||||||
|
|
|
@ -255,7 +255,7 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
|
||||||
!dev && new webpack.optimize.ModuleConcatenationPlugin(),
|
!dev && new webpack.optimize.ModuleConcatenationPlugin(),
|
||||||
!isServer && new PagesPlugin(),
|
!isServer && new PagesPlugin(),
|
||||||
!isServer && new DynamicChunksPlugin(),
|
!isServer && new DynamicChunksPlugin(),
|
||||||
isServer && new NextJsSsrImportPlugin({ dir, dist: config.distDir }),
|
isServer && new NextJsSsrImportPlugin(),
|
||||||
!isServer && new webpack.optimize.CommonsChunkPlugin({
|
!isServer && new webpack.optimize.CommonsChunkPlugin({
|
||||||
name: `commons`,
|
name: `commons`,
|
||||||
filename: `commons.js`,
|
filename: `commons.js`,
|
||||||
|
|
Loading…
Reference in a new issue