@ohos-ports/swc-core 1.3.14-beta.0
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 +11281 -0
- package/LICENSE +201 -0
- package/README.md +93 -0
- package/Visitor.d.ts +214 -0
- package/Visitor.js +1395 -0
- package/binding.d.ts +16 -0
- package/binding.js +283 -0
- package/index.d.ts +107 -0
- package/index.js +426 -0
- package/package.json +90 -0
- package/postinstall.js +147 -0
- package/run_swcx.js +85 -0
- package/spack.d.ts +50 -0
- package/spack.js +83 -0
- package/swc.openharmony-arm64.node +4 -0
- package/types.d.ts +1865 -0
- package/types.js +2 -0
- package/util.d.ts +1 -0
- package/util.js +104 -0
package/index.js
ADDED
|
@@ -0,0 +1,426 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
|
+
};
|
|
21
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
22
|
+
if (mod && mod.__esModule) return mod;
|
|
23
|
+
var result = {};
|
|
24
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
25
|
+
__setModuleDefault(result, mod);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
38
|
+
var t = {};
|
|
39
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
40
|
+
t[p] = s[p];
|
|
41
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
42
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
43
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
44
|
+
t[p[i]] = s[p[i]];
|
|
45
|
+
}
|
|
46
|
+
return t;
|
|
47
|
+
};
|
|
48
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
49
|
+
exports.DEFAULT_EXTENSIONS = exports.getBinaryMetadata = exports.__experimental_registerGlobalTraceConfig = exports.minifySync = exports.minify = exports.bundle = exports.transformFileSync = exports.transformFile = exports.transformSync = exports.transform = exports.printSync = exports.print = exports.parseFileSync = exports.parseFile = exports.parseSync = exports.parse = exports.Compiler = exports.plugins = exports.version = void 0;
|
|
50
|
+
const path_1 = require("path");
|
|
51
|
+
__exportStar(require("./types"), exports);
|
|
52
|
+
const spack_1 = require("./spack");
|
|
53
|
+
const assert = __importStar(require("assert"));
|
|
54
|
+
// Allow overrides to the location of the .node binding file
|
|
55
|
+
const bindingsOverride = process.env["SWC_BINARY_PATH"];
|
|
56
|
+
// `@swc/core` includes d.ts for the `@swc/wasm` to provide typed fallback bindings
|
|
57
|
+
// todo: fix package.json scripts
|
|
58
|
+
let fallbackBindings;
|
|
59
|
+
const bindings = (() => {
|
|
60
|
+
let binding;
|
|
61
|
+
try {
|
|
62
|
+
binding = !!bindingsOverride ? require((0, path_1.resolve)(bindingsOverride)) : require('./binding');
|
|
63
|
+
// If native binding loaded successfully, it should return proper target triple constant.
|
|
64
|
+
const triple = binding.getTargetTriple();
|
|
65
|
+
assert.ok(triple, 'Failed to read target triple from native binary.');
|
|
66
|
+
return binding;
|
|
67
|
+
}
|
|
68
|
+
catch (_) {
|
|
69
|
+
// postinstall supposed to install `@swc/wasm` already
|
|
70
|
+
fallbackBindings = require('@swc/wasm');
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
return binding;
|
|
74
|
+
}
|
|
75
|
+
})();
|
|
76
|
+
/**
|
|
77
|
+
* Version of the swc binding.
|
|
78
|
+
*/
|
|
79
|
+
exports.version = require("./package.json").version;
|
|
80
|
+
function plugins(ps) {
|
|
81
|
+
return mod => {
|
|
82
|
+
let m = mod;
|
|
83
|
+
for (const p of ps) {
|
|
84
|
+
m = p(m);
|
|
85
|
+
}
|
|
86
|
+
return m;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
exports.plugins = plugins;
|
|
90
|
+
class Compiler {
|
|
91
|
+
constructor() {
|
|
92
|
+
this.fallbackBindingsPluginWarningDisplayed = false;
|
|
93
|
+
}
|
|
94
|
+
minify(src, opts) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
if (bindings) {
|
|
97
|
+
return bindings.minify(toBuffer(src), toBuffer(opts !== null && opts !== void 0 ? opts : {}));
|
|
98
|
+
}
|
|
99
|
+
else if (fallbackBindings) {
|
|
100
|
+
return fallbackBindings.minify(src, opts);
|
|
101
|
+
}
|
|
102
|
+
throw new Error('Bindings not found.');
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
minifySync(src, opts) {
|
|
106
|
+
if (bindings) {
|
|
107
|
+
return bindings.minifySync(toBuffer(src), toBuffer(opts !== null && opts !== void 0 ? opts : {}));
|
|
108
|
+
}
|
|
109
|
+
else if (fallbackBindings) {
|
|
110
|
+
return fallbackBindings.minifySync(src, opts);
|
|
111
|
+
}
|
|
112
|
+
throw new Error('Bindings not found.');
|
|
113
|
+
}
|
|
114
|
+
parse(src, options, filename) {
|
|
115
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
+
options = options || { syntax: "ecmascript" };
|
|
117
|
+
options.syntax = options.syntax || "ecmascript";
|
|
118
|
+
if (!bindings && !!fallbackBindings) {
|
|
119
|
+
throw new Error('Fallback bindings does not support this interface yet.');
|
|
120
|
+
}
|
|
121
|
+
else if (!bindings) {
|
|
122
|
+
throw new Error('Bindings not found.');
|
|
123
|
+
}
|
|
124
|
+
if (bindings) {
|
|
125
|
+
const res = yield bindings.parse(src, toBuffer(options), filename);
|
|
126
|
+
return JSON.parse(res);
|
|
127
|
+
}
|
|
128
|
+
else if (fallbackBindings) {
|
|
129
|
+
return fallbackBindings.parse(src, options);
|
|
130
|
+
}
|
|
131
|
+
throw new Error('Bindings not found.');
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
parseSync(src, options, filename) {
|
|
135
|
+
options = options || { syntax: "ecmascript" };
|
|
136
|
+
options.syntax = options.syntax || "ecmascript";
|
|
137
|
+
if (bindings) {
|
|
138
|
+
return JSON.parse(bindings.parseSync(src, toBuffer(options), filename));
|
|
139
|
+
}
|
|
140
|
+
else if (fallbackBindings) {
|
|
141
|
+
return fallbackBindings.parseSync(src, options);
|
|
142
|
+
}
|
|
143
|
+
throw new Error('Bindings not found.');
|
|
144
|
+
}
|
|
145
|
+
parseFile(path, options) {
|
|
146
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
147
|
+
options = options || { syntax: "ecmascript" };
|
|
148
|
+
options.syntax = options.syntax || "ecmascript";
|
|
149
|
+
if (!bindings && !!fallbackBindings) {
|
|
150
|
+
throw new Error('Fallback bindings does not support filesystem access.');
|
|
151
|
+
}
|
|
152
|
+
else if (!bindings) {
|
|
153
|
+
throw new Error('Bindings not found.');
|
|
154
|
+
}
|
|
155
|
+
const res = yield bindings.parseFile(path, toBuffer(options));
|
|
156
|
+
return JSON.parse(res);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
parseFileSync(path, options) {
|
|
160
|
+
options = options || { syntax: "ecmascript" };
|
|
161
|
+
options.syntax = options.syntax || "ecmascript";
|
|
162
|
+
if (!bindings && !!fallbackBindings) {
|
|
163
|
+
throw new Error('Fallback bindings does not support filesystem access');
|
|
164
|
+
}
|
|
165
|
+
else if (!bindings) {
|
|
166
|
+
throw new Error('Bindings not found.');
|
|
167
|
+
}
|
|
168
|
+
return JSON.parse(bindings.parseFileSync(path, toBuffer(options)));
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Note: this method should be invoked on the compiler instance used
|
|
172
|
+
* for `parse()` / `parseSync()`.
|
|
173
|
+
*/
|
|
174
|
+
print(m, options) {
|
|
175
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
176
|
+
options = options || {};
|
|
177
|
+
if (bindings) {
|
|
178
|
+
return bindings.print(JSON.stringify(m), toBuffer(options));
|
|
179
|
+
}
|
|
180
|
+
else if (fallbackBindings) {
|
|
181
|
+
return fallbackBindings.print(m, options);
|
|
182
|
+
}
|
|
183
|
+
throw new Error('Bindings not found.');
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Note: this method should be invoked on the compiler instance used
|
|
188
|
+
* for `parse()` / `parseSync()`.
|
|
189
|
+
*/
|
|
190
|
+
printSync(m, options) {
|
|
191
|
+
options = options || {};
|
|
192
|
+
if (bindings) {
|
|
193
|
+
return bindings.printSync(JSON.stringify(m), toBuffer(options));
|
|
194
|
+
}
|
|
195
|
+
else if (fallbackBindings) {
|
|
196
|
+
return fallbackBindings.printSync(m, options);
|
|
197
|
+
}
|
|
198
|
+
throw new Error('Bindings not found.');
|
|
199
|
+
}
|
|
200
|
+
transform(src, options) {
|
|
201
|
+
var _a, _b, _c;
|
|
202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
+
const isModule = typeof src !== "string";
|
|
204
|
+
options = options || {};
|
|
205
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
206
|
+
options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
|
207
|
+
}
|
|
208
|
+
const { plugin } = options, newOptions = __rest(options, ["plugin"]);
|
|
209
|
+
if (bindings) {
|
|
210
|
+
if (plugin) {
|
|
211
|
+
const m = typeof src === "string"
|
|
212
|
+
? yield this.parse(src, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser, options.filename)
|
|
213
|
+
: src;
|
|
214
|
+
return this.transform(plugin(m), newOptions);
|
|
215
|
+
}
|
|
216
|
+
return bindings.transform(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
|
|
217
|
+
}
|
|
218
|
+
else if (fallbackBindings) {
|
|
219
|
+
if (plugin && !this.fallbackBindingsPluginWarningDisplayed) {
|
|
220
|
+
console.warn(`Fallback bindings does not support legacy plugins, it'll be ignored.`);
|
|
221
|
+
this.fallbackBindingsPluginWarningDisplayed = true;
|
|
222
|
+
}
|
|
223
|
+
return fallbackBindings.transform(src, options);
|
|
224
|
+
}
|
|
225
|
+
throw new Error('Bindings not found.');
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
transformSync(src, options) {
|
|
229
|
+
var _a, _b, _c;
|
|
230
|
+
const isModule = typeof src !== "string";
|
|
231
|
+
options = options || {};
|
|
232
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
233
|
+
options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
|
234
|
+
}
|
|
235
|
+
const { plugin } = options, newOptions = __rest(options, ["plugin"]);
|
|
236
|
+
if (bindings) {
|
|
237
|
+
if (plugin) {
|
|
238
|
+
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;
|
|
239
|
+
return this.transformSync(plugin(m), newOptions);
|
|
240
|
+
}
|
|
241
|
+
return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
|
|
242
|
+
}
|
|
243
|
+
else if (fallbackBindings) {
|
|
244
|
+
if (plugin && !this.fallbackBindingsPluginWarningDisplayed) {
|
|
245
|
+
console.warn(`Fallback bindings does not support legacy plugins, it'll be ignored.`);
|
|
246
|
+
this.fallbackBindingsPluginWarningDisplayed = true;
|
|
247
|
+
}
|
|
248
|
+
return fallbackBindings.transformSync(isModule ? JSON.stringify(src) : src, options);
|
|
249
|
+
}
|
|
250
|
+
throw new Error("Bindings not found");
|
|
251
|
+
}
|
|
252
|
+
transformFile(path, options) {
|
|
253
|
+
var _a, _b, _c;
|
|
254
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
255
|
+
if (!bindings && !!fallbackBindings) {
|
|
256
|
+
throw new Error('Fallback bindings does not support filesystem access.');
|
|
257
|
+
}
|
|
258
|
+
else if (!bindings) {
|
|
259
|
+
throw new Error('Bindings not found.');
|
|
260
|
+
}
|
|
261
|
+
options = options || {};
|
|
262
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
263
|
+
options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
|
264
|
+
}
|
|
265
|
+
const { plugin } = options, newOptions = __rest(options, ["plugin"]);
|
|
266
|
+
newOptions.filename = path;
|
|
267
|
+
if (plugin) {
|
|
268
|
+
const m = yield this.parseFile(path, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser);
|
|
269
|
+
return this.transform(plugin(m), newOptions);
|
|
270
|
+
}
|
|
271
|
+
return bindings.transformFile(path, false, toBuffer(newOptions));
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
transformFileSync(path, options) {
|
|
275
|
+
var _a, _b, _c;
|
|
276
|
+
if (!bindings && !!fallbackBindings) {
|
|
277
|
+
throw new Error('Fallback bindings does not support filesystem access.');
|
|
278
|
+
}
|
|
279
|
+
else if (!bindings) {
|
|
280
|
+
throw new Error('Bindings not found.');
|
|
281
|
+
}
|
|
282
|
+
options = options || {};
|
|
283
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
284
|
+
options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
|
285
|
+
}
|
|
286
|
+
const { plugin } = options, newOptions = __rest(options, ["plugin"]);
|
|
287
|
+
newOptions.filename = path;
|
|
288
|
+
if (plugin) {
|
|
289
|
+
const m = this.parseFileSync(path, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser);
|
|
290
|
+
return this.transformSync(plugin(m), newOptions);
|
|
291
|
+
}
|
|
292
|
+
return bindings.transformFileSync(path, /* isModule */ false, toBuffer(newOptions));
|
|
293
|
+
}
|
|
294
|
+
bundle(options) {
|
|
295
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
296
|
+
if (!bindings && !!fallbackBindings) {
|
|
297
|
+
throw new Error('Fallback bindings does not support this interface yet.');
|
|
298
|
+
}
|
|
299
|
+
else if (!bindings) {
|
|
300
|
+
throw new Error('Bindings not found.');
|
|
301
|
+
}
|
|
302
|
+
const opts = yield (0, spack_1.compileBundleOptions)(options);
|
|
303
|
+
if (Array.isArray(opts)) {
|
|
304
|
+
const all = yield Promise.all(opts.map((opt) => __awaiter(this, void 0, void 0, function* () {
|
|
305
|
+
return this.bundle(opt);
|
|
306
|
+
})));
|
|
307
|
+
let obj = {};
|
|
308
|
+
for (const o of all) {
|
|
309
|
+
obj = Object.assign(Object.assign({}, obj), o);
|
|
310
|
+
}
|
|
311
|
+
return obj;
|
|
312
|
+
}
|
|
313
|
+
return bindings.bundle(toBuffer(Object.assign({}, opts)));
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
exports.Compiler = Compiler;
|
|
318
|
+
const compiler = new Compiler();
|
|
319
|
+
function parse(src, options) {
|
|
320
|
+
return compiler.parse(src, options);
|
|
321
|
+
}
|
|
322
|
+
exports.parse = parse;
|
|
323
|
+
function parseSync(src, options) {
|
|
324
|
+
return compiler.parseSync(src, options);
|
|
325
|
+
}
|
|
326
|
+
exports.parseSync = parseSync;
|
|
327
|
+
function parseFile(path, options) {
|
|
328
|
+
return compiler.parseFile(path, options);
|
|
329
|
+
}
|
|
330
|
+
exports.parseFile = parseFile;
|
|
331
|
+
function parseFileSync(path, options) {
|
|
332
|
+
return compiler.parseFileSync(path, options);
|
|
333
|
+
}
|
|
334
|
+
exports.parseFileSync = parseFileSync;
|
|
335
|
+
function print(m, options) {
|
|
336
|
+
return compiler.print(m, options);
|
|
337
|
+
}
|
|
338
|
+
exports.print = print;
|
|
339
|
+
function printSync(m, options) {
|
|
340
|
+
return compiler.printSync(m, options);
|
|
341
|
+
}
|
|
342
|
+
exports.printSync = printSync;
|
|
343
|
+
function transform(src, options) {
|
|
344
|
+
return compiler.transform(src, options);
|
|
345
|
+
}
|
|
346
|
+
exports.transform = transform;
|
|
347
|
+
function transformSync(src, options) {
|
|
348
|
+
return compiler.transformSync(src, options);
|
|
349
|
+
}
|
|
350
|
+
exports.transformSync = transformSync;
|
|
351
|
+
function transformFile(path, options) {
|
|
352
|
+
return compiler.transformFile(path, options);
|
|
353
|
+
}
|
|
354
|
+
exports.transformFile = transformFile;
|
|
355
|
+
function transformFileSync(path, options) {
|
|
356
|
+
return compiler.transformFileSync(path, options);
|
|
357
|
+
}
|
|
358
|
+
exports.transformFileSync = transformFileSync;
|
|
359
|
+
function bundle(options) {
|
|
360
|
+
return compiler.bundle(options);
|
|
361
|
+
}
|
|
362
|
+
exports.bundle = bundle;
|
|
363
|
+
function minify(src, opts) {
|
|
364
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
365
|
+
return compiler.minify(src, opts);
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
exports.minify = minify;
|
|
369
|
+
function minifySync(src, opts) {
|
|
370
|
+
return compiler.minifySync(src, opts);
|
|
371
|
+
}
|
|
372
|
+
exports.minifySync = minifySync;
|
|
373
|
+
/**
|
|
374
|
+
* Configure custom trace configuration runs for a process lifecycle.
|
|
375
|
+
* Currently only chromium's trace event format is supported.
|
|
376
|
+
* (https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview)
|
|
377
|
+
*
|
|
378
|
+
* This should be called before calling any binding interfaces exported in `@swc/core`, such as
|
|
379
|
+
* `transform*`, or `parse*` or anything. To avoid breaking changes, each binding fn internally
|
|
380
|
+
* sets default trace subscriber if not set.
|
|
381
|
+
*
|
|
382
|
+
* Unlike other configuration, this does not belong to individual api surface using swcrc
|
|
383
|
+
* or api's parameters (`transform(..., {trace})`). This is due to current tracing subscriber
|
|
384
|
+
* can be configured only once for the global scope. Calling `registerGlobalTraceConfig` multiple
|
|
385
|
+
* time won't cause error, subsequent calls will be ignored.
|
|
386
|
+
*
|
|
387
|
+
* As name implies currently this is experimental interface may change over time without semver
|
|
388
|
+
* major breaking changes. Please provide feedbacks,
|
|
389
|
+
* or bug report at https://github.com/swc-project/swc/discussions.
|
|
390
|
+
*/
|
|
391
|
+
function __experimental_registerGlobalTraceConfig(traceConfig) {
|
|
392
|
+
// Do not raise error if binding doesn't exists - fallback binding will not support
|
|
393
|
+
// this ever.
|
|
394
|
+
if (bindings) {
|
|
395
|
+
if (traceConfig.type === 'traceEvent') {
|
|
396
|
+
bindings.initCustomTraceSubscriber(traceConfig.fileName);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
exports.__experimental_registerGlobalTraceConfig = __experimental_registerGlobalTraceConfig;
|
|
401
|
+
/**
|
|
402
|
+
* @ignore
|
|
403
|
+
*
|
|
404
|
+
* Returns current binary's metadata to determine which binary is actually loaded.
|
|
405
|
+
*
|
|
406
|
+
* This is undocumented interface, does not guarantee stability across `@swc/core`'s semver
|
|
407
|
+
* as internal representation may change anytime. Use it with caution.
|
|
408
|
+
*/
|
|
409
|
+
function getBinaryMetadata() {
|
|
410
|
+
return {
|
|
411
|
+
target: bindings ? bindings === null || bindings === void 0 ? void 0 : bindings.getTargetTriple() : undefined
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
exports.getBinaryMetadata = getBinaryMetadata;
|
|
415
|
+
exports.DEFAULT_EXTENSIONS = Object.freeze([
|
|
416
|
+
".js",
|
|
417
|
+
".jsx",
|
|
418
|
+
".es6",
|
|
419
|
+
".es",
|
|
420
|
+
".mjs",
|
|
421
|
+
".ts",
|
|
422
|
+
".tsx"
|
|
423
|
+
]);
|
|
424
|
+
function toBuffer(t) {
|
|
425
|
+
return Buffer.from(JSON.stringify(t));
|
|
426
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ohos-ports/swc-core",
|
|
3
|
+
"version": "1.3.14-beta.0",
|
|
4
|
+
"description": "Super-fast alternative for babel (HarmonyOS openharmony-arm64 port by ohos-ports)",
|
|
5
|
+
"homepage": "https://swc.rs",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"author": "강동윤 <kdy1997.dev@gmail.com>",
|
|
8
|
+
"license": "Apache-2.0",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"swc",
|
|
11
|
+
"swcpack",
|
|
12
|
+
"babel",
|
|
13
|
+
"typescript",
|
|
14
|
+
"rust",
|
|
15
|
+
"webpack",
|
|
16
|
+
"tsc",
|
|
17
|
+
"openharmony",
|
|
18
|
+
"ohos"
|
|
19
|
+
],
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=10"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/ohos-ports/ohos-ports.git",
|
|
26
|
+
"directory": "ports/@swc/core/1.3.14"
|
|
27
|
+
},
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/ohos-ports/ohos-ports/issues"
|
|
30
|
+
},
|
|
31
|
+
"napi": {
|
|
32
|
+
"binaryName": "swc",
|
|
33
|
+
"triples": {
|
|
34
|
+
"defaults": true,
|
|
35
|
+
"additional": [
|
|
36
|
+
"x86_64-unknown-linux-musl",
|
|
37
|
+
"i686-pc-windows-msvc",
|
|
38
|
+
"aarch64-unknown-linux-gnu",
|
|
39
|
+
"armv7-unknown-linux-gnueabihf",
|
|
40
|
+
"aarch64-apple-darwin",
|
|
41
|
+
"aarch64-unknown-linux-musl",
|
|
42
|
+
"aarch64-pc-windows-msvc"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"registry": "https://registry.npmjs.org/",
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"types": "./index.d.ts",
|
|
51
|
+
"scripts": {
|
|
52
|
+
"postinstall": "node postinstall.js"
|
|
53
|
+
},
|
|
54
|
+
"bin": {
|
|
55
|
+
"swcx": "run_swcx.js"
|
|
56
|
+
},
|
|
57
|
+
"optionalDependencies": {
|
|
58
|
+
"@swc/core-win32-x64-msvc": "1.3.14",
|
|
59
|
+
"@swc/core-darwin-x64": "1.3.14",
|
|
60
|
+
"@swc/core-linux-x64-gnu": "1.3.14",
|
|
61
|
+
"@swc/core-linux-x64-musl": "1.3.14",
|
|
62
|
+
"@swc/core-win32-ia32-msvc": "1.3.14",
|
|
63
|
+
"@swc/core-linux-arm64-gnu": "1.3.14",
|
|
64
|
+
"@swc/core-linux-arm-gnueabihf": "1.3.14",
|
|
65
|
+
"@swc/core-darwin-arm64": "1.3.14",
|
|
66
|
+
"@swc/core-linux-arm64-musl": "1.3.14",
|
|
67
|
+
"@swc/core-win32-arm64-msvc": "1.3.14"
|
|
68
|
+
},
|
|
69
|
+
"files": [
|
|
70
|
+
"CHANGELOG.md",
|
|
71
|
+
"Visitor.d.ts",
|
|
72
|
+
"index.d.ts",
|
|
73
|
+
"spack.js",
|
|
74
|
+
"util.d.ts",
|
|
75
|
+
"LICENSE",
|
|
76
|
+
"Visitor.js",
|
|
77
|
+
"binding.d.ts",
|
|
78
|
+
"index.js",
|
|
79
|
+
"types.d.ts",
|
|
80
|
+
"util.js",
|
|
81
|
+
"README.md",
|
|
82
|
+
"binding.js",
|
|
83
|
+
"package.json",
|
|
84
|
+
"spack.d.ts",
|
|
85
|
+
"types.js",
|
|
86
|
+
"postinstall.js",
|
|
87
|
+
"run_swcx.js",
|
|
88
|
+
"swc.openharmony-arm64.node"
|
|
89
|
+
]
|
|
90
|
+
}
|
package/postinstall.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
/**
|
|
36
|
+
* A postinstall script runs after `@swc/core` is installed.
|
|
37
|
+
*
|
|
38
|
+
* It checks if corresponding optional dependencies for native binary is installed and can be loaded properly.
|
|
39
|
+
* If it fails, it'll internally try to install `@swc/wasm` as fallback.
|
|
40
|
+
*/
|
|
41
|
+
const fs_1 = require("fs");
|
|
42
|
+
const assert = __importStar(require("assert"));
|
|
43
|
+
const path = __importStar(require("path"));
|
|
44
|
+
const child_process = __importStar(require("child_process"));
|
|
45
|
+
const fs = __importStar(require("fs"));
|
|
46
|
+
function removeRecursive(dir) {
|
|
47
|
+
for (const entry of fs.readdirSync(dir)) {
|
|
48
|
+
const entryPath = path.join(dir, entry);
|
|
49
|
+
let stats;
|
|
50
|
+
try {
|
|
51
|
+
stats = fs.lstatSync(entryPath);
|
|
52
|
+
}
|
|
53
|
+
catch (_a) {
|
|
54
|
+
continue; // Guard against https://github.com/nodejs/node/issues/4760
|
|
55
|
+
}
|
|
56
|
+
if (stats.isDirectory())
|
|
57
|
+
removeRecursive(entryPath);
|
|
58
|
+
else
|
|
59
|
+
fs.unlinkSync(entryPath);
|
|
60
|
+
}
|
|
61
|
+
fs.rmdirSync(dir);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Trying to validate @swc/core's native binary installation, then installs if it is not supported.
|
|
65
|
+
*/
|
|
66
|
+
const validateBinary = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
67
|
+
var _a;
|
|
68
|
+
try {
|
|
69
|
+
const { name } = require(path.resolve(process.env.INIT_CWD, 'package.json'));
|
|
70
|
+
if (name === '@swc/core') {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
catch (_) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
// TODO: We do not take care of the case if user try to install with `--no-optional`.
|
|
78
|
+
// For now, it is considered as deliberate decision.
|
|
79
|
+
let binding;
|
|
80
|
+
try {
|
|
81
|
+
binding = require('./binding');
|
|
82
|
+
// Check if binding binary actually works.
|
|
83
|
+
// For the latest version, checks target triple. If it's old version doesn't have target triple, use parseSync instead.
|
|
84
|
+
const triple = binding.getTargetTriple ? binding.getTargetTriple() : binding.parseSync('console.log()', Buffer.from(JSON.stringify({ syntax: "ecmascript" })));
|
|
85
|
+
assert.ok(triple, 'Failed to read target triple from native binary.');
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
// if error is unsupported architecture, ignore to display.
|
|
89
|
+
if (!((_a = error.message) === null || _a === void 0 ? void 0 : _a.includes('Unsupported architecture'))) {
|
|
90
|
+
console.warn(error);
|
|
91
|
+
}
|
|
92
|
+
console.warn(`@swc/core was not able to resolve native bindings installation. It'll try to use @swc/wasm as fallback instead.`);
|
|
93
|
+
}
|
|
94
|
+
if (!!binding) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
// User choose to override the binary installation. Skip remanining validation.
|
|
98
|
+
if (!!process.env["SWC_BINARY_PATH"]) {
|
|
99
|
+
console.warn(`@swc/core could not resolve native bindings installation, but found manual override config SWC_BINARY_PATH specified. Skipping remaning validation.`);
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
// Check if top-level package.json installs @swc/wasm separately already
|
|
103
|
+
let wasmBinding;
|
|
104
|
+
try {
|
|
105
|
+
wasmBinding = require.resolve(`@swc/wasm`);
|
|
106
|
+
}
|
|
107
|
+
catch (_) {
|
|
108
|
+
}
|
|
109
|
+
if (!!wasmBinding && (0, fs_1.existsSync)(wasmBinding)) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const env = Object.assign(Object.assign({}, process.env), { npm_config_global: undefined });
|
|
113
|
+
const { version } = require(path.join(path.dirname(require.resolve('@swc/core')), 'package.json'));
|
|
114
|
+
// We want to place @swc/wasm next to the @swc/core as if normal installation was done,
|
|
115
|
+
// but can't directly set cwd to INIT_CWD as npm seems to acquire lock to the working dir.
|
|
116
|
+
// Instead, create a temporary inner and move it out.
|
|
117
|
+
const coreDir = path.dirname(require.resolve('@swc/core'));
|
|
118
|
+
const installDir = path.join(coreDir, 'npm-install');
|
|
119
|
+
try {
|
|
120
|
+
fs.mkdirSync(installDir);
|
|
121
|
+
fs.writeFileSync(path.join(installDir, 'package.json'), '{}');
|
|
122
|
+
// Instead of carrying over own dependencies to download & resolve package which increases installation sizes of `@swc/core`,
|
|
123
|
+
// assume & relies on system's npm installation.
|
|
124
|
+
child_process.execSync(`npm install --no-save --loglevel=error --prefer-offline --no-audit --progress=false @swc/wasm@${version}`, { cwd: installDir, stdio: 'pipe', env });
|
|
125
|
+
const installedBinPath = path.join(installDir, 'node_modules', `@swc/wasm`);
|
|
126
|
+
// INIT_CWD is injected via npm. If it doesn't exists, can't proceed.
|
|
127
|
+
fs.renameSync(installedBinPath, path.resolve(process.env.INIT_CWD, 'node_modules', `@swc/wasm`));
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
console.error(error);
|
|
131
|
+
console.error(`Failed to install fallback @swc/wasm@${version}. @swc/core will not properly.
|
|
132
|
+
Please install @swc/wasm manually, or retry whole installation.
|
|
133
|
+
If there are unexpected errors, please report at https://github.com/swc-project/swc/issues`);
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
try {
|
|
137
|
+
removeRecursive(installDir);
|
|
138
|
+
}
|
|
139
|
+
catch (_) {
|
|
140
|
+
// Gracefully ignore any failures. This'll make few leftover files but it shouldn't block installation.
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
validateBinary().catch((error) => {
|
|
145
|
+
// for now just throw the error as-is.
|
|
146
|
+
throw error;
|
|
147
|
+
});
|