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 __exportStar = (this && this.__exportStar) || function(m, exports) {
|
10 | for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
|
11 | };
|
12 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
13 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
14 | return new (P || (P = Promise))(function (resolve, reject) {
|
15 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
16 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
17 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
18 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
19 | });
|
20 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | exports.DEFAULT_EXTENSIONS = 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;
|
23 | __exportStar(require("./types"), exports);
|
24 | const spack_1 = require("./spack");
|
25 | const helper_1 = require("@node-rs/helper");
|
26 | const bindings = helper_1.loadBinding(__dirname, "swc", "@swc/core");
|
27 |
|
28 |
|
29 |
|
30 | exports.version = require("./package.json").version;
|
31 | function plugins(ps) {
|
32 | return mod => {
|
33 | let m = mod;
|
34 | for (const p of ps) {
|
35 | m = p(m);
|
36 | }
|
37 | return m;
|
38 | };
|
39 | }
|
40 | exports.plugins = plugins;
|
41 | class Compiler {
|
42 | parse(src, options) {
|
43 | return __awaiter(this, void 0, void 0, function* () {
|
44 | options = options || { syntax: "ecmascript" };
|
45 | options.syntax = options.syntax || "ecmascript";
|
46 | const res = yield bindings.parse(src, toBuffer(options));
|
47 | return JSON.parse(res);
|
48 | });
|
49 | }
|
50 | parseSync(src, options) {
|
51 | options = options || { syntax: "ecmascript" };
|
52 | options.syntax = options.syntax || "ecmascript";
|
53 | return JSON.parse(bindings.parseSync(src, toBuffer(options)));
|
54 | }
|
55 | parseFile(path, options) {
|
56 | options = options || { syntax: "ecmascript" };
|
57 | options.syntax = options.syntax || "ecmascript";
|
58 | const res = bindings.parseFile(path, toBuffer(options));
|
59 | return JSON.parse(res);
|
60 | }
|
61 | parseFileSync(path, options) {
|
62 | options = options || { syntax: "ecmascript" };
|
63 | options.syntax = options.syntax || "ecmascript";
|
64 | return JSON.parse(bindings.parseFileSync(path, toBuffer(options)));
|
65 | }
|
66 | |
67 |
|
68 |
|
69 |
|
70 | print(m, options) {
|
71 | return __awaiter(this, void 0, void 0, function* () {
|
72 | options = options || {};
|
73 | return bindings.print(JSON.stringify(m), toBuffer(options));
|
74 | });
|
75 | }
|
76 | |
77 |
|
78 |
|
79 |
|
80 | printSync(m, options) {
|
81 | options = options || {};
|
82 | return bindings.printSync(JSON.stringify(m), toBuffer(options));
|
83 | }
|
84 | transform(src, options) {
|
85 | var _a, _b, _c;
|
86 | return __awaiter(this, void 0, void 0, function* () {
|
87 | const isModule = typeof src !== "string";
|
88 | options = options || {};
|
89 | if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
90 | options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
91 | }
|
92 | const plugin = options.plugin;
|
93 | delete options.plugin;
|
94 | if (plugin) {
|
95 | const m = typeof src === "string"
|
96 | ? yield this.parse(src, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser)
|
97 | : src;
|
98 | return this.transform(plugin(m), options);
|
99 | }
|
100 | return bindings.transform(isModule ? JSON.stringify(src) : src, isModule, toBuffer(options));
|
101 | });
|
102 | }
|
103 | transformSync(src, options) {
|
104 | var _a, _b, _c;
|
105 | const isModule = typeof src !== "string";
|
106 | options = options || {};
|
107 | if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
108 | options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
109 | }
|
110 | const plugin = options.plugin;
|
111 | delete options.plugin;
|
112 | if (plugin) {
|
113 | 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) : src;
|
114 | return this.transformSync(plugin(m), options);
|
115 | }
|
116 | return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(options));
|
117 | }
|
118 | transformFile(path, options) {
|
119 | var _a, _b, _c;
|
120 | return __awaiter(this, void 0, void 0, function* () {
|
121 | options = options || {};
|
122 | if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
123 | options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
124 | }
|
125 | const plugin = options.plugin;
|
126 | delete options.plugin;
|
127 | if (plugin) {
|
128 | const m = yield this.parseFile(path, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser);
|
129 | return this.transform(plugin(m), options);
|
130 | }
|
131 | return bindings.transformFile(path, false, toBuffer(options));
|
132 | });
|
133 | }
|
134 | transformFileSync(path, options) {
|
135 | var _a, _b, _c;
|
136 | options = options || {};
|
137 | if ((_a = options === null || options === void 0 ? void 0 : options.jsc) === null || _a === void 0 ? void 0 : _a.parser) {
|
138 | options.jsc.parser.syntax = (_b = options.jsc.parser.syntax) !== null && _b !== void 0 ? _b : 'ecmascript';
|
139 | }
|
140 | const plugin = options === null || options === void 0 ? void 0 : options.plugin;
|
141 | options === null || options === void 0 ? true : delete options.plugin;
|
142 | if (plugin) {
|
143 | const m = this.parseFileSync(path, (_c = options === null || options === void 0 ? void 0 : options.jsc) === null || _c === void 0 ? void 0 : _c.parser);
|
144 | return this.transformSync(plugin(m), options);
|
145 | }
|
146 | return bindings.transformFileSync(path, false, toBuffer(options));
|
147 | }
|
148 | bundle(options) {
|
149 | return __awaiter(this, void 0, void 0, function* () {
|
150 | const opts = yield spack_1.compileBundleOptions(options);
|
151 | if (Array.isArray(opts)) {
|
152 | const all = yield Promise.all(opts.map((opt) => __awaiter(this, void 0, void 0, function* () {
|
153 | return this.bundle(opt);
|
154 | })));
|
155 | let obj = {};
|
156 | for (const o of all) {
|
157 | obj = Object.assign(Object.assign({}, obj), o);
|
158 | }
|
159 | return obj;
|
160 | }
|
161 | return bindings.bundle(toBuffer(Object.assign({}, opts)));
|
162 | });
|
163 | }
|
164 | }
|
165 | exports.Compiler = Compiler;
|
166 | const compiler = new Compiler();
|
167 | function parse(src, options) {
|
168 | return compiler.parse(src, options);
|
169 | }
|
170 | exports.parse = parse;
|
171 | function parseSync(src, options) {
|
172 | return compiler.parseSync(src, options);
|
173 | }
|
174 | exports.parseSync = parseSync;
|
175 | function parseFile(path, options) {
|
176 | return compiler.parseFile(path, options);
|
177 | }
|
178 | exports.parseFile = parseFile;
|
179 | function parseFileSync(path, options) {
|
180 | return compiler.parseFileSync(path, options);
|
181 | }
|
182 | exports.parseFileSync = parseFileSync;
|
183 | function print(m, options) {
|
184 | return compiler.print(m, options);
|
185 | }
|
186 | exports.print = print;
|
187 | function printSync(m, options) {
|
188 | return compiler.printSync(m, options);
|
189 | }
|
190 | exports.printSync = printSync;
|
191 | function transform(src, options) {
|
192 | return compiler.transform(src, options);
|
193 | }
|
194 | exports.transform = transform;
|
195 | function transformSync(src, options) {
|
196 | return compiler.transformSync(src, options);
|
197 | }
|
198 | exports.transformSync = transformSync;
|
199 | function transformFile(path, options) {
|
200 | return compiler.transformFile(path, options);
|
201 | }
|
202 | exports.transformFile = transformFile;
|
203 | function transformFileSync(path, options) {
|
204 | return compiler.transformFileSync(path, options);
|
205 | }
|
206 | exports.transformFileSync = transformFileSync;
|
207 | function bundle(options) {
|
208 | return compiler.bundle(options);
|
209 | }
|
210 | exports.bundle = bundle;
|
211 | exports.DEFAULT_EXTENSIONS = Object.freeze([
|
212 | ".js",
|
213 | ".jsx",
|
214 | ".es6",
|
215 | ".es",
|
216 | ".mjs",
|
217 | ".ts",
|
218 | ".tsx"
|
219 | ]);
|
220 | function toBuffer(t) {
|
221 | return Buffer.from(JSON.stringify(t));
|
222 | }
|