@eventuras/vite-config 0.3.0 → 0.4.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/README.md CHANGED
@@ -10,7 +10,35 @@ This package provides reusable Vite configuration presets for different types of
10
10
 
11
11
  - Node.js 24+
12
12
  - Vite 7 or 8 (peer dependency)
13
- - TypeScript 6 in the consuming package the declaration step (`vite-plugin-dts` v5) needs the TypeScript JS Compiler API, which TypeScript 7 no longer ships by default
13
+ - TypeScript 6 or 7 in the consuming package, used to emit declarations (see below)
14
+
15
+ ## Type declarations
16
+
17
+ The presets build JavaScript only. Emit declarations with the TypeScript compiler
18
+ the package already has, **after** `vite build` (Vite empties `dist` first):
19
+
20
+ ```json
21
+ {
22
+ "scripts": {
23
+ "build": "vite build && tsc --emitDeclarationOnly"
24
+ }
25
+ }
26
+ ```
27
+
28
+ With `@eventuras/typescript-config/library.json` or `react-library.json`,
29
+ `outDir` already resolves to the package's own `dist`. Otherwise set
30
+ `"outDir": "dist"` in the package's `tsconfig.json`.
31
+
32
+ Keep test and story files out of the emitted types: the shared base config
33
+ excludes them, but a package `tsconfig.json` that sets its own `exclude` replaces
34
+ that list. Re-add the patterns, or point the build at a `tsconfig.build.json`
35
+ that does (`tsc -p tsconfig.build.json --emitDeclarationOnly`).
36
+
37
+ `tsc` does not rewrite path aliases such as `@/…` in emitted declarations. Use
38
+ relative imports in anything reachable from a public export.
39
+
40
+ `vite build --watch` rebuilds JavaScript only. Run `tsc --emitDeclarationOnly --watch`
41
+ alongside it when you need live types.
14
42
 
15
43
  ## Presets
16
44
 
