forked from expo/expo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
taskfile-swc.js
69 lines (58 loc) · 1.92 KB
/
taskfile-swc.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
// Based on Next.js swc taskr file.
// https://github.com/vercel/next.js/blob/5378db8f807dbb9ff0993662f0a39d0f6cba2452/packages/next/taskfile-swc.js
const { transform } = require('@swc/core');
const path = require('path');
module.exports = function (task) {
task.plugin('swc', {}, function* (file, environment, { stripExtension } = {}) {
// Don't compile .d.ts
if (file.base.endsWith('.d.ts')) return;
const setting = {
output: 'build',
options: {
module: {
type: 'commonjs',
},
env: {
targets: {
node: '14.17.6',
},
},
jsc: {
loose: true,
parser: {
syntax: 'typescript',
dynamicImport: true,
},
},
},
};
const filePath = path.join(file.dir, file.base);
const inputFilePath = path.join(__dirname, filePath);
const outputFilePath = path.dirname(path.join(__dirname, setting.output, filePath));
const options = {
filename: path.join(file.dir, file.base),
sourceMaps: true,
sourceFileName: path.relative(outputFilePath, inputFilePath),
...setting.options,
};
const output = yield transform(file.data.toString('utf-8'), options);
const ext = path.extname(file.base);
// Replace `.ts|.tsx` with `.js` in files with an extension
if (ext) {
const extRegex = new RegExp(ext.replace('.', '\\.') + '$', 'i');
// Remove the extension if stripExtension is enabled or replace it with `.js`
file.base = file.base.replace(extRegex, stripExtension ? '' : '.js');
}
if (output.map) {
const map = `${file.base}.map`;
output.code += Buffer.from(`\n//# sourceMappingURL=${map}`);
// add sourcemap to `files` array
this._.files.push({
base: map,
dir: file.dir,
data: Buffer.from(output.map),
});
}
file.data = Buffer.from(output.code);
});
};