11import path from "path" ;
22
3+ import { SourceMapConsumer } from "source-map" ;
4+
35import {
46 compile ,
57 getCompiler ,
68 getErrors ,
79 getModuleSource ,
810 getWarnings ,
911} from "./helpers" ;
12+ import readAsset from "./helpers/readAsset" ;
1013
1114describe ( "loader" , ( ) => {
1215 it ( "should work with a string value" , async ( ) => {
@@ -307,7 +310,7 @@ describe("loader", () => {
307310 expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
308311 } ) ;
309312
310- it ( 'should work with source maps when the "devtool" option is enabled' , async ( ) => {
313+ it ( 'should generate source maps when the "devtool" option is enabled' , async ( ) => {
311314 const compiler = getCompiler (
312315 "some-library.js" ,
313316 { } ,
@@ -322,30 +325,39 @@ describe("loader", () => {
322325 loader : path . resolve ( __dirname , "../src" ) ,
323326 options : { imports : "lib_1" } ,
324327 } ,
325- {
326- loader : "babel-loader" ,
327- } ,
328328 ] ,
329329 } ,
330330 ] ,
331331 } ,
332332 }
333333 ) ;
334334 const stats = await compile ( compiler ) ;
335-
336- expect ( getModuleSource ( "./some-library.js" , stats ) ) . toMatchSnapshot (
337- "module"
335+ const bundle = readAsset (
336+ "main.bundle.js" ,
337+ stats . compilation . compiler ,
338+ stats
339+ ) . split ( "\n" ) ;
340+ const sourceMap = readAsset (
341+ "main.bundle.js.map" ,
342+ stats . compilation . compiler ,
343+ stats
338344 ) ;
339- expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
340- expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
345+
346+ const consumer = new SourceMapConsumer ( sourceMap ) ;
347+ const result = consumer . generatedPositionFor ( {
348+ line : 1 ,
349+ column : 0 ,
350+ source : "webpack://ImportsLoader/some-library.js" ,
351+ } ) ;
352+ expect ( bundle [ result . line - 1 /* 1-indexed */ ] ) . toEqual ( "var someCode = {" ) ;
341353 } ) ;
342354
343- it ( 'should not work with source maps when the "devtool" options are disabled ' , async ( ) => {
355+ it ( 'should update source maps from previous loaders when the "devtool" option is enabled ' , async ( ) => {
344356 const compiler = getCompiler (
345357 "some-library.js" ,
346358 { } ,
347359 {
348- devtool : false ,
360+ devtool : "source-map" ,
349361 module : {
350362 rules : [
351363 {
@@ -355,22 +367,32 @@ describe("loader", () => {
355367 loader : path . resolve ( __dirname , "../src" ) ,
356368 options : { imports : "lib_1" } ,
357369 } ,
358- {
359- loader : "babel-loader" ,
360- } ,
370+ { loader : "babel-loader" } ,
361371 ] ,
362372 } ,
363373 ] ,
364374 } ,
365375 }
366376 ) ;
367377 const stats = await compile ( compiler ) ;
368-
369- expect ( getModuleSource ( "./some-library.js" , stats ) ) . toMatchSnapshot (
370- "module"
378+ const bundle = readAsset (
379+ "main.bundle.js" ,
380+ stats . compilation . compiler ,
381+ stats
382+ ) . split ( "\n" ) ;
383+ const sourceMap = readAsset (
384+ "main.bundle.js.map" ,
385+ stats . compilation . compiler ,
386+ stats
371387 ) ;
372- expect ( getErrors ( stats ) ) . toMatchSnapshot ( "errors" ) ;
373- expect ( getWarnings ( stats ) ) . toMatchSnapshot ( "warnings" ) ;
388+
389+ const consumer = new SourceMapConsumer ( sourceMap ) ;
390+ const result = consumer . generatedPositionFor ( {
391+ line : 1 ,
392+ column : 0 ,
393+ source : "webpack://ImportsLoader/some-library.js" ,
394+ } ) ;
395+ expect ( bundle [ result . line - 1 /* 1-indexed */ ] ) . toEqual ( "var someCode = {" ) ;
374396 } ) ;
375397
376398 it ( 'should work with "default" imports without syntax' , async ( ) => {
0 commit comments