@@ -52,7 +80,6 @@ export default defineReactLibConfig({
52
80
 
53
81
  **Features:**
54
82
  - React plugin (Babel or SWC)
55
- - TypeScript declaration generation
56
83
  - Optional Tailwind CSS support
57
84
  - 'use client' directive preservation for RSC
58
85
  - Configurable module preservation
@@ -106,10 +133,6 @@ All presets support these options:
106
133
  - **`preserveModules`**: Keep source structure in output (default: `true`)
107
134
  - **`preserveUseClientDirectives`**: Preserve 'use client' for RSC (default: `true`)
108
135
  - **`useSWC`**: Use SWC instead of Babel (default: `false`)
109
- - **`dts`**: TypeScript declaration options
110
- - `entryRoot`: Source root (default: `'src'`)
111
- - `outDir`: Output directory (default: `'dist'`)
112
- - `rollupTypes`: Bundle types into single file (default: `false`)
113
136
 
114
137
  ## Migration Guide
115
138
 
@@ -164,9 +187,8 @@ export default defineReactLibConfig({
164
187
  ## Troubleshooting
165
188
 
166
189
  ### Types not generated
167
- - Check that your TypeScript files are in `src/`
168
- - Ensure test files use `.test.ts` or `.spec.ts` extensions
169
- - Check `dts.entryRoot` and `dts.outDir` options
190
+ - Since 0.4.0 the presets no longer emit declarations. Add `tsc --emitDeclarationOnly` after `vite build` in the package's build script (see [Type declarations](#type-declarations))
191
+ - If `.d.ts` files land outside the package, `outDir` is being resolved relative to a shared config. Upgrade `@eventuras/typescript-config`, or set `outDir` in the package's own `tsconfig.json`
170
192
 
171
193
  ### 'use client' directives missing
172
194
  - Ensure `preserveUseClientDirectives: true` (default for Next.js)
@@ -35,26 +35,6 @@ export interface ReactLibConfig {
35
35
  * @default false
36
36
  */
37
37
  useSWC?: boolean;
38
- /**
39
- * TypeScript declaration options.
40
- */
41
- dts?: {
42
- /**
43
- * Entry root for DTS generation.
44
- * @default 'src'
45
- */
46
- entryRoot?: string;
47
- /**
48
- * Output directory for .d.ts files.
49
- * @default 'dist'
50
- */
51
- outDir?: string;
52
- /**
53
- * Roll up types into a single .d.ts file.
54
- * @default false
55
- */
56
- rollupTypes?: boolean;
57
- };
58
38
  /**
59
39
  * Additional Vite configuration to merge.
60
40
  */
@@ -62,6 +42,9 @@ export interface ReactLibConfig {
62
42
  }
63
43
  /**
64
44
  * Vite configuration preset for React libraries.
65
- * Includes React plugin, TypeScript declarations, and common React externals.
45
+ * Includes React plugin and common React externals.
46
+ *
47
+ * Emits JavaScript only. Declarations come from the package's own compiler:
48
+ * `vite build && tsc --emitDeclarationOnly`.
66
49
  */
67
50
  export declare function defineReactLibConfig(config: ReactLibConfig): UserConfig;
package/dist/react-lib.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { defineConfig } from 'vite';
2
2
  import react from '@vitejs/plugin-react';
3
3
  import tailwindcss from '@tailwindcss/vite';
4
- import dts from 'vite-plugin-dts';
5
4
  import { resolve } from 'node:path';
6
5
  import { glob } from 'glob';
7
6
  import fs from 'node:fs';
@@ -13,6 +12,14 @@ import { getRuntimeDependencyExternals, NODE_BUILTINS_EXTERNAL } from './externa
13
12
  * consumers never need.
14
13
  */
15
14
  const requireFromHere = createRequire(import.meta.url);
15
+ /**
16
+ * Strip leading whitespace and any interleaved mix of line and block comments,
17
+ * so a directive check sees the first real token. Sequential per-style passes
18
+ * miss mixed orders (a line comment, then a block comment, then the directive).
19
+ */
20
+ function stripLeadingComments(code) {
21
+ return code.replace(/^(?:\s+|\/\*[\s\S]*?\*\/\s*|\/\/.*(?:\r?\n|$)\s*)+/, '');
22
+ }
16
23
  /**
17
24
  * Plugin to preserve 'use client' directives in React Server Components.
18
25
  * This ensures client-side code is properly marked when building for Next.js.
@@ -30,18 +37,19 @@ function preserveUseClient() {
30
37
  try {
31
38
  const content = fs.readFileSync(id, 'utf-8');
32
39
  // Remove leading comments to check for 'use client'
33
- const withoutComments = content
34
- .replace(/^(\s*\/\/.*\n)+/, '')
35
- .replace(/^(\s*\/\*[\s\S]*?\*\/\s*)/, '');
36
- return (withoutComments.trimStart().startsWith("'use client'") ||
37
- withoutComments.trimStart().startsWith('"use client"'));
40
+ const withoutComments = stripLeadingComments(content);
41
+ return (withoutComments.startsWith("'use client'") ||
42
+ withoutComments.startsWith('"use client"'));
38
43
  }
39
44
  catch {
40
45
  return false;
41
46
  }
42
47
  });
43
48
  if (hasClientDirective) {
44
- const codeStart = chunkData.code.trimStart();
49
+ // Ignore leading banner/license comments (e.g. Rollup's
50
+ // output.banner) when checking for the directive, so we don't
51
+ // prepend a duplicate above a directive that's already there.
52
+ const codeStart = stripLeadingComments(chunkData.code);
45
53
  if (!codeStart.startsWith("'use client'") && !codeStart.startsWith('"use client"')) {
46
54
  chunkData.code = `'use client';\n${chunkData.code}`;
47
55
  }
@@ -53,10 +61,13 @@ function preserveUseClient() {
53
61
  }
54
62
  /**
55
63
  * Vite configuration preset for React libraries.
56
- * Includes React plugin, TypeScript declarations, and common React externals.
64
+ * Includes React plugin and common React externals.
65
+ *
66
+ * Emits JavaScript only. Declarations come from the package's own compiler:
67
+ * `vite build && tsc --emitDeclarationOnly`.
57
68
  */
58
69
  export function defineReactLibConfig(config) {
59
- const { entry, external, tailwind = false, preserveModules = true, preserveUseClientDirectives = true, useSWC = false, dts: dtsOptions = {}, viteConfig = {}, } = config;
70
+ const { entry, external, tailwind = false, preserveModules = true, preserveUseClientDirectives = true, useSWC = false, viteConfig = {}, } = config;
60
71
  const autoExternals = external === false ? [] : getRuntimeDependencyExternals();
61
72
  const userExternals = Array.isArray(external) ? external : [];
62
73
  // Determine entry points
@@ -86,17 +97,6 @@ export function defineReactLibConfig(config) {
86
97
  if (tailwind) {
87
98
  plugins.push(tailwindcss());
88
99
  }
89
- // Add DTS plugin
90
- plugins.push(dts({
91
- entryRoot: dtsOptions.entryRoot || 'src',
92
- // vite-plugin-dts v5 renamed `outDir` -> `outDirs` and
93
- // `rollupTypes` -> `bundleTypes`; the preset keeps the old names.
94
- outDirs: dtsOptions.outDir || 'dist',
95
- include: ['src/**/*'],
96
- exclude: ['**/*.test.ts', '**/*.test.tsx', '**/*.spec.ts', '**/*.spec.tsx', '**/*.stories.tsx'],
97
- copyDtsFiles: true,
98
- bundleTypes: dtsOptions.rollupTypes || false,
99
- }));
100
100
  // Add use client preservation if enabled
101
101
  if (preserveUseClientDirectives) {
102
102
  plugins.push(preserveUseClient());
@@ -31,5 +31,8 @@ export interface VanillaLibConfig {
31
31
  * - Minification is OFF — consumers minify their own bundle, and unminified
32
32
  * output preserves class names (so `instanceof` and stack traces work).
33
33
  * - Sourcemaps ON for debuggable consumer stack traces.
34
+ *
35
+ * Emits JavaScript only. Declarations come from the package's own compiler:
36
+ * `vite build && tsc --emitDeclarationOnly`.
34
37
  */
35
38
  export declare function defineVanillaLibConfig(config: VanillaLibConfig): UserConfig;
@@ -1,5 +1,4 @@
1
1
  import { defineConfig } from 'vite';
2
- import dts from 'vite-plugin-dts';
3
2
  import { resolve } from 'node:path';
4
3
  import { getRuntimeDependencyExternals, NODE_BUILTINS_EXTERNAL } from './externals.js';
5
4
  /**
@@ -12,19 +11,16 @@ import { getRuntimeDependencyExternals, NODE_BUILTINS_EXTERNAL } from './externa
12
11
  * - Minification is OFF — consumers minify their own bundle, and unminified
13
12
  * output preserves class names (so `instanceof` and stack traces work).
14
13
  * - Sourcemaps ON for debuggable consumer stack traces.
14
+ *
15
+ * Emits JavaScript only. Declarations come from the package's own compiler:
16
+ * `vite build && tsc --emitDeclarationOnly`.
15
17
  */
16
18
  export function defineVanillaLibConfig(config) {
17
19
  const { entry, name, external, viteConfig = {} } = config;
18
20
  const autoExternals = external === false ? [] : getRuntimeDependencyExternals();
19
21
  const userExternals = Array.isArray(external) ? external : [];
20
22
  return defineConfig({
21
- plugins: [
22
- dts({
23
- include: ['src/**/*'],
24
- exclude: ['**/*.test.ts', '**/*.test.tsx', '**/*.spec.ts', '**/*.spec.tsx'],
25
- }),
26
- ...(viteConfig.plugins || []),
27
- ],
23
+ plugins: [...(viteConfig.plugins || [])],
28
24
  build: {
29
25
  minify: false,
30
26
  sourcemap: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eventuras/vite-config",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,14 +33,13 @@
33
33
  "@tailwindcss/vite": "^4.3.2",
34
34
  "@vitejs/plugin-react": "^6.0.3",
35
35
  "@vitejs/plugin-react-swc": "^4.3.0",
36
- "glob": "^13.0.6",
37
- "vite-plugin-dts": "^5.0.3"
36
+ "glob": "^13.0.6"
38
37
  },
39
38
  "devDependencies": {
40
39
  "@types/node": "^24.12.0",
41
40
  "typescript": "^6.0.2",
42
41
  "vite": "^8.1.0",
43
- "@eventuras/typescript-config": "1.0.0"
42
+ "@eventuras/typescript-config": "1.1.0"
44
43
  },
45
44
  "peerDependencies": {
46
45
  "vite": "^7.0.0 || ^8.0.0"