Merge pull request #15413 from cool-little-fish/fix-15274

fix: use relative path in source map for context module
This commit is contained in:
Tobias Koppers 2022-03-08 08:07:54 +01:00 committed by GitHub
commit 0d685d4e0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 53 additions and 8 deletions

View File

@ -3328,7 +3328,8 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
dependencyTemplates,
runtimeTemplate,
runtime,
codeGenerationResults: results
codeGenerationResults: results,
compilation: this
});
} catch (err) {
errors.push(new CodeGenerationError(module, err));

View File

@ -19,7 +19,11 @@ const {
keepOriginalOrder,
compareModulesById
} = require("./util/comparators");
const { contextify, parseResource } = require("./util/identifier");
const {
contextify,
parseResource,
makePathsRelative
} = require("./util/identifier");
const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("webpack-sources").Source} Source */
@ -1066,9 +1070,21 @@ module.exports = webpackEmptyAsyncContext;`;
return this.getSourceForEmptyContext(id, runtimeTemplate);
}
getSource(sourceString) {
/**
* @param {string} sourceString source content
* @param {Compilation=} compilation the compilation
* @returns {Source} generated source
*/
getSource(sourceString, compilation) {
if (this.useSourceMap || this.useSimpleSourceMap) {
return new OriginalSource(sourceString, this.identifier());
return new OriginalSource(
sourceString,
`webpack://${makePathsRelative(
(compilation && compilation.compiler.context) || "",
this.identifier(),
compilation && compilation.compiler.root
)}`
);
}
return new RawSource(sourceString);
}
@ -1078,11 +1094,14 @@ module.exports = webpackEmptyAsyncContext;`;
* @returns {CodeGenerationResult} result
*/
codeGeneration(context) {
const { chunkGraph } = context;
const { chunkGraph, compilation } = context;
const sources = new Map();
sources.set(
"javascript",
this.getSource(this.getSourceString(this.options.mode, context))
this.getSource(
this.getSourceString(this.options.mode, context),
compilation
)
);
const set = new Set();
const allDeps = /** @type {ContextElementDependency[]} */ (

View File

@ -49,6 +49,7 @@ const makeSerializable = require("./util/makeSerializable");
* @property {string=} type the type of source that should be generated
*/
// TODO webpack 6: compilation will be required in CodeGenerationContext
/**
* @typedef {Object} CodeGenerationContext
* @property {DependencyTemplates} dependencyTemplates the dependency templates
@ -58,6 +59,7 @@ const makeSerializable = require("./util/makeSerializable");
* @property {RuntimeSpec} runtime the runtimes code should be generated for
* @property {ConcatenationScope=} concatenationScope when in concatenated module, information about other concatenated modules
* @property {CodeGenerationResults} codeGenerationResults code generation results of other modules (need to have a codeGenerationDependency to use that)
* @property {Compilation=} compilation the compilation
*/
/**

View File

@ -738,7 +738,7 @@ exports[`StatsTestCases should print correct stats for concat-and-sideeffects 1`
exports[`StatsTestCases should print correct stats for context-independence 1`] = `
"asset main-1aad2f42f93e93c4e0b4.js 12.7 KiB [emitted] [immutable] (name: main)
sourceMap main-1aad2f42f93e93c4e0b4.js.map 11.1 KiB [emitted] [dev] (auxiliary name: main)
sourceMap main-1aad2f42f93e93c4e0b4.js.map 11 KiB [emitted] [dev] (auxiliary name: main)
asset 695-4dd37417c69a0af66bac.js 455 bytes [emitted] [immutable]
sourceMap 695-4dd37417c69a0af66bac.js.map 342 bytes [emitted] [dev]
runtime modules 6.6 KiB 9 modules
@ -754,7 +754,7 @@ built modules 500 bytes [built]
webpack x.x.x compiled successfully in X ms
asset main-1aad2f42f93e93c4e0b4.js 12.7 KiB [emitted] [immutable] (name: main)
sourceMap main-1aad2f42f93e93c4e0b4.js.map 11.1 KiB [emitted] [dev] (auxiliary name: main)
sourceMap main-1aad2f42f93e93c4e0b4.js.map 11 KiB [emitted] [dev] (auxiliary name: main)
asset 695-4dd37417c69a0af66bac.js 455 bytes [emitted] [immutable]
sourceMap 695-4dd37417c69a0af66bac.js.map 342 bytes [emitted] [dev]
runtime modules 6.6 KiB 9 modules

View File

@ -0,0 +1 @@
module.exports = "a";

View File

@ -0,0 +1 @@
module.exports = "b";

View File

@ -0,0 +1,9 @@
const foo = Math.random() > 0.5 ? "a" : "b";
require(`./foo/${foo}.js`);
it("context module should use relative path in source map file", () => {
var fs = require("fs");
var source = fs.readFileSync(__filename + ".map", "utf-8");
var map = JSON.parse(source);
expect(map.sources).toContain("webpack:///./foo/ sync ^\\.\\/.*\\.js$");
});

View File

@ -0,0 +1,7 @@
module.exports = {
node: {
__dirname: false,
__filename: false
},
devtool: "source-map"
};

5
types.d.ts vendored
View File

@ -1239,6 +1239,11 @@ declare interface CodeGenerationContext {
* code generation results of other modules (need to have a codeGenerationDependency to use that)
*/
codeGenerationResults: CodeGenerationResults;
/**
* the compilation
*/
compilation?: Compilation;
}
declare interface CodeGenerationResult {
/**