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

Make sure hot-self-accept-loader cleans up the route correctly (#4092)

This commit is contained in:
Tim Neutkens 2018-03-31 23:19:06 +02:00 committed by GitHub
parent 7f335cb032
commit c3318d8ef6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View file

@ -1,9 +1,11 @@
import { resolve, relative } from 'path'
import loaderUtils from 'loader-utils'
module.exports = function (content, sourceMap) {
this.cacheable()
const route = getRoute(this)
const options = loaderUtils.getOptions(this)
const route = getRoute(this, options)
// Webpack has a built in system to prevent default from colliding, giving it a random letter per export.
// We can safely check if Component is undefined since all other pages imported into the entrypoint don't have __webpack_exports__.default
@ -30,11 +32,16 @@ module.exports = function (content, sourceMap) {
const nextPagesDir = resolve(__dirname, '..', '..', '..', 'pages')
function getRoute (loaderContext) {
function getRoute (loaderContext, options) {
const pagesDir = resolve(loaderContext.options.context, 'pages')
const { resourcePath } = loaderContext
const dir = [pagesDir, nextPagesDir]
.find((d) => resourcePath.indexOf(d) === 0)
const path = relative(dir, resourcePath)
if (!options.extensions) {
throw new Error('extensions is not provided to hot-self-accept-loader. Please upgrade all next-plugins to the latest version.')
}
const path = relative(dir, resourcePath).replace(options.extensions, '.js')
return '/' + path.replace(/((^|\/)index)?\.js$/, '')
}

View file

@ -177,15 +177,18 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
module: {
rules: [
dev && !isServer && {
test: /\.(js|jsx)(\?[^?]*)?$/,
test: /\.(js|jsx)$/,
loader: 'hot-self-accept-loader',
include: [
path.join(dir, 'pages'),
nextPagesDir
]
],
options: {
extensions: /\.(js|jsx)$/
}
},
{
test: /\.+(js|jsx)$/,
test: /\.(js|jsx)$/,
include: [dir],
exclude: /node_modules/,
use: defaultLoaders.babel