forked from babel/babelify
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
147 lines (127 loc) · 3.78 KB
/
index.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
"use strict";
var stream = require("stream");
var util = require("util");
var path = require("path");
var swc = require("@swc/core");
var fs = require("fs");
var inlineSourcemap = require('inline-source-map-comment')
var defaults = {
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": true,
"numericSeparator": false,
"classPrivateProperty": true,
"privateMethod": false,
"classProperty": true,
"functionBind": false,
"decorators": false,
"decoratorsBeforeExport": false
},
"target": "es2016",
"transform": {
"react": {
"pragma": "React.createElement",
"pragmaFrag": "React.Fragment",
"throwIfNamespace": true,
"development": false,
"useBuiltins": false
},
"optimizer": {
"globals": {
"vars": {
"__DEBUG__": "true"
}
}
}
}
},
"module": {
"type": "commonjs",
"strict": false,
"strictMode": true,
"lazy": false,
"noInterop": false
}
}
module.exports = buildTransform();
module.exports.defaults = defaults;
module.exports.configure = buildTransform;
// TODO: essentially we have to handle all CLI-tool options
// some of them are taken from browserify flags
// Allow projects to import this module and check `foo instanceof swcify`
// to see if the current stream they are working with is one created
// by SWCify.
Object.defineProperty(module.exports, Symbol.hasInstance, {
value: function hasInstance(obj) {
return obj instanceof SwcifyStream;
},
});
function buildTransform(opts) {
var configCache = {}
return function (filename, config) {
var _flags = config._flags || {}
config = Object.assign({}, config)
delete config._flags
// unwrap nested config
if (config.config) {
config = config
}
var basedir = path.resolve(_flags.basedir || ".");
var configPath = path.resolve(basedir, '.swcrc');
var configJSON, parsedConfig = configCache[configPath];
// if no cached config found - try to read from file
if (!parsedConfig && fs.existsSync(configPath)) {
// read filepath config
// read .swcrc relative to basedir
// Browserify doesn't actually always normalize the filename passed
// to transforms, so we manually ensure that the filename is relative
configJSON = fs.readFileSync(configPath, 'utf-8');
// bad config will throw error
parsedConfig = JSON.parse(configJSON)
configCache[configPath] = parsedConfig
}
// no config found, falling back to default options
// create current config from options extended with the config
config = Object.assign({
sourceMaps: _flags.debug
}, defaults, opts, parsedConfig, config)
// normalize config quirks
if (!config.sourceMaps) delete config.sourceMaps
return new SwcifyStream({
config: config,
filename: path.resolve(basedir, filename)
});
};
}
class SwcifyStream extends stream.Transform {
constructor(opts) {
super();
this._code = [];
this._sourceMap = [];
if (!opts) opts = {}
this._opts = opts;
}
_transform(buf, enc, callback) {
var self = this
swc.transform(buf.toString(), this._opts.config).then(function(result) {
var code = result !== null ? result.code : data;
if (result.map) {
code += inlineSourcemap(result.map)
}
self.push(code);
self._code.push(code);
self._sourceMap.push(result && result.map)
callback();
}, callback)
}
_flush(callback) {
// Merge the buffer pieces after all are available, instead of one at a time,
// to avoid corrupting multibyte characters.
this.emit("swcify", {
code: this._code.join('')
// FIXME: how to join sourcemaps
}, this._opts.filename);
callback()
}
}