@budsbox/builder_vite 1.0.0 → 2.0.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/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { createBaseConfig, formatVarName, formatFileName } from './lib.js';
1
+ export { createConfigFactory, formatFileName, formatVarName } from './lib.js';
2
2
  export { usePlainConfig } from './plain.js';
3
3
  export { useReactConfig } from './react.js';
4
4
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
- export { createBaseConfig, formatVarName, formatFileName } from './lib.js';
1
+ // Value exports
2
+ export { createConfigFactory, formatFileName, formatVarName } from './lib.js';
2
3
  export { usePlainConfig } from './plain.js';
3
4
  export { useReactConfig } from './react.js';
4
5
  //# sourceMappingURL=index.js.map
package/dist/lib.d.ts CHANGED
@@ -1,16 +1,45 @@
1
- import { type LibraryOptions, type UserConfigExport, type UserConfigFnPromise } from 'vite';
1
+ import type { Awaitable, Undef } from '@budsbox/lib-types';
2
+ import { type ConfigEnv, type LibraryOptions, type UserConfig, type UserConfigExport, type UserConfigFnPromise } from 'vite';
2
3
  /**
3
- * Creates a base configuration function that merges a provided base configuration
4
- * with a custom configuration. The result is a function that resolves and merges
5
- * both configurations based on the given environment.
4
+ * Represents a custom user configuration function used to generate Vite configuration.
6
5
  *
7
- * @param base - The base configuration, which can either be a `UserConfigExport` object
8
- * or a function that resolves to a `UserConfigExport` object based on the provided environment.
9
- * @returns A function that accepts a custom configuration of type `UserConfigExport`
10
- * and returns a promise-based configuration function (`UserConfigFnPromise`) that resolves
11
- * to a merged configuration.
6
+ * @typeParam TOptions - The type of the custom options object, with a default to `object`.
7
+ * @param viteEnv - A readonly object representing the Vite configuration environment.
8
+ * @param options - An optional readonly object representing additional custom configuration options.
9
+ * @returns A promise or value containing the generated Vite user configuration.
12
10
  */
