Skip to content

Commit f45809a

Browse files
committed
fix: explicitly fullfill json module to avoid json module dep issue in npm package
1 parent f456d9d commit f45809a

4 files changed

Lines changed: 81 additions & 2 deletions

File tree

src/trace.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import hackMoment from './transformers/hack-moment';
33
import alias from './transformers/alias';
44
import conventionalAlias from './transformers/conventional-alias';
55
import text from './transformers/text';
6+
import json from './transformers/json';
67
import wasm from './transformers/wasm';
78
import esmToCjs from './transformers/esm-to-cjs';
89
import replace from './transformers/replace';
@@ -56,6 +57,8 @@ export default function(unit, opts = {}) {
5657
);
5758
} else if (extname === '.wasm') {
5859
transformers.push(wasm);
60+
} else if (extname === '.json') {
61+
transformers.push(json);
5962
} else {
6063
// use text! for everything else including unknown extname
6164
transformers.push(text);

src/transformers/json.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// wrap html/svg/css into amd
2+
export default function (unit) {
3+
const {moduleId, contents, sourceMap, path} = unit;
4+
5+
const newUnit = {
6+
defined: [moduleId, 'text!' + moduleId],
7+
contents: `define('text!${moduleId}',function(){return ${JSON.stringify(contents)};});define('${moduleId}',['text!${moduleId}'],function(m){return JSON.parse(m);});`,
8+
deps: []
9+
};
10+
11+
if (!sourceMap) {
12+
const filename = path.replace(/\\/g, '/');
13+
// an identity source map to just showing the original file in browser dev console.
14+
// there would not be any runtime error leading to this mapping.
15+
newUnit.sourceMap = {
16+
version: 3,
17+
file: filename,
18+
sources: [filename],
19+
mappings: 'AAAA',
20+
names: [],
21+
sourcesContent: [contents]
22+
};
23+
}
24+
25+
return newUnit;
26+
}

test/trace.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ test('trace transforms json', t => {
214214
trace(unit).then(traced => {
215215
t.deepEqual(traced, {
216216
path: 'src/foo/bar.json',
217-
contents: "define('text!foo/bar.json',function(){return \"{\\\"a\\\":1}\";});",
217+
contents: "define('text!foo/bar.json',function(){return \"{\\\"a\\\":1}\";});define('foo/bar.json',['text!foo/bar.json'],function(m){return JSON.parse(m);});",
218218
sourceMap: {
219219
version: 3,
220220
file: 'src/foo/bar.json',
@@ -224,7 +224,7 @@ test('trace transforms json', t => {
224224
sourcesContent: [ '{"a":1}' ]
225225
},
226226
moduleId: 'foo/bar.json',
227-
defined: ['text!foo/bar.json'],
227+
defined: ['foo/bar.json', 'text!foo/bar.json'],
228228
deps: []
229229
})
230230
t.end();

test/transformers/json.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import test from 'tape';
2+
import json from '../../src/transformers/json';
3+
4+
test('json wraps json into amd module', t => {
5+
const source = '{"a":1}';
6+
const target = "define('text!a.json',function(){return \"{\\\"a\\\":1}\";});define('a.json',['text!a.json'],function(m){return JSON.parse(m);});";
7+
8+
t.deepEqual(json({
9+
moduleId: 'a.json',
10+
contents: source,
11+
path: 'src/a.json'
12+
}), {
13+
defined: ['a.json', 'text!a.json'],
14+
deps: [],
15+
contents: target,
16+
sourceMap: {
17+
version: 3,
18+
file: 'src/a.json',
19+
sources: ['src/a.json'],
20+
mappings: 'AAAA',
21+
names: [],
22+
sourcesContent: [source]
23+
}
24+
});
25+
t.end();
26+
});
27+
28+
test('json skips identity source map when there is existing map', t => {
29+
const source = '{"a":1}';
30+
const target = "define('text!a.json',function(){return \"{\\\"a\\\":1}\";});define('a.json',['text!a.json'],function(m){return JSON.parse(m);});";
31+
32+
t.deepEqual(json({
33+
moduleId: 'a.json',
34+
contents: source,
35+
path: 'src/a.json',
36+
sourceMap: {
37+
version: 3,
38+
file: 'src/a.json',
39+
sources: ['src/a.json'],
40+
mappings: '',
41+
names: [],
42+
sourcesContent: [source]
43+
}
44+
}), {
45+
defined: ['a.json', 'text!a.json'],
46+
deps: [],
47+
contents: target
48+
});
49+
t.end();
50+
});

0 commit comments

Comments
 (0)