forked from webpack/webpack
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a reproduction for issue webpack#17747
- Loading branch information
Showing
8 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"asset":"./asset.txt"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { urlToRequest } = require("loader-utils"); | ||
const { createResolve } = require("./util.js"); | ||
|
||
/** @type {import("../../../types.js").LoaderDefinition<{ i: string }>} */ | ||
module.exports = function loader(entryJson) { | ||
const callback = this.async(); | ||
|
||
main(this, entryJson) | ||
.then((result) => { | ||
callback(null, result); | ||
}) | ||
.catch((error) => { | ||
callback(error); | ||
}); | ||
}; | ||
|
||
async function main(loader, entryJson) { | ||
loader.cacheable(); | ||
const resolve = createResolve(loader); | ||
|
||
const entry = JSON.parse(entryJson); | ||
entry.asset = await resolve(urlToRequest(entry.asset)); | ||
|
||
return JSON.stringify(entry); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module.exports = function Issue17747Plugin() { | ||
const loaderPath = require.resolve("./loader.js"); | ||
|
||
this.apply = (compiler) => { | ||
const { context, webpack } = compiler; | ||
const { EntryPlugin } = webpack; | ||
|
||
compiler.hooks.make.tapPromise("Issue17747Plugin", handleMake); | ||
|
||
async function handleMake(compilation) { | ||
const {outputOptions: { publicPath: optionsPublicPath }} = compilation; | ||
const publicPath = optionsPublicPath === "auto" ? "" : optionsPublicPath; | ||
const outputOptions = { filename: "[name]", publicPath }; | ||
|
||
const childCompiler = compilation.createChildCompiler( | ||
"Issue17747PluginChildCompiler", | ||
outputOptions, | ||
); | ||
childCompiler.context = context; | ||
|
||
const load = new EntryPlugin( | ||
context, | ||
`!!${loaderPath}!./entry.json`, | ||
"issue-17747-chunk", | ||
); | ||
load.apply(childCompiler); | ||
|
||
const [, childCompilation] = await new Promise( | ||
(resolve, reject) => { | ||
childCompiler.runAsChild((error, ...args) => { | ||
error ? reject(error) : resolve(args); | ||
}); | ||
}, | ||
); | ||
|
||
if (childCompilation.errors.length > 0) { | ||
const errorDetails = childCompilation.errors | ||
.map(({ message, error }) => message + (error ? `:\n${error}` : "")) | ||
.join("\n"); | ||
|
||
throw new Error(`Child compilation failed:\n${errorDetails}`); | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const { interpolateName } = require("loader-utils"); | ||
|
||
module.exports = { | ||
createResolve, | ||
}; | ||
|
||
function createResolve(loader) { | ||
return function resolve(request) { | ||
return new Promise((resolve, reject) => { | ||
loader.resolve(loader.context, request, (error, inputFilePath) => { | ||
if (error) { | ||
reject(error); | ||
|
||
return; | ||
} | ||
|
||
loader.addDependency(inputFilePath); | ||
loader.loadModule(inputFilePath, (error, source, sourceMap) => { | ||
if (error) { | ||
reject(error); | ||
|
||
return; | ||
} | ||
|
||
let { | ||
assetModuleFilename, | ||
hashDigest, | ||
hashDigestLength, | ||
hashFunction, | ||
} = loader._compilation.outputOptions; | ||
|
||
assetModuleFilename = assetModuleFilename.replace( | ||
/\[ext\]/gi, | ||
".[ext]", | ||
); | ||
assetModuleFilename = assetModuleFilename.replace( | ||
/\[(?:([^:\]]+):)?(hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\]/gi, | ||
( | ||
_, | ||
fn = hashFunction, | ||
type, | ||
digest = hashDigest, | ||
length = hashDigestLength, | ||
) => { | ||
return `[${fn}:${type}:${digest}:${length}]`; | ||
}, | ||
); | ||
|
||
const outputFilePath = interpolateName( | ||
{ | ||
resourcePath: inputFilePath, | ||
}, | ||
assetModuleFilename, | ||
{ | ||
context: loader.context, | ||
content: source, | ||
}, | ||
); | ||
|
||
loader.emitFile(outputFilePath, source, sourceMap); | ||
|
||
resolve(outputFilePath); | ||
}); | ||
}); | ||
}); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const Issue17747Plugin = require("./plugin.js"); | ||
|
||
/** @type {import("../../../").Configuration} */ | ||
module.exports = { | ||
mode: "production", | ||
entry: "./index.js", | ||
plugins: [new Issue17747Plugin()], | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.(json|txt)$/, | ||
type: "asset/resource" | ||
} | ||
] | ||
} | ||
}; |