1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/build/webpack/plugins/nextjs-ssr-import.js
Tim Neutkens 2e62b87fff
Make wasm files load on ssr (#4942)
* Make wasm files load on ssr

* Upgrade React for tests
2018-08-10 09:37:27 -07:00

21 lines
1 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.hooks.compilation.tap('NextJsSSRImport', (compilation) => {
compilation.mainTemplate.hooks.requireEnsure.tap('NextJsSSRImport', (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, '/')
return code.replace('require("./"', `require("${relativePathToBaseDirNormalized}/"`).replace('readFile(join(__dirname', `readFile(join(__dirname, "${relativePathToBaseDirNormalized}"`)
})
})
}
}