@gytx/unplugin 3.1.0-patch.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.
@@ -0,0 +1,140 @@
1
+ import { resolve } from "node:path";
2
+ import picomatch from "picomatch";
3
+ import pify from "pify";
4
+ //#region src/utils/general.ts
5
+ function toArray(array) {
6
+ array = array || [];
7
+ if (Array.isArray(array)) return array;
8
+ return [array];
9
+ }
10
+ //#endregion
11
+ //#region src/utils/filter.ts
12
+ const BACKSLASH_REGEX = /\\/g;
13
+ function normalize$1(path) {
14
+ return path.replace(BACKSLASH_REGEX, "/");
15
+ }
16
+ const ABSOLUTE_PATH_REGEX = /^(?:\/|(?:[A-Z]:)?[/\\|])/i;
17
+ function isAbsolute$1(path) {
18
+ return ABSOLUTE_PATH_REGEX.test(path);
19
+ }
20
+ function getMatcherString(glob, cwd) {
21
+ if (glob.startsWith("**") || isAbsolute$1(glob)) return normalize$1(glob);
22
+ return normalize$1(resolve(cwd, glob));
23
+ }
24
+ function patternToIdFilter(pattern) {
25
+ if (pattern instanceof RegExp) return (id) => {
26
+ const normalizedId = normalize$1(id);
27
+ const result = pattern.test(normalizedId);
28
+ pattern.lastIndex = 0;
29
+ return result;
30
+ };
31
+ const matcher = picomatch(getMatcherString(pattern, process.cwd()), { dot: true });
32
+ return (id) => {
33
+ return matcher(normalize$1(id));
34
+ };
35
+ }
36
+ function patternToCodeFilter(pattern) {
37
+ if (pattern instanceof RegExp) return (code) => {
38
+ const result = pattern.test(code);
39
+ pattern.lastIndex = 0;
40
+ return result;
41
+ };
42
+ return (code) => code.includes(pattern);
43
+ }
44
+ function createFilter(exclude, include) {
45
+ if (!exclude && !include) return;
46
+ return (input) => {
47
+ if (exclude?.some((filter) => filter(input))) return false;
48
+ if (include?.some((filter) => filter(input))) return true;
49
+ return !(include && include.length > 0);
50
+ };
51
+ }
52
+ function normalizeFilter(filter) {
53
+ if (typeof filter === "string" || filter instanceof RegExp) return { include: [filter] };
54
+ if (Array.isArray(filter)) return { include: filter };
55
+ return {
56
+ exclude: filter.exclude ? toArray(filter.exclude) : void 0,
57
+ include: filter.include ? toArray(filter.include) : void 0
58
+ };
59
+ }
60
+ function createIdFilter(filter) {
61
+ if (!filter) return;
62
+ const { exclude, include } = normalizeFilter(filter);
63
+ const excludeFilter = exclude?.map(patternToIdFilter);
64
+ const includeFilter = include?.map(patternToIdFilter);
65
+ return createFilter(excludeFilter, includeFilter);
66
+ }
67
+ function createCodeFilter(filter) {
68
+ if (!filter) return;
69
+ const { exclude, include } = normalizeFilter(filter);
70
+ const excludeFilter = exclude?.map(patternToCodeFilter);
71
+ const includeFilter = include?.map(patternToCodeFilter);
72
+ return createFilter(excludeFilter, includeFilter);
73
+ }
74
+ function createFilterForId(filter) {
75
+ const filterFunction = createIdFilter(filter);
76
+ return filterFunction ? (id) => !!filterFunction(id) : void 0;
77
+ }
78
+ function createFilterForTransform(idFilter, codeFilter) {
79
+ if (!idFilter && !codeFilter) return;
80
+ const idFilterFunction = createIdFilter(idFilter);
81
+ const codeFilterFunction = createCodeFilter(codeFilter);
82
+ return (id, code) => {
83
+ let fallback = true;
84
+ if (idFilterFunction) fallback &&= idFilterFunction(id);
85
+ if (!fallback) return false;
86
+ if (codeFilterFunction) fallback &&= codeFilterFunction(code);
87
+ return fallback;
88
+ };
89
+ }
90
+ function normalizeObjectHook(name, hook) {
91
+ let handler;
92
+ let filter;
93
+ if (typeof hook === "function") handler = hook;
94
+ else {
95
+ handler = hook.handler;
96
+ const hookFilter = hook.filter;
97
+ if (name === "resolveId" || name === "load") filter = createFilterForId(hookFilter?.id);
98
+ else filter = createFilterForTransform(hookFilter?.id, hookFilter?.code);
99
+ }
100
+ return {
101
+ handler,
102
+ filter: filter || (() => true)
103
+ };
104
+ }
105
+ //#endregion
106
+ //#region src/utils/fs.ts
107
+ function createNodeFs() {
108
+ const fsPromiseModule = import("node:fs/promises");
109
+ return {
110
+ readFile: async (path, options) => {
111
+ return (await fsPromiseModule).readFile(path, options);
112
+ },
113
+ stat: async (path, options) => {
114
+ return (await fsPromiseModule).stat(path, options);
115
+ },
116
+ lstat: async (path, options) => {
117
+ return (await fsPromiseModule).lstat(path, options);
118
+ }
119
+ };
120
+ }
121
+ function createBuildContextFs(inputFs) {
122
+ const fs = inputFs ? pify(inputFs) : createNodeFs();
123
+ return {
124
+ readFile: fs.readFile,
125
+ stat: fs.stat,
126
+ lstat: fs.lstat
127
+ };
128
+ }
129
+ //#endregion
130
+ //#region src/utils/parse.ts
131
+ let parseImpl;
132
+ function parse(code, opts = {}) {
133
+ if (!parseImpl) throw new Error("Parse implementation is not set. Please call setParseImpl first.");
134
+ return parseImpl(code, opts);
135
+ }
136
+ function setParseImpl(customParse) {
137
+ parseImpl = customParse;
138
+ }
139
+ //#endregion
140
+ export { toArray as a, normalizeObjectHook as i, setParseImpl as n, createBuildContextFs as r, parse as t };
@@ -0,0 +1,6 @@
1
+ import { LoaderContext } from "@rspack/core";
2
+
3
+ //#region src/rspack/loaders/load.d.ts
4
+ declare function load(this: LoaderContext, source: string, map: any): Promise<void>;
5
+ //#endregion
6
+ export { load as default };
@@ -0,0 +1,25 @@
1
+ import { i as normalizeObjectHook } from "../../parse-C8UGVJ_5.mjs";
2
+ import { t as normalizeAbsolutePath } from "../../webpack-like-m4jN95Ew.mjs";
3
+ import { n as createContext, t as createBuildContext } from "../../context-cZ84xoAB.mjs";
4
+ import { i as isVirtualModuleId, n as decodeVirtualModuleId } from "../../utils-DjVnpaM4.mjs";
5
+ //#region src/rspack/loaders/load.ts
6
+ async function load(source, map) {
7
+ const callback = this.async();
8
+ const { plugin } = this.query;
9
+ let id = this.resource;
10
+ if (!plugin?.load || !id) return callback(null, source, map);
11
+ if (isVirtualModuleId(id, plugin)) id = decodeVirtualModuleId(id, plugin);
12
+ const context = createContext(this);
13
+ const { handler } = normalizeObjectHook("load", plugin.load);
14
+ try {
15
+ const res = await handler.call(Object.assign({}, this._compilation && createBuildContext(this._compiler, this._compilation, this), context), normalizeAbsolutePath(id));
16
+ if (res == null) callback(null, source, map);
17
+ else if (typeof res !== "string") callback(null, res.code, res.map ?? map);
18
+ else callback(null, res, map);
19
+ } catch (error) {
20
+ if (error instanceof Error) callback(error);
21
+ else callback(new Error(String(error)));
22
+ }
23
+ }
24
+ //#endregion
25
+ export { load as default };
@@ -0,0 +1,6 @@
1
+ import { LoaderContext } from "@rspack/core";
2
+
3
+ //#region src/rspack/loaders/transform.d.ts
4
+ declare function transform(this: LoaderContext, source: string, map: any): Promise<void>;
5
+ //#endregion
6
+ export { transform as default };
@@ -0,0 +1,23 @@
1
+ import { i as normalizeObjectHook } from "../../parse-C8UGVJ_5.mjs";
2
+ import { n as createContext, t as createBuildContext } from "../../context-cZ84xoAB.mjs";
3
+ //#region src/rspack/loaders/transform.ts
4
+ async function transform(source, map) {
5
+ const callback = this.async();
6
+ const { plugin } = this.query;
7
+ if (!plugin?.transform) return callback(null, source, map);
8
+ const id = this.resource;
9
+ const context = createContext(this);
10
+ const { handler, filter } = normalizeObjectHook("transform", plugin.transform);
11
+ if (!filter(this.resource, source)) return callback(null, source, map);
12
+ try {
13
+ const res = await handler.call(Object.assign({}, this._compilation && createBuildContext(this._compiler, this._compilation, this, map), context), source, id);
14
+ if (res == null) callback(null, source, map);
15
+ else if (typeof res !== "string") callback(null, res.code, map == null ? map : res.map || map);
16
+ else callback(null, res, map);
17
+ } catch (error) {
18
+ if (error instanceof Error) callback(error);
19
+ else callback(new Error(String(error)));
20
+ }
21
+ }
22
+ //#endregion
23
+ export { transform as default };
@@ -0,0 +1,53 @@
1
+ import { basename, dirname, resolve } from "node:path";
2
+ import fs from "node:fs";
3
+ //#region src/rspack/utils.ts
4
+ function encodeVirtualModuleId(id, plugin) {
5
+ return resolve(plugin.__virtualModulePrefix, encodeURIComponent(id));
6
+ }
7
+ function decodeVirtualModuleId(encoded, _plugin) {
8
+ return decodeURIComponent(basename(encoded));
9
+ }
10
+ function isVirtualModuleId(encoded, plugin) {
11
+ return dirname(encoded) === plugin.__virtualModulePrefix;
12
+ }
13
+ var FakeVirtualModulesPlugin = class FakeVirtualModulesPlugin {
14
+ plugin;
15
+ name = "FakeVirtualModulesPlugin";
16
+ static counters = /* @__PURE__ */ new Map();
17
+ static initCleanup = false;
18
+ constructor(plugin) {
19
+ this.plugin = plugin;
20
+ if (!FakeVirtualModulesPlugin.initCleanup) {
21
+ FakeVirtualModulesPlugin.initCleanup = true;
22
+ process.once("exit", () => {
23
+ FakeVirtualModulesPlugin.counters.forEach((_, dir) => {
24
+ fs.rmSync(dir, {
25
+ recursive: true,
26
+ force: true
27
+ });
28
+ });
29
+ });
30
+ }
31
+ }
32
+ apply(compiler) {
33
+ const dir = this.plugin.__virtualModulePrefix;
34
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
35
+ const counter = FakeVirtualModulesPlugin.counters.get(dir) ?? 0;
36
+ FakeVirtualModulesPlugin.counters.set(dir, counter + 1);
37
+ compiler.hooks.shutdown.tap(this.name, () => {
38
+ const counter = (FakeVirtualModulesPlugin.counters.get(dir) ?? 1) - 1;
39
+ if (counter === 0) {
40
+ FakeVirtualModulesPlugin.counters.delete(dir);
41
+ fs.rmSync(dir, {
42
+ recursive: true,
43
+ force: true
44
+ });
45
+ } else FakeVirtualModulesPlugin.counters.set(dir, counter);
46
+ });
47
+ }
48
+ async writeModule(file) {
49
+ return fs.promises.writeFile(file, "");
50
+ }
51
+ };
52
+ //#endregion
53
+ export { isVirtualModuleId as i, decodeVirtualModuleId as n, encodeVirtualModuleId as r, FakeVirtualModulesPlugin as t };
@@ -0,0 +1,6 @@
1
+ import { LoaderContext } from "webpack";
2
+
3
+ //#region src/webpack/loaders/load.d.ts
4
+ declare function load(this: LoaderContext<any>, source: string, map: any): Promise<void>;
5
+ //#endregion
6
+ export { load as default };
@@ -0,0 +1,26 @@
1
+ import { i as normalizeObjectHook } from "../../parse-C8UGVJ_5.mjs";
2
+ import { t as normalizeAbsolutePath } from "../../webpack-like-m4jN95Ew.mjs";
3
+ import { n as createBuildContext, r as createContext } from "../../context-Dv7UHMCI.mjs";
4
+ //#region src/webpack/loaders/load.ts
5
+ async function load(source, map) {
6
+ const callback = this.async();
7
+ const { plugin } = this.query;
8
+ let id = this.resource;
9
+ if (!plugin?.load || !id) return callback(null, source, map);
10
+ if (id.startsWith(plugin.__virtualModulePrefix)) id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length));
11
+ const context = createContext(this);
12
+ const { handler } = normalizeObjectHook("load", plugin.load);
13
+ const res = await handler.call(Object.assign({}, createBuildContext({
14
+ addWatchFile: (file) => {
15
+ this.addDependency(file);
16
+ },
17
+ getWatchFiles: () => {
18
+ return this.getDependencies();
19
+ }
20
+ }, this._compiler, this._compilation, this), context), normalizeAbsolutePath(id));
21
+ if (res == null) callback(null, source, map);
22
+ else if (typeof res !== "string") callback(null, res.code, res.map ?? map);
23
+ else callback(null, res, map);
24
+ }
25
+ //#endregion
26
+ export { load as default };
@@ -0,0 +1,6 @@
1
+ import { LoaderContext } from "webpack";
2
+
3
+ //#region src/webpack/loaders/transform.d.ts
4
+ declare function transform(this: LoaderContext<any>, source: string, map: any): Promise<void>;
5
+ //#endregion
6
+ export { transform as default };
@@ -0,0 +1,29 @@
1
+ import { i as normalizeObjectHook } from "../../parse-C8UGVJ_5.mjs";
2
+ import { n as createBuildContext, r as createContext } from "../../context-Dv7UHMCI.mjs";
3
+ //#region src/webpack/loaders/transform.ts
4
+ async function transform(source, map) {
5
+ const callback = this.async();
6
+ const { plugin } = this.query;
7
+ if (!plugin?.transform) return callback(null, source, map);
8
+ const context = createContext(this);
9
+ const { handler, filter } = normalizeObjectHook("transform", plugin.transform);
10
+ if (!filter(this.resource, source)) return callback(null, source, map);
11
+ try {
12
+ const res = await handler.call(Object.assign({}, createBuildContext({
13
+ addWatchFile: (file) => {
14
+ this.addDependency(file);
15
+ },
16
+ getWatchFiles: () => {
17
+ return this.getDependencies();
18
+ }
19
+ }, this._compiler, this._compilation, this, map), context), source, this.resource);
20
+ if (res == null) callback(null, source, map);
21
+ else if (typeof res !== "string") callback(null, res.code, map == null ? map : res.map || map);
22
+ else callback(null, res, map);
23
+ } catch (error) {
24
+ if (error instanceof Error) callback(error);
25
+ else callback(new Error(String(error)));
26
+ }
27
+ }
28
+ //#endregion
29
+ export { transform as default };
@@ -0,0 +1,31 @@
1
+ import { i as normalizeObjectHook } from "./parse-C8UGVJ_5.mjs";
2
+ import { isAbsolute, normalize } from "node:path";
3
+ //#region src/utils/webpack-like.ts
4
+ function transformUse(data, plugin, transformLoader) {
5
+ if (data.resource == null) return [];
6
+ const id = normalizeAbsolutePath(data.resource + (data.resourceQuery || ""));
7
+ if (plugin.transformInclude && !plugin.transformInclude(id)) return [];
8
+ const { filter } = normalizeObjectHook("load", plugin.transform);
9
+ if (!filter(id)) return [];
10
+ return [{
11
+ loader: transformLoader,
12
+ options: { plugin },
13
+ ident: plugin.name
14
+ }];
15
+ }
16
+ /**
17
+ * Normalizes a given path when it's absolute. Normalizing means returning a new path by converting
18
+ * the input path to the native os format. This is useful in cases where we want to normalize
19
+ * the `id` argument of a hook. Any absolute ids should be in the default format
20
+ * of the operating system. Any relative imports or node_module imports should remain
21
+ * untouched.
22
+ *
23
+ * @param path - Path to normalize.
24
+ * @returns a new normalized path.
25
+ */
26
+ function normalizeAbsolutePath(path) {
27
+ if (isAbsolute(path)) return normalize(path);
28
+ else return path;
29
+ }
30
+ //#endregion
31
+ export { transformUse as n, normalizeAbsolutePath as t };
package/package.json ADDED
@@ -0,0 +1,95 @@
1
+ {
2
+ "name": "@gytx/unplugin",
3
+ "type": "module",
4
+ "version": "3.1.0-patch.1",
5
+ "packageManager": "pnpm@11.0.8",
6
+ "description": "Unified plugin system for build tools",
7
+ "license": "MIT",
8
+ "homepage": "https://unplugin.unjs.io",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/unjs/unplugin.git"
12
+ },
13
+ "sideEffects": false,
14
+ "exports": {
15
+ ".": "./dist/index.mjs",
16
+ "./rspack/loaders/load": "./dist/rspack/loaders/load.mjs",
17
+ "./rspack/loaders/transform": "./dist/rspack/loaders/transform.mjs",
18
+ "./webpack/loaders/load": "./dist/webpack/loaders/load.mjs",
19
+ "./webpack/loaders/transform": "./dist/webpack/loaders/transform.mjs",
20
+ "./package.json": "./package.json"
21
+ },
22
+ "types": "./dist/index.d.mts",
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "engines": {
27
+ "node": "^20.19.0 || >=22.12.0"
28
+ },
29
+ "publishConfig": {
30
+ "tag": "beta"
31
+ },
32
+ "scripts": {
33
+ "build": "tsdown",
34
+ "dev": "tsdown --watch src",
35
+ "lint": "eslint --cache .",
36
+ "lint:fix": "nr lint --fix",
37
+ "typecheck": "tsgo --noEmit",
38
+ "docs:dev": "pnpm -C docs run dev",
39
+ "docs:build": "pnpm -C docs run build",
40
+ "docs:gen-files": "pnpm -C docs run gen-files",
41
+ "prepublishOnly": "nr build",
42
+ "release": "bumpp",
43
+ "test": "nr test:build && vitest run",
44
+ "test:build": "jiti scripts/buildFixtures.ts"
45
+ },
46
+ "dependencies": {
47
+ "@jridgewell/remapping": "catalog:prod",
48
+ "picomatch": "catalog:prod",
49
+ "pify": "catalog:prod",
50
+ "webpack-virtual-modules": "catalog:prod"
51
+ },
52
+ "devDependencies": {
53
+ "@antfu/eslint-config": "catalog:dev",
54
+ "@antfu/ni": "catalog:dev",
55
+ "@arethetypeswrong/core": "catalog:dev",
56
+ "@farmfe/cli": "catalog:test",
57
+ "@farmfe/core": "catalog:peer",
58
+ "@rspack/cli": "catalog:test",
59
+ "@rspack/core": "catalog:peer",
60
+ "@types/node": "catalog:dev",
61
+ "@types/picomatch": "catalog:dev",
62
+ "@typescript/native-preview": "catalog:dev",
63
+ "ansis": "catalog:dev",
64
+ "bumpp": "catalog:dev",
65
+ "bun-types-no-globals": "catalog:dev",
66
+ "esbuild": "catalog:dev",
67
+ "eslint": "catalog:dev",
68
+ "eslint-plugin-format": "catalog:dev",
69
+ "jiti": "catalog:dev",
70
+ "lint-staged": "catalog:dev",
71
+ "magic-string": "catalog:test",
72
+ "publint": "catalog:dev",
73
+ "rolldown": "catalog:peer",
74
+ "rollup": "catalog:peer",
75
+ "simple-git-hooks": "catalog:dev",
76
+ "tsdown": "catalog:dev",
77
+ "typescript": "catalog:dev",
78
+ "unloader": "catalog:peer",
79
+ "unplugin": "workspace:*",
80
+ "unplugin-unused": "catalog:dev",
81
+ "vite": "catalog:peer",
82
+ "vitest": "catalog:test",
83
+ "webpack": "catalog:peer",
84
+ "webpack-cli": "catalog:test"
85
+ },
86
+ "resolutions": {
87
+ "esbuild": "catalog:dev"
88
+ },
89
+ "simple-git-hooks": {
90
+ "pre-commit": "pnpm i --frozen-lockfile --ignore-scripts --offline && pnpm exec lint-staged"
91
+ },
92
+ "lint-staged": {
93
+ "*": "eslint --fix"
94
+ }
95
+ }