13
- export declare function createBaseConfig(base: UserConfigExport): (custom?: UserConfigExport) => UserConfigFnPromise;
11
+ export type CustomUserConfigFn<TOptions extends object = object> = (viteEnv: Readonly<ConfigEnv>, options?: Undef<Readonly<TOptions>>) => Awaitable<UserConfig>;
12
+ /**
13
+ * A `ConfigFactory` is a function type that generates an asynchronous Vite's configuration object generator function.
14
+ *
15
+ * @param custom - An optional custom configuration of type `UserConfigExport`. It can be used
16
+ * to override or extend default configurations.
17
+ * @returns A function to be invoked by Vite to generate the final configuration object.
18
+ */
19
+ export type ConfigFactory = (custom?: UserConfigExport) => UserConfigFnPromise;
20
+ /**
21
+ * A `ConfigFactory` is a function type that generates an asynchronous Vite's configuration object generator function.
22
+ *
23
+ * @param custom - An optional custom configuration of type `UserConfigExport`. It can be used
24
+ * to override or extend default configurations.
25
+ * @param options - An optional object containing additional configuration options, specific to the factory.
26
+ * @returns A function to be invoked by Vite to generate the final configuration object.
27
+ */
28
+ export type ConfigFactoryWithOptions<TOptions extends object> = (custom?: UserConfigExport, options?: Readonly<Partial<TOptions>>) => UserConfigFnPromise;
29
+ /**
30
+ * Creates a configuration factory that extends or modifies a base configuration.
31
+ *
32
+ * @param base - A custom user configuration function that defines the base options.
33
+ * @returns A configuration factory function that accepts a custom config and an options object to customize or extend the base configuration.
34
+ */
35
+ export declare function createConfigFactory<TOptions extends object>(base: CustomUserConfigFn<TOptions>): ConfigFactoryWithOptions<TOptions>;
36
+ /**
37
+ * Creates a configuration factory function that merges a base configuration with a custom configuration.
38
+ *
39
+ * @param base - The base configuration to be used as the default.
40
+ * @returns A function that accepts an optional custom configuration and returns a promise resolving to the merged configuration function.
41
+ */
42
+ export declare function createConfigFactory(base: UserConfigExport): (custom?: UserConfigExport) => UserConfigFnPromise;
14
43
  /**
15
44
  * Formats a package name into a standardized string format, suitable for use as a variable name.
16
45
  *
package/dist/lib.js CHANGED
@@ -2,20 +2,9 @@ import { mergeConfig, } from 'vite';
2
2
  import { isFunction, isNotNil } from '@budsbox/lib-es/guards';
3
3
  import { fif } from '@budsbox/lib-es/logical';
4
4
  import { camelCase, parsePackageName } from '@budsbox/lib-es/string';
5
- /**
6
- * Creates a base configuration function that merges a provided base configuration
7
- * with a custom configuration. The result is a function that resolves and merges
8
- * both configurations based on the given environment.
9
- *
10
- * @param base - The base configuration, which can either be a `UserConfigExport` object
11
- * or a function that resolves to a `UserConfigExport` object based on the provided environment.
12
- * @returns A function that accepts a custom configuration of type `UserConfigExport`
13
- * and returns a promise-based configuration function (`UserConfigFnPromise`) that resolves
14
- * to a merged configuration.
15
- */
16
- export function createBaseConfig(base) {
17
- return (custom) => async (env) => {
18
- const resolvedBase = await fif(await base, isFunction, (baseFn) => baseFn(env), (baseObj) => baseObj);
5
+ export function createConfigFactory(base) {
6
+ return (custom, options) => async (env) => {
7
+ const resolvedBase = await fif(await base, isFunction, (baseFn) => baseFn(env, options), (baseObj) => baseObj);
19
8
  const resolvedCustom = await fif(await custom, isFunction, (customFn) => customFn(env), (customObj) => customObj);
20
9
  return mergeConfig(resolvedBase, resolvedCustom ?? {});
21
10
  };
package/dist/plain.d.ts CHANGED
@@ -1,2 +1,25 @@
1
- export declare const usePlainConfig: (custom?: import("vite").UserConfigExport) => import("vite").UserConfigFnPromise;
1
+ /**
2
+ * Represents configuration options for plain configurations.
3
+ */
4
+ export interface PlainConfigOptions {
5
+ /**
6
+ * The `import.meta` object.
7
+ */
8
+ importMeta?: ImportMeta;
9
+ /**
10
+ * Whether the configuration is for a library.
11
+ */
12
+ lib?: boolean;
13
+ /**
14
+ * An array of strings representing the chunks to exclude from the path part when generating scoped names for CSS modules.
15
+ *
16
+ * @default ['src']
17
+ */
18
+ generateScopedNameExcludedPathChunks?: readonly string[];
19
+ /**
20
+ * Whether to add the scope of the package to the conditions for module `exports` resolution.
21
+ */
22
+ addScopeToConditions?: boolean;
23
+ }
24
+ export declare const usePlainConfig: import("./lib.js").ConfigFactoryWithOptions<PlainConfigOptions>;
2
25
  //# sourceMappingURL=plain.d.ts.map
package/dist/plain.js CHANGED
@@ -1,21 +1,104 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { basename, dirname, relative } from 'node:path';
3
+ import cssesc from 'cssesc';
1
4
  import { NodePackageImporter } from 'sass-embedded';
2
- import { createBaseConfig } from './lib.js';
3
- export const usePlainConfig = createBaseConfig(({ mode }) => ({
4
- root: 'src',
5
- build: {
6
- outDir: '../dist',
7
- emptyOutDir: true,
8
- },
9
- css: {
10
- modules: {
11
- localsConvention: 'camelCaseOnly',
12
- generateScopedName: mode === 'production' ? '[hash:hex]' : '[path][name]__[local]',
5
+ import { defaultClientConditions } from 'vite';
6
+ import { createCachedFn } from '@budsbox/lib-es/function';
7
+ import { isNil, isNotNil, isString } from '@budsbox/lib-es/guards';
8
+ import { fifs } from '@budsbox/lib-es/logical';
9
+ import { parsePackageName, splitPath } from '@budsbox/lib-es/string';
10
+ import { lookupFileSync } from '@budsbox/lib-node/fs';
11
+ import { findCurrentPackageJson } from '@budsbox/lib-node/pckg';
12
+ import packageJson from '#package.json' with { type: 'json' };
13
+ import { createConfigFactory, formatFileName, formatVarName } from './lib.js';
14
+ const readPackageJson = createCachedFn((path) => {
15
+ if (!path.endsWith('package.json'))
16
+ throw new Error(`Not a package.json path: ${path}`);
17
+ return JSON.parse(readFileSync(path, 'utf-8'));
18
+ });
19
+ export const usePlainConfig = createConfigFactory(async ({ mode }, { importMeta, lib = false, generateScopedNameExcludedPathChunks = ['src'], addScopeToConditions = false, } = {}) => {
20
+ const runnerPackage = await findCurrentPackageJson(importMeta);
21
+ const { scope: runnerScope } = parsePackageName(runnerPackage.json.name ?? 'anon', true);
22
+ const lookupCache = new Map();
23
+ const packageCache = new Map();
24
+ const excludeChunks = new Set(generateScopedNameExcludedPathChunks);
25
+ const clientConditions = ((addScopeToConditions ||
26
+ // enabled by default in this monorepo
27
+ sameScope(packageJson, runnerPackage.json)) &&
28
+ isNotNil(runnerScope)) ?
29
+ [runnerScope, ...defaultClientConditions]
30
+ : [...defaultClientConditions];
31
+ const generateScopedName = createCachedFn((localName, filepath) => {
32
+ const foundPath = lookupFileSync({
33
+ startDir: dirname(filepath),
34
+ filename: 'package.json',
35
+ cache: lookupCache,
36
+ });
37
+ let pckgPrefix = '';
38
+ if (isString(foundPath)) {
39
+ if (foundPath !== runnerPackage.path) {
40
+ const pckg = readPackageJson(packageCache, foundPath);
41
+ const { scope, name } = parsePackageName(pckg.name ?? '', true);
42
+ pckgPrefix =
43
+ scope === runnerScope || isNil(scope) ?
44
+ name
45
+ : `${scope}_-_${name}`;
46
+ }
47
+ }
48
+ else {
49
+ pckgPrefix = '-standalone-';
50
+ }
51
+ const subPath = relative(dirname(foundPath ?? runnerPackage.path), filepath);
52
+ const pathPart = splitPath(dirname(subPath))
53
+ .map((chunk) => (chunk === '..' ? '_--_' : chunk))
54
+ .filter((chunk) => !excludeChunks.has(chunk))
55
+ .join('-');
56
+ const nameChunks = basename(filepath).split('.').slice(0, -1);
57
+ if (nameChunks.at(-1) === 'module') {
58
+ nameChunks.pop();
59
+ }
60
+ if (nameChunks.at(-1) === 'style') {
61
+ nameChunks.pop();
62
+ }
63
+ return cssesc([
64
+ pckgPrefix,
65
+ [pathPart, nameChunks.join('-')].filter(Boolean).join('_'),
66
+ localName,
67
+ ]
68
+ .filter(Boolean)
69
+ .join('__'));
70
+ }, (...args) => args.join(':'));
71
+ const scopedNameCache = new Map();
72
+ return {
73
+ root: 'src',
74
+ build: {
75
+ outDir: '../dist',
76
+ emptyOutDir: true,
77
+ ...fifs(lib, {
78
+ lib: {
79
+ entry: 'index.ts',
80
+ name: formatVarName(runnerPackage.json.name ?? '_anon_'),
81
+ fileName: formatFileName,
82
+ cssFileName: 'index',
83
+ },
84
+ }),
13
85
  },
14
- preprocessorOptions: {
15
- scss: {
16
- importers: [new NodePackageImporter()],
86
+ resolve: {
87
+ conditions: clientConditions,
88
+ },
89
+ css: {
90
+ modules: {
91
+ localsConvention: 'camelCaseOnly',
92
+ generateScopedName: mode === 'production' ? '[hash:hex]' : ((localName, filepath) => generateScopedName(scopedNameCache, localName, filepath)),
93
+ },
94
+ preprocessorOptions: {
95
+ scss: {
96
+ importers: [new NodePackageImporter()],
97
+ },
17
98
  },
18
99
  },
19
- },
20
- }));
100
+ };
101
+ });
102
+ const sameScope = (p1, p2) => parsePackageName(p1.name ?? '', true).scope ===
103
+ parsePackageName(p2.name ?? '', true).scope;
21
104
  //# sourceMappingURL=plain.js.map
package/dist/react.d.ts CHANGED
@@ -1,2 +1,8 @@
1
- export declare const useReactConfig: (custom?: import("vite").UserConfigExport) => import("vite").UserConfigFnPromise;
1
+ import { type PlainConfigOptions } from './plain.js';
2
+ /**
3
+ * Represents configuration options for Vite+React configurations.
4
+ */
5
+ export interface ReactConfigOptions extends PlainConfigOptions {
6
+ }
7
+ export declare const useReactConfig: import("./lib.js").ConfigFactoryWithOptions<ReactConfigOptions>;
2
8
  //# sourceMappingURL=react.d.ts.map
package/dist/react.js CHANGED
@@ -1,13 +1,31 @@
1
1
  import react from '@vitejs/plugin-react';
2
- import { createBaseConfig } from './lib.js';
2
+ import { isTrue } from '@budsbox/lib-es/guards';
3
+ import { fif } from '@budsbox/lib-es/logical';
4
+ import { createConfigFactory } from './lib.js';
3
5
  import { usePlainConfig } from './plain.js';
4
- export const useReactConfig = createBaseConfig(usePlainConfig(() => ({
5
- plugins: [
6
- react({
7
- babel: {
8
- plugins: ['babel-plugin-react-compiler'],
6
+ export const useReactConfig = createConfigFactory((env, options) => usePlainConfig(() => {
7
+ const libConfig = fif(options?.lib, isTrue, {
8
+ build: {
9
+ rollupOptions: {
10
+ external: ['react', 'react-dom'],
11
+ output: {
12
+ globals: {
13
+ 'react': 'React',
14
+ 'react-dom': 'ReactDOM',
15
+ },
16
+ },
9
17
  },
10
- }),
11
- ],
12
- })));
18
+ },
19
+ });
20
+ return {
21
+ plugins: [
22
+ react({
23
+ babel: {
24
+ plugins: ['babel-plugin-react-compiler'],
25
+ },
26
+ }),
27
+ ],
28
+ ...libConfig,
29
+ };
30
+ }, options)(env));
13
31
  //# sourceMappingURL=react.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@budsbox/builder_vite",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "homepage": "https://gitlab.com/budsbox/fe/seed",
5
5
  "bugs": {
6
6
  "url": "https://gitlab.com/budsbox/fe/seed/-/issues"
@@ -28,22 +28,25 @@
28
28
  "prepack": "yarn p:ts:prepack"
29
29
  },
30
30
  "dependencies": {
31
- "@budsbox/lib-es": "^2.2.0",
32
- "tslib": "^2.8.1"
31
+ "@budsbox/lib-es": "^2.3.0",
32
+ "@budsbox/lib-node": "^1.1.0",
33
+ "@types/node": "^22.15.2",
34
+ "cssesc": "^3.0.0",
35
+ "tslib": "^2.8.1",
36
+ "type-fest": "^4.32.0"
33
37
  },
34
38
  "devDependencies": {
35
39
  "@budsbox/eslint": "^1.2.0",
36
- "@budsbox/eslint_presets-node-lib": "^1.0.3",
37
- "@budsbox/eslint_presets-tools": "^1.0.3",
38
- "@budsbox/lib-types": "^1.1.0",
39
- "@budsbox/tsconfigs": "^4.3.0",
40
+ "@budsbox/eslint_presets-node-lib": "^1.0.4",
41
+ "@budsbox/eslint_presets-tools": "^1.0.4",
42
+ "@budsbox/lib-types": "^1.2.0",
43
+ "@budsbox/tsconfigs": "^4.3.1",
44
+ "@types/cssesc": "^3",
40
45
  "@types/eslint": "^9.6.1",
41
- "@types/node": "^22.15.2",
42
46
  "@vitejs/plugin-react": "^5.0.3",
43
47
  "babel-plugin-react-compiler": "^19.1.0-rc.3",
44
48
  "eslint": "^9.26.0",
45
49
  "sass-embedded": "^1.92.1",
46
- "type-fest": "^4.32.0",
47
50
  "typescript": "^5.8.3",
48
51
  "vite": "^7.1.5"
49
52
  },