-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.js
175 lines (161 loc) · 4.84 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
const Metalsmith = require('metalsmith');
const metalsmithBabel = require('.');
const test = require('tape');
test('metalsmith-babel', t => {
t.plan(20);
new Metalsmith('.')
.use(metalsmithBabel())
.run({
'non-js.txt': {contents: Buffer.from('Hi')},
'typescript.ts': {contents: Buffer.from('null ;')},
'source.js': {contents: Buffer.from('( ) => 1')}
}, (err, files) => {
t.equal(err, null, 'should be used as a metalsmith plugin.');
t.equal(
files['non-js.txt'].contents.toString(),
'Hi',
'should not transform non-JavaScript files.'
);
t.equal(
files['typescript.ts'].contents.toString(),
'null ;',
'should not transform TypeScript files when @babel/preset-typescript is not loaded.'
);
t.equal(
files['source.js'].contents.toString(),
'() => 1;',
'should transform JavaScript files.'
);
});
new Metalsmith('.')
.use(metalsmithBabel({
presets: ['@babel/react'],
plugins: ['@babel/plugin-proposal-function-bind'],
minified: true,
sourceMaps: true,
sourceRoot: 'dir'
}))
.run({
'dir/source.jsx': {contents: Buffer.from('a::b(<p />)')}
}, (err, files) => {
t.equal(err, null, 'should support Babel options.');
t.notOk('dir/source.jsx' in files, 'should rename .jsx file to .js.');
t.equal(
files['dir/source.js'].contents.toString(),
'var _context;(_context=a,b).call(_context,React.createElement("p",null));\n//# sourceMappingURL=source.js.map\n',
'should append a source map URL to the bottom of code.'
);
t.equal(
files['dir/source.js.map'].contents.toString(),
JSON.stringify({
version: 3,
sources: ['source.jsx'],
names: ['a', 'b'],
mappings: 'aAAA,UAAAA,CAAC,CAAEC,CAAH,gBAAK,6BAAL',
sourceRoot: 'dir',
sourcesContent: ['a::b(<p />)'],
file: 'dir/source.js'
}),
'should create a source map file.'
);
});
new Metalsmith('.')
.use(metalsmithBabel({sourceMap: 'both'}))
.run({
'🐟/🐠.mjs': {contents: Buffer.from('1')}
}, (err, files) => {
t.equal(err, null, 'should support .mjs files.');
t.equal(
files['🐟/🐠.mjs'].contents.toString(),
'1;\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIvCfkKAubWpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIiwic291cmNlc0NvbnRlbnQiOlsiMSJdfQ==',
'should support `sourceMap` ⇆ `sourceMaps` option alias.'
);
t.equal(
files['🐟/🐠.mjs.map'].contents.toString(),
JSON.stringify({
version: 3,
sources: ['🐠.mjs'],
names: [],
mappings: 'AAAA',
sourcesContent: ['1'],
file: '🐟/🐠.mjs'
}),
'should create a source map file.'
);
});
new Metalsmith('.')
.use(metalsmithBabel({
presets: ['@babel/preset-typescript']
}))
.run({
'source.ts': {contents: Buffer.from('const x: number = 1')}
}, (err, files) => {
t.equal(err, null, 'should support TypeScript when @babel/preset-typescript is loaded.');
t.equal(
files['source.js'].contents.toString(),
'const x = 1;',
'should compile TypeScript when @babel/preset-typescript is loaded.'
);
});
new Metalsmith('.')
.use(metalsmithBabel({
presets: ['@babel/react', '@babel/preset-typescript'],
comments: false
}))
.run({
'source.tsx': {contents: Buffer.from(`declare namespace JSX {
interface IntrinsicElements {
foo: { bar?: boolean }
}
}
<foo bar />;`)}
}, (err, files) => {
t.equal(err, null, 'should support TSX when @babel/preset-typescript is loaded.');
t.equal(
files['source.js'].contents.toString(),
'React.createElement("foo", {\n bar: true\n});',
'should compile TSX when both @babel/preset-react and @babel/preset-typescript are loaded.'
);
});
new Metalsmith('.')
.use(metalsmithBabel())
.run({'FOO.JS': {contents: Buffer.from('1=a')}}, ({code}) => {
t.equal(
code,
'BABEL_PARSE_ERROR',
'should fail when Babel cannot transpile the code.'
);
});
new Metalsmith('.')
.use(metalsmithBabel({
preset: 'must be `preset**s**`'
}))
.run({'source.tsx': {contents: Buffer.from('')}}, err => {
t.ok(
err.toString().startsWith('ReferenceError: Unknown option: .preset.'),
'should fail when it takes an unknown option.'
);
});
new Metalsmith('.')
.use(metalsmithBabel({
plugins: ['babel-plugin-notfound']
}))
.run({'source.ts': {contents: Buffer.from('')}}, ({message}) => {
t.equal(
message,
`Cannot find module 'babel-plugin-notfound' from '${__dirname}'`,
'should fail when Babel cannot resolve the path of a given plugin.'
);
});
t.throws(
() => metalsmithBabel(new Int32Array()),
/^TypeError.*Expdected an options object to set @babel\/core options, but got Int32Array \[\]\./u,
'should throw an error when it takes a non-plain object argument.'
);
t.throws(
() => metalsmithBabel({}, {}),
/^RangeError.*Expected 0 or 1 argument \(\[<Object>\]\), but got 2 arguments\./u,
'should throw an error when it takes too many arguments.'
);
});