@hzab/vite-config 0.0.2-alpha.0 → 0.0.3-alpha.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.
- package/CHANGELOG.md +95 -83
- package/LICENSE +21 -21
- package/README.md +184 -173
- package/dist/create-app-vite-config.cjs +160 -27
- package/dist/create-app-vite-config.cjs.map +1 -1
- package/dist/create-app-vite-config.js +160 -27
- package/dist/create-app-vite-config.js.map +1 -1
- package/dist/create-library-vite-config.cjs +39 -4
- package/dist/create-library-vite-config.cjs.map +1 -1
- package/dist/create-library-vite-config.js +38 -3
- package/dist/create-library-vite-config.js.map +1 -1
- package/dist/create-package-vitest-config.cjs.map +1 -1
- package/dist/create-package-vitest-config.js.map +1 -1
- package/dist/create-vite-config.cjs +160 -27
- package/dist/create-vite-config.cjs.map +1 -1
- package/dist/create-vite-config.js +160 -27
- package/dist/create-vite-config.js.map +1 -1
- package/dist/index.cjs +198 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +199 -31
- package/dist/index.js.map +1 -1
- package/dist/plugins/copy.cjs.map +1 -1
- package/dist/plugins/copy.js.map +1 -1
- package/dist/plugins/hash-deploy.cjs +150 -25
- package/dist/plugins/hash-deploy.cjs.map +1 -1
- package/dist/plugins/hash-deploy.d.cts +16 -5
- package/dist/plugins/hash-deploy.d.ts +16 -5
- package/dist/plugins/hash-deploy.js +150 -25
- package/dist/plugins/hash-deploy.js.map +1 -1
- package/dist/plugins/less-tilde.cjs.map +1 -1
- package/dist/plugins/less-tilde.d.cts +17 -2
- package/dist/plugins/less-tilde.d.ts +17 -2
- package/dist/plugins/less-tilde.js.map +1 -1
- package/dist/plugins/lifecycle.cjs.map +1 -1
- package/dist/plugins/lifecycle.js.map +1 -1
- package/dist/plugins/public-assets.cjs.map +1 -1
- package/dist/plugins/public-assets.js.map +1 -1
- package/dist/plugins/public-config.cjs.map +1 -1
- package/dist/plugins/public-config.js.map +1 -1
- package/dist/plugins/react-compiler.cjs.map +1 -1
- package/dist/plugins/react-compiler.js.map +1 -1
- package/dist/plugins/relative-base.cjs.map +1 -1
- package/dist/plugins/relative-base.js.map +1 -1
- package/dist/plugins/stub-less.cjs.map +1 -1
- package/dist/plugins/stub-less.js.map +1 -1
- package/dist/plugins/svg-react.cjs.map +1 -1
- package/dist/plugins/svg-react.js.map +1 -1
- package/dist/types.d.cts +19 -8
- package/dist/types.d.ts +19 -8
- package/dist/vitest.cjs.map +1 -1
- package/dist/vitest.js.map +1 -1
- package/docs/README.md +15 -15
- package/docs/abt-management-ui/vite.config.mts +226 -226
- package/docs/api.md +404 -366
- package/docs/migration-from-webpack.md +723 -722
- package/docs/sync-public-path.md +52 -52
- package/docs/vite-config-integration.md +477 -476
- package/docs/vite-pnpm-integration.md +279 -279
- package/docs/vs-ccc-vite-config.md +26 -26
- package/docs/webpack-gap.md +108 -107
- package/package.json +1 -1
- package/src/client.d.ts +9 -9
- package/src/create-app-vite-config.ts +236 -218
- package/src/create-library-vite-config.ts +248 -199
- package/src/create-package-vitest-config.ts +54 -54
- package/src/create-vite-config.ts +22 -22
- package/src/index.ts +41 -42
- package/src/plugins/copy.ts +25 -25
- package/src/plugins/hash-deploy.ts +239 -199
- package/src/plugins/hash-dir-name.ts +212 -0
- package/src/plugins/less-tilde.ts +72 -44
- package/src/plugins/lifecycle.ts +40 -40
- package/src/plugins/public-assets.ts +28 -28
- package/src/plugins/public-config.ts +34 -34
- package/src/plugins/react-compiler.ts +32 -32
- package/src/plugins/relative-base.ts +20 -20
- package/src/plugins/stub-less.ts +26 -26
- package/src/plugins/svg-react.ts +90 -90
- package/src/svg-react-runtime.ts +4 -4
- package/src/svg-react.d.ts +6 -6
- package/src/types.ts +325 -314
- package/src/vitest-types.ts +20 -20
- package/src/vitest.ts +2 -2
- package/templates/src/vite-env.d.ts +2 -2
- package/templates/tsconfig.json +12 -12
- package/templates/vite.config.ts +26 -26
package/src/plugins/stub-less.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import type { Plugin } from "vite";
|
|
2
|
-
|
|
3
|
-
const cssModuleStub = `
|
|
4
|
-
const styles = new Proxy({}, {
|
|
5
|
-
get: (_target, property) => typeof property === "string" ? property : undefined,
|
|
6
|
-
});
|
|
7
|
-
export default styles;
|
|
8
|
-
`;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 单测不解析 less,并为 CSS Modules 提供 identity stub
|
|
12
|
-
* @public
|
|
13
|
-
*/
|
|
14
|
-
export function stubLessPlugin(): Plugin {
|
|
15
|
-
return {
|
|
16
|
-
name: "stub-less",
|
|
17
|
-
load(id) {
|
|
18
|
-
if (/\.module\.less(?:$|\?)/.test(id)) {
|
|
19
|
-
return cssModuleStub;
|
|
20
|
-
}
|
|
21
|
-
if (/\.less(?:$|\?)/.test(id)) {
|
|
22
|
-
return "";
|
|
23
|
-
}
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
}
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
|
|
3
|
+
const cssModuleStub = `
|
|
4
|
+
const styles = new Proxy({}, {
|
|
5
|
+
get: (_target, property) => typeof property === "string" ? property : undefined,
|
|
6
|
+
});
|
|
7
|
+
export default styles;
|
|
8
|
+
`;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 单测不解析 less,并为 CSS Modules 提供 identity stub
|
|
12
|
+
* @public
|
|
13
|
+
*/
|
|
14
|
+
export function stubLessPlugin(): Plugin {
|
|
15
|
+
return {
|
|
16
|
+
name: "stub-less",
|
|
17
|
+
load(id) {
|
|
18
|
+
if (/\.module\.less(?:$|\?)/.test(id)) {
|
|
19
|
+
return cssModuleStub;
|
|
20
|
+
}
|
|
21
|
+
if (/\.less(?:$|\?)/.test(id)) {
|
|
22
|
+
return "";
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
package/src/plugins/svg-react.ts
CHANGED
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
import fs from "node:fs/promises";
|
|
2
|
-
import { transformWithOxc } from "vite";
|
|
3
|
-
|
|
4
|
-
import type { Config } from "@svgr/core";
|
|
5
|
-
import type { Plugin } from "vite";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @see https://react-svgr.com/docs/options/
|
|
9
|
-
* @public
|
|
10
|
-
*/
|
|
11
|
-
export interface SvgReactOptions {
|
|
12
|
-
/** @defaultValue true */
|
|
13
|
-
svgo?: boolean;
|
|
14
|
-
icon?: boolean;
|
|
15
|
-
[key: string]: unknown;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const SVG_EXT_RE = /\.svg$/i;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* `*.svg?react`(及带其它 query 时的 `react` 段)。
|
|
22
|
-
* 不匹配 `?reactive` / webpack 的 `?svgEle`。
|
|
23
|
-
*/
|
|
24
|
-
export function isSvgReactId(id: string): boolean {
|
|
25
|
-
const queryStart = id.indexOf("?");
|
|
26
|
-
if (queryStart === -1) return false;
|
|
27
|
-
const file = id.slice(0, queryStart);
|
|
28
|
-
if (!SVG_EXT_RE.test(file)) return false;
|
|
29
|
-
return /(?:^|&)react(?:&|$)/.test(id.slice(queryStart + 1));
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function missingPeerError(pkg: string, cause: unknown): Error {
|
|
33
|
-
const error = new Error(
|
|
34
|
-
`[svg-react] "${pkg}" is required to transform *.svg?react. Install the optional peer or disable CreateAppViteConfigOptions.svgr.`,
|
|
35
|
-
);
|
|
36
|
-
error.cause = cause;
|
|
37
|
-
return error;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async function resolveSvgrConfig(options: SvgReactOptions): Promise<Config> {
|
|
41
|
-
const merged: Config = { svgo: true, ...options };
|
|
42
|
-
if (merged.plugins != null) return merged;
|
|
43
|
-
|
|
44
|
-
const plugins: NonNullable<Config["plugins"]> = [];
|
|
45
|
-
if (merged.svgo !== false) {
|
|
46
|
-
try {
|
|
47
|
-
plugins.push((await import("@svgr/plugin-svgo")).default);
|
|
48
|
-
} catch (cause) {
|
|
49
|
-
throw missingPeerError("@svgr/plugin-svgo", cause);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
try {
|
|
53
|
-
plugins.push((await import("@svgr/plugin-jsx")).default);
|
|
54
|
-
} catch (cause) {
|
|
55
|
-
throw missingPeerError("@svgr/plugin-jsx", cause);
|
|
56
|
-
}
|
|
57
|
-
return { ...merged, plugins };
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* 将 `import Icon from "./icon.svg?react"` 编成 React 组件。
|
|
62
|
-
* 普通 `*.svg` 仍走 Vite 默认资源 URL,本插件不处理。
|
|
63
|
-
*
|
|
64
|
-
* `@svgr/core` / `@svgr/plugin-jsx`(以及默认开启 SVGO 时的 `@svgr/plugin-svgo`)
|
|
65
|
-
* 仅在真正命中 `?react` 时动态加载,根入口不会静态依赖它们。
|
|
66
|
-
* @public
|
|
67
|
-
*/
|
|
68
|
-
export function svgReactPlugin(options: SvgReactOptions = {}): Plugin {
|
|
69
|
-
return {
|
|
70
|
-
name: "ccc:svg-react",
|
|
71
|
-
enforce: "pre",
|
|
72
|
-
async load(id) {
|
|
73
|
-
if (!isSvgReactId(id)) return;
|
|
74
|
-
|
|
75
|
-
const filePath = id.slice(0, id.indexOf("?"));
|
|
76
|
-
const svg = await fs.readFile(filePath, "utf8");
|
|
77
|
-
let transform;
|
|
78
|
-
try {
|
|
79
|
-
({ transform } = await import("@svgr/core"));
|
|
80
|
-
} catch (cause) {
|
|
81
|
-
throw missingPeerError("@svgr/core", cause);
|
|
82
|
-
}
|
|
83
|
-
const componentCode = await transform(svg, await resolveSvgrConfig(options), { filePath });
|
|
84
|
-
const result = await transformWithOxc(componentCode, `${filePath}.jsx`, {
|
|
85
|
-
lang: "jsx",
|
|
86
|
-
});
|
|
87
|
-
return { code: result.code, map: result.map };
|
|
88
|
-
},
|
|
89
|
-
};
|
|
90
|
-
}
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { transformWithOxc } from "vite";
|
|
3
|
+
|
|
4
|
+
import type { Config } from "@svgr/core";
|
|
5
|
+
import type { Plugin } from "vite";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @see https://react-svgr.com/docs/options/
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
export interface SvgReactOptions {
|
|
12
|
+
/** @defaultValue true */
|
|
13
|
+
svgo?: boolean;
|
|
14
|
+
icon?: boolean;
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const SVG_EXT_RE = /\.svg$/i;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* `*.svg?react`(及带其它 query 时的 `react` 段)。
|
|
22
|
+
* 不匹配 `?reactive` / webpack 的 `?svgEle`。
|
|
23
|
+
*/
|
|
24
|
+
export function isSvgReactId(id: string): boolean {
|
|
25
|
+
const queryStart = id.indexOf("?");
|
|
26
|
+
if (queryStart === -1) return false;
|
|
27
|
+
const file = id.slice(0, queryStart);
|
|
28
|
+
if (!SVG_EXT_RE.test(file)) return false;
|
|
29
|
+
return /(?:^|&)react(?:&|$)/.test(id.slice(queryStart + 1));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function missingPeerError(pkg: string, cause: unknown): Error {
|
|
33
|
+
const error = new Error(
|
|
34
|
+
`[svg-react] "${pkg}" is required to transform *.svg?react. Install the optional peer or disable CreateAppViteConfigOptions.svgr.`,
|
|
35
|
+
);
|
|
36
|
+
error.cause = cause;
|
|
37
|
+
return error;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async function resolveSvgrConfig(options: SvgReactOptions): Promise<Config> {
|
|
41
|
+
const merged: Config = { svgo: true, ...options };
|
|
42
|
+
if (merged.plugins != null) return merged;
|
|
43
|
+
|
|
44
|
+
const plugins: NonNullable<Config["plugins"]> = [];
|
|
45
|
+
if (merged.svgo !== false) {
|
|
46
|
+
try {
|
|
47
|
+
plugins.push((await import("@svgr/plugin-svgo")).default);
|
|
48
|
+
} catch (cause) {
|
|
49
|
+
throw missingPeerError("@svgr/plugin-svgo", cause);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
try {
|
|
53
|
+
plugins.push((await import("@svgr/plugin-jsx")).default);
|
|
54
|
+
} catch (cause) {
|
|
55
|
+
throw missingPeerError("@svgr/plugin-jsx", cause);
|
|
56
|
+
}
|
|
57
|
+
return { ...merged, plugins };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 将 `import Icon from "./icon.svg?react"` 编成 React 组件。
|
|
62
|
+
* 普通 `*.svg` 仍走 Vite 默认资源 URL,本插件不处理。
|
|
63
|
+
*
|
|
64
|
+
* `@svgr/core` / `@svgr/plugin-jsx`(以及默认开启 SVGO 时的 `@svgr/plugin-svgo`)
|
|
65
|
+
* 仅在真正命中 `?react` 时动态加载,根入口不会静态依赖它们。
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
export function svgReactPlugin(options: SvgReactOptions = {}): Plugin {
|
|
69
|
+
return {
|
|
70
|
+
name: "ccc:svg-react",
|
|
71
|
+
enforce: "pre",
|
|
72
|
+
async load(id) {
|
|
73
|
+
if (!isSvgReactId(id)) return;
|
|
74
|
+
|
|
75
|
+
const filePath = id.slice(0, id.indexOf("?"));
|
|
76
|
+
const svg = await fs.readFile(filePath, "utf8");
|
|
77
|
+
let transform;
|
|
78
|
+
try {
|
|
79
|
+
({ transform } = await import("@svgr/core"));
|
|
80
|
+
} catch (cause) {
|
|
81
|
+
throw missingPeerError("@svgr/core", cause);
|
|
82
|
+
}
|
|
83
|
+
const componentCode = await transform(svg, await resolveSvgrConfig(options), { filePath });
|
|
84
|
+
const result = await transformWithOxc(componentCode, `${filePath}.jsx`, {
|
|
85
|
+
lang: "jsx",
|
|
86
|
+
});
|
|
87
|
+
return { code: result.code, map: result.map };
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
package/src/svg-react-runtime.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Runtime counterpart of the `@hzab/vite-config/svg-react` types entry.
|
|
2
|
-
// The ambient types for `*.svg?react` live in `src/svg-react.d.ts`; this module
|
|
3
|
-
// intentionally exports nothing and exists only so the subpath resolves at runtime.
|
|
4
|
-
export {};
|
|
1
|
+
// Runtime counterpart of the `@hzab/vite-config/svg-react` types entry.
|
|
2
|
+
// The ambient types for `*.svg?react` live in `src/svg-react.d.ts`; this module
|
|
3
|
+
// intentionally exports nothing and exists only so the subpath resolves at runtime.
|
|
4
|
+
export {};
|
package/src/svg-react.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
declare module "*.svg?react" {
|
|
2
|
-
import type { FunctionComponent, SVGProps } from "react";
|
|
3
|
-
|
|
4
|
-
const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement>>;
|
|
5
|
-
export default ReactComponent;
|
|
6
|
-
}
|
|
1
|
+
declare module "*.svg?react" {
|
|
2
|
+
import type { FunctionComponent, SVGProps } from "react";
|
|
3
|
+
|
|
4
|
+
const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement>>;
|
|
5
|
+
export default ReactComponent;
|
|
6
|
+
}
|