@@ -7,13 +7,22 @@ var RequestShortener = require("./RequestShortener");
77var Template = require ( "./Template" ) ;
88var ConcatSource = require ( "webpack-core/lib/ConcatSource" ) ;
99var RawSource = require ( "webpack-core/lib/RawSource" ) ;
10+ var CheapOriginalSource = require ( "webpack-core/lib/CheapOriginalSource" )
1011var ModuleFilenameHelpers = require ( "./ModuleFilenameHelpers" ) ;
1112
12- function SourceMapDevToolPlugin ( sourceMapFilename , sourceMappingURLComment , moduleFilenameTemplate , fallbackModuleFilenameTemplate ) {
13- this . sourceMapFilename = sourceMapFilename ;
14- this . sourceMappingURLComment = sourceMappingURLComment === false ? false : sourceMappingURLComment || "\n//# sourceMappingURL=[url]" ;
15- this . moduleFilenameTemplate = moduleFilenameTemplate || "webpack:///[resourcePath]" ;
16- this . fallbackModuleFilenameTemplate = fallbackModuleFilenameTemplate || "webpack:///[resourcePath]?[hash]" ;
13+ function SourceMapDevToolPlugin ( options , sourceMappingURLComment , moduleFilenameTemplate , fallbackModuleFilenameTemplate ) {
14+ if ( typeof options !== "object" ) {
15+ this . sourceMapFilename = options ;
16+ this . sourceMappingURLComment = sourceMappingURLComment === false ? false : sourceMappingURLComment || "\n//# sourceMappingURL=[url]" ;
17+ this . moduleFilenameTemplate = moduleFilenameTemplate || "webpack:///[resourcePath]" ;
18+ this . fallbackModuleFilenameTemplate = fallbackModuleFilenameTemplate || "webpack:///[resourcePath]?[hash]" ;
19+ } else {
20+ this . sourceMapFilename = options . filename ;
21+ this . sourceMappingURLComment = options . append === false ? false : options . append || "\n//# sourceMappingURL=[url]" ;
22+ this . moduleFilenameTemplate = options . moduleFilenameTemplate || "webpack:///[resourcePath]" ;
23+ this . fallbackModuleFilenameTemplate = options . fallbackModuleFilenameTemplate || "webpack:///[resourcePath]?[hash]" ;
24+ this . cheapMode = options . cheapMode ;
25+ }
1726}
1827module . exports = SourceMapDevToolPlugin ;
1928SourceMapDevToolPlugin . prototype . apply = function ( compiler ) {
@@ -22,10 +31,18 @@ SourceMapDevToolPlugin.prototype.apply = function(compiler) {
2231 var moduleFilenameTemplate = this . moduleFilenameTemplate ;
2332 var fallbackModuleFilenameTemplate = this . fallbackModuleFilenameTemplate ;
2433 var requestShortener = new RequestShortener ( compiler . context ) ;
34+ var cheapMode = this . cheapMode ;
2535 compiler . plugin ( "compilation" , function ( compilation ) {
26- compilation . plugin ( "build-module" , function ( module ) {
27- module . useSourceMap = true ;
28- } ) ;
36+ if ( cheapMode ) {
37+ compilation . moduleTemplate . plugin ( "module" , function ( source , module ) {
38+ var str = source . source ( ) ;
39+ return new CheapOriginalSource ( str , module . resource ) ;
40+ } ) ;
41+ } else {
42+ compilation . plugin ( "build-module" , function ( module ) {
43+ module . useSourceMap = true ;
44+ } ) ;
45+ }
2946 compilation . plugin ( "after-optimize-chunk-assets" , function ( chunks ) {
3047 var allModules = [ ] ;
3148 var allModuleFilenames = [ ] ;
@@ -94,10 +111,12 @@ SourceMapDevToolPlugin.prototype.apply = function(compiler) {
94111 var moduleFilenames = task . moduleFilenames ;
95112 var modules = task . modules ;
96113 sourceMap . sources = moduleFilenames ;
97- if ( sourceMap . sourcesContent ) {
114+ if ( sourceMap . sourcesContent && ! cheapMode ) {
98115 sourceMap . sourcesContent = sourceMap . sourcesContent . map ( function ( content , i ) {
99116 return content + "\n\n\n" + ModuleFilenameHelpers . createFooter ( modules [ i ] , requestShortener ) ;
100117 } ) ;
118+ } else {
119+ sourceMap . sourcesContent = undefined ;
101120 }
102121 sourceMap . sourceRoot = "" ;
103122 sourceMap . file = file ;
@@ -126,7 +145,15 @@ SourceMapDevToolPlugin.prototype.apply = function(compiler) {
126145 asset . __SourceMapDevTool_Data [ sourceMapFile ] = this . assets [ sourceMapFile ] = new RawSource ( JSON . stringify ( sourceMap ) ) ;
127146 chunk . files . push ( sourceMapFile ) ;
128147 } else {
129- asset . __SourceMapDevTool_Data [ file ] = this . assets [ file ] = new ConcatSource ( asset , currentSourceMappingURLComment . replace ( / \[ u r l \] / g, "data:application/json;base64," + new Buffer ( JSON . stringify ( sourceMap ) ) . toString ( "base64" ) ) ) ;
148+ asset . __SourceMapDevTool_Data [ file ] = this . assets [ file ] = new ConcatSource ( asset , currentSourceMappingURLComment
149+ . replace ( / \[ m a p \] / g, function ( ) {
150+ return JSON . stringify ( sourceMap ) ;
151+ } )
152+ . replace ( / \[ u r l \] / g, function ( ) {
153+ return "data:application/json;base64," +
154+ new Buffer ( JSON . stringify ( sourceMap ) ) . toString ( "base64" ) ;
155+ } )
156+ ) ;
130157 }
131158 } , this ) ;
132159 } ) ;
0 commit comments