Skip to content

Commit 25d2d31

Browse files
test: mcep and contenthash
1 parent 8fe8bf6 commit 25d2d31

File tree

3 files changed

+304
-1
lines changed

3 files changed

+304
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"stylus": "^0.54.8",
9292
"stylus-loader": "^3.0.2",
9393
"url-loader": "^4.1.0",
94-
"webpack": "^4.44.0"
94+
"webpack": "^4.44.1"
9595
},
9696
"keywords": [
9797
"webpack",

test/__snapshots__/sourceMap-option.test.js.snap

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,160 @@ Array [
194194

195195
exports[`"sourceMap" option true should generate source maps #2: warnings 1`] = `Array []`;
196196

197+
exports[`"sourceMap" option true should generate source maps and do not change "[contenthash]" on different platform: errors 1`] = `Array []`;
198+
199+
exports[`"sourceMap" option true should generate source maps and do not change "[contenthash]" on different platform: module 1`] = `
200+
"// Imports
201+
import ___CSS_LOADER_API_IMPORT___ from \\"../../../src/runtime/api.js\\";
202+
import ___CSS_LOADER_AT_RULE_IMPORT_0___ from \\"-!../../../src/index.js??[ident]!./nested/nested.css\\";
203+
var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___(true);
204+
___CSS_LOADER_EXPORT___.i(___CSS_LOADER_AT_RULE_IMPORT_0___);
205+
// Module
206+
___CSS_LOADER_EXPORT___.push([module.id, \\".class {\\\\n color: red;\\\\n}\\\\n\\", \\"\\",{\\"version\\":3,\\"sources\\":[\\"webpack:///source-map/basic.css\\"],\\"names\\":[],\\"mappings\\":\\"AAEA;EACE,UAAU;AACZ\\",\\"sourcesContent\\":[\\"@import \\\\\\"./nested/nested.css\\\\\\";\\\\n\\\\n.class {\\\\n color: red;\\\\n}\\\\n\\"]}]);
207+
// Exports
208+
export default ___CSS_LOADER_EXPORT___;
209+
"
210+
`;
211+
212+
exports[`"sourceMap" option true should generate source maps and do not change "[contenthash]" on different platform: result 1`] = `
213+
Array [
214+
Array [
215+
"../../src/index.js?[ident]!./source-map/nested/nested.css",
216+
".nested {
217+
color: blue;
218+
}
219+
",
220+
"",
221+
Object {
222+
"mappings": "AAAA;EACE,WAAW;AACb",
223+
"names": Array [],
224+
"sources": Array [
225+
"webpack:///source-map/nested/nested.css",
226+
],
227+
"sourcesContent": Array [
228+
".nested {
229+
color: blue;
230+
}
231+
",
232+
],
233+
"version": 3,
234+
},
235+
],
236+
Array [
237+
"./source-map/basic.css",
238+
".class {
239+
color: red;
240+
}
241+
",
242+
"",
243+
Object {
244+
"mappings": "AAEA;EACE,UAAU;AACZ",
245+
"names": Array [],
246+
"sources": Array [
247+
"webpack:///source-map/basic.css",
248+
],
249+
"sourcesContent": Array [
250+
"@import \\"./nested/nested.css\\";
251+
252+
.class {
253+
color: red;
254+
}
255+
",
256+
],
257+
"version": 3,
258+
},
259+
],
260+
]
261+
`;
262+
263+
exports[`"sourceMap" option true should generate source maps and do not change "[contenthash]" on different platform: warnings 1`] = `Array []`;
264+
265+
exports[`"sourceMap" option true should generate source maps when css was extracted and do not change "[contenthash]" on different platform: errors 1`] = `Array []`;
266+
267+
exports[`"sourceMap" option true should generate source maps when css was extracted and do not change "[contenthash]" on different platform: extracted css 1`] = `
268+
".nested {
269+
color: blue;
270+
}
271+
272+
.class {
273+
color: red;
274+
}
275+
276+
277+
/*# sourceMappingURL=main.994a357f71e1a515446a.css.map*/"
278+
`;
279+
280+
exports[`"sourceMap" option true should generate source maps when css was extracted and do not change "[contenthash]" on different platform: source map 1`] = `
281+
Object {
282+
"file": "main.994a357f71e1a515446a.css",
283+
"mappings": "AAAA;EACE,WAAW;AACb;;ACAA;EACE,UAAU;AACZ",
284+
"names": Array [],
285+
"sourceRoot": "",
286+
"sources": Array [
287+
"webpack:////source-map/nested/nested.css",
288+
"webpack:////source-map/basic.css",
289+
],
290+
"sourcesContent": Array [
291+
".nested {
292+
color: blue;
293+
}
294+
",
295+
"@import \\"./nested/nested.css\\";
296+
297+
.class {
298+
color: red;
299+
}
300+
",
301+
],
302+
"version": 3,
303+
}
304+
`;
305+
306+
exports[`"sourceMap" option true should generate source maps when css was extracted and do not change "[contenthash]" on different platform: warnings 1`] = `Array []`;
307+
308+
exports[`"sourceMap" option true should generate source maps when css was extracted: errors 1`] = `Array []`;
309+
310+
exports[`"sourceMap" option true should generate source maps when css was extracted: extracted css 1`] = `
311+
".nested {
312+
color: blue;
313+
}
314+
315+
.class {
316+
color: red;
317+
}
318+
319+
320+
/*# sourceMappingURL=main.css.map*/"
321+
`;
322+
323+
exports[`"sourceMap" option true should generate source maps when css was extracted: source map 1`] = `
324+
Object {
325+
"file": "main.css",
326+
"mappings": "AAAA;EACE,WAAW;AACb;;ACAA;EACE,UAAU;AACZ",
327+
"names": Array [],
328+
"sourceRoot": "",
329+
"sources": Array [
330+
"webpack:////source-map/nested/nested.css",
331+
"webpack:////source-map/basic.css",
332+
],
333+
"sourcesContent": Array [
334+
".nested {
335+
color: blue;
336+
}
337+
",
338+
"@import \\"./nested/nested.css\\";
339+
340+
.class {
341+
color: red;
342+
}
343+
",
344+
],
345+
"version": 3,
346+
}
347+
`;
348+
349+
exports[`"sourceMap" option true should generate source maps when css was extracted: warnings 1`] = `Array []`;
350+
197351
exports[`"sourceMap" option true should generate source maps when previous loader does not generate source maps: errors 1`] = `Array []`;
198352

199353
exports[`"sourceMap" option true should generate source maps when previous loader does not generate source maps: module 1`] = `

test/sourceMap-option.test.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import path from 'path';
22

3+
import webpack from 'webpack';
34
import postcssPresetEnv from 'postcss-preset-env';
5+
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
46

57
import {
68
compile,
@@ -9,6 +11,7 @@ import {
911
getExecutedCode,
1012
getModuleSource,
1113
getWarnings,
14+
readAsset,
1215
} from './helpers/index';
1316

1417
jest.setTimeout(10000);
@@ -380,6 +383,152 @@ describe('"sourceMap" option', () => {
380383
expect(getWarnings(stats)).toMatchSnapshot('warnings');
381384
expect(getErrors(stats)).toMatchSnapshot('errors');
382385
});
386+
387+
it('should generate source maps and do not change "[contenthash]" on different platform', async () => {
388+
const compiler = getCompiler(
389+
'./source-map/basic.js',
390+
{},
391+
{
392+
output: {
393+
path: path.resolve(__dirname, '../outputs'),
394+
filename: '[name].[contenthash].bundle.js',
395+
chunkFilename: '[name].[contenthash].chunk.js',
396+
publicPath: '/webpack/public/path/',
397+
},
398+
module: {
399+
rules: [
400+
{
401+
test: /\.css$/i,
402+
rules: [
403+
{
404+
loader: path.resolve(__dirname, '../src'),
405+
options: { sourceMap: true },
406+
},
407+
],
408+
},
409+
],
410+
},
411+
}
412+
);
413+
const stats = await compile(compiler);
414+
const [chunkName] = Object.keys(stats.compilation.assets);
415+
416+
expect(chunkName).toBe(
417+
webpack.version[0] === '5'
418+
? 'main.1486d6842beed772ed7b.bundle.js'
419+
: 'main.6edc30724d77c84b9674.bundle.js'
420+
);
421+
expect(getModuleSource('./source-map/basic.css', stats)).toMatchSnapshot(
422+
'module'
423+
);
424+
expect(getExecutedCode(chunkName, compiler, stats)).toMatchSnapshot(
425+
'result'
426+
);
427+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
428+
expect(getErrors(stats)).toMatchSnapshot('errors');
429+
});
430+
431+
it('should generate source maps when css was extracted', async () => {
432+
const compiler = getCompiler(
433+
'./source-map/basic.js',
434+
{},
435+
{
436+
devtool: 'source-map',
437+
output: {
438+
path: path.resolve(__dirname, '../outputs'),
439+
filename: '[name].bundle.js',
440+
chunkFilename: '[name].chunk.js',
441+
publicPath: '/webpack/public/path/',
442+
},
443+
plugins: [
444+
new MiniCssExtractPlugin({
445+
filename: '[name].css',
446+
}),
447+
],
448+
module: {
449+
rules: [
450+
{
451+
test: /\.css$/i,
452+
rules: [
453+
{
454+
loader: MiniCssExtractPlugin.loader,
455+
},
456+
{
457+
loader: path.resolve(__dirname, '../src'),
458+
options: { sourceMap: true },
459+
},
460+
],
461+
},
462+
],
463+
},
464+
}
465+
);
466+
const stats = await compile(compiler);
467+
468+
expect(readAsset('main.css', compiler, stats)).toMatchSnapshot(
469+
'extracted css'
470+
);
471+
expect(
472+
JSON.parse(readAsset('main.css.map', compiler, stats))
473+
).toMatchSnapshot('source map');
474+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
475+
expect(getErrors(stats)).toMatchSnapshot('errors');
476+
});
477+
478+
it('should generate source maps when css was extracted and do not change "[contenthash]" on different platform', async () => {
479+
const compiler = getCompiler(
480+
'./source-map/basic.js',
481+
{},
482+
{
483+
devtool: 'source-map',
484+
output: {
485+
path: path.resolve(__dirname, '../outputs'),
486+
filename: '[name].bundle.js',
487+
chunkFilename: '[name].chunk.js',
488+
publicPath: '/webpack/public/path/',
489+
},
490+
plugins: [
491+
new MiniCssExtractPlugin({
492+
filename: '[name].[contenthash].css',
493+
}),
494+
],
495+
module: {
496+
rules: [
497+
{
498+
test: /\.css$/i,
499+
rules: [
500+
{
501+
loader: MiniCssExtractPlugin.loader,
502+
},
503+
{
504+
loader: path.resolve(__dirname, '../src'),
505+
options: { sourceMap: true },
506+
},
507+
],
508+
},
509+
],
510+
},
511+
}
512+
);
513+
const stats = await compile(compiler);
514+
const chunkName = Object.keys(
515+
stats.compilation.assets
516+
).find((assetName) => /\.css/.test(assetName));
517+
518+
expect(chunkName).toBe(
519+
webpack.version[0] === '5'
520+
? 'main.65d6a11b49507d66bf3b.css'
521+
: 'main.994a357f71e1a515446a.css'
522+
);
523+
expect(readAsset(chunkName, compiler, stats)).toMatchSnapshot(
524+
'extracted css'
525+
);
526+
expect(
527+
JSON.parse(readAsset(`${chunkName}.map`, compiler, stats))
528+
).toMatchSnapshot('source map');
529+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
530+
expect(getErrors(stats)).toMatchSnapshot('errors');
531+
});
383532
});
384533

385534
describe('false', () => {

0 commit comments

Comments
 (0)