unplugin-oxc 0.4.1 → 0.4.3
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/dist/esbuild.js +1 -1
- package/dist/farm.js +1 -1
- package/dist/index.js +1 -1
- package/dist/rolldown.js +1 -1
- package/dist/rollup.js +1 -1
- package/dist/rspack.js +1 -1
- package/dist/src-VARAeVoC.js +148 -0
- package/dist/unloader.js +1 -1
- package/dist/vite.js +1 -1
- package/dist/webpack.js +1 -1
- package/package.json +5 -5
- package/dist/src-Bw_izk11.js +0 -130
package/dist/esbuild.js
CHANGED
package/dist/farm.js
CHANGED
package/dist/index.js
CHANGED
package/dist/rolldown.js
CHANGED
package/dist/rollup.js
CHANGED
package/dist/rspack.js
CHANGED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import process from "node:process";
|
|
4
|
+
import { ResolverFactory } from "oxc-resolver";
|
|
5
|
+
import { transform } from "oxc-transform";
|
|
6
|
+
import { createUnplugin } from "unplugin";
|
|
7
|
+
import { createFilter } from "unplugin-utils";
|
|
8
|
+
|
|
9
|
+
//#region src/core/options.ts
|
|
10
|
+
function resolveOptions(options, framework) {
|
|
11
|
+
return {
|
|
12
|
+
include: options.include || [/\.[cm]?[jt]sx?$/],
|
|
13
|
+
exclude: options.exclude || [/node_modules/],
|
|
14
|
+
enforce: "enforce" in options ? options.enforce : "pre",
|
|
15
|
+
transform: options.transform || {},
|
|
16
|
+
resolve: options.resolve || {},
|
|
17
|
+
resolveNodeModules: options.resolveNodeModules || false,
|
|
18
|
+
minify: options.minify || false,
|
|
19
|
+
sourcemap: options.sourcemap ?? framework === "unloader"
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/core/utils.ts
|
|
25
|
+
function getModuleFormat(id) {
|
|
26
|
+
const ext = path.extname(id);
|
|
27
|
+
switch (ext) {
|
|
28
|
+
case ".mjs":
|
|
29
|
+
case ".mts": return "module";
|
|
30
|
+
case ".cjs":
|
|
31
|
+
case ".cts": return "commonjs";
|
|
32
|
+
case ".json": return "json";
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
//#endregion
|
|
37
|
+
//#region src/index.ts
|
|
38
|
+
const Oxc = createUnplugin((rawOptions = {}, { framework }) => {
|
|
39
|
+
const options = resolveOptions(rawOptions, framework);
|
|
40
|
+
const filter = createFilter(options.include, options.exclude);
|
|
41
|
+
const resolveId = options.resolve !== false ? (id, importer, resolveOptions$1) => {
|
|
42
|
+
if (!options.resolveNodeModules && id[0] !== "." && !path.isAbsolute(id)) return;
|
|
43
|
+
const resolver = new ResolverFactory({
|
|
44
|
+
extensions: [
|
|
45
|
+
".mjs",
|
|
46
|
+
".js",
|
|
47
|
+
".ts",
|
|
48
|
+
".jsx",
|
|
49
|
+
".tsx",
|
|
50
|
+
".json",
|
|
51
|
+
".node"
|
|
52
|
+
],
|
|
53
|
+
conditionNames: resolveOptions$1?.conditions ? Array.from(resolveOptions$1.conditions) : [
|
|
54
|
+
"import",
|
|
55
|
+
"require",
|
|
56
|
+
"browser",
|
|
57
|
+
"node",
|
|
58
|
+
"default"
|
|
59
|
+
],
|
|
60
|
+
builtinModules: true,
|
|
61
|
+
...options.resolve
|
|
62
|
+
});
|
|
63
|
+
const directory = importer ? path.dirname(importer) : process.cwd();
|
|
64
|
+
const resolved = resolver.sync(directory, id);
|
|
65
|
+
if (resolved.error?.startsWith("Builtin module")) return {
|
|
66
|
+
id,
|
|
67
|
+
external: true,
|
|
68
|
+
moduleSideEffects: false
|
|
69
|
+
};
|
|
70
|
+
if (resolved.path) {
|
|
71
|
+
const format = getModuleFormat(resolved.path) || resolved.moduleType || "commonjs";
|
|
72
|
+
return {
|
|
73
|
+
id: resolved.path,
|
|
74
|
+
format
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
} : void 0;
|
|
78
|
+
const transform$1 = options.transform !== false ? (code, id, ...args) => {
|
|
79
|
+
const [transformOptions] = args;
|
|
80
|
+
const result = transform(id, code, {
|
|
81
|
+
...options.transform,
|
|
82
|
+
sourceType: guessSourceType(id, transformOptions?.format),
|
|
83
|
+
sourcemap: options.sourcemap
|
|
84
|
+
});
|
|
85
|
+
if (result.errors.length) throw new SyntaxError(result.errors.map((error) => error.message).join("\n"));
|
|
86
|
+
return {
|
|
87
|
+
code: result.code,
|
|
88
|
+
map: result.map
|
|
89
|
+
};
|
|
90
|
+
} : void 0;
|
|
91
|
+
const renderChunk = options.minify !== false ? async (code, chunk) => {
|
|
92
|
+
const { minify } = await import("oxc-minify");
|
|
93
|
+
const result = minify(chunk.fileName, code, {
|
|
94
|
+
...options.minify === true ? {} : options.minify,
|
|
95
|
+
sourcemap: options.sourcemap
|
|
96
|
+
});
|
|
97
|
+
return {
|
|
98
|
+
code: result.code,
|
|
99
|
+
map: result.map
|
|
100
|
+
};
|
|
101
|
+
} : void 0;
|
|
102
|
+
const unloader = {
|
|
103
|
+
options(config) {
|
|
104
|
+
config.sourcemap ||= options.sourcemap;
|
|
105
|
+
},
|
|
106
|
+
load(id) {
|
|
107
|
+
if (id.endsWith(".json")) {
|
|
108
|
+
let code = readFileSync(id, "utf8");
|
|
109
|
+
const json = JSON.parse(code);
|
|
110
|
+
code = `const json = ${code}\nexport default json\n`;
|
|
111
|
+
const i = 0;
|
|
112
|
+
for (const key of Object.keys(json)) {
|
|
113
|
+
const sanitizedKey = `_${key.replaceAll(/\W/g, "_")}${i}`;
|
|
114
|
+
code += `\nconst ${sanitizedKey} = json[${JSON.stringify(key)}]\nexport { ${sanitizedKey} as ${JSON.stringify(key)} }\n`;
|
|
115
|
+
}
|
|
116
|
+
return {
|
|
117
|
+
code,
|
|
118
|
+
format: "module"
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
if (!filter(id)) return;
|
|
122
|
+
const contents = readFileSync(id, "utf8");
|
|
123
|
+
return contents;
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
name: "unplugin-oxc",
|
|
128
|
+
enforce: options.enforce,
|
|
129
|
+
resolveId,
|
|
130
|
+
transformInclude(id) {
|
|
131
|
+
return filter(id);
|
|
132
|
+
},
|
|
133
|
+
transform: transform$1,
|
|
134
|
+
rollup: { renderChunk },
|
|
135
|
+
rolldown: { renderChunk },
|
|
136
|
+
vite: { renderChunk },
|
|
137
|
+
unloader
|
|
138
|
+
};
|
|
139
|
+
});
|
|
140
|
+
function guessSourceType(id, format) {
|
|
141
|
+
if (format === "module" || format === "module-typescript") return "module";
|
|
142
|
+
else if (format === "commonjs" || format === "commonjs-typescript") return "script";
|
|
143
|
+
const moduleFormat = getModuleFormat(id);
|
|
144
|
+
if (moduleFormat) return moduleFormat === "module" ? "module" : "script";
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
//#endregion
|
|
148
|
+
export { Oxc };
|
package/dist/unloader.js
CHANGED
package/dist/vite.js
CHANGED
package/dist/webpack.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unplugin-oxc",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "Oxc integration for unplugin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -64,9 +64,9 @@
|
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
66
|
"oxc-minify": "^0.69.0",
|
|
67
|
-
"oxc-resolver": "^9.0.
|
|
67
|
+
"oxc-resolver": "^9.0.2",
|
|
68
68
|
"oxc-transform": "^0.69.0",
|
|
69
|
-
"unplugin": "^2.3.
|
|
69
|
+
"unplugin": "^2.3.3",
|
|
70
70
|
"unplugin-utils": "^0.2.4"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
@@ -79,10 +79,10 @@
|
|
|
79
79
|
"prettier": "^3.5.3",
|
|
80
80
|
"rollup": "^4.40.2",
|
|
81
81
|
"tinyexec": "^1.0.1",
|
|
82
|
-
"tsdown": "^0.11.
|
|
82
|
+
"tsdown": "^0.11.5",
|
|
83
83
|
"tsx": "^4.19.4",
|
|
84
84
|
"typescript": "^5.8.3",
|
|
85
|
-
"unloader": "^0.4.
|
|
85
|
+
"unloader": "^0.4.5",
|
|
86
86
|
"vite": "^6.3.5",
|
|
87
87
|
"vitest": "^3.1.3"
|
|
88
88
|
},
|
package/dist/src-Bw_izk11.js
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import { readFileSync } from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import process from "node:process";
|
|
4
|
-
import { ResolverFactory } from "oxc-resolver";
|
|
5
|
-
import { transform } from "oxc-transform";
|
|
6
|
-
import { createUnplugin } from "unplugin";
|
|
7
|
-
import { createFilter } from "unplugin-utils";
|
|
8
|
-
|
|
9
|
-
//#region src/core/options.ts
|
|
10
|
-
function resolveOptions(options, framework) {
|
|
11
|
-
return {
|
|
12
|
-
include: options.include || [/\.[cm]?[jt]sx?$/],
|
|
13
|
-
exclude: options.exclude || [/node_modules/],
|
|
14
|
-
enforce: "enforce" in options ? options.enforce : "pre",
|
|
15
|
-
transform: options.transform || {},
|
|
16
|
-
resolve: options.resolve || {},
|
|
17
|
-
resolveNodeModules: options.resolveNodeModules || false,
|
|
18
|
-
minify: options.minify || false,
|
|
19
|
-
sourcemap: options.sourcemap ?? framework === "unloader"
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
|
-
//#region src/core/utils.ts
|
|
25
|
-
function getModuleFormat(id) {
|
|
26
|
-
const ext = path.extname(id);
|
|
27
|
-
switch (ext) {
|
|
28
|
-
case ".mjs":
|
|
29
|
-
case ".mts": return "module";
|
|
30
|
-
case ".cjs":
|
|
31
|
-
case ".cts": return "commonjs";
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
//#endregion
|
|
36
|
-
//#region src/index.ts
|
|
37
|
-
const Oxc = createUnplugin((rawOptions = {}, { framework }) => {
|
|
38
|
-
const options = resolveOptions(rawOptions, framework);
|
|
39
|
-
const filter = createFilter(options.include, options.exclude);
|
|
40
|
-
const renderChunk = options.minify !== false ? async (code, chunk) => {
|
|
41
|
-
const { minify } = await import("oxc-minify");
|
|
42
|
-
const result = minify(chunk.fileName, code, {
|
|
43
|
-
...options.minify === true ? {} : options.minify,
|
|
44
|
-
sourcemap: options.sourcemap
|
|
45
|
-
});
|
|
46
|
-
return {
|
|
47
|
-
code: result.code,
|
|
48
|
-
map: result.map
|
|
49
|
-
};
|
|
50
|
-
} : void 0;
|
|
51
|
-
return {
|
|
52
|
-
name: "unplugin-oxc",
|
|
53
|
-
enforce: options.enforce,
|
|
54
|
-
resolveId: options.resolve !== false ? (id, importer, resolveOptions$1) => {
|
|
55
|
-
if (!options.resolveNodeModules && id[0] !== "." && !path.isAbsolute(id)) return;
|
|
56
|
-
const resolver = new ResolverFactory({
|
|
57
|
-
extensions: [
|
|
58
|
-
".mjs",
|
|
59
|
-
".js",
|
|
60
|
-
".ts",
|
|
61
|
-
".jsx",
|
|
62
|
-
".tsx",
|
|
63
|
-
".json",
|
|
64
|
-
".node"
|
|
65
|
-
],
|
|
66
|
-
conditionNames: resolveOptions$1?.conditions ? Array.from(resolveOptions$1.conditions) : [
|
|
67
|
-
"import",
|
|
68
|
-
"require",
|
|
69
|
-
"browser",
|
|
70
|
-
"node",
|
|
71
|
-
"default"
|
|
72
|
-
],
|
|
73
|
-
builtinModules: true,
|
|
74
|
-
...options.resolve
|
|
75
|
-
});
|
|
76
|
-
const directory = importer ? path.dirname(importer) : process.cwd();
|
|
77
|
-
const resolved = resolver.sync(directory, id);
|
|
78
|
-
if (resolved.error?.startsWith("Builtin module")) return {
|
|
79
|
-
id,
|
|
80
|
-
external: true,
|
|
81
|
-
moduleSideEffects: false
|
|
82
|
-
};
|
|
83
|
-
if (resolved.path) {
|
|
84
|
-
const format = getModuleFormat(resolved.path) || resolved.moduleType || "commonjs";
|
|
85
|
-
return {
|
|
86
|
-
id: resolved.path,
|
|
87
|
-
format
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
} : void 0,
|
|
91
|
-
transformInclude(id) {
|
|
92
|
-
return filter(id);
|
|
93
|
-
},
|
|
94
|
-
transform: options.transform !== false ? (code, id, ...args) => {
|
|
95
|
-
const [transformOptions] = args;
|
|
96
|
-
const result = transform(id, code, {
|
|
97
|
-
...options.transform,
|
|
98
|
-
sourceType: guessSourceType(id, transformOptions?.format),
|
|
99
|
-
sourcemap: options.sourcemap
|
|
100
|
-
});
|
|
101
|
-
if (result.errors.length) throw new SyntaxError(result.errors.map((error) => error.message).join("\n"));
|
|
102
|
-
return {
|
|
103
|
-
code: result.code,
|
|
104
|
-
map: result.map
|
|
105
|
-
};
|
|
106
|
-
} : void 0,
|
|
107
|
-
rollup: { renderChunk },
|
|
108
|
-
rolldown: { renderChunk },
|
|
109
|
-
vite: { renderChunk },
|
|
110
|
-
unloader: {
|
|
111
|
-
options(config) {
|
|
112
|
-
config.sourcemap ||= options.sourcemap;
|
|
113
|
-
},
|
|
114
|
-
load(id) {
|
|
115
|
-
if (!filter(id)) return;
|
|
116
|
-
const contents = readFileSync(id, "utf8");
|
|
117
|
-
return contents;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
});
|
|
122
|
-
function guessSourceType(id, format) {
|
|
123
|
-
if (format === "module" || format === "module-typescript") return "module";
|
|
124
|
-
else if (format === "commonjs" || format === "commonjs-typescript") return "script";
|
|
125
|
-
const moduleFormat = getModuleFormat(id);
|
|
126
|
-
if (moduleFormat) return moduleFormat === "module" ? "module" : "script";
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
//#endregion
|
|
130
|
-
export { Oxc };
|