oxc-webpack-loader 0.0.1

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.
Files changed (4) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +134 -0
  3. package/package.json +24 -0
  4. package/src/index.js +72 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present VoidZero Inc. & Contributors
4
+ Copyright (c) 2023 Boshen
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,134 @@
1
+ <p align="center">
2
+ <br>
3
+ <br>
4
+ <a href="https://oxc.rs" target="_blank" rel="noopener noreferrer">
5
+ <picture>
6
+ <source media="(prefers-color-scheme: dark)" srcset="https://oxc.rs/oxc-light.svg">
7
+ <source media="(prefers-color-scheme: light)" srcset="https://oxc.rs/oxc-dark.svg">
8
+ <img alt="Oxc logo" src="https://oxc.rs/oxc-dark.svg" height="60">
9
+ </picture>
10
+ </a>
11
+ <br>
12
+ <br>
13
+ <br>
14
+ </p>
15
+
16
+ <div align="center">
17
+
18
+ [![MIT licensed][license-badge]][license-url]
19
+ [![Build Status][ci-badge]][ci-url]
20
+ [![npm][npm-badge]][npm-url]
21
+
22
+ [![Discord chat][discord-badge]][discord-url]
23
+ [![Website][website-badge]][website-url]
24
+
25
+ </div>
26
+
27
+ ## oxc-loader
28
+
29
+ Webpack loader for [oxc-transform](https://npmx.dev/package/oxc-transform). A drop-in replacement for `swc-loader` and `babel-loader` that uses Oxc for fast JavaScript and TypeScript transformation.
30
+
31
+ ## Install
32
+
33
+ ```bash
34
+ npm install -D oxc-webpack-loader oxc-transform
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ **webpack.config.js**
40
+
41
+ ```js
42
+ module.exports = {
43
+ module: {
44
+ rules: [
45
+ {
46
+ test: /\.[jt]sx?$/,
47
+ exclude: /node_modules/,
48
+ use: {
49
+ loader: "oxc-webpack-loader",
50
+ options: {
51
+ // oxc-transform options
52
+ // https://npmx.dev/package/oxc-transform
53
+ },
54
+ },
55
+ },
56
+ ],
57
+ },
58
+ };
59
+ ```
60
+
61
+ ### JSX
62
+
63
+ ```js
64
+ {
65
+ loader: "oxc-webpack-loader",
66
+ options: {
67
+ jsx: {
68
+ runtime: "automatic", // or "classic"
69
+ },
70
+ },
71
+ }
72
+ ```
73
+
74
+ ### TypeScript
75
+
76
+ TypeScript is supported out of the box. No additional configuration is needed - just include `.ts` and `.tsx` in the `test` pattern.
77
+
78
+ ### Target
79
+
80
+ ```js
81
+ {
82
+ loader: "oxc-webpack-loader",
83
+ options: {
84
+ target: "es2015",
85
+ },
86
+ }
87
+ ```
88
+
89
+ ### Source Maps
90
+
91
+ ```js
92
+ {
93
+ loader: "oxc-webpack-loader",
94
+ options: {
95
+ sourcemap: true,
96
+ },
97
+ }
98
+ ```
99
+
100
+ Source maps are also automatically enabled when webpack's `devtool` option is set.
101
+
102
+ ### Sync Mode
103
+
104
+ ```js
105
+ {
106
+ loader: "oxc-webpack-loader",
107
+ options: {
108
+ sync: true,
109
+ },
110
+ }
111
+ ```
112
+
113
+ ## Options
114
+
115
+ All [oxc-transform `TransformOptions`](https://npmx.dev/package/oxc-transform) are supported, plus:
116
+
117
+ | Option | Type | Default | Description |
118
+ | ------ | --------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
119
+ | `sync` | `boolean` | `false` | Use `transformSync` instead of `transform`. The async `transform` spawns a thread per call, which can be slower due to overhead. Sync mode runs on the main thread and may be faster for small files. |
120
+
121
+ ## License
122
+
123
+ Oxc is free and open-source software licensed under the [MIT License](./LICENSE).
124
+
125
+ [license-badge]: https://img.shields.io/badge/license-MIT-blue.svg
126
+ [license-url]: https://github.com/oxc-project/oxc-webpack-loader/blob/main/LICENSE
127
+ [ci-badge]: https://github.com/oxc-project/oxc-webpack-loader/actions/workflows/ci.yml/badge.svg?event=push&branch=main
128
+ [ci-url]: https://github.com/oxc-project/oxc-webpack-loader/actions/workflows/ci.yml?query=event%3Apush+branch%3Amain
129
+ [npm-badge]: https://img.shields.io/npm/v/oxc-loader
130
+ [npm-url]: https://npmx.dev/package/oxc-webpack-loader
131
+ [discord-badge]: https://img.shields.io/discord/1079625926024900739?logo=discord&label=Discord
132
+ [discord-url]: https://discord.gg/9uXCAwqQZW
133
+ [website-badge]: https://img.shields.io/badge/Website-blue
134
+ [website-url]: https://oxc.rs
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "oxc-webpack-loader",
3
+ "version": "0.0.1",
4
+ "description": "Webpack loader for oxc",
5
+ "license": "MIT",
6
+ "author": "oxc-project",
7
+ "repository": "https://github.com/oxc-project/oxc-webpack-loader",
8
+ "files": [
9
+ "src"
10
+ ],
11
+ "main": "src/index.js",
12
+ "devDependencies": {
13
+ "oxc-transform": "^0.115.0",
14
+ "vitest": "^4.0.18",
15
+ "webpack": "^5.98.0"
16
+ },
17
+ "peerDependencies": {
18
+ "oxc-transform": "^0.115.0",
19
+ "webpack": ">=5"
20
+ },
21
+ "scripts": {
22
+ "test": "vitest run"
23
+ }
24
+ }
package/src/index.js ADDED
@@ -0,0 +1,72 @@
1
+ const { transform, transformSync } = require("oxc-transform");
2
+
3
+ function makeLoader() {
4
+ return function (source, inputSourceMap) {
5
+ const callback = this.async();
6
+ const filename = this.resourcePath;
7
+
8
+ let loaderOptions = (typeof this.getOptions === "function" ? this.getOptions() : {}) || {};
9
+
10
+ const sourceMaps =
11
+ loaderOptions.sourcemap === undefined ? this.sourceMap : loaderOptions.sourcemap;
12
+
13
+ const transformOptions = Object.assign({}, loaderOptions, {
14
+ sourcemap: !!sourceMaps,
15
+ });
16
+
17
+ // Auto detect development mode for JSX
18
+ if (
19
+ this.mode &&
20
+ transformOptions.jsx &&
21
+ typeof transformOptions.jsx === "object" &&
22
+ !Object.prototype.hasOwnProperty.call(transformOptions.jsx, "development")
23
+ ) {
24
+ transformOptions.jsx = Object.assign({}, transformOptions.jsx, {
25
+ development: this.mode === "development",
26
+ });
27
+ }
28
+
29
+ // Auto detect lang when jsx options are provided but file has .js extension
30
+ if (!transformOptions.lang && transformOptions.jsx && transformOptions.jsx !== "preserve") {
31
+ const ext = filename.slice(filename.lastIndexOf("."));
32
+ if (ext === ".js") {
33
+ transformOptions.lang = "jsx";
34
+ } else if (ext === ".ts") {
35
+ transformOptions.lang = "tsx";
36
+ }
37
+ }
38
+
39
+ // Remove loader-specific options
40
+ const sync = transformOptions.sync;
41
+ delete transformOptions.sync;
42
+
43
+ try {
44
+ if (sync) {
45
+ const output = transformSync(filename, source, transformOptions);
46
+ if (output.errors.length > 0) {
47
+ callback(new Error(output.errors.map((e) => e.message).join("\n")));
48
+ return;
49
+ }
50
+ callback(null, output.code, output.map ?? undefined);
51
+ } else {
52
+ transform(filename, source, transformOptions).then(
53
+ (output) => {
54
+ if (output.errors.length > 0) {
55
+ callback(new Error(output.errors.map((e) => e.message).join("\n")));
56
+ return;
57
+ }
58
+ callback(null, output.code, output.map ?? undefined);
59
+ },
60
+ (err) => {
61
+ callback(err);
62
+ },
63
+ );
64
+ }
65
+ } catch (e) {
66
+ callback(e);
67
+ }
68
+ };
69
+ }
70
+
71
+ module.exports = makeLoader();
72
+ module.exports.custom = makeLoader;