Skip to content

Commit

Permalink
find fixture basic index at runtime
Browse files Browse the repository at this point in the history
In webpack 5.0.0, the mapObj.sources are

[
  'webpack://babel-loader/./test/fixtures/basic.js',
  'webpack://babel-loader/./test/fixtures/constant.js',
  'webpack://babel-loader/./test/fixtures/import.js',
  'webpack://babel-loader/webpack/bootstrap',
  'webpack://babel-loader/webpack/startup'
]

In later versions the `'webpack://babel-loader/./test/fixtures/basic.js'` is moved after webpack bootstrap, causing different indices across different webpack versions.
  • Loading branch information
JLHwung committed Jul 28, 2023
1 parent 0d361b3 commit 2d15893
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions test/sourcemaps.test.js
Expand Up @@ -100,10 +100,14 @@ test("should output webpack's sourcemap properly when set 'inline'", async t =>
const data = fs.readFileSync(path.resolve(t.context.directory, map[0]));
const mapObj = JSON.parse(data);

t.is(mapObj.sources[3], "webpack://babel-loader/./test/fixtures/basic.js");
const fixtureBasicIndex = mapObj.sources.indexOf(
"webpack://babel-loader/./test/fixtures/basic.js",
);
// The index may vary across webpack versions
t.not(fixtureBasicIndex, -1);

// Ensure that the map contains the original code, not the compiled src.
t.falsy(mapObj.sourcesContent[3].includes("__esModule"));
t.falsy(mapObj.sourcesContent[fixtureBasicIndex].includes("__esModule"));
});

test("should output webpack's devtoolModuleFilename option", async t => {
Expand Down Expand Up @@ -188,11 +192,15 @@ test("should disable sourcemap output with 'sourceMaps:false'", async t => {
const data = fs.readFileSync(path.resolve(t.context.directory, map[0]));
const mapObj = JSON.parse(data);

t.is(mapObj.sources[3], "webpack://babel-loader/./test/fixtures/basic.js");
const fixtureBasicIndex = mapObj.sources.indexOf(
"webpack://babel-loader/./test/fixtures/basic.js",
);
// The index may vary across webpack versions
t.not(fixtureBasicIndex, -1);

// Ensure that the code contains Babel's compiled output, because
// sourcemaps from Babel are disabled.
t.truthy(mapObj.sourcesContent[3].includes("__esModule"));
t.truthy(mapObj.sourcesContent[fixtureBasicIndex].includes("__esModule"));
});

test("should disable sourcemap output with 'sourceMap:false'", async t => {
Expand Down Expand Up @@ -228,9 +236,13 @@ test("should disable sourcemap output with 'sourceMap:false'", async t => {
const data = fs.readFileSync(path.resolve(t.context.directory, map[0]));
const mapObj = JSON.parse(data);

t.is(mapObj.sources[3], "webpack://babel-loader/./test/fixtures/basic.js");
const fixtureBasicIndex = mapObj.sources.indexOf(
"webpack://babel-loader/./test/fixtures/basic.js",
);
// The index may vary across webpack versions
t.not(fixtureBasicIndex, -1);

// Ensure that the code contains Babel's compiled output, because
// sourcemaps from Babel are disabled.
t.truthy(mapObj.sourcesContent[3].includes("__esModule"));
t.truthy(mapObj.sourcesContent[fixtureBasicIndex].includes("__esModule"));
});

0 comments on commit 2d15893

Please sign in to comment.