@swc/core 1.2.212 → 1.2.218
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +179 -0
- package/Visitor.d.ts +17 -4
- package/Visitor.js +96 -9
- package/index.js +123 -11
- package/package.json +40 -18
- package/postinstall.js +143 -0
- package/types.d.ts +163 -95
- package/.prettierignore +0 -63
- package/.prettierrc.json +0 -1
- package/babel.d.ts +0 -14
- package/babel.js +0 -2
- package/run_swcx.d.ts +0 -2
package/index.js
CHANGED
|
@@ -6,9 +6,21 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
6
6
|
if (k2 === undefined) k2 = k;
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
9
14
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
15
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
16
|
};
|
|
17
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
18
|
+
if (mod && mod.__esModule) return mod;
|
|
19
|
+
var result = {};
|
|
20
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
21
|
+
__setModuleDefault(result, mod);
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
12
24
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
13
25
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
14
26
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -34,9 +46,27 @@ exports.DEFAULT_EXTENSIONS = exports.getBinaryMetadata = exports.__experimental_
|
|
|
34
46
|
const path_1 = require("path");
|
|
35
47
|
__exportStar(require("./types"), exports);
|
|
36
48
|
const spack_1 = require("./spack");
|
|
49
|
+
const assert = __importStar(require("assert"));
|
|
37
50
|
// Allow overrides to the location of the .node binding file
|
|
38
51
|
const bindingsOverride = process.env["SWC_BINARY_PATH"];
|
|
39
|
-
|
|
52
|
+
let fallbackBindings;
|
|
53
|
+
const bindings = (() => {
|
|
54
|
+
let binding;
|
|
55
|
+
try {
|
|
56
|
+
binding = !!bindingsOverride ? require((0, path_1.resolve)(bindingsOverride)) : require('./binding');
|
|
57
|
+
// If native binding loaded successfully, it should return proper target triple constant.
|
|
58
|
+
const triple = binding.getTargetTriple();
|
|
59
|
+
assert.ok(triple, 'Failed to read target triple from native binary.');
|
|
60
|
+
return binding;
|
|
61
|
+
}
|
|
62
|
+
catch (_) {
|
|
63
|
+
// postinstall supposed to install `@swc/wasm` already
|
|
64
|
+
fallbackBindings = require('@swc/wasm');
|
|
65
|
+
}
|
|
66
|
+
finally {
|
|
67
|
+
return binding;
|
|
68
|
+
}
|
|
69
|
+
})();
|
|
40
70
|
/**
|
|
41
71
|
* Version of the swc binding.
|
|
42
72
|
*/
|
|
@@ -54,16 +84,34 @@ exports.plugins = plugins;
|
|
|
54
84
|
class Compiler {
|
|
55
85
|
minify(src, opts) {
|
|
56
86
|
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
+
if (!bindings && !!fallbackBindings) {
|
|
88
|
+
throw new Error('Fallback bindings does not support this interface yet.');
|
|
89
|
+
}
|
|
90
|
+
else if (!bindings) {
|
|
91
|
+
throw new Error('Bindings not found.');
|
|
92
|
+
}
|
|
57
93
|
return bindings.minify(toBuffer(src), toBuffer(opts !== null && opts !== void 0 ? opts : {}));
|
|
58
94
|
});
|
|
59
95
|
}
|
|
60
96
|
minifySync(src, opts) {
|
|
61
|
-
|
|
97
|
+
if (bindings) {
|
|
98
|
+
return bindings.minifySync(toBuffer(src), toBuffer(opts !== null && opts !== void 0 ? opts : {}));
|
|
99
|
+
}
|
|
100
|
+
else if (fallbackBindings) {
|
|
101
|
+
return fallbackBindings.minifySync(src, opts);
|
|
102
|
+
}
|
|
103
|
+
throw new Error('Bindings not found.');
|
|
62
104
|
}
|
|
63
105
|
parse(src, options, filename) {
|
|
64
106
|
return __awaiter(this, void 0, void 0, function* () {
|
|
65
107
|
options = options || { syntax: "ecmascript" };
|
|
66
108
|
options.syntax = options.syntax || "ecmascript";
|
|
109
|
+
if (!bindings && !!fallbackBindings) {
|
|
110
|
+
throw new Error('Fallback bindings does not support this interface yet.');
|
|
111
|
+
}
|
|
112
|
+
else if (!bindings) {
|
|
113
|
+
throw new Error('Bindings not found.');
|
|
114
|
+
}
|
|
67
115
|
const res = yield bindings.parse(src, toBuffer(options), filename);
|
|
68
116
|
return JSON.parse(res);
|
|
69
117
|
});
|
|
@@ -71,12 +119,24 @@ class Compiler {
|
|
|
71
119
|
parseSync(src, options, filename) {
|
|
72
120
|
options = options || { syntax: "ecmascript" };
|
|
73
121
|
options.syntax = options.syntax || "ecmascript";
|
|
74
|
-
|
|
122
|
+
if (bindings) {
|
|
123
|
+
return JSON.parse(bindings.parseSync(src, toBuffer(options), filename));
|
|
124
|
+
}
|
|
125
|
+
else if (fallbackBindings) {
|
|
126
|
+
return JSON.parse(fallbackBindings.parseSync(src, options));
|
|
127
|
+
}
|
|
128
|
+
throw new Error('Bindings not found.');
|
|
75
129
|
}
|
|
76
130
|
parseFile(path, options) {
|
|
77
131
|
return __awaiter(this, void 0, void 0, function* () {
|
|
78
132
|
options = options || { syntax: "ecmascript" };
|
|
79
133
|
options.syntax = options.syntax || "ecmascript";
|
|
134
|
+
if (!bindings && !!fallbackBindings) {
|
|
135
|
+
throw new Error('Fallback bindings does not support filesystem access.');
|
|
136
|
+
}
|
|
137
|
+
else if (!bindings) {
|
|
138
|
+
throw new Error('Bindings not found.');
|
|
139
|
+
}
|
|
80
140
|
const res = yield bindings.parseFile(path, toBuffer(options));
|
|
81
141
|
return JSON.parse(res);
|
|
82
142
|
});
|
|
@@ -84,6 +144,12 @@ class Compiler {
|
|
|
84
144
|
parseFileSync(path, options) {
|
|
85
145
|
options = options || { syntax: "ecmascript" };
|
|
86
146
|
options.syntax = options.syntax || "ecmascript";
|
|
147
|
+
if (!bindings && !!fallbackBindings) {
|
|
148
|
+
throw new Error('Fallback bindings does not support filesystem access');
|
|
149
|
+
}
|
|
150
|
+
else if (!bindings) {
|
|
151
|
+
throw new Error('Bindings not found.');
|
|
152
|
+
}
|
|
87
153
|
return JSON.parse(bindings.parseFileSync(path, toBuffer(options)));
|
|
88
154
|
}
|
|
89
155
|
/**
|
|
@@ -93,6 +159,12 @@ class Compiler {
|
|
|
93
159
|
print(m, options) {
|
|
94
160
|
return __awaiter(this, void 0, void 0, function* () {
|
|
95
161
|
options = options || {};
|
|
162
|
+
if (!bindings && !!fallbackBindings) {
|
|
163
|
+
throw new Error('Fallback bindings does not support this interface yet.');
|
|
164
|
+
}
|
|
165
|
+
else if (!bindings) {
|
|
166
|
+
throw new Error('Bindings not found.');
|
|
167
|
+
}
|
|
96
168
|
return bindings.print(JSON.stringify(m), toBuffer(options));
|
|
97
169
|
});
|
|
98
170
|
}
|
|
@@ -102,11 +174,23 @@ class Compiler {
|
|
|
102
174
|
*/
|
|
103
175
|
printSync(m, options) {
|
|
104
176
|
options = options || {};
|
|
105
|
-
|
|
177
|
+
if (bindings) {
|
|
178
|
+
return bindings.printSync(JSON.stringify(m), toBuffer(options));
|
|
179
|
+
}
|
|
180
|
+
else if (fallbackBindings) {
|
|
181
|
+
return fallbackBindings.printSync(JSON.stringify(m), options);
|
|
182
|
+
}
|
|
183
|
+
throw new Error('Bindings not found.');
|
|
106
184
|
}
|
|
107
185
|
transform(src, options) {
|
|
108
186
|
var _a, _b, _c;
|
|
109
187
|
return __awaiter(this, void 0, void 0, function* () {
|
|
188
|
+
if (!bindings && !!fallbackBindings) {
|
|
189
|
+
throw new Error('Fallback bindings does not support this interface yet.');
|
|
190
|
+
}
|
|
191
|
+
else if (!bindings) {
|
|
192
|
+
throw new Error('Bindings not found.');
|
|
193
|
+
}
|
|
110
194
|
const isModule = typeof src !== "string";
|
|
111
195
|
options = options || {};
|
|
112
196
|
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
@@ -129,16 +213,28 @@ class Compiler {
|
|
|
129
213
|
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
130
214
|
options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
|
131
215
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
216
|
+
if (bindings) {
|
|
217
|
+
const { plugin } = options, newOptions = __rest(options, ["plugin"]);
|
|
218
|
+
if (plugin) {
|
|
219
|
+
const m = typeof src === "string" ? this.parseSync(src, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser, options.filename) : src;
|
|
220
|
+
return this.transformSync(plugin(m), newOptions);
|
|
221
|
+
}
|
|
222
|
+
return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
|
|
223
|
+
}
|
|
224
|
+
else if (fallbackBindings) {
|
|
225
|
+
return fallbackBindings.transformSync(src, options);
|
|
136
226
|
}
|
|
137
|
-
|
|
227
|
+
throw new Error("Bindings not found");
|
|
138
228
|
}
|
|
139
229
|
transformFile(path, options) {
|
|
140
230
|
var _a, _b, _c;
|
|
141
231
|
return __awaiter(this, void 0, void 0, function* () {
|
|
232
|
+
if (!bindings && !!fallbackBindings) {
|
|
233
|
+
throw new Error('Fallback bindings does not support filesystem access.');
|
|
234
|
+
}
|
|
235
|
+
else if (!bindings) {
|
|
236
|
+
throw new Error('Bindings not found.');
|
|
237
|
+
}
|
|
142
238
|
options = options || {};
|
|
143
239
|
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
144
240
|
options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
|
@@ -154,6 +250,12 @@ class Compiler {
|
|
|
154
250
|
}
|
|
155
251
|
transformFileSync(path, options) {
|
|
156
252
|
var _a, _b, _c;
|
|
253
|
+
if (!bindings && !!fallbackBindings) {
|
|
254
|
+
throw new Error('Fallback bindings does not support filesystem access.');
|
|
255
|
+
}
|
|
256
|
+
else if (!bindings) {
|
|
257
|
+
throw new Error('Bindings not found.');
|
|
258
|
+
}
|
|
157
259
|
options = options || {};
|
|
158
260
|
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
159
261
|
options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
|
@@ -168,6 +270,12 @@ class Compiler {
|
|
|
168
270
|
}
|
|
169
271
|
bundle(options) {
|
|
170
272
|
return __awaiter(this, void 0, void 0, function* () {
|
|
273
|
+
if (!bindings && !!fallbackBindings) {
|
|
274
|
+
throw new Error('Fallback bindings does not support this interface yet.');
|
|
275
|
+
}
|
|
276
|
+
else if (!bindings) {
|
|
277
|
+
throw new Error('Bindings not found.');
|
|
278
|
+
}
|
|
171
279
|
const opts = yield (0, spack_1.compileBundleOptions)(options);
|
|
172
280
|
if (Array.isArray(opts)) {
|
|
173
281
|
const all = yield Promise.all(opts.map((opt) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -258,8 +366,12 @@ exports.minifySync = minifySync;
|
|
|
258
366
|
* or bug report at https://github.com/swc-project/swc/discussions.
|
|
259
367
|
*/
|
|
260
368
|
function __experimental_registerGlobalTraceConfig(traceConfig) {
|
|
261
|
-
if
|
|
262
|
-
|
|
369
|
+
// Do not raise error if binding doesn't exists - fallback binding will not support
|
|
370
|
+
// this ever.
|
|
371
|
+
if (bindings) {
|
|
372
|
+
if (traceConfig.type === 'traceEvent') {
|
|
373
|
+
bindings.initCustomTraceSubscriber(traceConfig.fileName);
|
|
374
|
+
}
|
|
263
375
|
}
|
|
264
376
|
}
|
|
265
377
|
exports.__experimental_registerGlobalTraceConfig = __experimental_registerGlobalTraceConfig;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swc/core",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.218",
|
|
4
4
|
"description": "Super-fast alternative for babel",
|
|
5
5
|
"homepage": "https://swc.rs",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -54,6 +54,7 @@
|
|
|
54
54
|
"scripts": {
|
|
55
55
|
"changelog": "git cliff --output CHANGELOG.md",
|
|
56
56
|
"prepare": "husky install && git config feature.manyFiles true && node ./crates/swc_ecma_preset_env/scripts/copy-data.js",
|
|
57
|
+
"postinstall": "node postinstall.js",
|
|
57
58
|
"artifacts": "napi artifacts --dist scripts/npm",
|
|
58
59
|
"prepublishOnly": "tsc -d && napi prepublish -p scripts/npm --tagstyle npm",
|
|
59
60
|
"pack": "wasm-pack",
|
|
@@ -61,7 +62,7 @@
|
|
|
61
62
|
"build:wasm": "npm-run-all \"pack -- build ./crates/wasm --scope swc {1} -t {2} --features plugin\" --",
|
|
62
63
|
"build": "tsc -d && napi build --platform --cargo-name binding_core_node --js ./node-swc/src/binding.js --dts ./node-swc/src/binding.d.ts -p binding_core_node --release",
|
|
63
64
|
"build:dev": "tsc -d && napi build --platform --cargo-name binding_core_node --js ./node-swc/src/binding.js --dts ./node-swc/src/binding.d.ts -p binding_core_node",
|
|
64
|
-
"test": "cross-env NODE_OPTIONS='--experimental-vm-modules' jest node-swc/
|
|
65
|
+
"test": "cross-env NODE_OPTIONS='--experimental-vm-modules' jest --config ./node-swc/jest.config.js",
|
|
65
66
|
"version": "napi version -p scripts/npm"
|
|
66
67
|
},
|
|
67
68
|
"lint-staged": {
|
|
@@ -97,7 +98,7 @@
|
|
|
97
98
|
"@napi-rs/cli": "^2.10.0",
|
|
98
99
|
"@swc/helpers": "^0.4.2",
|
|
99
100
|
"@taplo/cli": "^0.3.2",
|
|
100
|
-
"@types/jest": "^
|
|
101
|
+
"@types/jest": "^28.1.4",
|
|
101
102
|
"@types/node": "^14.14.41",
|
|
102
103
|
"acorn": "^8.6.0",
|
|
103
104
|
"acorn-jsx": "^5.3.2",
|
|
@@ -110,8 +111,9 @@
|
|
|
110
111
|
"cross-env": "^7.0.3",
|
|
111
112
|
"cspell": "^5.12.3",
|
|
112
113
|
"expect": "^27.4.2",
|
|
114
|
+
"glob": "^8.0.3",
|
|
113
115
|
"husky": "^7.0.2",
|
|
114
|
-
"jest": "^
|
|
116
|
+
"jest": "^28.1.2",
|
|
115
117
|
"js-beautify": "^1.14.3",
|
|
116
118
|
"lint-staged": "^12.3.6",
|
|
117
119
|
"lodash": "^4.17.21",
|
|
@@ -135,18 +137,38 @@
|
|
|
135
137
|
"url": "https://opencollective.com/swc"
|
|
136
138
|
},
|
|
137
139
|
"optionalDependencies": {
|
|
138
|
-
"@swc/core-win32-x64-msvc": "1.2.
|
|
139
|
-
"@swc/core-darwin-x64": "1.2.
|
|
140
|
-
"@swc/core-linux-x64-gnu": "1.2.
|
|
141
|
-
"@swc/core-linux-x64-musl": "1.2.
|
|
142
|
-
"@swc/core-freebsd-x64": "1.2.
|
|
143
|
-
"@swc/core-win32-ia32-msvc": "1.2.
|
|
144
|
-
"@swc/core-linux-arm64-gnu": "1.2.
|
|
145
|
-
"@swc/core-linux-arm-gnueabihf": "1.2.
|
|
146
|
-
"@swc/core-darwin-arm64": "1.2.
|
|
147
|
-
"@swc/core-android-arm64": "1.2.
|
|
148
|
-
"@swc/core-linux-arm64-musl": "1.2.
|
|
149
|
-
"@swc/core-win32-arm64-msvc": "1.2.
|
|
150
|
-
"@swc/core-android-arm-eabi": "1.2.
|
|
151
|
-
}
|
|
140
|
+
"@swc/core-win32-x64-msvc": "1.2.218",
|
|
141
|
+
"@swc/core-darwin-x64": "1.2.218",
|
|
142
|
+
"@swc/core-linux-x64-gnu": "1.2.218",
|
|
143
|
+
"@swc/core-linux-x64-musl": "1.2.218",
|
|
144
|
+
"@swc/core-freebsd-x64": "1.2.218",
|
|
145
|
+
"@swc/core-win32-ia32-msvc": "1.2.218",
|
|
146
|
+
"@swc/core-linux-arm64-gnu": "1.2.218",
|
|
147
|
+
"@swc/core-linux-arm-gnueabihf": "1.2.218",
|
|
148
|
+
"@swc/core-darwin-arm64": "1.2.218",
|
|
149
|
+
"@swc/core-android-arm64": "1.2.218",
|
|
150
|
+
"@swc/core-linux-arm64-musl": "1.2.218",
|
|
151
|
+
"@swc/core-win32-arm64-msvc": "1.2.218",
|
|
152
|
+
"@swc/core-android-arm-eabi": "1.2.218"
|
|
153
|
+
},
|
|
154
|
+
"files": [
|
|
155
|
+
"CHANGELOG.md",
|
|
156
|
+
"Visitor.d.ts",
|
|
157
|
+
"index.d.ts",
|
|
158
|
+
"spack.js",
|
|
159
|
+
"util.d.ts",
|
|
160
|
+
"LICENSE",
|
|
161
|
+
"Visitor.js",
|
|
162
|
+
"binding.d.ts",
|
|
163
|
+
"index.js",
|
|
164
|
+
"run_swcx.js",
|
|
165
|
+
"types.d.ts",
|
|
166
|
+
"util.js",
|
|
167
|
+
"README.md",
|
|
168
|
+
"binding.js",
|
|
169
|
+
"package.json",
|
|
170
|
+
"spack.d.ts",
|
|
171
|
+
"types.js",
|
|
172
|
+
"postinstall.js"
|
|
173
|
+
]
|
|
152
174
|
}
|
package/postinstall.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
/**
|
|
32
|
+
* A postinstall script runs after `@swc/core` is installed.
|
|
33
|
+
*
|
|
34
|
+
* It checks if corresponding optional dependencies for native binary is installed and can be loaded properly.
|
|
35
|
+
* If it fails, it'll internally try to install `@swc/wasm` as fallback.
|
|
36
|
+
*/
|
|
37
|
+
const fs_1 = require("fs");
|
|
38
|
+
const assert = __importStar(require("assert"));
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const child_process = __importStar(require("child_process"));
|
|
41
|
+
const fs = __importStar(require("fs"));
|
|
42
|
+
function removeRecursive(dir) {
|
|
43
|
+
for (const entry of fs.readdirSync(dir)) {
|
|
44
|
+
const entryPath = path.join(dir, entry);
|
|
45
|
+
let stats;
|
|
46
|
+
try {
|
|
47
|
+
stats = fs.lstatSync(entryPath);
|
|
48
|
+
}
|
|
49
|
+
catch (_a) {
|
|
50
|
+
continue; // Guard against https://github.com/nodejs/node/issues/4760
|
|
51
|
+
}
|
|
52
|
+
if (stats.isDirectory())
|
|
53
|
+
removeRecursive(entryPath);
|
|
54
|
+
else
|
|
55
|
+
fs.unlinkSync(entryPath);
|
|
56
|
+
}
|
|
57
|
+
fs.rmdirSync(dir);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Trying to validate @swc/core's native binary installation, then installs if it is not supported.
|
|
61
|
+
*/
|
|
62
|
+
const validateBinary = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
var _a;
|
|
64
|
+
try {
|
|
65
|
+
const { name } = require(path.resolve(process.env.INIT_CWD, 'package.json'));
|
|
66
|
+
if (name === '@swc/core') {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (_) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
// TODO: We do not take care of the case if user try to install with `--no-optional`.
|
|
74
|
+
// For now, it is considered as deliberate decision.
|
|
75
|
+
let binding;
|
|
76
|
+
try {
|
|
77
|
+
binding = require('./binding');
|
|
78
|
+
// Check if binding binary actually works.
|
|
79
|
+
// For the latest version, checks target triple. If it's old version doesn't have target triple, use parseSync instead.
|
|
80
|
+
const triple = binding.getTargetTriple ? binding.getTargetTriple() : binding.parseSync('console.log()', Buffer.from(JSON.stringify({ syntax: "ecmascript" })));
|
|
81
|
+
assert.ok(triple, 'Failed to read target triple from native binary.');
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
// if error is unsupported architecture, ignore to display.
|
|
85
|
+
if (!((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Unsupported architecture'))) {
|
|
86
|
+
console.warn(error);
|
|
87
|
+
}
|
|
88
|
+
console.warn(`@swc/core was not able to resolve native bindings installation. It'll try to use @swc/wasm as fallback instead.`);
|
|
89
|
+
}
|
|
90
|
+
if (!!binding) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
// User choose to override the binary installation. Skip remanining validation.
|
|
94
|
+
if (!!process.env["SWC_BINARY_PATH"]) {
|
|
95
|
+
console.warn(`@swc/core could not resolve native bindings installation, but found manual override config SWC_BINARY_PATH specified. Skipping remaning validation.`);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// Check if top-level package.json installs @swc/wasm separately already
|
|
99
|
+
let wasmBinding;
|
|
100
|
+
try {
|
|
101
|
+
wasmBinding = require.resolve(`@swc/wasm`);
|
|
102
|
+
}
|
|
103
|
+
catch (_) {
|
|
104
|
+
}
|
|
105
|
+
if (!!wasmBinding && (0, fs_1.existsSync)(wasmBinding)) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const env = Object.assign(Object.assign({}, process.env), { npm_config_global: undefined });
|
|
109
|
+
const { version } = require(path.join(path.dirname(require.resolve('@swc/core')), 'package.json'));
|
|
110
|
+
// We want to place @swc/wasm next to the @swc/core as if normal installation was done,
|
|
111
|
+
// but can't directly set cwd to INIT_CWD as npm seems to acquire lock to the working dir.
|
|
112
|
+
// Instead, create a temporary inner and move it out.
|
|
113
|
+
const coreDir = path.dirname(require.resolve('@swc/core'));
|
|
114
|
+
const installDir = path.join(coreDir, 'npm-install');
|
|
115
|
+
try {
|
|
116
|
+
fs.mkdirSync(installDir);
|
|
117
|
+
fs.writeFileSync(path.join(installDir, 'package.json'), '{}');
|
|
118
|
+
// Instead of carrying over own dependencies to download & resolve package which increases installation sizes of `@swc/core`,
|
|
119
|
+
// assume & relies on system's npm installation.
|
|
120
|
+
child_process.execSync(`npm install --no-save --loglevel=error --prefer-offline --no-audit --progress=false @swc/wasm@${version}`, { cwd: installDir, stdio: 'pipe', env });
|
|
121
|
+
const installedBinPath = path.join(installDir, 'node_modules', `@swc/wasm`);
|
|
122
|
+
// INIT_CWD is injected via npm. If it doesn't exists, can't proceed.
|
|
123
|
+
fs.renameSync(installedBinPath, path.resolve(process.env.INIT_CWD, 'node_modules', `@swc/wasm`));
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
console.error(error);
|
|
127
|
+
console.error(`Failed to install fallback @swc/wasm@${version}. @swc/core will not properly.
|
|
128
|
+
Please install @swc/wasm manually, or retry whole installation.
|
|
129
|
+
If there are unexpected errors, please report at https://github.com/swc-project/swc/issues`);
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
try {
|
|
133
|
+
removeRecursive(installDir);
|
|
134
|
+
}
|
|
135
|
+
catch (_) {
|
|
136
|
+
// Gracefully ignore any failures. This'll make few leftover files but it shouldn't block installation.
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
validateBinary().catch((error) => {
|
|
141
|
+
// for now just throw the error as-is.
|
|
142
|
+
throw error;
|
|
143
|
+
});
|