@@ -5,7 +5,7 @@ const defaults = require('lodash.defaultsdeep');
55const fs = require ( 'fs' ) ;
66const mkdirp = require ( 'mkdirp' ) ;
77const path = require ( 'path' ) ;
8- const srcURL = require ( 'source-map-url ' ) ;
8+ const getSourceMapContent = require ( './get-sourcemap-content ' ) ;
99
1010const terser = require ( 'terser' ) ;
1111
@@ -38,14 +38,9 @@ module.exports = function processFile(inFile, outFile, relativePath, outDir, sil
3838
3939 let sourceMap = { filename, url } ;
4040
41- if ( srcURL . existsIn ( src ) ) {
42- let url = srcURL . getFrom ( src ) ;
43- let sourceMapPath = path . join ( path . dirname ( inFile ) , url ) ;
44- if ( fs . existsSync ( sourceMapPath ) ) {
45- sourceMap . content = JSON . parse ( fs . readFileSync ( sourceMapPath ) ) ;
46- } else if ( ! silent ) {
47- console . warn ( `[WARN] (broccoli-uglify-sourcemap) "${ url } " referenced in "${ relativePath } " could not be found` ) ;
48- }
41+ let content = getSourceMapContent ( src , path . dirname ( inFile ) , relativePath , silent ) ;
42+ if ( content ) {
43+ sourceMap . content = content ;
4944 }
5045
5146 options = defaults ( options , { sourceMap } ) ;
@@ -71,11 +66,22 @@ module.exports = function processFile(inFile, outFile, relativePath, outDir, sil
7166 let newSourceMap = JSON . parse ( result . map ) ;
7267
7368 newSourceMap . sources = newSourceMap . sources . map ( function ( path ) {
74- // If out output file has the same name as one of our original
75- // sources, they will shadow eachother in Dev Tools. So instead we
76- // alter the reference to the upstream file.
7769 if ( path === relativePath ) {
78- path = path . replace ( / \. j s $ / , '-orig.js' ) ;
70+ // If out output file has the same name as one of our original
71+ // sources, they will shadow eachother in Dev Tools. So instead we
72+ // alter the reference to the upstream file.
73+ return path . replace ( / \. j s $ / , '-orig.js' ) ;
74+ } else if ( path === '0' ) {
75+ // In [terser-js](https://github.com/terser-js/terser#source-map-options),
76+ // sources are always 0 if old sourcemaps are not provided.
77+ // The value passed for `sourceMap.url` is only used to set
78+ // `//# sourceMappingURL=out.js.map` in `result.code`.
79+ // The value of `filename` is only used to set `file` attribute
80+ // in source map file.
81+ // In broccoli-uglify-sourcemap we know in this case we are generating
82+ // sourcemap for the file we are processing, changing 0 to the actual
83+ // file gives us the correct source.
84+ return relativePath ;
7985 }
8086 return path ;
8187 } ) ;
0 commit comments