Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rbuckton opened this issue Jun 12, 2018 · 3 comments
Labels
needs-triage Unconfirmed issues

Comments

@rbuckton
Copy link

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
Copy link
Author

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
Copy link
Author

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
Copy link

jgcmarins commented Jun 15, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Unconfirmed issues
Projects
None yet
Development

No branches or pull requests

3 participants