@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/binding.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ export const bundle: any;
2
+ export const minify: any;
3
+ export const minifySync: any;
4
+ export const parse: any;
5
+ export const parseSync: any;
6
+ export const parseFileSync: any;
7
+ export const parseFile: any;
8
+ export const print: any;
9
+ export const printSync: any;
10
+ export const transform: any;
11
+ export const transformSync: any;
12
+ export const transformFile: any;
13
+ export const transformFileSync: any;
14
+ export const getTargetTriple: any;
15
+ export const initCustomTraceSubscriber: any;
16
+ export const Compiler: any;
package/binding.js ADDED
@@ -0,0 +1,283 @@
1
+ "use strict";
2
+ const { existsSync, readFileSync } = require('fs');
3
+ const { join } = require('path');
4
+ const { platform, arch } = process;
5
+ let nativeBinding = null;
6
+ let localFileExisted = false;
7
+ let loadError = null;
8
+ function isMusl() {
9
+ // For Node 10
10
+ if (!process.report || typeof process.report.getReport !== 'function') {
11
+ try {
12
+ return readFileSync('/usr/bin/ldd', 'utf8').includes('musl');
13
+ }
14
+ catch (e) {
15
+ return true;
16
+ }
17
+ }
18
+ else {
19
+ const { glibcVersionRuntime } = process.report.getReport().header;
20
+ return !glibcVersionRuntime;
21
+ }
22
+ }
23
+ switch (platform) {
24
+ case 'android':
25
+ switch (arch) {
26
+ case 'arm64':
27
+ localFileExisted = existsSync(join(__dirname, 'swc.android-arm64.node'));
28
+ try {
29
+ if (localFileExisted) {
30
+ nativeBinding = require('./swc.android-arm64.node');
31
+ }
32
+ else {
33
+ nativeBinding = require('@swc/core-android-arm64');
34
+ }
35
+ }
36
+ catch (e) {
37
+ loadError = e;
38
+ }
39
+ break;
40
+ case 'arm':
41
+ localFileExisted = existsSync(join(__dirname, 'swc.android-arm-eabi.node'));
42
+ try {
43
+ if (localFileExisted) {
44
+ nativeBinding = require('./swc.android-arm-eabi.node');
45
+ }
46
+ else {
47
+ nativeBinding = require('@swc/core-android-arm-eabi');
48
+ }
49
+ }
50
+ catch (e) {
51
+ loadError = e;
52
+ }
53
+ break;
54
+ default:
55
+ throw new Error(`Unsupported architecture on Android ${arch}`);
56
+ }
57
+ break;
58
+ case 'win32':
59
+ switch (arch) {
60
+ case 'x64':
61
+ localFileExisted = existsSync(join(__dirname, 'swc.win32-x64-msvc.node'));
62
+ try {
63
+ if (localFileExisted) {
64
+ nativeBinding = require('./swc.win32-x64-msvc.node');
65
+ }
66
+ else {
67
+ nativeBinding = require('@swc/core-win32-x64-msvc');
68
+ }
69
+ }
70
+ catch (e) {
71
+ loadError = e;
72
+ }
73
+ break;
74
+ case 'ia32':
75
+ localFileExisted = existsSync(join(__dirname, 'swc.win32-ia32-msvc.node'));
76
+ try {
77
+ if (localFileExisted) {
78
+ nativeBinding = require('./swc.win32-ia32-msvc.node');
79
+ }
80
+ else {
81
+ nativeBinding = require('@swc/core-win32-ia32-msvc');
82
+ }
83
+ }
84
+ catch (e) {
85
+ loadError = e;
86
+ }
87
+ break;
88
+ case 'arm64':
89
+ localFileExisted = existsSync(join(__dirname, 'swc.win32-arm64-msvc.node'));
90
+ try {
91
+ if (localFileExisted) {
92
+ nativeBinding = require('./swc.win32-arm64-msvc.node');
93
+ }
94
+ else {
95
+ nativeBinding = require('@swc/core-win32-arm64-msvc');
96
+ }
97
+ }
98
+ catch (e) {
99
+ loadError = e;
100
+ }
101
+ break;
102
+ default:
103
+ throw new Error(`Unsupported architecture on Windows: ${arch}`);
104
+ }
105
+ break;
106
+ case 'darwin':
107
+ switch (arch) {
108
+ case 'x64':
109
+ localFileExisted = existsSync(join(__dirname, 'swc.darwin-x64.node'));
110
+ try {
111
+ if (localFileExisted) {
112
+ nativeBinding = require('./swc.darwin-x64.node');
113
+ }
114
+ else {
115
+ nativeBinding = require('@swc/core-darwin-x64');
116
+ }
117
+ }
118
+ catch (e) {
119
+ loadError = e;
120
+ }
121
+ break;
122
+ case 'arm64':
123
+ localFileExisted = existsSync(join(__dirname, 'swc.darwin-arm64.node'));
124
+ try {
125
+ if (localFileExisted) {
126
+ nativeBinding = require('./swc.darwin-arm64.node');
127
+ }
128
+ else {
129
+ nativeBinding = require('@swc/core-darwin-arm64');
130
+ }
131
+ }
132
+ catch (e) {
133
+ loadError = e;
134
+ }
135
+ break;
136
+ default:
137
+ throw new Error(`Unsupported architecture on macOS: ${arch}`);
138
+ }
139
+ break;
140
+ case 'freebsd':
141
+ if (arch !== 'x64') {
142
+ throw new Error(`Unsupported architecture on FreeBSD: ${arch}`);
143
+ }
144
+ localFileExisted = existsSync(join(__dirname, 'swc.freebsd-x64.node'));
145
+ try {
146
+ if (localFileExisted) {
147
+ nativeBinding = require('./swc.freebsd-x64.node');
148
+ }
149
+ else {
150
+ nativeBinding = require('@swc/core-freebsd-x64');
151
+ }
152
+ }
153
+ catch (e) {
154
+ loadError = e;
155
+ }
156
+ break;
157
+ case 'openharmony':
158
+ // [ohos-port] self-contained HarmonyOS binding: prefer bundled swc.openharmony-arm64.node
159
+ switch (arch) {
160
+ case 'arm64':
161
+ localFileExisted = existsSync(join(__dirname, 'swc.openharmony-arm64.node'));
162
+ try {
163
+ if (localFileExisted) {
164
+ nativeBinding = require('./swc.openharmony-arm64.node');
165
+ }
166
+ else {
167
+ nativeBinding = require('@swc/core-openharmony-arm64');
168
+ }
169
+ }
170
+ catch (e) {
171
+ loadError = e;
172
+ }
173
+ break;
174
+ default:
175
+ throw new Error(`Unsupported architecture on OpenHarmony ${arch}`);
176
+ }
177
+ break;
178
+ case 'linux':
179
+ switch (arch) {
180
+ case 'x64':
181
+ if (isMusl()) {
182
+ localFileExisted = existsSync(join(__dirname, 'swc.linux-x64-musl.node'));
183
+ try {
184
+ if (localFileExisted) {
185
+ nativeBinding = require('./swc.linux-x64-musl.node');
186
+ }
187
+ else {
188
+ nativeBinding = require('@swc/core-linux-x64-musl');
189
+ }
190
+ }
191
+ catch (e) {
192
+ loadError = e;
193
+ }
194
+ }
195
+ else {
196
+ localFileExisted = existsSync(join(__dirname, 'swc.linux-x64-gnu.node'));
197
+ try {
198
+ if (localFileExisted) {
199
+ nativeBinding = require('./swc.linux-x64-gnu.node');
200
+ }
201
+ else {
202
+ nativeBinding = require('@swc/core-linux-x64-gnu');
203
+ }
204
+ }
205
+ catch (e) {
206
+ loadError = e;
207
+ }
208
+ }
209
+ break;
210
+ case 'arm64':
211
+ if (isMusl()) {
212
+ localFileExisted = existsSync(join(__dirname, 'swc.linux-arm64-musl.node'));
213
+ try {
214
+ if (localFileExisted) {
215
+ nativeBinding = require('./swc.linux-arm64-musl.node');
216
+ }
217
+ else {
218
+ nativeBinding = require('@swc/core-linux-arm64-musl');
219
+ }
220
+ }
221
+ catch (e) {
222
+ loadError = e;
223
+ }
224
+ }
225
+ else {
226
+ localFileExisted = existsSync(join(__dirname, 'swc.linux-arm64-gnu.node'));
227
+ try {
228
+ if (localFileExisted) {
229
+ nativeBinding = require('./swc.linux-arm64-gnu.node');
230
+ }
231
+ else {
232
+ nativeBinding = require('@swc/core-linux-arm64-gnu');
233
+ }
234
+ }
235
+ catch (e) {
236
+ loadError = e;
237
+ }
238
+ }
239
+ break;
240
+ case 'arm':
241
+ localFileExisted = existsSync(join(__dirname, 'swc.linux-arm-gnueabihf.node'));
242
+ try {
243
+ if (localFileExisted) {
244
+ nativeBinding = require('./swc.linux-arm-gnueabihf.node');
245
+ }
246
+ else {
247
+ nativeBinding = require('@swc/core-linux-arm-gnueabihf');
248
+ }
249
+ }
250
+ catch (e) {
251
+ loadError = e;
252
+ }
253
+ break;
254
+ default:
255
+ throw new Error(`Unsupported architecture on Linux: ${arch}`);
256
+ }
257
+ break;
258
+ default:
259
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`);
260
+ }
261
+ if (!nativeBinding) {
262
+ if (loadError) {
263
+ throw loadError;
264
+ }
265
+ throw new Error(`Failed to load native binding`);
266
+ }
267
+ const { bundle, minify, minifySync, parse, parseSync, parseFileSync, parseFile, print, printSync, transform, transformSync, transformFile, transformFileSync, getTargetTriple, initCustomTraceSubscriber, Compiler } = nativeBinding;
268
+ module.exports.bundle = bundle;
269
+ module.exports.minify = minify;
270
+ module.exports.minifySync = minifySync;
271
+ module.exports.parse = parse;
272
+ module.exports.parseSync = parseSync;
273
+ module.exports.parseFileSync = parseFileSync;
274
+ module.exports.parseFile = parseFile;
275
+ module.exports.print = print;
276
+ module.exports.printSync = printSync;
277
+ module.exports.transform = transform;
278
+ module.exports.transformSync = transformSync;
279
+ module.exports.transformFile = transformFile;
280
+ module.exports.transformFileSync = transformFileSync;
281
+ module.exports.getTargetTriple = getTargetTriple;
282
+ module.exports.initCustomTraceSubscriber = initCustomTraceSubscriber;
283
+ module.exports.Compiler = Compiler;
package/index.d.ts ADDED
@@ -0,0 +1,107 @@
1
+ import { Plugin, ParseOptions, Module, Output, Options, Script, Program, JsMinifyOptions } from "./types";
2
+ export * from "./types";
3
+ import { BundleInput } from "./spack";
4
+ /**
5
+ * Version of the swc binding.
6
+ */
7
+ export declare const version: string;
8
+ export declare function plugins(ps: Plugin[]): Plugin;
9
+ export declare class Compiler {
10
+ private fallbackBindingsPluginWarningDisplayed;
11
+ minify(src: string, opts?: JsMinifyOptions): Promise<Output>;
12
+ minifySync(src: string, opts?: JsMinifyOptions): Output;
13
+ parse(src: string, options: ParseOptions & {
14
+ isModule: false;
15
+ }): Promise<Script>;
16
+ parse(src: string, options?: ParseOptions, filename?: string): Promise<Module>;
17
+ parseSync(src: string, options: ParseOptions & {
18
+ isModule: false;
19
+ }): Script;
20
+ parseSync(src: string, options?: ParseOptions, filename?: string): Module;
21
+ parseFile(path: string, options: ParseOptions & {
22
+ isModule: false;
23
+ }): Promise<Script>;
24
+ parseFile(path: string, options?: ParseOptions): Promise<Module>;
25
+ parseFileSync(path: string, options: ParseOptions & {
26
+ isModule: false;
27
+ }): Script;
28
+ parseFileSync(path: string, options?: ParseOptions): Module;
29
+ /**
30
+ * Note: this method should be invoked on the compiler instance used
31
+ * for `parse()` / `parseSync()`.
32
+ */
33
+ print(m: Program, options?: Options): Promise<Output>;
34
+ /**
35
+ * Note: this method should be invoked on the compiler instance used
36
+ * for `parse()` / `parseSync()`.
37
+ */
38
+ printSync(m: Program, options?: Options): Output;
39
+ transform(src: string | Program, options?: Options): Promise<Output>;
40
+ transformSync(src: string | Program, options?: Options): Output;
41
+ transformFile(path: string, options?: Options): Promise<Output>;
42
+ transformFileSync(path: string, options?: Options): Output;
43
+ bundle(options?: BundleInput | string): Promise<{
44
+ [name: string]: Output;
45
+ }>;
46
+ }
47
+ export declare function parse(src: string, options: ParseOptions & {
48
+ isModule: false;
49
+ }): Promise<Script>;
50
+ export declare function parse(src: string, options?: ParseOptions): Promise<Module>;
51
+ export declare function parseSync(src: string, options: ParseOptions & {
52
+ isModule: false;
53
+ }): Script;
54
+ export declare function parseSync(src: string, options?: ParseOptions): Module;
55
+ export declare function parseFile(path: string, options: ParseOptions & {
56
+ isModule: false;
57
+ }): Promise<Script>;
58
+ export declare function parseFile(path: string, options?: ParseOptions): Promise<Module>;
59
+ export declare function parseFileSync(path: string, options: ParseOptions & {
60
+ isModule: false;
61
+ }): Script;
62
+ export declare function parseFileSync(path: string, options?: ParseOptions): Module;
63
+ export declare function print(m: Program, options?: Options): Promise<Output>;
64
+ export declare function printSync(m: Program, options?: Options): Output;
65
+ export declare function transform(src: string | Program, options?: Options): Promise<Output>;
66
+ export declare function transformSync(src: string | Program, options?: Options): Output;
67
+ export declare function transformFile(path: string, options?: Options): Promise<Output>;
68
+ export declare function transformFileSync(path: string, options?: Options): Output;
69
+ export declare function bundle(options?: BundleInput | string): Promise<{
70
+ [name: string]: Output;
71
+ }>;
72
+ export declare function minify(src: string, opts?: JsMinifyOptions): Promise<Output>;
73
+ export declare function minifySync(src: string, opts?: JsMinifyOptions): Output;
74
+ /**
75
+ * Configure custom trace configuration runs for a process lifecycle.
76
+ * Currently only chromium's trace event format is supported.
77
+ * (https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview)
78
+ *
79
+ * This should be called before calling any binding interfaces exported in `@swc/core`, such as
80
+ * `transform*`, or `parse*` or anything. To avoid breaking changes, each binding fn internally
81
+ * sets default trace subscriber if not set.
82
+ *
83
+ * Unlike other configuration, this does not belong to individual api surface using swcrc
84
+ * or api's parameters (`transform(..., {trace})`). This is due to current tracing subscriber
85
+ * can be configured only once for the global scope. Calling `registerGlobalTraceConfig` multiple
86
+ * time won't cause error, subsequent calls will be ignored.
87
+ *
88
+ * As name implies currently this is experimental interface may change over time without semver
89
+ * major breaking changes. Please provide feedbacks,
90
+ * or bug report at https://github.com/swc-project/swc/discussions.
91
+ */
92
+ export declare function __experimental_registerGlobalTraceConfig(traceConfig: {
93
+ type: 'traceEvent';
94
+ fileName?: string;
95
+ }): void;
96
+ /**
97
+ * @ignore
98
+ *
99
+ * Returns current binary's metadata to determine which binary is actually loaded.
100
+ *
101
+ * This is undocumented interface, does not guarantee stability across `@swc/core`'s semver
102
+ * as internal representation may change anytime. Use it with caution.
103
+ */
104
+ export declare function getBinaryMetadata(): {
105
+ target: string | undefined;
106
+ };
107
+ export declare const DEFAULT_EXTENSIONS: readonly string[];