forked from mattdesl/esmify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transform.js
85 lines (71 loc) · 2.58 KB
/
transform.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
const babel = require('@babel/core');
const through = require('through2');
const { PassThrough } = require('stream');
const path = require('path');
const concat = require('concat-stream');
const duplexer = require('duplexer2');
const pluginDynamicImport = require('@babel/plugin-syntax-dynamic-import');
const pluginCJS = require('@babel/plugin-transform-modules-commonjs');
const pluginImportToRequire = require('babel-plugin-import-to-require');
// Gotta add these as well so babel doesn't bail out when it sees new syntax
const pluginSyntaxRestSpread = require('@babel/plugin-syntax-object-rest-spread');
const pluginSyntaxGenerator = require('@babel/plugin-syntax-async-generators');
module.exports = createTransform();
module.exports.createTransform = createTransform;
function createTransform (babelOpts = {}) {
return function babelify (file, opts = {}) {
const ext = path.extname(file);
if (!babel.DEFAULT_EXTENSIONS.includes(ext)) {
return new PassThrough();
}
if (typeof babelOpts.filterFile === 'function') {
if (!babelOpts.filterFile(file, opts)) {
return new PassThrough();
}
}
if (babelOpts.logFile) console.log('Checking', file);
const output = through();
const stream = duplexer(concat(code => {
code = code.toString();
let isFilterAccept = true;
if (typeof babelOpts.filterSource === 'function') {
isFilterAccept = babelOpts.filterSource(code, file, opts);
}
// Skip files that don't use ES6 import/export syntax
if (!isFilterAccept || !/\b(import|export)\b/g.test(code)) {
output.push(code);
output.push(null);
return;
}
let plainImports = [].concat(babelOpts.plainImports).filter(Boolean);
const settings = Object.assign({}, babelOpts, {
babelrc: false,
sourceMaps: 'inline',
plugins: [
pluginSyntaxRestSpread,
pluginSyntaxGenerator,
plainImports.length > 0
? [ pluginImportToRequire, { modules: plainImports } ]
: false,
pluginDynamicImport,
pluginCJS
].filter(Boolean),
filename: file
});
if (babelOpts.logFile) console.log('Transforming', file);
delete settings.filterFile;
delete settings.filterSource;
delete settings.logFile;
delete settings.plainImports;
babel.transform(code, settings, (err, result) => {
if (err) {
stream.emit('error', err);
} else {
output.push(result.code);
}
output.push(null);
});
}), output);
return stream;
};
}