Unverified Commit e7ca6aa4 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Correctly compute unknown module ids (#6898)

parent cd9a1085
......@@ -312,7 +312,7 @@ export default function getBaseWebpackConfig (dir: string, {dev = false, debug =
!dev && new webpack.HashedModuleIdsPlugin(),
// This must come after HashedModuleIdsPlugin (it sets any modules that
// were missed by HashedModuleIdsPlugin)
!dev && new AllModulesIdentifiedPlugin(),
!dev && __selectivePageBuilding && new AllModulesIdentifiedPlugin(dir),
// This sets chunk ids to be hashed versions of their names to reduce
// bundle churn
!dev && new HashedChunkIdsPlugin(buildId),
......
import { Compiler, Plugin } from 'webpack'
import { createHash } from 'crypto'
import escapeRegex from 'escape-string-regexp'
import path from 'path'
function getRawModuleIdentifier(m: any, dir: string) {
// webpack impl:
// new RawModule(
// "/* (ignored) */",
// `ignored ${context} ${request}`,
// `${request} (ignored)`
// )
let request = m.readableIdentifierStr.replace(/ \(ignored\)$/, '')
let context = m.identifierStr
.match(new RegExp(`^ignored (.*) ${escapeRegex(request)}$`))
.pop()
if (path.isAbsolute(request)) {
request = path.relative(dir, request)
}
if (path.isAbsolute(context)) {
context = path.relative(dir, context)
}
const identifier = `${context}::${request}`
console.warn(
`[module identifier] RawModule ${m.identifier()} => ${identifier}`
)
return identifier
}
function getMultiModuleIdentifier(m: any) {
const mods = m.dependencies.map((d: any) => d.module)
if (mods.some((d: any) => !Boolean(d))) {
throw new Error('Cannot handle a MultiModule with moduleless dependencies')
}
const ids = mods.map((m: any) => m.id)
if (ids.some((i: any) => !Boolean(i))) {
throw new Error(
'Cannot handle a MultiModule dependency without a module id'
)
}
const identifier = ids.sort().join('::')
console.warn(
`[module identifier] MultiModule ${m.identifier()} => ${identifier}`
)
return identifier
}
export class AllModulesIdentifiedPlugin implements Plugin {
private dir: string
constructor(dir: string) {
this.dir = dir
}
apply(compiler: Compiler) {
compiler.hooks.compilation.tap(
'AllModulesIdentifiedPlugin',
......@@ -10,11 +65,27 @@ export class AllModulesIdentifiedPlugin implements Plugin {
'AllModulesIdentifiedPlugin',
modules => {
;(modules as any[]).forEach(m => {
if (m.id != null || !m.identifier) {
if (m.id != null) {
return
}
const identifier = m.identifier()
let identifier: string
if (m.constructor && m.constructor.name === 'RawModule') {
identifier = getRawModuleIdentifier(m, this.dir)
} else if (
m.constructor &&
m.constructor.name === 'MultiModule'
) {
identifier = getMultiModuleIdentifier(m)
} else {
throw new Error(
`Do not know how to handle module: ${
m.name
}, ${m.identifier && m.identifier()}, ${
m.type
}, ${m.constructor && m.constructor.name}`
)
}
// This hashing algorithm is consistent with how the rest of
// webpack does it (n.b. HashedModuleIdsPlugin)
......
......@@ -60,6 +60,7 @@
"babel-plugin-transform-async-to-promises": "0.8.7",
"babel-plugin-transform-react-remove-prop-types": "0.4.15",
"chalk": "2.4.2",
"escape-string-regexp": "1.0.5",
"find-cache-dir": "2.0.0",
"find-up": "2.1.0",
"fresh": "0.5.2",
......@@ -104,6 +105,7 @@
"@types/babel__template": "7.0.1",
"@types/babel__traverse": "7.0.3",
"@types/cross-spawn": "6.0.0",
"@types/escape-string-regexp": "1.0.0",
"@types/etag": "1.8.0",
"@types/fresh": "0.5.0",
"@types/loader-utils": "1.1.3",
......
......@@ -1571,6 +1571,11 @@
dependencies:
"@types/node" "*"
"@types/escape-string-regexp@1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/escape-string-regexp/-/escape-string-regexp-1.0.0.tgz#052d16d87db583b72daceae4eaabddb66954424c"
integrity sha512-KAruqgk9/340M4MYYasdBET+lyYN8KMXUuRKWO72f4SbmIMMFp9nnJiXUkJS0HC2SFe4x0R/fLozXIzqoUfSjA==
"@types/etag@1.8.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@types/etag/-/etag-1.8.0.tgz#37f0b1f3ea46da7ae319bbedb607e375b4c99f7e"
......@@ -4780,7 +4785,7 @@ escape-html@~1.0.3:
resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988"
integrity sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=
escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
escape-string-regexp@1.0.5, escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment