@swc/core 1.5.23 → 1.5.25
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/Visitor.d.ts +218 -0
- package/Visitor.js +1408 -0
- package/binding.d.ts +92 -0
- package/index.d.ts +110 -0
- package/index.js +436 -0
- package/package.json +24 -24
- package/spack.d.ts +51 -0
- package/spack.js +87 -0
- package/util.d.ts +1 -0
- package/util.js +104 -0
package/binding.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/* auto-generated by NAPI-RS */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export class Compiler {
|
|
5
|
+
constructor();
|
|
6
|
+
}
|
|
7
|
+
export type JsCompiler = Compiler;
|
|
8
|
+
|
|
9
|
+
export function bundle(
|
|
10
|
+
confItems: Buffer,
|
|
11
|
+
signal?: AbortSignal | undefined | null
|
|
12
|
+
): Promise<{ [index: string]: { code: string; map?: string } }>;
|
|
13
|
+
|
|
14
|
+
export function getTargetTriple(): string;
|
|
15
|
+
|
|
16
|
+
export function initCustomTraceSubscriber(
|
|
17
|
+
traceOutFilePath?: string | undefined | null
|
|
18
|
+
): void;
|
|
19
|
+
|
|
20
|
+
export function minify(
|
|
21
|
+
code: Buffer,
|
|
22
|
+
opts: Buffer,
|
|
23
|
+
signal?: AbortSignal | undefined | null
|
|
24
|
+
): Promise<TransformOutput>;
|
|
25
|
+
|
|
26
|
+
export function minifySync(code: Buffer, opts: Buffer): TransformOutput;
|
|
27
|
+
|
|
28
|
+
export function parse(
|
|
29
|
+
src: string,
|
|
30
|
+
options: Buffer,
|
|
31
|
+
filename?: string | undefined | null,
|
|
32
|
+
signal?: AbortSignal | undefined | null
|
|
33
|
+
): Promise<string>;
|
|
34
|
+
|
|
35
|
+
export function parseFile(
|
|
36
|
+
path: string,
|
|
37
|
+
options: Buffer,
|
|
38
|
+
signal?: AbortSignal | undefined | null
|
|
39
|
+
): Promise<string>;
|
|
40
|
+
|
|
41
|
+
export function parseFileSync(path: string, opts: Buffer): string;
|
|
42
|
+
|
|
43
|
+
export function parseSync(
|
|
44
|
+
src: string,
|
|
45
|
+
opts: Buffer,
|
|
46
|
+
filename?: string | undefined | null
|
|
47
|
+
): string;
|
|
48
|
+
|
|
49
|
+
export function print(
|
|
50
|
+
programJson: string,
|
|
51
|
+
options: Buffer,
|
|
52
|
+
signal?: AbortSignal | undefined | null
|
|
53
|
+
): Promise<TransformOutput>;
|
|
54
|
+
|
|
55
|
+
export function printSync(program: string, options: Buffer): TransformOutput;
|
|
56
|
+
|
|
57
|
+
export function transform(
|
|
58
|
+
src: string,
|
|
59
|
+
isModule: boolean,
|
|
60
|
+
options: Buffer,
|
|
61
|
+
signal?: AbortSignal | undefined | null
|
|
62
|
+
): Promise<TransformOutput>;
|
|
63
|
+
|
|
64
|
+
export function transformFile(
|
|
65
|
+
src: string,
|
|
66
|
+
isModule: boolean,
|
|
67
|
+
options: Buffer,
|
|
68
|
+
signal?: AbortSignal | undefined | null
|
|
69
|
+
): Promise<TransformOutput>;
|
|
70
|
+
|
|
71
|
+
export function transformFileSync(
|
|
72
|
+
s: string,
|
|
73
|
+
isModule: boolean,
|
|
74
|
+
opts: Buffer
|
|
75
|
+
): TransformOutput;
|
|
76
|
+
|
|
77
|
+
export interface TransformOutput {
|
|
78
|
+
code: string;
|
|
79
|
+
map?: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Hack for `Type Generation` */
|
|
83
|
+
export interface TransformOutput {
|
|
84
|
+
code: string;
|
|
85
|
+
map?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function transformSync(
|
|
89
|
+
s: string,
|
|
90
|
+
isModule: boolean,
|
|
91
|
+
opts: Buffer
|
|
92
|
+
): TransformOutput;
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { Plugin, ParseOptions, Module, Output, Options, Script, Program, JsMinifyOptions } from "@swc/types";
|
|
2
|
+
export type * from "@swc/types";
|
|
3
|
+
import { BundleInput } from "./spack";
|
|
4
|
+
/**
|
|
5
|
+
* Version of the swc binding.
|
|
6
|
+
*/
|
|
7
|
+
export declare const version: string;
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated JavaScript API is deprecated. Please use Wasm plugin instead.
|
|
10
|
+
*/
|
|
11
|
+
export declare function plugins(ps: Plugin[]): Plugin;
|
|
12
|
+
export declare class Compiler {
|
|
13
|
+
private fallbackBindingsPluginWarningDisplayed;
|
|
14
|
+
minify(src: string, opts?: JsMinifyOptions): Promise<Output>;
|
|
15
|
+
minifySync(src: string, opts?: JsMinifyOptions): Output;
|
|
16
|
+
parse(src: string, options: ParseOptions & {
|
|
17
|
+
isModule: false;
|
|
18
|
+
}): Promise<Script>;
|
|
19
|
+
parse(src: string, options?: ParseOptions, filename?: string): Promise<Module>;
|
|
20
|
+
parseSync(src: string, options: ParseOptions & {
|
|
21
|
+
isModule: false;
|
|
22
|
+
}): Script;
|
|
23
|
+
parseSync(src: string, options?: ParseOptions, filename?: string): Module;
|
|
24
|
+
parseFile(path: string, options: ParseOptions & {
|
|
25
|
+
isModule: false;
|
|
26
|
+
}): Promise<Script>;
|
|
27
|
+
parseFile(path: string, options?: ParseOptions): Promise<Module>;
|
|
28
|
+
parseFileSync(path: string, options: ParseOptions & {
|
|
29
|
+
isModule: false;
|
|
30
|
+
}): Script;
|
|
31
|
+
parseFileSync(path: string, options?: ParseOptions): Module;
|
|
32
|
+
/**
|
|
33
|
+
* Note: this method should be invoked on the compiler instance used
|
|
34
|
+
* for `parse()` / `parseSync()`.
|
|
35
|
+
*/
|
|
36
|
+
print(m: Program, options?: Options): Promise<Output>;
|
|
37
|
+
/**
|
|
38
|
+
* Note: this method should be invoked on the compiler instance used
|
|
39
|
+
* for `parse()` / `parseSync()`.
|
|
40
|
+
*/
|
|
41
|
+
printSync(m: Program, options?: Options): Output;
|
|
42
|
+
transform(src: string | Program, options?: Options): Promise<Output>;
|
|
43
|
+
transformSync(src: string | Program, options?: Options): Output;
|
|
44
|
+
transformFile(path: string, options?: Options): Promise<Output>;
|
|
45
|
+
transformFileSync(path: string, options?: Options): Output;
|
|
46
|
+
bundle(options?: BundleInput | string): Promise<{
|
|
47
|
+
[name: string]: Output;
|
|
48
|
+
}>;
|
|
49
|
+
}
|
|
50
|
+
export declare function parse(src: string, options: ParseOptions & {
|
|
51
|
+
isModule: false;
|
|
52
|
+
}): Promise<Script>;
|
|
53
|
+
export declare function parse(src: string, options?: ParseOptions): Promise<Module>;
|
|
54
|
+
export declare function parseSync(src: string, options: ParseOptions & {
|
|
55
|
+
isModule: false;
|
|
56
|
+
}): Script;
|
|
57
|
+
export declare function parseSync(src: string, options?: ParseOptions): Module;
|
|
58
|
+
export declare function parseFile(path: string, options: ParseOptions & {
|
|
59
|
+
isModule: false;
|
|
60
|
+
}): Promise<Script>;
|
|
61
|
+
export declare function parseFile(path: string, options?: ParseOptions): Promise<Module>;
|
|
62
|
+
export declare function parseFileSync(path: string, options: ParseOptions & {
|
|
63
|
+
isModule: false;
|
|
64
|
+
}): Script;
|
|
65
|
+
export declare function parseFileSync(path: string, options?: ParseOptions): Module;
|
|
66
|
+
export declare function print(m: Program, options?: Options): Promise<Output>;
|
|
67
|
+
export declare function printSync(m: Program, options?: Options): Output;
|
|
68
|
+
export declare function transform(src: string | Program, options?: Options): Promise<Output>;
|
|
69
|
+
export declare function transformSync(src: string | Program, options?: Options): Output;
|
|
70
|
+
export declare function transformFile(path: string, options?: Options): Promise<Output>;
|
|
71
|
+
export declare function transformFileSync(path: string, options?: Options): Output;
|
|
72
|
+
export declare function bundle(options?: BundleInput | string): Promise<{
|
|
73
|
+
[name: string]: Output;
|
|
74
|
+
}>;
|
|
75
|
+
export declare function minify(src: string, opts?: JsMinifyOptions): Promise<Output>;
|
|
76
|
+
export declare function minifySync(src: string, opts?: JsMinifyOptions): Output;
|
|
77
|
+
/**
|
|
78
|
+
* Configure custom trace configuration runs for a process lifecycle.
|
|
79
|
+
* Currently only chromium's trace event format is supported.
|
|
80
|
+
* (https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview)
|
|
81
|
+
*
|
|
82
|
+
* This should be called before calling any binding interfaces exported in `@swc/core`, such as
|
|
83
|
+
* `transform*`, or `parse*` or anything. To avoid breaking changes, each binding fn internally
|
|
84
|
+
* sets default trace subscriber if not set.
|
|
85
|
+
*
|
|
86
|
+
* Unlike other configuration, this does not belong to individual api surface using swcrc
|
|
87
|
+
* or api's parameters (`transform(..., {trace})`). This is due to current tracing subscriber
|
|
88
|
+
* can be configured only once for the global scope. Calling `registerGlobalTraceConfig` multiple
|
|
89
|
+
* time won't cause error, subsequent calls will be ignored.
|
|
90
|
+
*
|
|
91
|
+
* As name implies currently this is experimental interface may change over time without semver
|
|
92
|
+
* major breaking changes. Please provide feedbacks,
|
|
93
|
+
* or bug report at https://github.com/swc-project/swc/discussions.
|
|
94
|
+
*/
|
|
95
|
+
export declare function __experimental_registerGlobalTraceConfig(traceConfig: {
|
|
96
|
+
type: "traceEvent";
|
|
97
|
+
fileName?: string;
|
|
98
|
+
}): void;
|
|
99
|
+
/**
|
|
100
|
+
* @ignore
|
|
101
|
+
*
|
|
102
|
+
* Returns current binary's metadata to determine which binary is actually loaded.
|
|
103
|
+
*
|
|
104
|
+
* This is undocumented interface, does not guarantee stability across `@swc/core`'s semver
|
|
105
|
+
* as internal representation may change anytime. Use it with caution.
|
|
106
|
+
*/
|
|
107
|
+
export declare function getBinaryMetadata(): {
|
|
108
|
+
target: string | undefined;
|
|
109
|
+
};
|
|
110
|
+
export declare const DEFAULT_EXTENSIONS: readonly string[];
|
package/index.js
ADDED
|
@@ -0,0 +1,436 @@
|
|
|
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
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
35
|
+
var t = {};
|
|
36
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
37
|
+
t[p] = s[p];
|
|
38
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
39
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
40
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
41
|
+
t[p[i]] = s[p[i]];
|
|
42
|
+
}
|
|
43
|
+
return t;
|
|
44
|
+
};
|
|
45
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
+
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;
|
|
47
|
+
const path_1 = require("path");
|
|
48
|
+
const spack_1 = require("./spack");
|
|
49
|
+
const assert = __importStar(require("assert"));
|
|
50
|
+
// Allow overrides to the location of the .node binding file
|
|
51
|
+
const bindingsOverride = process.env["SWC_BINARY_PATH"];
|
|
52
|
+
// `@swc/core` includes d.ts for the `@swc/wasm` to provide typed fallback bindings
|
|
53
|
+
// todo: fix package.json scripts
|
|
54
|
+
let fallbackBindings;
|
|
55
|
+
const bindings = (() => {
|
|
56
|
+
let binding;
|
|
57
|
+
try {
|
|
58
|
+
binding = !!bindingsOverride
|
|
59
|
+
? require((0, path_1.resolve)(bindingsOverride))
|
|
60
|
+
: require("./binding.js");
|
|
61
|
+
// If native binding loaded successfully, it should return proper target triple constant.
|
|
62
|
+
const triple = binding.getTargetTriple();
|
|
63
|
+
assert.ok(triple, "Failed to read target triple from native binary.");
|
|
64
|
+
return binding;
|
|
65
|
+
}
|
|
66
|
+
catch (_) {
|
|
67
|
+
// postinstall supposed to install `@swc/wasm` already
|
|
68
|
+
fallbackBindings = require("@swc/wasm");
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
return binding;
|
|
72
|
+
}
|
|
73
|
+
})();
|
|
74
|
+
/**
|
|
75
|
+
* Version of the swc binding.
|
|
76
|
+
*/
|
|
77
|
+
exports.version = require("./package.json").version;
|
|
78
|
+
/**
|
|
79
|
+
* @deprecated JavaScript API is deprecated. Please use Wasm plugin instead.
|
|
80
|
+
*/
|
|
81
|
+
function plugins(ps) {
|
|
82
|
+
return (mod) => {
|
|
83
|
+
let m = mod;
|
|
84
|
+
for (const p of ps) {
|
|
85
|
+
m = p(m);
|
|
86
|
+
}
|
|
87
|
+
return m;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
exports.plugins = plugins;
|
|
91
|
+
class Compiler {
|
|
92
|
+
constructor() {
|
|
93
|
+
this.fallbackBindingsPluginWarningDisplayed = false;
|
|
94
|
+
}
|
|
95
|
+
minify(src, opts) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
if (bindings) {
|
|
98
|
+
return bindings.minify(toBuffer(src), toBuffer(opts !== null && opts !== void 0 ? opts : {}));
|
|
99
|
+
}
|
|
100
|
+
else if (fallbackBindings) {
|
|
101
|
+
return fallbackBindings.minify(src, opts);
|
|
102
|
+
}
|
|
103
|
+
throw new Error("Bindings not found.");
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
minifySync(src, opts) {
|
|
107
|
+
if (bindings) {
|
|
108
|
+
return bindings.minifySync(toBuffer(src), toBuffer(opts !== null && opts !== void 0 ? opts : {}));
|
|
109
|
+
}
|
|
110
|
+
else if (fallbackBindings) {
|
|
111
|
+
return fallbackBindings.minifySync(src, opts);
|
|
112
|
+
}
|
|
113
|
+
throw new Error("Bindings not found.");
|
|
114
|
+
}
|
|
115
|
+
parse(src, options, filename) {
|
|
116
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
+
options = options || { syntax: "ecmascript" };
|
|
118
|
+
options.syntax = options.syntax || "ecmascript";
|
|
119
|
+
if (!bindings && !!fallbackBindings) {
|
|
120
|
+
throw new Error("Fallback bindings does not support this interface yet.");
|
|
121
|
+
}
|
|
122
|
+
else if (!bindings) {
|
|
123
|
+
throw new Error("Bindings not found.");
|
|
124
|
+
}
|
|
125
|
+
if (bindings) {
|
|
126
|
+
const res = yield bindings.parse(src, toBuffer(options), filename);
|
|
127
|
+
return JSON.parse(res);
|
|
128
|
+
}
|
|
129
|
+
else if (fallbackBindings) {
|
|
130
|
+
return fallbackBindings.parse(src, options);
|
|
131
|
+
}
|
|
132
|
+
throw new Error("Bindings not found.");
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
parseSync(src, options, filename) {
|
|
136
|
+
options = options || { syntax: "ecmascript" };
|
|
137
|
+
options.syntax = options.syntax || "ecmascript";
|
|
138
|
+
if (bindings) {
|
|
139
|
+
return JSON.parse(bindings.parseSync(src, toBuffer(options), filename));
|
|
140
|
+
}
|
|
141
|
+
else if (fallbackBindings) {
|
|
142
|
+
return fallbackBindings.parseSync(src, options);
|
|
143
|
+
}
|
|
144
|
+
throw new Error("Bindings not found.");
|
|
145
|
+
}
|
|
146
|
+
parseFile(path, options) {
|
|
147
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
+
options = options || { syntax: "ecmascript" };
|
|
149
|
+
options.syntax = options.syntax || "ecmascript";
|
|
150
|
+
if (!bindings && !!fallbackBindings) {
|
|
151
|
+
throw new Error("Fallback bindings does not support filesystem access.");
|
|
152
|
+
}
|
|
153
|
+
else if (!bindings) {
|
|
154
|
+
throw new Error("Bindings not found.");
|
|
155
|
+
}
|
|
156
|
+
const res = yield bindings.parseFile(path, toBuffer(options));
|
|
157
|
+
return JSON.parse(res);
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
parseFileSync(path, options) {
|
|
161
|
+
options = options || { syntax: "ecmascript" };
|
|
162
|
+
options.syntax = options.syntax || "ecmascript";
|
|
163
|
+
if (!bindings && !!fallbackBindings) {
|
|
164
|
+
throw new Error("Fallback bindings does not support filesystem access");
|
|
165
|
+
}
|
|
166
|
+
else if (!bindings) {
|
|
167
|
+
throw new Error("Bindings not found.");
|
|
168
|
+
}
|
|
169
|
+
return JSON.parse(bindings.parseFileSync(path, toBuffer(options)));
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Note: this method should be invoked on the compiler instance used
|
|
173
|
+
* for `parse()` / `parseSync()`.
|
|
174
|
+
*/
|
|
175
|
+
print(m, options) {
|
|
176
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
177
|
+
options = options || {};
|
|
178
|
+
if (bindings) {
|
|
179
|
+
return bindings.print(JSON.stringify(m), toBuffer(options));
|
|
180
|
+
}
|
|
181
|
+
else if (fallbackBindings) {
|
|
182
|
+
return fallbackBindings.print(m, options);
|
|
183
|
+
}
|
|
184
|
+
throw new Error("Bindings not found.");
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Note: this method should be invoked on the compiler instance used
|
|
189
|
+
* for `parse()` / `parseSync()`.
|
|
190
|
+
*/
|
|
191
|
+
printSync(m, options) {
|
|
192
|
+
options = options || {};
|
|
193
|
+
if (bindings) {
|
|
194
|
+
return bindings.printSync(JSON.stringify(m), toBuffer(options));
|
|
195
|
+
}
|
|
196
|
+
else if (fallbackBindings) {
|
|
197
|
+
return fallbackBindings.printSync(m, options);
|
|
198
|
+
}
|
|
199
|
+
throw new Error("Bindings not found.");
|
|
200
|
+
}
|
|
201
|
+
transform(src, options) {
|
|
202
|
+
var _a, _b, _c;
|
|
203
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
204
|
+
const isModule = typeof src !== "string";
|
|
205
|
+
options = options || {};
|
|
206
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
207
|
+
options.jsc.parser.syntax =
|
|
208
|
+
(_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : "ecmascript";
|
|
209
|
+
}
|
|
210
|
+
const { plugin } = options, newOptions = __rest(options, ["plugin"]);
|
|
211
|
+
if (bindings) {
|
|
212
|
+
if (plugin) {
|
|
213
|
+
const m = typeof src === "string"
|
|
214
|
+
? yield this.parse(src, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser, options.filename)
|
|
215
|
+
: src;
|
|
216
|
+
return this.transform(plugin(m), newOptions);
|
|
217
|
+
}
|
|
218
|
+
return bindings.transform(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
|
|
219
|
+
}
|
|
220
|
+
else if (fallbackBindings) {
|
|
221
|
+
if (plugin && !this.fallbackBindingsPluginWarningDisplayed) {
|
|
222
|
+
console.warn(`Fallback bindings does not support legacy plugins, it'll be ignored.`);
|
|
223
|
+
this.fallbackBindingsPluginWarningDisplayed = true;
|
|
224
|
+
}
|
|
225
|
+
return fallbackBindings.transform(src, options);
|
|
226
|
+
}
|
|
227
|
+
throw new Error("Bindings not found.");
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
transformSync(src, options) {
|
|
231
|
+
var _a, _b, _c;
|
|
232
|
+
const isModule = typeof src !== "string";
|
|
233
|
+
options = options || {};
|
|
234
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
235
|
+
options.jsc.parser.syntax =
|
|
236
|
+
(_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : "ecmascript";
|
|
237
|
+
}
|
|
238
|
+
const { plugin } = options, newOptions = __rest(options, ["plugin"]);
|
|
239
|
+
if (bindings) {
|
|
240
|
+
if (plugin) {
|
|
241
|
+
const m = typeof src === "string"
|
|
242
|
+
? this.parseSync(src, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser, options.filename)
|
|
243
|
+
: src;
|
|
244
|
+
return this.transformSync(plugin(m), newOptions);
|
|
245
|
+
}
|
|
246
|
+
return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
|
|
247
|
+
}
|
|
248
|
+
else if (fallbackBindings) {
|
|
249
|
+
if (plugin && !this.fallbackBindingsPluginWarningDisplayed) {
|
|
250
|
+
console.warn(`Fallback bindings does not support legacy plugins, it'll be ignored.`);
|
|
251
|
+
this.fallbackBindingsPluginWarningDisplayed = true;
|
|
252
|
+
}
|
|
253
|
+
return fallbackBindings.transformSync(isModule ? JSON.stringify(src) : src, options);
|
|
254
|
+
}
|
|
255
|
+
throw new Error("Bindings not found");
|
|
256
|
+
}
|
|
257
|
+
transformFile(path, options) {
|
|
258
|
+
var _a, _b, _c;
|
|
259
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
260
|
+
if (!bindings && !!fallbackBindings) {
|
|
261
|
+
throw new Error("Fallback bindings does not support filesystem access.");
|
|
262
|
+
}
|
|
263
|
+
else if (!bindings) {
|
|
264
|
+
throw new Error("Bindings not found.");
|
|
265
|
+
}
|
|
266
|
+
options = options || {};
|
|
267
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
268
|
+
options.jsc.parser.syntax =
|
|
269
|
+
(_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : "ecmascript";
|
|
270
|
+
}
|
|
271
|
+
const { plugin } = options, newOptions = __rest(options, ["plugin"]);
|
|
272
|
+
newOptions.filename = path;
|
|
273
|
+
if (plugin) {
|
|
274
|
+
const m = yield this.parseFile(path, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser);
|
|
275
|
+
return this.transform(plugin(m), newOptions);
|
|
276
|
+
}
|
|
277
|
+
return bindings.transformFile(path, false, toBuffer(newOptions));
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
transformFileSync(path, options) {
|
|
281
|
+
var _a, _b, _c;
|
|
282
|
+
if (!bindings && !!fallbackBindings) {
|
|
283
|
+
throw new Error("Fallback bindings does not support filesystem access.");
|
|
284
|
+
}
|
|
285
|
+
else if (!bindings) {
|
|
286
|
+
throw new Error("Bindings not found.");
|
|
287
|
+
}
|
|
288
|
+
options = options || {};
|
|
289
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
|
290
|
+
options.jsc.parser.syntax =
|
|
291
|
+
(_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : "ecmascript";
|
|
292
|
+
}
|
|
293
|
+
const { plugin } = options, newOptions = __rest(options, ["plugin"]);
|
|
294
|
+
newOptions.filename = path;
|
|
295
|
+
if (plugin) {
|
|
296
|
+
const m = this.parseFileSync(path, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser);
|
|
297
|
+
return this.transformSync(plugin(m), newOptions);
|
|
298
|
+
}
|
|
299
|
+
return bindings.transformFileSync(path,
|
|
300
|
+
/* isModule */ false, toBuffer(newOptions));
|
|
301
|
+
}
|
|
302
|
+
bundle(options) {
|
|
303
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
304
|
+
if (!bindings && !!fallbackBindings) {
|
|
305
|
+
throw new Error("Fallback bindings does not support this interface yet.");
|
|
306
|
+
}
|
|
307
|
+
else if (!bindings) {
|
|
308
|
+
throw new Error("Bindings not found.");
|
|
309
|
+
}
|
|
310
|
+
const opts = yield (0, spack_1.compileBundleOptions)(options);
|
|
311
|
+
if (Array.isArray(opts)) {
|
|
312
|
+
const all = yield Promise.all(opts.map((opt) => __awaiter(this, void 0, void 0, function* () {
|
|
313
|
+
return this.bundle(opt);
|
|
314
|
+
})));
|
|
315
|
+
let obj = {};
|
|
316
|
+
for (const o of all) {
|
|
317
|
+
obj = Object.assign(Object.assign({}, obj), o);
|
|
318
|
+
}
|
|
319
|
+
return obj;
|
|
320
|
+
}
|
|
321
|
+
return bindings.bundle(toBuffer(Object.assign({}, opts)));
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
exports.Compiler = Compiler;
|
|
326
|
+
const compiler = new Compiler();
|
|
327
|
+
function parse(src, options) {
|
|
328
|
+
return compiler.parse(src, options);
|
|
329
|
+
}
|
|
330
|
+
exports.parse = parse;
|
|
331
|
+
function parseSync(src, options) {
|
|
332
|
+
return compiler.parseSync(src, options);
|
|
333
|
+
}
|
|
334
|
+
exports.parseSync = parseSync;
|
|
335
|
+
function parseFile(path, options) {
|
|
336
|
+
return compiler.parseFile(path, options);
|
|
337
|
+
}
|
|
338
|
+
exports.parseFile = parseFile;
|
|
339
|
+
function parseFileSync(path, options) {
|
|
340
|
+
return compiler.parseFileSync(path, options);
|
|
341
|
+
}
|
|
342
|
+
exports.parseFileSync = parseFileSync;
|
|
343
|
+
function print(m, options) {
|
|
344
|
+
return compiler.print(m, options);
|
|
345
|
+
}
|
|
346
|
+
exports.print = print;
|
|
347
|
+
function printSync(m, options) {
|
|
348
|
+
return compiler.printSync(m, options);
|
|
349
|
+
}
|
|
350
|
+
exports.printSync = printSync;
|
|
351
|
+
function transform(src, options) {
|
|
352
|
+
return compiler.transform(src, options);
|
|
353
|
+
}
|
|
354
|
+
exports.transform = transform;
|
|
355
|
+
function transformSync(src, options) {
|
|
356
|
+
return compiler.transformSync(src, options);
|
|
357
|
+
}
|
|
358
|
+
exports.transformSync = transformSync;
|
|
359
|
+
function transformFile(path, options) {
|
|
360
|
+
return compiler.transformFile(path, options);
|
|
361
|
+
}
|
|
362
|
+
exports.transformFile = transformFile;
|
|
363
|
+
function transformFileSync(path, options) {
|
|
364
|
+
return compiler.transformFileSync(path, options);
|
|
365
|
+
}
|
|
366
|
+
exports.transformFileSync = transformFileSync;
|
|
367
|
+
function bundle(options) {
|
|
368
|
+
return compiler.bundle(options);
|
|
369
|
+
}
|
|
370
|
+
exports.bundle = bundle;
|
|
371
|
+
function minify(src, opts) {
|
|
372
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
373
|
+
return compiler.minify(src, opts);
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
exports.minify = minify;
|
|
377
|
+
function minifySync(src, opts) {
|
|
378
|
+
return compiler.minifySync(src, opts);
|
|
379
|
+
}
|
|
380
|
+
exports.minifySync = minifySync;
|
|
381
|
+
/**
|
|
382
|
+
* Configure custom trace configuration runs for a process lifecycle.
|
|
383
|
+
* Currently only chromium's trace event format is supported.
|
|
384
|
+
* (https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview)
|
|
385
|
+
*
|
|
386
|
+
* This should be called before calling any binding interfaces exported in `@swc/core`, such as
|
|
387
|
+
* `transform*`, or `parse*` or anything. To avoid breaking changes, each binding fn internally
|
|
388
|
+
* sets default trace subscriber if not set.
|
|
389
|
+
*
|
|
390
|
+
* Unlike other configuration, this does not belong to individual api surface using swcrc
|
|
391
|
+
* or api's parameters (`transform(..., {trace})`). This is due to current tracing subscriber
|
|
392
|
+
* can be configured only once for the global scope. Calling `registerGlobalTraceConfig` multiple
|
|
393
|
+
* time won't cause error, subsequent calls will be ignored.
|
|
394
|
+
*
|
|
395
|
+
* As name implies currently this is experimental interface may change over time without semver
|
|
396
|
+
* major breaking changes. Please provide feedbacks,
|
|
397
|
+
* or bug report at https://github.com/swc-project/swc/discussions.
|
|
398
|
+
*/
|
|
399
|
+
function __experimental_registerGlobalTraceConfig(traceConfig) {
|
|
400
|
+
// Do not raise error if binding doesn't exists - fallback binding will not support
|
|
401
|
+
// this ever.
|
|
402
|
+
if (bindings) {
|
|
403
|
+
if (traceConfig.type === "traceEvent") {
|
|
404
|
+
bindings.initCustomTraceSubscriber(traceConfig.fileName);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
exports.__experimental_registerGlobalTraceConfig = __experimental_registerGlobalTraceConfig;
|
|
409
|
+
/**
|
|
410
|
+
* @ignore
|
|
411
|
+
*
|
|
412
|
+
* Returns current binary's metadata to determine which binary is actually loaded.
|
|
413
|
+
*
|
|
414
|
+
* This is undocumented interface, does not guarantee stability across `@swc/core`'s semver
|
|
415
|
+
* as internal representation may change anytime. Use it with caution.
|
|
416
|
+
*/
|
|
417
|
+
function getBinaryMetadata() {
|
|
418
|
+
return {
|
|
419
|
+
target: bindings ? bindings === null || bindings === void 0 ? void 0 : bindings.getTargetTriple() : undefined,
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
exports.getBinaryMetadata = getBinaryMetadata;
|
|
423
|
+
exports.DEFAULT_EXTENSIONS = Object.freeze([
|
|
424
|
+
".js",
|
|
425
|
+
".jsx",
|
|
426
|
+
".es6",
|
|
427
|
+
".es",
|
|
428
|
+
".mjs",
|
|
429
|
+
".ts",
|
|
430
|
+
".tsx",
|
|
431
|
+
".cts",
|
|
432
|
+
".mts",
|
|
433
|
+
]);
|
|
434
|
+
function toBuffer(t) {
|
|
435
|
+
return Buffer.from(JSON.stringify(t));
|
|
436
|
+
}
|