@o.z/vite-plugin-swc 0.6.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/LICENSE ADDED
@@ -0,0 +1,45 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Zero Red<github.com/zero-red-dev>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
23
+ ---------------------------------------
24
+
25
+ MIT License
26
+
27
+ Copyright (c) 2023 Timothée “Tim” Pillard @ziir
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining a copy
30
+ of this software and associated documentation files (the "Software"), to deal
31
+ in the Software without restriction, including without limitation the rights
32
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
33
+ copies of the Software, and to permit persons to whom the Software is
34
+ furnished to do so, subject to the following conditions:
35
+
36
+ The above copyright notice and this permission notice shall be included in all
37
+ copies or substantial portions of the Software.
38
+
39
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
43
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
44
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,209 @@
1
+ # @o.z/vite-plugin-swc
2
+ [![npm](https://img.shields.io/npm/v/@o.z/vite-plugin-swc)](https://www.npmjs.com/package/@o.z/vite-plugin-swc) [![license](https://img.shields.io/npm/l/@o.z/vite-plugin-swc)](https://www.npmjs.com/package/@o.z/vite-plugin-swc)
3
+
4
+ A [Vite](https://vitejs.dev/) plugin that transforms TypeScript and JavaScript source files using [SWC](https://swc.rs) during the build process. SWC is a super-fast Rust-based compiler that significantly accelerates your build times compared to traditional Babel or tsc compilation.
5
+
6
+
7
+ ## ✨ Features
8
+ - ⚡ Blazing Fast: Leverages SWC's Rust-based compilation for faster builds
9
+
10
+ - 🔧 Flexible Configuration: Supports both inline options and .swcrc configuration files
11
+
12
+ - 🎯 TypeScript First: Excellent TypeScript support with decorators and metadata
13
+
14
+ - 🔌 Seamless Integration: Works out of the box with Vite's build pipeline
15
+
16
+ - 🛠 Modern JavaScript: Supports latest ECMAScript features including top-level await
17
+
18
+ ## 📦 Installation
19
+
20
+ yarn
21
+ ```bash
22
+ yarn add @o.z/vite-plugin-swc --dev
23
+ ```
24
+
25
+ pnpm
26
+ ```bash
27
+ pnpm add @o.z/vite-plugin-swc --save-dev
28
+ ```
29
+
30
+ npm
31
+ ```bash
32
+ npm install @o.z/vite-plugin-swc --save-dev
33
+ ```
34
+
35
+
36
+ ## 🚀 Basic Usage
37
+ Add the plugin to your Vite configuration:
38
+
39
+ ```ts
40
+ // vite.config.ts
41
+ import { defineConfig } from "vite";
42
+ import swc from "@o.z/vite-plugin-swc";
43
+
44
+ export default defineConfig({
45
+ plugins: [swc()],
46
+ });
47
+ ```
48
+
49
+
50
+ ## ⚙️ Configuration
51
+ ### Default Configuration
52
+ When no options are provided, the plugin uses the following defaults:
53
+
54
+ ```ts
55
+ {
56
+ include: /\.ts?$/,
57
+ exclude: "node_modules",
58
+ swcrc: false,
59
+ configFile: false,
60
+ minify: true,
61
+ jsc: {
62
+ parser: {
63
+ syntax: "typescript",
64
+ decorators: true,
65
+ },
66
+ transform: {
67
+ decoratorMetadata: true,
68
+ decoratorVersion: "2022-03",
69
+ },
70
+ },
71
+ }
72
+ ```
73
+
74
+ This configuration provides:
75
+
76
+ - TypeScript support with modern decorators (Stage 3)
77
+
78
+ - Decorator metadata transformation
79
+
80
+ - Minification enabled by default
81
+
82
+ - Top-level await support
83
+
84
+
85
+ ### Custom Configuration
86
+ You can override any of the default options:
87
+ ```ts
88
+ // vite.config.ts
89
+ import { defineConfig } from "vite";
90
+ import swc from "@o.z/vite-plugin-swc";
91
+
92
+ export default defineConfig({
93
+ plugins: [
94
+ swc({
95
+ include: /\.(ts|tsx|js|jsx)?$/,
96
+ exclude: /node_modules/,
97
+ minify: process.env.NODE_ENV === "production",
98
+ jsc: {
99
+ parser: {
100
+ syntax: "typescript",
101
+ tsx: true,
102
+ decorators: true,
103
+ },
104
+ transform: {
105
+ react: {
106
+ runtime: "automatic",
107
+ },
108
+ decoratorMetadata: true,
109
+ decoratorVersion: "2022-03",
110
+ },
111
+ },
112
+ }),
113
+ ],
114
+ });
115
+ ```
116
+
117
+ ### Using .swcrc Configuration File
118
+ If you prefer to use a configuration file, set swcrc and configFile to true:
119
+
120
+ ```ts
121
+ // vite.config.ts
122
+ import { defineConfig } from "vite";
123
+ import swc from "@o.z/vite-plugin-swc";
124
+
125
+ export default defineConfig({
126
+ plugins: [
127
+ swc({
128
+ include: /\.ts?$/,
129
+ swcrc: true,
130
+ configFile: true,
131
+ }),
132
+ ],
133
+ });
134
+ ```
135
+
136
+ Then create a .swcrc file in your project root:
137
+
138
+ ```json
139
+ {
140
+ "$schema": "https://json.schemastore.org/swcrc",
141
+ "exclude": "node_modules",
142
+ "jsc": {
143
+ "parser": {
144
+ "syntax": "typescript",
145
+ "decorators": true,
146
+ "dynamicImport": true
147
+ },
148
+ "transform": {
149
+ "decoratorMetadata": true,
150
+ "decoratorVersion": "2022-03"
151
+ },
152
+ "target": "es2022",
153
+ "loose": false,
154
+ "externalHelpers": false
155
+ },
156
+ "minify": true
157
+ }
158
+ ```
159
+
160
+ ## 🎯 Use Cases
161
+ ### 1. Faster TypeScript Builds
162
+ Replace tsc with SWC for significantly faster TypeScript compilation during production builds.
163
+
164
+ ### 2. Modern Decorator Support
165
+ Use the latest decorator syntax (Stage 3) with metadata reflection.
166
+
167
+ ### 3. Library Development
168
+ Build libraries with optimized output and modern JavaScript features.
169
+
170
+ ### 4. Large Projects
171
+ Speed up build times in large codebases where traditional TypeScript compilation becomes a bottleneck.
172
+
173
+ ## 🔄 Migration from vite-plugin-swc-transform
174
+ This plugin is a fork of [vite-plugin-swc-transform](https://github.com/ziir/vite-plugin-swc-transform). The migration is straightforward:
175
+
176
+ 1. Install the new package:
177
+ ```bash
178
+ npm uninstall vite-plugin-swc-transform
179
+
180
+ npm install @o.z/vite-plugin-swc --save-dev
181
+ ```
182
+
183
+ 2. Update your Vite config import:
184
+ ```diff
185
+ - import swc from "vite-plugin-swc-transform"
186
+ + import swc from "@o.z/vite-plugin-swc"
187
+ ```
188
+
189
+ 3. Enjoy improved performance and additional features!
190
+
191
+ ## 🤝 Acknowledgements
192
+ This project is a fork of [vite-plugin-swc-transform](https://github.com/ziir/vite-plugin-swc-transform). Special thanks to [Timothée “Tim” Pillard](https://github.com/ziir) for the original implementation and great work.
193
+
194
+ ## 📚 Additional Resources
195
+ [SWC Documentation](https://swc.rs/docs)
196
+
197
+ [Vite Plugin Development Guide](https://vitejs.dev/guide/api-plugin.html)
198
+
199
+ [TypeScript Decorators Proposal](https://github.com/tc39/proposal-decorators)
200
+
201
+ [SWC Configuration Schema](https://swc.rs/docs/configuration/swcrc)
202
+
203
+ ## 📚 Dev API Reference
204
+ [Dev API Docs](docs/api/README.md)
205
+
206
+ ## 🐛 Issues and Contributions
207
+ Found a bug or have a feature request? Please open an issue on [GitHub](https://github.com/z-npm/vite-plugin-swc/issues).
208
+
209
+ Contributions are welcome! Please feel free to submit a Pull Request.
package/dist/index.cjs ADDED
@@ -0,0 +1,293 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
4
+
5
+ const pluginutils = require('@rollup/pluginutils');
6
+ const core = require('@swc/core');
7
+
8
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
9
+ try {
10
+ var info = gen[key](arg);
11
+ var value = info.value;
12
+ } catch (error) {
13
+ reject(error);
14
+ return;
15
+ }
16
+ if (info.done) {
17
+ resolve(value);
18
+ } else {
19
+ Promise.resolve(value).then(_next, _throw);
20
+ }
21
+ }
22
+ function _async_to_generator(fn) {
23
+ return function() {
24
+ var self = this, args = arguments;
25
+ return new Promise(function(resolve, reject) {
26
+ var gen = fn.apply(self, args);
27
+ function _next(value) {
28
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
29
+ }
30
+ function _throw(err) {
31
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
32
+ }
33
+ _next(undefined);
34
+ });
35
+ };
36
+ }
37
+ function _define_property(obj, key, value) {
38
+ if (key in obj) {
39
+ Object.defineProperty(obj, key, {
40
+ value: value,
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true
44
+ });
45
+ } else {
46
+ obj[key] = value;
47
+ }
48
+ return obj;
49
+ }
50
+ function _object_spread(target) {
51
+ for(var i = 1; i < arguments.length; i++){
52
+ var source = arguments[i] != null ? arguments[i] : {};
53
+ var ownKeys = Object.keys(source);
54
+ if (typeof Object.getOwnPropertySymbols === "function") {
55
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
56
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
57
+ }));
58
+ }
59
+ ownKeys.forEach(function(key) {
60
+ _define_property(target, key, source[key]);
61
+ });
62
+ }
63
+ return target;
64
+ }
65
+ function _object_without_properties(source, excluded) {
66
+ if (source == null) return {};
67
+ var target = {}, sourceKeys, key, i;
68
+ if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
69
+ sourceKeys = Reflect.ownKeys(source);
70
+ for(i = 0; i < sourceKeys.length; i++){
71
+ key = sourceKeys[i];
72
+ if (excluded.indexOf(key) >= 0) continue;
73
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
74
+ target[key] = source[key];
75
+ }
76
+ return target;
77
+ }
78
+ target = _object_without_properties_loose(source, excluded);
79
+ if (Object.getOwnPropertySymbols) {
80
+ sourceKeys = Object.getOwnPropertySymbols(source);
81
+ for(i = 0; i < sourceKeys.length; i++){
82
+ key = sourceKeys[i];
83
+ if (excluded.indexOf(key) >= 0) continue;
84
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
85
+ target[key] = source[key];
86
+ }
87
+ }
88
+ return target;
89
+ }
90
+ function _object_without_properties_loose(source, excluded) {
91
+ if (source == null) return {};
92
+ var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
93
+ for(i = 0; i < sourceKeys.length; i++){
94
+ key = sourceKeys[i];
95
+ if (excluded.indexOf(key) >= 0) continue;
96
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
97
+ target[key] = source[key];
98
+ }
99
+ return target;
100
+ }
101
+ function _ts_generator(thisArg, body) {
102
+ var f, y, t, _ = {
103
+ label: 0,
104
+ sent: function() {
105
+ if (t[0] & 1) throw t[1];
106
+ return t[1];
107
+ },
108
+ trys: [],
109
+ ops: []
110
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
111
+ return d(g, "next", {
112
+ value: verb(0)
113
+ }), d(g, "throw", {
114
+ value: verb(1)
115
+ }), d(g, "return", {
116
+ value: verb(2)
117
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
118
+ value: function() {
119
+ return this;
120
+ }
121
+ }), g;
122
+ function verb(n) {
123
+ return function(v) {
124
+ return step([
125
+ n,
126
+ v
127
+ ]);
128
+ };
129
+ }
130
+ function step(op) {
131
+ if (f) throw new TypeError("Generator is already executing.");
132
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
133
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
134
+ if (y = 0, t) op = [
135
+ op[0] & 2,
136
+ t.value
137
+ ];
138
+ switch(op[0]){
139
+ case 0:
140
+ case 1:
141
+ t = op;
142
+ break;
143
+ case 4:
144
+ _.label++;
145
+ return {
146
+ value: op[1],
147
+ done: false
148
+ };
149
+ case 5:
150
+ _.label++;
151
+ y = op[1];
152
+ op = [
153
+ 0
154
+ ];
155
+ continue;
156
+ case 7:
157
+ op = _.ops.pop();
158
+ _.trys.pop();
159
+ continue;
160
+ default:
161
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
162
+ _ = 0;
163
+ continue;
164
+ }
165
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
166
+ _.label = op[1];
167
+ break;
168
+ }
169
+ if (op[0] === 6 && _.label < t[1]) {
170
+ _.label = t[1];
171
+ t = op;
172
+ break;
173
+ }
174
+ if (t && _.label < t[2]) {
175
+ _.label = t[2];
176
+ _.ops.push(op);
177
+ break;
178
+ }
179
+ if (t[2]) _.ops.pop();
180
+ _.trys.pop();
181
+ continue;
182
+ }
183
+ op = body.call(thisArg, _);
184
+ } catch (e) {
185
+ op = [
186
+ 6,
187
+ e
188
+ ];
189
+ y = 0;
190
+ } finally{
191
+ f = t = 0;
192
+ }
193
+ if (op[0] & 5) throw op[1];
194
+ return {
195
+ value: op[0] ? op[1] : void 0,
196
+ done: true
197
+ };
198
+ }
199
+ }
200
+ /**
201
+ * Vite plugin that transforms TypeScript and JavaScript files using SWC.
202
+ * It disables Vite's default esbuild transform to let SWC handle the compilation,
203
+ * resulting in significantly faster builds, especially for large codebases.
204
+ *
205
+ * @example
206
+ * ```ts
207
+ * // vite.config.ts
208
+ * import { defineConfig } from 'vite'
209
+ * import swc from '@o.z/vite-plugin-swc'
210
+ *
211
+ * export default defineConfig({
212
+ * plugins: [swc()]
213
+ * })
214
+ * ```
215
+ *
216
+ * @param options - Configuration options for the plugin and SWC.
217
+ * @returns A Vite plugin instance.
218
+ */ var swc = function swc() {
219
+ var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
220
+ // Default include/exclude patterns – now covering .ts, .tsx, .js, .jsx
221
+ var _options_include = options.include, include = _options_include === void 0 ? /\.(ts|tsx|js|jsx)$/ : _options_include, _options_exclude = options.exclude, exclude = _options_exclude === void 0 ? "node_modules" : _options_exclude, swcOptions = _object_without_properties(options, [
222
+ "include",
223
+ "exclude"
224
+ ]);
225
+ var filter = pluginutils.createFilter(include, exclude);
226
+ return {
227
+ name: "vite-plugin-swc",
228
+ enforce: "pre",
229
+ config: function config() {
230
+ return {
231
+ esbuild: false
232
+ };
233
+ },
234
+ transform: function transform(code, id) {
235
+ return _async_to_generator(function() {
236
+ var _this_environment_config_css, sourceMaps, result, error;
237
+ return _ts_generator(this, function(_state) {
238
+ switch(_state.label){
239
+ case 0:
240
+ if (!filter(id)) return [
241
+ 2,
242
+ null
243
+ ];
244
+ _state.label = 1;
245
+ case 1:
246
+ _state.trys.push([
247
+ 1,
248
+ 3,
249
+ ,
250
+ 4
251
+ ]);
252
+ // Determine if source maps should be generated based on Vite's config
253
+ // `this.environment.config` is available inside the transform hook
254
+ sourceMaps = this.environment.config.command === "build" ? !!this.environment.config.build.sourcemap : !!((_this_environment_config_css = this.environment.config.css) === null || _this_environment_config_css === void 0 ? void 0 : _this_environment_config_css.devSourcemap); // For dev, you might want to align with css sourcemaps or a dedicated flag
255
+ return [
256
+ 4,
257
+ core.transform(code, _object_spread({
258
+ filename: id,
259
+ sourceFileName: id.split("?", 1)[0],
260
+ sourceMaps: sourceMaps
261
+ }, swcOptions))
262
+ ];
263
+ case 2:
264
+ result = _state.sent();
265
+ // SWC returns { code, map } when sourceMaps are enabled, otherwise just { code }
266
+ return [
267
+ 2,
268
+ {
269
+ code: result.code,
270
+ map: result.map
271
+ }
272
+ ];
273
+ case 3:
274
+ error = _state.sent();
275
+ // Enhance error message with file information and re-throw as a plugin error
276
+ this.error("SWC transform failed in ".concat(id, ": ").concat((error === null || error === void 0 ? void 0 : error.message) || error));
277
+ return [
278
+ 3,
279
+ 4
280
+ ];
281
+ case 4:
282
+ return [
283
+ 2
284
+ ];
285
+ }
286
+ });
287
+ }).call(this);
288
+ }
289
+ };
290
+ };
291
+
292
+ exports.default = swc;
293
+ exports.swc = swc;
@@ -0,0 +1,40 @@
1
+ import { Plugin } from 'vite';
2
+ import { FilterPattern } from '@rollup/pluginutils';
3
+ import { Options as SWCOption } from '@swc/core';
4
+ /**
5
+ * Options for the Vite SWC plugin.
6
+ * Extends SWC's transformation options, omitting `filename`, `sourceFileName` and `exclude`
7
+ */
8
+ export interface Options extends Omit<SWCOption, "filename" | "sourceFileName" | "exclude"> {
9
+ /**
10
+ * A picomatch pattern, or array of patterns, which specifies the files to include.
11
+ * @default /\.(ts|tsx|js|jsx)$/
12
+ */
13
+ include?: FilterPattern;
14
+ /**
15
+ * A picomatch pattern, or array of patterns, which specifies the files to exclude.
16
+ * @default "node_modules"
17
+ */
18
+ exclude?: FilterPattern;
19
+ }
20
+ /**
21
+ * Vite plugin that transforms TypeScript and JavaScript files using SWC.
22
+ * It disables Vite's default esbuild transform to let SWC handle the compilation,
23
+ * resulting in significantly faster builds, especially for large codebases.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * // vite.config.ts
28
+ * import { defineConfig } from 'vite'
29
+ * import swc from '@o.z/vite-plugin-swc'
30
+ *
31
+ * export default defineConfig({
32
+ * plugins: [swc()]
33
+ * })
34
+ * ```
35
+ *
36
+ * @param options - Configuration options for the plugin and SWC.
37
+ * @returns A Vite plugin instance.
38
+ */
39
+ export declare const swc: (options?: Options) => Plugin;
40
+ export default swc;
package/dist/index.js ADDED
@@ -0,0 +1,288 @@
1
+ import { createFilter } from '@rollup/pluginutils';
2
+ import { transform } from '@swc/core';
3
+
4
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
5
+ try {
6
+ var info = gen[key](arg);
7
+ var value = info.value;
8
+ } catch (error) {
9
+ reject(error);
10
+ return;
11
+ }
12
+ if (info.done) {
13
+ resolve(value);
14
+ } else {
15
+ Promise.resolve(value).then(_next, _throw);
16
+ }
17
+ }
18
+ function _async_to_generator(fn) {
19
+ return function() {
20
+ var self = this, args = arguments;
21
+ return new Promise(function(resolve, reject) {
22
+ var gen = fn.apply(self, args);
23
+ function _next(value) {
24
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
25
+ }
26
+ function _throw(err) {
27
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
28
+ }
29
+ _next(undefined);
30
+ });
31
+ };
32
+ }
33
+ function _define_property(obj, key, value) {
34
+ if (key in obj) {
35
+ Object.defineProperty(obj, key, {
36
+ value: value,
37
+ enumerable: true,
38
+ configurable: true,
39
+ writable: true
40
+ });
41
+ } else {
42
+ obj[key] = value;
43
+ }
44
+ return obj;
45
+ }
46
+ function _object_spread(target) {
47
+ for(var i = 1; i < arguments.length; i++){
48
+ var source = arguments[i] != null ? arguments[i] : {};
49
+ var ownKeys = Object.keys(source);
50
+ if (typeof Object.getOwnPropertySymbols === "function") {
51
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
52
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
53
+ }));
54
+ }
55
+ ownKeys.forEach(function(key) {
56
+ _define_property(target, key, source[key]);
57
+ });
58
+ }
59
+ return target;
60
+ }
61
+ function _object_without_properties(source, excluded) {
62
+ if (source == null) return {};
63
+ var target = {}, sourceKeys, key, i;
64
+ if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
65
+ sourceKeys = Reflect.ownKeys(source);
66
+ for(i = 0; i < sourceKeys.length; i++){
67
+ key = sourceKeys[i];
68
+ if (excluded.indexOf(key) >= 0) continue;
69
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
70
+ target[key] = source[key];
71
+ }
72
+ return target;
73
+ }
74
+ target = _object_without_properties_loose(source, excluded);
75
+ if (Object.getOwnPropertySymbols) {
76
+ sourceKeys = Object.getOwnPropertySymbols(source);
77
+ for(i = 0; i < sourceKeys.length; i++){
78
+ key = sourceKeys[i];
79
+ if (excluded.indexOf(key) >= 0) continue;
80
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
81
+ target[key] = source[key];
82
+ }
83
+ }
84
+ return target;
85
+ }
86
+ function _object_without_properties_loose(source, excluded) {
87
+ if (source == null) return {};
88
+ var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
89
+ for(i = 0; i < sourceKeys.length; i++){
90
+ key = sourceKeys[i];
91
+ if (excluded.indexOf(key) >= 0) continue;
92
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
93
+ target[key] = source[key];
94
+ }
95
+ return target;
96
+ }
97
+ function _ts_generator(thisArg, body) {
98
+ var f, y, t, _ = {
99
+ label: 0,
100
+ sent: function() {
101
+ if (t[0] & 1) throw t[1];
102
+ return t[1];
103
+ },
104
+ trys: [],
105
+ ops: []
106
+ }, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
107
+ return d(g, "next", {
108
+ value: verb(0)
109
+ }), d(g, "throw", {
110
+ value: verb(1)
111
+ }), d(g, "return", {
112
+ value: verb(2)
113
+ }), typeof Symbol === "function" && d(g, Symbol.iterator, {
114
+ value: function() {
115
+ return this;
116
+ }
117
+ }), g;
118
+ function verb(n) {
119
+ return function(v) {
120
+ return step([
121
+ n,
122
+ v
123
+ ]);
124
+ };
125
+ }
126
+ function step(op) {
127
+ if (f) throw new TypeError("Generator is already executing.");
128
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
129
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
130
+ if (y = 0, t) op = [
131
+ op[0] & 2,
132
+ t.value
133
+ ];
134
+ switch(op[0]){
135
+ case 0:
136
+ case 1:
137
+ t = op;
138
+ break;
139
+ case 4:
140
+ _.label++;
141
+ return {
142
+ value: op[1],
143
+ done: false
144
+ };
145
+ case 5:
146
+ _.label++;
147
+ y = op[1];
148
+ op = [
149
+ 0
150
+ ];
151
+ continue;
152
+ case 7:
153
+ op = _.ops.pop();
154
+ _.trys.pop();
155
+ continue;
156
+ default:
157
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
158
+ _ = 0;
159
+ continue;
160
+ }
161
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
162
+ _.label = op[1];
163
+ break;
164
+ }
165
+ if (op[0] === 6 && _.label < t[1]) {
166
+ _.label = t[1];
167
+ t = op;
168
+ break;
169
+ }
170
+ if (t && _.label < t[2]) {
171
+ _.label = t[2];
172
+ _.ops.push(op);
173
+ break;
174
+ }
175
+ if (t[2]) _.ops.pop();
176
+ _.trys.pop();
177
+ continue;
178
+ }
179
+ op = body.call(thisArg, _);
180
+ } catch (e) {
181
+ op = [
182
+ 6,
183
+ e
184
+ ];
185
+ y = 0;
186
+ } finally{
187
+ f = t = 0;
188
+ }
189
+ if (op[0] & 5) throw op[1];
190
+ return {
191
+ value: op[0] ? op[1] : void 0,
192
+ done: true
193
+ };
194
+ }
195
+ }
196
+ /**
197
+ * Vite plugin that transforms TypeScript and JavaScript files using SWC.
198
+ * It disables Vite's default esbuild transform to let SWC handle the compilation,
199
+ * resulting in significantly faster builds, especially for large codebases.
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * // vite.config.ts
204
+ * import { defineConfig } from 'vite'
205
+ * import swc from '@o.z/vite-plugin-swc'
206
+ *
207
+ * export default defineConfig({
208
+ * plugins: [swc()]
209
+ * })
210
+ * ```
211
+ *
212
+ * @param options - Configuration options for the plugin and SWC.
213
+ * @returns A Vite plugin instance.
214
+ */ var swc = function swc() {
215
+ var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
216
+ // Default include/exclude patterns – now covering .ts, .tsx, .js, .jsx
217
+ var _options_include = options.include, include = _options_include === void 0 ? /\.(ts|tsx|js|jsx)$/ : _options_include, _options_exclude = options.exclude, exclude = _options_exclude === void 0 ? "node_modules" : _options_exclude, swcOptions = _object_without_properties(options, [
218
+ "include",
219
+ "exclude"
220
+ ]);
221
+ var filter = createFilter(include, exclude);
222
+ return {
223
+ name: "vite-plugin-swc",
224
+ enforce: "pre",
225
+ config: function config() {
226
+ return {
227
+ esbuild: false
228
+ };
229
+ },
230
+ transform: function transform$1(code, id) {
231
+ return _async_to_generator(function() {
232
+ var _this_environment_config_css, sourceMaps, result, error;
233
+ return _ts_generator(this, function(_state) {
234
+ switch(_state.label){
235
+ case 0:
236
+ if (!filter(id)) return [
237
+ 2,
238
+ null
239
+ ];
240
+ _state.label = 1;
241
+ case 1:
242
+ _state.trys.push([
243
+ 1,
244
+ 3,
245
+ ,
246
+ 4
247
+ ]);
248
+ // Determine if source maps should be generated based on Vite's config
249
+ // `this.environment.config` is available inside the transform hook
250
+ sourceMaps = this.environment.config.command === "build" ? !!this.environment.config.build.sourcemap : !!((_this_environment_config_css = this.environment.config.css) === null || _this_environment_config_css === void 0 ? void 0 : _this_environment_config_css.devSourcemap); // For dev, you might want to align with css sourcemaps or a dedicated flag
251
+ return [
252
+ 4,
253
+ transform(code, _object_spread({
254
+ filename: id,
255
+ sourceFileName: id.split("?", 1)[0],
256
+ sourceMaps: sourceMaps
257
+ }, swcOptions))
258
+ ];
259
+ case 2:
260
+ result = _state.sent();
261
+ // SWC returns { code, map } when sourceMaps are enabled, otherwise just { code }
262
+ return [
263
+ 2,
264
+ {
265
+ code: result.code,
266
+ map: result.map
267
+ }
268
+ ];
269
+ case 3:
270
+ error = _state.sent();
271
+ // Enhance error message with file information and re-throw as a plugin error
272
+ this.error("SWC transform failed in ".concat(id, ": ").concat((error === null || error === void 0 ? void 0 : error.message) || error));
273
+ return [
274
+ 3,
275
+ 4
276
+ ];
277
+ case 4:
278
+ return [
279
+ 2
280
+ ];
281
+ }
282
+ });
283
+ }).call(this);
284
+ }
285
+ };
286
+ };
287
+
288
+ export { swc as default, swc };
package/package.json ADDED
@@ -0,0 +1,126 @@
1
+ {
2
+ "name": "@o.z/vite-plugin-swc",
3
+ "version": "0.6.0",
4
+ "description": "A high-performance Vite plugin that transforms TypeScript and JavaScript files using SWC for lightning-fast builds",
5
+ "homepage": "https://github.com/z-npm/vite-plugin-swc#readme",
6
+ "docs": "https://github.com/z-npm/vite-plugin-swc#readme",
7
+ "bugs": {
8
+ "url": "https://github.com/z-npm/vite-plugin-swc/issues"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/z-npm/vite-plugin-swc.git"
13
+ },
14
+ "author": {
15
+ "name": "Zero Red",
16
+ "email": "github@zero-red.dev",
17
+ "url": "https://github.com/zero-red-dev"
18
+ },
19
+ "maintainers": [
20
+ {
21
+ "name": "Zero Red",
22
+ "email": "github@zero-red.dev",
23
+ "url": "https://github.com/zero-red-dev"
24
+ }
25
+ ],
26
+ "contributors": [
27
+ {
28
+ "name": "Timothée Pillard",
29
+ "url": "https://github.com/ziir"
30
+ }
31
+ ],
32
+ "license": "MIT",
33
+ "funding": {
34
+ "type": "github",
35
+ "url": "https://github.com/sponsors/zero-red-dev"
36
+ },
37
+ "social": {
38
+ "tiktok": "@zero.red.dev",
39
+ "github": "zero-red-dev"
40
+ },
41
+ "keywords": [
42
+ "vite",
43
+ "vite-plugin",
44
+ "swc",
45
+ "typescript",
46
+ "javascript",
47
+ "transformer",
48
+ "compiler",
49
+ "build",
50
+ "rollup",
51
+ "rollup-plugin",
52
+ "decorators",
53
+ "top-level-await",
54
+ "fast",
55
+ "performance"
56
+ ],
57
+ "type": "module",
58
+ "files": [
59
+ "dist"
60
+ ],
61
+ "peerDependencies": {
62
+ "rollup": ">3",
63
+ "vite": ">5"
64
+ },
65
+ "peerDependenciesMeta": {
66
+ "rollup": {
67
+ "optional": true
68
+ },
69
+ "vite": {
70
+ "optional": true
71
+ }
72
+ },
73
+ "engines": {
74
+ "node": ">=18.0.0",
75
+ "npm": ">=8.0.0"
76
+ },
77
+ "publishConfig": {
78
+ "access": "public",
79
+ "registry": "https://registry.npmjs.org/"
80
+ },
81
+ "sideEffects": false,
82
+ "scripts": {
83
+ "dev": "vite",
84
+ "build": "tsc && vite build && yarn docs",
85
+ "local": "yarn build && yarn unlink && yarn link",
86
+ "docs": "typedoc",
87
+ "docs:watch": "typedoc --watch",
88
+ "test": "vitest",
89
+ "test:ui": "vitest --ui",
90
+ "test:coverage": "vitest run --coverage"
91
+ },
92
+ "devDependencies": {
93
+ "@testing-library/dom": "^10.4.1",
94
+ "@testing-library/user-event": "^14.6.1",
95
+ "@types/jsdom": "^27.0.0",
96
+ "@types/node": "^25.2.3",
97
+ "@vitest/coverage-v8": "^4.0.18",
98
+ "@vitest/ui": "^4.0.18",
99
+ "glob": "^13.0.3",
100
+ "jsdom": "^28.0.0",
101
+ "rollup-plugin-node-externals": "^8.1.2",
102
+ "sass": "^1.97.3",
103
+ "typedoc": "^0.28.17",
104
+ "typedoc-plugin-markdown": "^4.10.0",
105
+ "typescript": "^5.9.3",
106
+ "vite": "^7.3.1",
107
+ "vite-plugin-dts": "^4.5.4",
108
+ "vite-plugin-lib-inject-css": "^2.2.2",
109
+ "vitest": "^4.0.18"
110
+ },
111
+ "dependencies": {
112
+ "@rollup/pluginutils": "^5.3.0",
113
+ "@swc/core": "^1.15.11"
114
+ },
115
+ "packageManager": "yarn@4.12.0",
116
+ "types": "./dist/index.d.ts",
117
+ "module": "./dist/index.js",
118
+ "main": "./dist/index.cjs",
119
+ "exports": {
120
+ ".": {
121
+ "types": "./dist/index.d.ts",
122
+ "import": "./dist/index.js",
123
+ "require": "./dist/index.cjs"
124
+ }
125
+ }
126
+ }