forked from parcel-bundler/parcel
/
transpilation.js
332 lines (272 loc) Β· 10.9 KB
/
transpilation.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// @flow
import assert from 'assert';
import path from 'path';
import {
bundle,
distDir,
inputFS as fs,
outputFS,
overlayFS,
run,
ncp,
} from '@parcel/test-utils';
import {symlinkSync} from 'fs';
const inputDir = path.join(__dirname, '/input');
describe('transpilation', function () {
it('should not transpile if no targets are defined', async function () {
await bundle(path.join(__dirname, '/integration/babel-default/index.js'), {
defaultTargetOptions: {
engines: undefined,
shouldOptimize: false,
},
});
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('class Foo'));
assert(file.includes('class Bar'));
});
it('should support transpiling using browserlist', async function () {
await bundle(
path.join(__dirname, '/integration/babel-browserslist/index.js'),
);
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('function Foo'));
assert(file.includes('function Bar'));
});
it('should support transpiling when engines have semver ranges', async () => {
let fixtureDir = path.join(__dirname, '/integration/babel-semver-engine');
await bundle(path.join(fixtureDir, 'index.js'));
let legacy = await outputFS.readFile(
path.join(fixtureDir, 'dist', 'legacy.js'),
'utf8',
);
assert(legacy.includes('function Foo'));
assert(legacy.includes('function Bar'));
let modern = await outputFS.readFile(
path.join(fixtureDir, 'dist', 'modern.js'),
'utf8',
);
assert(modern.includes('class Foo'));
assert(modern.includes('class Bar'));
});
it('should not transpile node_modules by default', async function () {
await bundle(
path.join(__dirname, '/integration/babel-node-modules/index.js'),
);
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(/class \S+ \{/.test(file));
assert(file.includes('function Bar'));
});
it('should not compile node_modules with a source field in package.json when not symlinked', async function () {
await bundle(
path.join(
__dirname,
'/integration/babel-node-modules-source-unlinked/index.js',
),
);
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(!file.includes('function Foo'));
assert(file.includes('function Bar'));
});
it('should support compiling JSX', async function () {
await bundle(path.join(__dirname, '/integration/jsx/index.jsx'));
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('React.createElement("div"'));
assert(file.includes('fileName: "integration/jsx/index.jsx"'));
});
it('should support compiling JSX correctly with member expression type', async function () {
await bundle(path.join(__dirname, '/integration/jsx-member/index.jsx'));
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('React.createElement(S.Foo'));
});
it('should support compiling JSX in JS files with React dependency', async function () {
await bundle(path.join(__dirname, '/integration/jsx-react/index.js'));
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('React.createElement("div"'));
});
it('should support compiling JSX with pure annotations', async function () {
let b = await bundle(
path.join(__dirname, '/integration/jsx-react/pure-comment.js'),
);
let file = await outputFS.readFile(
path.join(distDir, 'pure-comment.js'),
'utf8',
);
assert(file.includes('/*#__PURE__*/ _reactDefault.default.createElement'));
let res = await run(b);
assert(res.Foo());
});
it('should support compiling JSX in JS files with React aliased to Preact', async function () {
await bundle(path.join(__dirname, '/integration/jsx-react-alias/index.js'));
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('h("div"'));
});
it('should support compiling JSX in JS files with Preact dependency', async function () {
await bundle(path.join(__dirname, '/integration/jsx-preact/index.js'));
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('h("div"'));
});
it('should support compiling JSX in TS files with Preact dependency', async function () {
let b = await bundle(
path.join(__dirname, '/integration/jsx-preact-ts/index.tsx'),
);
assert(typeof (await run(b)) === 'object');
});
it('should support compiling JSX in JS files with Nerv dependency', async function () {
await bundle(path.join(__dirname, '/integration/jsx-nervjs/index.js'));
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('Nerv.createElement("div"'));
});
it('should support compiling JSX in JS files with Hyperapp dependency', async function () {
await bundle(path.join(__dirname, '/integration/jsx-hyperapp/index.js'));
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('h("div"'));
});
it('should not transpile spread in JSX with modern targets', async function () {
let b = await bundle(
path.join(__dirname, '/integration/jsx-spread/index.jsx'),
);
let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('React.createElement("div"'));
assert(file.includes('...a'));
assert(!file.includes('@swc/helpers'));
});
it('should support the automatic JSX runtime with React >= 17', async function () {
let b = await bundle(
path.join(__dirname, '/integration/jsx-automatic/index.js'),
);
let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('react/jsx-runtime'));
assert(file.includes('_jsxRuntime.jsx("div"'));
});
it('should support the automatic JSX runtime with preact >= 10.5', async function () {
let b = await bundle(
path.join(__dirname, '/integration/jsx-automatic-preact/index.js'),
);
let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('preact/jsx-runtime'));
assert(file.includes('_jsxRuntime.jsx("div"'));
});
it('should support the automatic JSX runtime with preact with alias', async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/jsx-automatic-preact-with-alias/index.js',
),
);
let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('preact/jsx-runtime'));
assert(file.includes('_jsxRuntime.jsx("div"'));
});
it('should support the automatic JSX runtime with explicit tsconfig.json', async function () {
let b = await bundle(
path.join(__dirname, '/integration/jsx-automatic-tsconfig/index.js'),
);
let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('preact/jsx-runtime'));
assert(file.includes('_jsxRuntime.jsx("div"'));
});
it('should support explicit JSX pragma in tsconfig.json', async function () {
let b = await bundle(
path.join(__dirname, '/integration/jsx-pragma-tsconfig/index.js'),
);
let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('JSX(JSXFragment'));
assert(file.includes('JSX("div"'));
});
it('should support explicitly enabling JSX in tsconfig.json', async function () {
let b = await bundle(
path.join(__dirname, '/integration/jsx-tsconfig/index.js'),
);
let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(file.includes('React.createElement("div"'));
});
it('should support enabling decorators in tsconfig.json', async function () {
let b = await bundle(
path.join(__dirname, '/integration/decorators/index.ts'),
);
let output = [];
await run(b, {
output(o) {
output.push(o);
},
});
assert.deepEqual(output, [
'first(): factory evaluated',
'second(): factory evaluated',
'second(): called',
'first(): called',
]);
});
it('should support transpiling optional chaining', async function () {
let b = await bundle(
path.join(__dirname, '/integration/babel-optional-chaining/index.js'),
);
let file = await outputFS.readFile(b.getBundles()[0].filePath, 'utf8');
assert(!file.includes('?.'));
let output = await run(b);
assert.equal(typeof output, 'object');
assert.deepEqual(output.default, [undefined, undefined]);
});
it('should only include necessary parts of core-js using browserlist', async function () {
await bundle(path.join(__dirname, '/integration/babel-core-js/index.js'));
let file = await outputFS.readFile(path.join(distDir, 'index.js'), 'utf8');
// console.log(file)
assert(file.includes('async function Bar() {'));
// Check that core-js's globalThis polyfill is referenced.
// NOTE: This may change if core-js internals change.
assert(file.includes('esnext.global-this'));
assert(!file.includes('es.array.concat'));
});
it('should resolve @swc/helpers and regenerator-runtime relative to parcel', async function () {
let dir = path.join('/tmp/' + Math.random().toString(36).slice(2));
await outputFS.mkdirp(dir);
await ncp(path.join(__dirname, '/integration/swc-helpers'), dir);
await bundle(path.join(dir, 'index.js'), {
mode: 'production',
inputFS: overlayFS,
defaultTargetOptions: {
engines: {
browsers: '>= 0.25%',
},
},
});
});
describe('tests needing the real filesystem', () => {
afterEach(async () => {
if (process.platform === 'win32') {
return;
}
try {
await fs.rimraf(inputDir);
await fs.rimraf(distDir);
} catch (e) {
// ignore
}
});
it('should compile node_modules when symlinked with a source field in package.json', async function () {
if (process.platform === 'win32') {
this.skip();
return;
}
const inputDir = path.join(__dirname, '/input');
await fs.rimraf(inputDir);
await fs.mkdirp(path.join(inputDir, 'node_modules'));
await fs.ncp(
path.join(
path.join(__dirname, '/integration/babel-node-modules-source'),
),
inputDir,
);
// Create the symlink here to prevent cross platform and git issues
symlinkSync(
path.join(inputDir, 'packages/foo'),
path.join(inputDir, 'node_modules/foo'),
'dir',
);
await bundle(inputDir + '/index.js', {outputFS: fs});
let file = await fs.readFile(path.join(distDir, 'index.js'), 'utf8');
assert(file.includes('function Foo'));
assert(file.includes('function Bar'));
});
});
});