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