diff --git a/.changepacks/changepack_log_include_package_match.json b/.changepacks/changepack_log_include_package_match.json new file mode 100644 index 000000000..93fb2c1e7 --- /dev/null +++ b/.changepacks/changepack_log_include_package_match.json @@ -0,0 +1,9 @@ +{ + "changes": { + "packages/plugin-utils/package.json": "Patch", + "packages/vite-plugin/package.json": "Patch", + "packages/rsbuild-plugin/package.json": "Patch" + }, + "note": "`include` package names match only the whole package directory under the innermost `node_modules`: `foo.bar` no longer compiles `fooXbar`, `pkg` no longer compiles `@scope/pkg` or `pkg-extra`, names with regex characters such as `foo+bar` are compiled, and dependencies nested in an included package's own `node_modules` are no longer compiled with it. Vite and Rsbuild build the matcher once instead of on every transformed module. No new build errors", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 639d7cfe3..87804ed85 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -761,12 +761,7 @@ describe('DevupUINextPlugin', () => { condition: { not: { path: new RegExp( - `(node_modules(?!.*(${['@devup-ui', '@devup-editor'] - .join('|') - .replaceAll( - '/', - '[\\/\\\\_]', - )})([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)`, + '((?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)))|(.mdx.[tj]sx?$)', ), }, }, @@ -814,12 +809,7 @@ describe('DevupUINextPlugin', () => { condition: { not: { path: new RegExp( - `(node_modules(?!.*(${['@devup-ui', '@devup-editor'] - .join('|') - .replaceAll( - '/', - '[\\/\\\\_]', - )})([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)`, + '((?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)))|(.mdx.[tj]sx?$)', ), }, }, @@ -910,12 +900,7 @@ describe('DevupUINextPlugin', () => { condition: { not: { path: new RegExp( - `(node_modules(?!.*(${['@devup-ui', '@devup-editor'] - .join('|') - .replaceAll( - '/', - '[\\/\\\\_]', - )})([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)`, + '((?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)))|(.mdx.[tj]sx?$)', ), }, }, diff --git a/packages/plugin-utils/src/__tests__/shared.test.ts b/packages/plugin-utils/src/__tests__/shared.test.ts index dd119cfd8..796302786 100644 --- a/packages/plugin-utils/src/__tests__/shared.test.ts +++ b/packages/plugin-utils/src/__tests__/shared.test.ts @@ -99,6 +99,48 @@ describe('createNodeModulesExcludeRegex', () => { expect(regex.test('node_modules/@devup-ui/react/index.js')).toBe(false) }) + it('should match included names as whole package directories', () => { + const regex = createNodeModulesExcludeRegex(['foo.bar', 'pkg', 'foo+bar']) + expect(regex.test('node_modules/foo.bar/index.js')).toBe(false) + expect(regex.test('node_modules/fooXbar/index.js')).toBe(true) + expect(regex.test('node_modules/pkg/index.js')).toBe(false) + expect(regex.test('node_modules/pkg')).toBe(false) + expect(regex.test('node_modules/@scope/pkg/index.js')).toBe(true) + expect(regex.test('node_modules/pkg-extra/index.js')).toBe(true) + expect(regex.test('node_modules/foo+bar/index.js')).toBe(false) + expect(regex.test('node_modules/foooobar/index.js')).toBe(true) + }) + + it('should match included packages in Windows and store paths', () => { + const regex = createNodeModulesExcludeRegex(['@scope/pkg', 'pkg']) + expect(regex.test('C:\\app\\node_modules\\@scope\\pkg\\index.js')).toBe( + false, + ) + expect(regex.test('C:\\app\\node_modules\\@other\\pkg\\index.js')).toBe( + true, + ) + expect( + regex.test( + '/app/node_modules/.pnpm/pkg@1.0.0/node_modules/pkg/dist/index.js', + ), + ).toBe(false) + expect( + regex.test( + '/app/node_modules/.bun/@scope+pkg@1.0.0/node_modules/@scope/pkg/index.js', + ), + ).toBe(false) + }) + + it('should take the package from the innermost node_modules', () => { + const regex = createNodeModulesExcludeRegex(['pkg']) + expect( + regex.test('node_modules/@devup-ui/react/node_modules/lodash/index.js'), + ).toBe(true) + expect(regex.test('node_modules/pkg/node_modules/dep/index.js')).toBe(true) + expect(regex.test('node_modules/dep/node_modules/pkg/index.js')).toBe(false) + expect(regex.test('src/my_node_modules/lodash/index.js')).toBe(false) + }) + it('should handle empty include array', () => { const regex = createNodeModulesExcludeRegex([]) expect(regex.test('node_modules/some-package/index.js')).toBe(true) diff --git a/packages/plugin-utils/src/shared.ts b/packages/plugin-utils/src/shared.ts index 20b7aac0e..4cba30bb8 100644 --- a/packages/plugin-utils/src/shared.ts +++ b/packages/plugin-utils/src/shared.ts @@ -47,17 +47,28 @@ export function getFileNumByFilename(filename: string): number | null { * Create a regex that excludes node_modules paths, except for @devup-ui * and any additional included packages. * + * The package a file belongs to is the directory right under its innermost + * `node_modules` directory (pnpm and Bun stores included), and a name matches + * only that whole directory: `foo.bar` does not match `fooXbar`, `pkg` does not + * match `@scope/pkg` or `pkg-extra`, a name with regex characters such as + * `foo+bar` matches itself, and a dependency nested in an included package's + * own `node_modules` is not included with it. + * * @param include - Additional package names to include (not exclude) * @param extraExcludes - Optional extra regex pattern to OR with the node_modules exclusion * @returns A RegExp for use in bundler exclude/condition rules */ export function createNodeModulesExcludeRegex( - include: string[], + include: readonly string[], extraExcludes?: string, ): RegExp { - const base = `node_modules(?!.*(${['@devup-ui', '@devup-editor', ...include] + const separator = '[\\/\\\\]' + const names = ['@devup-ui', '@devup-editor', ...include] + .map((name) => + name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replaceAll('/', '[\\/\\\\_]'), + ) .join('|') - .replaceAll('/', '[\\/\\\\_]')})([\\/\\\\.]|$))` + const base = `(?:^|${separator})node_modules${separator}(?!.*${separator}node_modules${separator})(?!(?:${names})(?:${separator}|$))` return new RegExp(extraExcludes ? `(${base})|(${extraExcludes})` : base) } diff --git a/packages/rsbuild-plugin/src/plugin.ts b/packages/rsbuild-plugin/src/plugin.ts index 91dae66c8..e8403ae9d 100644 --- a/packages/rsbuild-plugin/src/plugin.ts +++ b/packages/rsbuild-plugin/src/plugin.ts @@ -139,6 +139,7 @@ export const DevupUI = ({ importAliases: userImportAliases, }: Partial = {}): RsbuildPlugin => { const normalizedShorthands = normalizeShorthands(shorthands ?? {}) + const excludeRegex = createNodeModulesExcludeRegex(include) const compiler = loadCompiler(engine) compiler.registerShorthands(normalizedShorthands) const importAliases = mergeImportAliases( @@ -463,8 +464,7 @@ export const DevupUI = ({ test: /\.(tsx|ts|js|mjs|jsx)$/, }, async ({ code, resourcePath, addDependency }) => { - if (createNodeModulesExcludeRegex(include).test(resourcePath)) - return code + if (excludeRegex.test(resourcePath)) return code for (const file of tailwindFiles) addDependency(file) // The stylesheet import is emitted relative to the importing file, as // in the next/webpack/vite loaders. An absolute cssDir would bake this diff --git a/packages/vite-plugin/src/plugin.ts b/packages/vite-plugin/src/plugin.ts index f9ce736fc..d330058d1 100644 --- a/packages/vite-plugin/src/plugin.ts +++ b/packages/vite-plugin/src/plugin.ts @@ -327,6 +327,7 @@ export function DevupUI({ importAliases: userImportAliases, }: Partial = {}): PluginOption { const normalizedShorthands = normalizeShorthands(shorthands ?? {}) + const excludeRegex = createNodeModulesExcludeRegex(include) const compiler = loadCompiler(engine) // A build starts from its own options: whatever an earlier build in this // process left in the engine (prefix, hoisting, routes, buckets, numbers, @@ -748,7 +749,7 @@ export function DevupUI({ const fileName = id.split('?')[0] if (!/\.(tsx|ts|js|mjs|jsx)$/i.test(fileName)) return - if (createNodeModulesExcludeRegex(include).test(fileName)) { + if (excludeRegex.test(fileName)) { return } await reservation.reserve() diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index f22c489e3..7f3b11474 100644 --- a/packages/webpack-plugin/src/__tests__/plugin.test.ts +++ b/packages/webpack-plugin/src/__tests__/plugin.test.ts @@ -526,19 +526,19 @@ describe('devupUIWebpackPlugin', () => { { input: ['lib'], output: new RegExp( - '(node_modules(?!.*(@devup-ui|@devup-editor|lib)([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)', + '((?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor|lib)(?:[\\/\\\\]|$)))|(.mdx.[tj]sx?$)', ), }, { input: [], output: new RegExp( - '(node_modules(?!.*(@devup-ui|@devup-editor)([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)', + '((?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)))|(.mdx.[tj]sx?$)', ), }, { input: ['lib', 'lib2'], output: new RegExp( - '(node_modules(?!.*(@devup-ui|@devup-editor|lib|lib2)([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)', + '((?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor|lib|lib2)(?:[\\/\\\\]|$)))|(.mdx.[tj]sx?$)', ), }, ],