mirror of https://github.com/webpack/webpack.git
				
				
				
			Merge pull request #10182 from webpack/bugfix/handle-source-root
handle sourceRoot during source map contextify
This commit is contained in:
		
						commit
						bc96b6640f
					
				|  | @ -56,6 +56,7 @@ const makeSerializable = require("./util/makeSerializable"); | |||
|  * @returns {string} new source path | ||||
|  */ | ||||
| const contextifySourceUrl = (context, source, associatedObjectForCache) => { | ||||
| 	if (source.startsWith("webpack://")) return source; | ||||
| 	return `webpack://${contextify(context, source, associatedObjectForCache)}`; | ||||
| }; | ||||
| 
 | ||||
|  | @ -67,11 +68,26 @@ const contextifySourceUrl = (context, source, associatedObjectForCache) => { | |||
|  */ | ||||
| const contextifySourceMap = (context, sourceMap, associatedObjectForCache) => { | ||||
| 	if (!Array.isArray(sourceMap.sources)) return sourceMap; | ||||
| 	const sourceRoot = sourceMap.sourceRoot; | ||||
| 	/** @type {function(string): string} */ | ||||
| 	const mapper = !sourceRoot | ||||
| 		? source => source | ||||
| 		: sourceRoot.endsWith("/") | ||||
| 		? source => | ||||
| 				source.startsWith("/") | ||||
| 					? `${sourceRoot.slice(0, -1)}${source}` | ||||
| 					: `${sourceRoot}${source}` | ||||
| 		: source => | ||||
| 				source.startsWith("/") | ||||
| 					? `${sourceRoot}${source}` | ||||
| 					: `${sourceRoot}/${source}`; | ||||
| 	const newSources = sourceMap.sources.map(source => | ||||
| 		contextifySourceUrl(context, source, associatedObjectForCache) | ||||
| 		contextifySourceUrl(context, mapper(source), associatedObjectForCache) | ||||
| 	); | ||||
| 	return { | ||||
| 		...sourceMap, | ||||
| 		file: "x", | ||||
| 		sourceRoot: undefined, | ||||
| 		sources: newSources | ||||
| 	}; | ||||
| }; | ||||
|  |  | |||
|  | @ -684,52 +684,52 @@ exports[`StatsTestCases should print correct stats for concat-and-sideeffects 1` | |||
| `; | ||||
| 
 | ||||
| exports[`StatsTestCases should print correct stats for context-independence 1`] = ` | ||||
| "Hash: 7bc8b93f783425f912017bc8b93f783425f912012178c4311212dff34c272178c4311212dff34c27 | ||||
| "Hash: 3c59c3d45edbe20ee0af3c59c3d45edbe20ee0af39303b7d3ef1ce28f20139303b7d3ef1ce28f201 | ||||
| Child | ||||
|     Hash: 7bc8b93f783425f91201 | ||||
|     Hash: 3c59c3d45edbe20ee0af | ||||
|     Time: Xms | ||||
|     Built at: 1970-04-20 12:42:42 | ||||
|                                Asset       Size | ||||
|          703-97267ca95e3cfbc183d4.js  438 bytes  [emitted] [immutable] | ||||
|      703-97267ca95e3cfbc183d4.js.map  343 bytes  [emitted] [dev] | ||||
|         main-aeb159b6fc904b34b7a7.js   8.02 KiB  [emitted] [immutable]  [name: main] | ||||
|     main-aeb159b6fc904b34b7a7.js.map   7.14 KiB  [emitted] [dev]        [name: (main)] | ||||
|     Entrypoint main = main-aeb159b6fc904b34b7a7.js (main-aeb159b6fc904b34b7a7.js.map) | ||||
|          703-35b05b4f9ece3e5b7253.js  438 bytes  [emitted] [immutable] | ||||
|      703-35b05b4f9ece3e5b7253.js.map  343 bytes  [emitted] [dev] | ||||
|         main-322147d992bb0a885b10.js   8.02 KiB  [emitted] [immutable]  [name: main] | ||||
|     main-322147d992bb0a885b10.js.map   7.14 KiB  [emitted] [dev]        [name: (main)] | ||||
|     Entrypoint main = main-322147d992bb0a885b10.js (main-322147d992bb0a885b10.js.map) | ||||
|     ./a/index.js 40 bytes [built] | ||||
|     ./a/chunk.js + 1 modules 66 bytes [built] | ||||
|         + 6 hidden modules | ||||
| Child | ||||
|     Hash: 7bc8b93f783425f91201 | ||||
|     Hash: 3c59c3d45edbe20ee0af | ||||
|     Time: Xms | ||||
|     Built at: 1970-04-20 12:42:42 | ||||
|                                Asset       Size | ||||
|          703-97267ca95e3cfbc183d4.js  438 bytes  [emitted] [immutable] | ||||
|      703-97267ca95e3cfbc183d4.js.map  343 bytes  [emitted] [dev] | ||||
|         main-aeb159b6fc904b34b7a7.js   8.02 KiB  [emitted] [immutable]  [name: main] | ||||
|     main-aeb159b6fc904b34b7a7.js.map   7.14 KiB  [emitted] [dev]        [name: (main)] | ||||
|     Entrypoint main = main-aeb159b6fc904b34b7a7.js (main-aeb159b6fc904b34b7a7.js.map) | ||||
|          703-35b05b4f9ece3e5b7253.js  438 bytes  [emitted] [immutable] | ||||
|      703-35b05b4f9ece3e5b7253.js.map  343 bytes  [emitted] [dev] | ||||
|         main-322147d992bb0a885b10.js   8.02 KiB  [emitted] [immutable]  [name: main] | ||||
|     main-322147d992bb0a885b10.js.map   7.14 KiB  [emitted] [dev]        [name: (main)] | ||||
|     Entrypoint main = main-322147d992bb0a885b10.js (main-322147d992bb0a885b10.js.map) | ||||
|     ./b/index.js 40 bytes [built] | ||||
|     ./b/chunk.js + 1 modules 66 bytes [built] | ||||
|         + 6 hidden modules | ||||
| Child | ||||
|     Hash: 2178c4311212dff34c27 | ||||
|     Hash: 39303b7d3ef1ce28f201 | ||||
|     Time: Xms | ||||
|     Built at: 1970-04-20 12:42:42 | ||||
|                            Asset      Size | ||||
|      703-60017c0a0b42e422e290.js  1.49 KiB  [emitted] [immutable] | ||||
|     main-96b332b4af5b68dc9590.js  8.91 KiB  [emitted] [immutable]  [name: main] | ||||
|     Entrypoint main = main-96b332b4af5b68dc9590.js | ||||
|      703-d460da0006247c80e6fd.js  1.49 KiB  [emitted] [immutable] | ||||
|     main-d716298143ad43b551dc.js  8.91 KiB  [emitted] [immutable]  [name: main] | ||||
|     Entrypoint main = main-d716298143ad43b551dc.js | ||||
|     ./a/index.js 40 bytes [built] | ||||
|     ./a/chunk.js + 1 modules 66 bytes [built] | ||||
|         + 6 hidden modules | ||||
| Child | ||||
|     Hash: 2178c4311212dff34c27 | ||||
|     Hash: 39303b7d3ef1ce28f201 | ||||
|     Time: Xms | ||||
|     Built at: 1970-04-20 12:42:42 | ||||
|                            Asset      Size | ||||
|      703-60017c0a0b42e422e290.js  1.49 KiB  [emitted] [immutable] | ||||
|     main-96b332b4af5b68dc9590.js  8.91 KiB  [emitted] [immutable]  [name: main] | ||||
|     Entrypoint main = main-96b332b4af5b68dc9590.js | ||||
|      703-d460da0006247c80e6fd.js  1.49 KiB  [emitted] [immutable] | ||||
|     main-d716298143ad43b551dc.js  8.91 KiB  [emitted] [immutable]  [name: main] | ||||
|     Entrypoint main = main-d716298143ad43b551dc.js | ||||
|     ./b/index.js 40 bytes [built] | ||||
|     ./b/chunk.js + 1 modules 66 bytes [built] | ||||
|         + 6 hidden modules" | ||||
|  |  | |||
|  | @ -0,0 +1,19 @@ | |||
| it("should run", () => { | ||||
| 	require("./loader-source-root!"); | ||||
| 	require("./loader-source-root-slash!"); | ||||
| 	require("./loader-source-root-source-slash!"); | ||||
| 	require("./loader-source-root-2-slash!"); | ||||
| 	require("./loader-no-source-root!"); | ||||
| 	require("./loader-pre-relative!"); | ||||
| }); | ||||
| 
 | ||||
| it("should generate the correct SourceMap", function() { | ||||
| 	var fs = require("fs"); | ||||
| 	var source = JSON.parse(fs.readFileSync(__filename + ".map", "utf-8")); | ||||
| 	expect(source.sources).toContain("webpack:///./folder/test1.txt"); | ||||
| 	expect(source.sources).toContain("webpack:///./folder/test2.txt"); | ||||
| 	expect(source.sources).toContain("webpack:///./folder/test3.txt"); | ||||
| 	expect(source.sources).toContain("webpack:///./folder/test4.txt"); | ||||
| 	expect(source.sources).toContain("webpack:///./folder/test5.txt"); | ||||
| 	expect(source.sources).toContain("webpack:///./folder/test6.txt"); | ||||
| }); | ||||
|  | @ -0,0 +1,10 @@ | |||
| const path = require("path"); | ||||
| module.exports = function() { | ||||
| 	this.callback(null, "module.exports = 'ok';", { | ||||
| 		version: 3, | ||||
| 		file: "/should/be/removed", | ||||
| 		sources: [path.join(__dirname, "folder", "test5.txt")], | ||||
| 		sourcesContent: ["Test"], | ||||
| 		mappings: "AAAA" | ||||
| 	}); | ||||
| }; | ||||
|  | @ -0,0 +1,9 @@ | |||
| module.exports = function() { | ||||
| 	this.callback(null, "module.exports = 'ok';", { | ||||
| 		version: 3, | ||||
| 		file: "/should/be/removed", | ||||
| 		sources: ["webpack://./folder/test6.txt"], | ||||
| 		sourcesContent: ["Test"], | ||||
| 		mappings: "AAAA" | ||||
| 	}); | ||||
| }; | ||||
|  | @ -0,0 +1,11 @@ | |||
| const path = require("path"); | ||||
| module.exports = function() { | ||||
| 	this.callback(null, "module.exports = 'ok';", { | ||||
| 		version: 3, | ||||
| 		file: "/should/be/removed", | ||||
| 		sourceRoot: path.join(__dirname, "folder") + "/", | ||||
| 		sources: ["/test4.txt"], | ||||
| 		sourcesContent: ["Test"], | ||||
| 		mappings: "AAAA" | ||||
| 	}); | ||||
| }; | ||||
|  | @ -0,0 +1,11 @@ | |||
| const path = require("path"); | ||||
| module.exports = function() { | ||||
| 	this.callback(null, "module.exports = 'ok';", { | ||||
| 		version: 3, | ||||
| 		file: "/should/be/removed", | ||||
| 		sourceRoot: path.join(__dirname, "folder") + "/", | ||||
| 		sources: ["test3.txt"], | ||||
| 		sourcesContent: ["Test"], | ||||
| 		mappings: "AAAA" | ||||
| 	}); | ||||
| }; | ||||
|  | @ -0,0 +1,11 @@ | |||
| const path = require("path"); | ||||
| module.exports = function() { | ||||
| 	this.callback(null, "module.exports = 'ok';", { | ||||
| 		version: 3, | ||||
| 		file: "/should/be/removed", | ||||
| 		sourceRoot: path.join(__dirname, "folder"), | ||||
| 		sources: ["/test2.txt"], | ||||
| 		sourcesContent: ["Test"], | ||||
| 		mappings: "AAAA" | ||||
| 	}); | ||||
| }; | ||||
|  | @ -0,0 +1,11 @@ | |||
| const path = require("path"); | ||||
| module.exports = function() { | ||||
| 	this.callback(null, "module.exports = 'ok';", { | ||||
| 		version: 3, | ||||
| 		file: "/should/be/removed", | ||||
| 		sourceRoot: path.join(__dirname, "folder"), | ||||
| 		sources: ["test1.txt"], | ||||
| 		sourcesContent: ["Test"], | ||||
| 		mappings: "AAAA" | ||||
| 	}); | ||||
| }; | ||||
|  | @ -0,0 +1,8 @@ | |||
| module.exports = { | ||||
| 	mode: "development", | ||||
| 	node: { | ||||
| 		__dirname: false, | ||||
| 		__filename: false | ||||
| 	}, | ||||
| 	devtool: "source-map" | ||||
| }; | ||||
		Loading…
	
		Reference in New Issue