-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
82 lines (75 loc) · 2.57 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict';
const Metalsmith = require('metalsmith');
const buble = require('.');
const test = require('tape');
test('metalsmith-buble', t => {
t.plan(11);
new Metalsmith('.')
.use(buble())
.run({
'source.js': {contents: Buffer.from('() => 1')},
'non-js.txt': {contents: Buffer.from('Hi')}
}, (err, files) => {
t.strictEqual(err, null, 'should be used as a metalsmith plugin.');
t.strictEqual(
String(files['source.js'].contents),
'!function() { return 1; }',
'should transform JavaScript files.'
);
t.strictEqual(
String(files['non-js.txt'].contents),
'Hi',
'should not transform non-JavaScript files.'
);
});
new Metalsmith('.')
.use(buble({sourceMap: true}))
.run({
'dir/source.jsx': {contents: Buffer.from('<div />')}
}, (err, files) => {
t.strictEqual(err, null, 'should support JSX.');
t.strictEqual(
String(files['dir/source.js'].contents),
'React.createElement( \'div\', null )\n//# sourceMappingURL=source.js.map\n',
'should append a URL to the bottom of code when `sourceMap` option is true.'
);
t.strictEqual(
String(files['dir/source.js.map'].contents),
JSON.stringify({
version: 3,
file: 'source.js',
sources: ['../../src/dir/source.jsx'],
sourcesContent: ['<div />'],
names: [],
mappings: 'AAAA,sBAAC,UAAG'
}),
'should create a source map file.'
);
t.notOk(Object.keys(files).includes('dir/source.jsx'), 'should rename .jsx file to .js.');
});
new Metalsmith('.')
.use(buble({
target: {firefox: 43},
transforms: {arrow: true},
sourceMap: 'inline'
}))
.run({'☺️.js': {contents: Buffer.from('let x = y => `z`')}}, (err, files) => {
t.strictEqual(err, null, 'should support non-ASCII filename.');
t.strictEqual(
String(files['☺️.js'].contents),
`var x = function (y) { return \`z\`; }
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoi4pi677iPLmpzIiwic291cmNlcyI6WyIuLi9zcmMv4pi677iPLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImxldCB4ID0geSA9PiBgemAiXSwibmFtZXMiOlsibGV0Il0sIm1hcHBpbmdzIjoiQUFBQUEsR0FBRyxDQUFDLENBQUMsYUFBRyxFQUFDLENBQUMsU0FBRyxDQUFDLENBQUMifQ==`,
'should support `target` and `transforms` options.'
);
});
new Metalsmith('.')
.use(buble({sourceMap: false}))
.run({'FOO.JS': {contents: Buffer.from('1=a')}}, err => {
t.ok(err instanceof SyntaxError, 'should fail when Buble cannot transpile the code.');
});
t.throws(
() => buble({sourceMap: [1, 2, 3]}),
/^TypeError.* `sourceMap` option must be true, false or 'inline', but got \[ 1, 2, 3 ]\./,
'should throw a type error when it takes an invalid `sourceMap` option value.'
);
});