Skip to content

Commit dfefc11

Browse files
authored
feat(terser): support .map files in directory inputs (#1250)
1 parent 5e43e18 commit dfefc11

7 files changed

Lines changed: 96 additions & 15 deletions

File tree

packages/terser/src/index.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,27 @@ function isDirectory(input) {
2929
/**
3030
* Replaces directory url with the outputFile name in the url option of source-map argument
3131
*/
32-
function directoryArgs(residualArgs, outputFile) {
32+
function directoryArgs(residualArgs, inputFile, outputFile) {
3333
const sourceMapIndex = residualArgs.indexOf('--source-map');
3434
if (sourceMapIndex === -1) {
3535
return residualArgs;
3636
}
3737

38-
const sourceMapOptions = residualArgs[sourceMapIndex + 1].split(',');
39-
const newSourceMapOptions = sourceMapOptions.map(
38+
// set the correct sourcemap url for this output file
39+
let sourceMapOptions = residualArgs[sourceMapIndex + 1].split(',').map(
4040
o => o.startsWith('url=') ? `url='${path.basename(outputFile)}.map'` : o);
4141

42+
// if an input .map file exists then set the correct sourcemap content option
43+
if (fs.existsSync(`${inputFile}.map`)) {
44+
// even on Windows terser expects '/' path separators so we normalize these in the sourcemap
45+
// content file path below
46+
sourceMapOptions = sourceMapOptions.map(
47+
o => o.startsWith('content=') ? `content='${inputFile.replace(/\\/g, '/')}.map'` : o);
48+
}
49+
4250
return [
4351
...residualArgs.slice(0, sourceMapIndex + 1),
44-
newSourceMapOptions.join(','),
52+
sourceMapOptions.join(','),
4553
...residualArgs.slice(sourceMapIndex + 2),
4654
];
4755
}
@@ -59,8 +67,10 @@ function terserDirectory(input, output, residual, terserBinary) {
5967

6068
function exec([inputFile, outputFile]) {
6169
active++;
62-
let args =
63-
[terserBinary, inputFile, '--output', outputFile, ...directoryArgs(residual, outputFile)];
70+
let args = [
71+
terserBinary, inputFile, '--output', outputFile,
72+
...directoryArgs(residual, inputFile, outputFile)
73+
];
6474

6575
spawn(process.execPath, args)
6676
.then(

packages/terser/test/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")
33
jasmine_node_test(
44
name = "test",
55
srcs = ["directory-args.spec.js"],
6-
deps = ["@npm_bazel_terser//:index.js"],
6+
deps = [
7+
"@npm//tmp",
8+
"@npm_bazel_terser//:index.js",
9+
],
710
)

packages/terser/test/directory-args.spec.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
const {directoryArgs} = require('npm_bazel_terser/index')
2+
const fs = require('fs');
3+
const path = require('path');
4+
const tmp = require('tmp');
25

36
describe('directoryArgs', () => {
47
it('return a new array ref', () => {
5-
const args = ['--source-map', ''];
8+
const args = ['--source-map', '', ''];
69
expect(directoryArgs(args, '')).not.toBe(args);
710
});
811

@@ -11,18 +14,32 @@ describe('directoryArgs', () => {
1114
expect(directoryArgs(args)).toBe(args);
1215
});
1316

14-
it('should replace the directory url with the file url', () => {
17+
it('should set the correct file url and souremap content', () => {
18+
const out = tmp.dirSync().name;
19+
const input = path.join(out, 'file.js');
20+
const output = '/test/file.js';
1521
const args = [
1622
'--ie8',
1723
'--source-map',
18-
`root='http://foo.com/src',url='some_wrong_name'`,
24+
`root='http://foo.com/src',url='some_wrong_name',content=inline`,
1925
'--keep-fnames',
2026
];
21-
const output = '/test/file.js';
22-
expect(directoryArgs(args, output)).toEqual([
27+
// if no corresponding map file exists then sourcemap content should
28+
// be left as inline
29+
expect(directoryArgs(args, input, output)).toEqual([
30+
'--ie8',
31+
'--source-map',
32+
`root='http://foo.com/src',url='${path.basename(output)}.map',content=inline`,
33+
'--keep-fnames',
34+
]);
35+
// if a corresponding map file exists then sourcemap content should be set
36+
// to the map file
37+
fs.writeFileSync(`${input}.map`, '');
38+
expect(directoryArgs(args, input, output)).toEqual([
2339
'--ie8',
2440
'--source-map',
25-
`root='http://foo.com/src',url='file.js.map'`,
41+
`root='http://foo.com/src',url='${path.basename(output)}.map',content='${
42+
input.replace(/\\/g, '/')}.map'`,
2643
'--keep-fnames',
2744
]);
2845
});

packages/terser/test/directory_input/BUILD.bazel

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
load("@build_bazel_rules_nodejs//:tools/declare_directory.bzl", "declare_directory")
12
load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")
23
load("@npm_bazel_terser//:index.from_src.bzl", "terser_minified")
3-
load(":rule.bzl", "declare_directory")
44

5-
# Check that filegroups work
65
declare_directory(
76
name = "dir",
87
srcs = glob(["input*.js"]),

packages/terser/test/sourcemap/BUILD.bazel

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@build_bazel_rules_nodejs//:tools/declare_directory.bzl", "declare_directory")
12
load("@npm_bazel_jasmine//:index.from_src.bzl", "jasmine_node_test")
23
load("@npm_bazel_terser//:index.from_src.bzl", "terser_minified")
34

@@ -24,3 +25,27 @@ jasmine_node_test(
2425
":src1.min",
2526
],
2627
)
28+
29+
declare_directory(
30+
name = "dir",
31+
srcs = [
32+
"src1.js",
33+
"src1.js.map",
34+
],
35+
)
36+
37+
terser_minified(
38+
name = "dir.min",
39+
src = "dir",
40+
)
41+
42+
jasmine_node_test(
43+
name = "directory_test",
44+
srcs = [
45+
"directory_spec.js",
46+
],
47+
data = ["@npm//source-map"],
48+
deps = [
49+
":dir.min",
50+
],
51+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const fs = require('fs');
2+
const sm = require('source-map');
3+
const path = require('path');
4+
const {runfiles} = require('build_bazel_rules_nodejs/internal/linker');
5+
6+
describe('terser on a directory with map files', () => {
7+
it('should produce an output for each input', () => {
8+
const out = runfiles.resolvePackageRelative('dir.min');
9+
expect(fs.existsSync(out + '/src1.js')).toBeTruthy();
10+
});
11+
12+
it('should produce a sourcemap output', async () => {
13+
const out = runfiles.resolvePackageRelative('dir.min');
14+
const file = require.resolve(out + '/src1.js.map');
15+
const rawSourceMap = JSON.parse(fs.readFileSync(file, 'utf-8'));
16+
await sm.SourceMapConsumer.with(rawSourceMap, null, consumer => {
17+
const pos = consumer.originalPositionFor(
18+
// position of MyClass in terser_minified output src1.min.js
19+
// depends on DEBUG flag
20+
!process.env['DEBUG'] ? {line: 1, column: 18} : {line: 3, column: 5});
21+
expect(pos.source).toBe('src1.ts');
22+
expect(pos.line).toBe(2);
23+
expect(pos.column).toBe(14);
24+
expect(pos.name).toBe('MyClass');
25+
});
26+
});
27+
});
File renamed without changes.

0 commit comments

Comments
 (0)