Skip to content

Commit

Permalink
Remove applySourceMap for now
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Mar 4, 2024
1 parent cc2da5c commit dfcccc7
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 268 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 0 additions & 15 deletions src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
toEncodedMap,
setSourceContent,
fromMap,
applySourceMap,
} from '@jridgewell/gen-mapping';

import type {
Expand Down Expand Up @@ -167,20 +166,6 @@ export class SourceMapGenerator {
setSourceContent(this._map, source, content);
}

applySourceMap(
sourceMapConsumer: SourceMapConsumer,
sourceFile?: string,
sourceMapPath?: string,
) {
// TODO :: not sure how sourceMapConsumer should be typed / converted
applySourceMap(
this._map,
new AnyMap(sourceMapConsumer) as unknown as any,
sourceFile,
sourceMapPath,
);
}

toJSON(): ReturnType<typeof toEncodedMap> {
return toEncodedMap(this._map);
}
Expand Down
252 changes: 0 additions & 252 deletions test/source-map-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,255 +247,3 @@ describe('SourceMapGenerator.fromSourceMap', () => {
utils.assertEqualMaps(assert, map.toJSON(), fixtures.testMapMultiSourcesMappingRefersSingleSourceOnly);
});
})


describe('SourceMapGenerator.applySourceMap', () => {
it('test applySourceMap', () => {

var mapStep1 = {
version: 3,
sources: [ 'fileX', 'fileY' ],
names: [],
mappings: 'AACA;;ACAA;;ADDA;;ACAA',
file: 'fileA',
sourcesContent: [ 'lineX1\nlineX2\n', null ]
};

var mapStep2 = {
version: 3,
sources: [ 'fileA', 'fileB' ],
names: [],
mappings: ';AAAA;AACA;AACA;AACA;ACHA;AACA',
file: 'fileGen',
sourcesContent: [ null, 'lineB1\nlineB2\n' ]
};

var expectedMap = {
version: 3,
sources: [ 'fileX', 'fileA', 'fileY', 'fileB' ],
names: [],
mappings: ';AACA;ACAA;ACAA;ADEA;AEHA;AACA',
file: 'fileGen',
sourcesContent: [ 'lineX1\nlineX2\n', null, null, 'lineB1\nlineB2\n' ]
};

// apply source map "mapStep1" to "mapStep2"
var generator = SourceMapGenerator.fromSourceMap(new SourceMapConsumer(mapStep2));
generator.applySourceMap(new SourceMapConsumer(mapStep1));
var actualMap = generator.toJSON();

utils.assertEqualMaps(assert, actualMap, expectedMap);
});

it('test applySourceMap throws when file is missing', () => {
var map = new SourceMapGenerator({
file: 'test.js'
});
var map2 = new SourceMapGenerator();
assert.throws(function() {
map.applySourceMap(new SourceMapConsumer(map2.toJSON()));
});
});

it('test the two additional parameters of applySourceMap', () => {
// Assume the following directory structure:
//
// http://foo.org/
// bar.coffee
// app/
// coffee/
// foo.coffee
// temp/
// bundle.js
// temp_maps/
// bundle.js.map
// public/
// bundle.min.js
// bundle.min.js.map
//
// http://www.example.com/
// baz.coffee

var bundleMap = new SourceMapGenerator({
file: 'bundle.js'
});
bundleMap.addMapping({
generated: { line: 3, column: 3 },
original: { line: 2, column: 2 },
source: '../../coffee/foo.coffee'
});
bundleMap.setSourceContent('../../coffee/foo.coffee', 'foo coffee');
bundleMap.addMapping({
generated: { line: 13, column: 13 },
original: { line: 12, column: 12 },
source: '/bar.coffee'
});
bundleMap.setSourceContent('/bar.coffee', 'bar coffee');
bundleMap.addMapping({
generated: { line: 23, column: 23 },
original: { line: 22, column: 22 },
source: 'http://www.example.com/baz.coffee'
});
bundleMap.setSourceContent(
'http://www.example.com/baz.coffee',
'baz coffee'
);
bundleMap = new SourceMapConsumer(bundleMap.toJSON());

var minifiedMap = new SourceMapGenerator({
file: 'bundle.min.js',
sourceRoot: '..'
});
minifiedMap.addMapping({
generated: { line: 1, column: 1 },
original: { line: 3, column: 3 },
source: 'temp/bundle.js'
});
minifiedMap.addMapping({
generated: { line: 11, column: 11 },
original: { line: 13, column: 13 },
source: 'temp/bundle.js'
});
minifiedMap.addMapping({
generated: { line: 21, column: 21 },
original: { line: 23, column: 23 },
source: 'temp/bundle.js'
});
minifiedMap = new SourceMapConsumer(minifiedMap.toJSON());

var expectedMap = function (sources) {
var map = new SourceMapGenerator({
file: 'bundle.min.js',
sourceRoot: '..'
});
map.addMapping({
generated: { line: 1, column: 1 },
original: { line: 2, column: 2 },
source: sources[0]
});
map.setSourceContent(sources[0], 'foo coffee');
map.addMapping({
generated: { line: 11, column: 11 },
original: { line: 12, column: 12 },
source: sources[1]
});
map.setSourceContent(sources[1], 'bar coffee');
map.addMapping({
generated: { line: 21, column: 21 },
original: { line: 22, column: 22 },
source: sources[2]
});
map.setSourceContent(sources[2], 'baz coffee');
return map.toJSON();
}

var actualMap = function (aSourceMapPath) {
var map = SourceMapGenerator.fromSourceMap(minifiedMap);
// Note that relying on `bundleMap.file` (which is simply 'bundle.js')
// instead of supplying the second parameter wouldn't work here.
map.applySourceMap(bundleMap, '../temp/bundle.js', aSourceMapPath);
return map.toJSON();
}

utils.assertEqualMaps(assert, actualMap('../temp/temp_maps'), expectedMap([
'coffee/foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee'
]));

utils.assertEqualMaps(assert, actualMap('/app/temp/temp_maps'), expectedMap([
'/app/coffee/foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee'
]));

utils.assertEqualMaps(assert, actualMap('http://foo.org/app/temp/temp_maps'), expectedMap([
'http://foo.org/app/coffee/foo.coffee',
'http://foo.org/bar.coffee',
'http://www.example.com/baz.coffee'
]));

// If the third parameter is omitted or set to the current working
// directory we get incorrect source paths:

utils.assertEqualMaps(assert, actualMap(), expectedMap([
'../coffee/foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee'
]));

utils.assertEqualMaps(assert, actualMap(''), expectedMap([
'../coffee/foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee'
]));

utils.assertEqualMaps(assert, actualMap('.'), expectedMap([
'../coffee/foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee'
]));

utils.assertEqualMaps(assert, actualMap('./'), expectedMap([
'../coffee/foo.coffee',
'/bar.coffee',
'http://www.example.com/baz.coffee'
]));
});

it('test applySourceMap name handling', () => {
// Imagine some CoffeeScript code being compiled into JavaScript and then
// minified.

var assertName = function(coffeeName, jsName, expectedName) {
var minifiedMap = new SourceMapGenerator({
file: 'test.js.min'
});
minifiedMap.addMapping({
generated: { line: 1, column: 4 },
original: { line: 1, column: 4 },
source: 'test.js',
name: jsName
});

var coffeeMap = new SourceMapGenerator({
file: 'test.js'
});
coffeeMap.addMapping({
generated: { line: 1, column: 4 },
original: { line: 1, column: 0 },
source: 'test.coffee',
name: coffeeName
});

minifiedMap.applySourceMap(new SourceMapConsumer(coffeeMap.toJSON()));

new SourceMapConsumer(minifiedMap.toJSON()).eachMapping(function(mapping) {
assert.equal(mapping.name, expectedName);
});
};

// `foo = 1` -> `var foo = 1;` -> `var a=1`
// CoffeeScript doesn’t rename variables, so there’s no need for it to
// provide names in its source maps. Minifiers do rename variables and
// therefore do provide names in their source maps. So that name should be
// retained if the original map lacks names.
assertName(null, 'foo', 'foo');

// `foo = 1` -> `var coffee$foo = 1;` -> `var a=1`
// Imagine that CoffeeScript prefixed all variables with `coffee$`. Even
// though the minifier then also provides a name, the original name is
// what corresponds to the source.
assertName('foo', 'coffee$foo', 'foo');

// `foo = 1` -> `var coffee$foo = 1;` -> `var coffee$foo=1`
// Minifiers can turn off variable mangling. Then there’s no need to
// provide names in the source map, but the names from the original map are
// still needed.
assertName('foo', null, 'foo');

// `foo = 1` -> `var foo = 1;` -> `var foo=1`
// No renaming at all.
assertName(null, null, null);
});
})

0 comments on commit dfcccc7

Please sign in to comment.