mozilla / source-map

Consume and generate source maps.
Other
3.57k stars 360 forks source link

TypeError when creating a SourceMapGenerator from a SourceMapConsumer of an index map #345

Open rbuckton opened 6 years ago

rbuckton commented 6 years ago

When calling SourceMapGenerator.fromSourceMap with a SourceMapConsumer for an index map, the following error is thrown:

TypeError: Cannot read property 'replace' of undefined
    at Object.relative (C:\dev\scratch\source-map-test\node_modules\source-map\lib\util.js:255:17)
    at C:\dev\scratch\source-map-test\node_modules\source-map\lib\source-map-generator.js:75:31
    at Array.forEach (<anonymous>)
    at Function.fromSourceMap (C:\dev\scratch\source-map-test\node_modules\source-map\lib\source-map-generator.js:72:32)
    at test (C:\dev\scratch\source-map-test\test.js:17:52)
    at <anonymous>

test.js

const sourcemap = require("source-map");
async function test() {
    const map = { 
        file: "generated.js",
        version: 3,
        sections: [
            { offset: { column: 0, line: 0 }, map: {
                file: "part.js",
                version: 3,
                sources: ["source.js"],
                mappings: "",
                names: []
            } }
        ]
    };
    const consumer = await new sourcemap.SourceMapConsumer(map);
    const generator = sourcemap.SourceMapGenerator.fromSourceMap(consumer); // TypeError
}

The error seems to be due to a strict inequality (!==) check on sourceRoot whereas all other checks against sourceRoot use loose inequality (!=).

rbuckton commented 6 years ago

Actually, it also seems to stem from the fact that the first line of SourceMapGenerator.fromSourceMap reads aSourceMapConsumer.sourceRoot, but a SourceMapConsumer for an index map does not have a sourceRoot.

rbuckton commented 6 years ago

There is a workaround for this issue:

    const consumer = await new sourcemap.SourceMapConsumer(map);
    consumer.sourceRoot = null; // Workaround
    const generator = sourcemap.SourceMapGenerator.fromSourceMap(consumer); // TypeError

However, there is still a TypeError due to another issue in the underlying implementation:

Error: "source.js" is not in the SourceMap.
    at IndexedSourceMapConsumer.sourceContentFor (C:\dev\scratch\source-map-test\node_modules\source-map\lib\source-map-consumer.js:980:11)
    at C:\dev\scratch\source-map-test\node_modules\source-map\lib\source-map-generator.js:82:42
    at Array.forEach (<anonymous>)
    at Function.fromSourceMap (C:\dev\scratch\source-map-test\node_modules\source-map\lib\source-map-generator.js:72:32)
    at test (C:\dev\scratch\source-map-test\test.js:18:52)
    at <anonymous>

It seems fromSourceMap is calling sourceContentFor without passing true for the nullOnMissing param.

jgcmarins commented 6 years ago

I am also facing this:

92% chunk asset optimization 92% chunk asset optimization UglifyJSPlugin/node_modules/uglifyjs-webpack-plugin/node_modules/source-map/lib/source-map-consumer.js:20
  return sourceMap.sections != null
                  ^

TypeError: Cannot read property 'sections' of null
    at new SourceMapConsumer (/node_modules/uglifyjs-webpack-plugin/node_modules/source-map/lib/source-map-consumer.js:20:19)
    at UglifyJsPlugin.buildError (/node_modules/uglifyjs-webpack-plugin/dist/index.js:116:23)
    at /node_modules/uglifyjs-webpack-plugin/dist/index.js:275:45
    at Array.forEach (native)
    at /node_modules/uglifyjs-webpack-plugin/dist/index.js:259:19
    at step (/node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:90:11)
    at done (/node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:99:22)
    at /node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:105:15
    at /node_modules/worker-farm/lib/farm.js:191:19
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)