From cada2da536d3adaa1a3f5d897dc5438ed09493de Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 09:45:47 +0900 Subject: [PATCH 1/3] fix(plugin-utils): match include package names exactly `createNodeModulesExcludeRegex` joined the `include` names into the regex unescaped and matched them anywhere after `node_modules`. `foo.bar` also compiled `node_modules/fooXbar`, `pkg` also compiled `@scope/pkg`, and `foo+bar` never matched itself. Escape each name and match it only as a whole package directory right under a `node_modules` directory. Vite and Rsbuild now build the matcher once per plugin instead of on every transformed module. Refs #686 (PLG-05, PLG-28) Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_include_package_match.json | 9 ++++++ .../plugin-utils/src/__tests__/shared.test.ts | 32 +++++++++++++++++++ packages/plugin-utils/src/shared.ts | 14 ++++++-- packages/rsbuild-plugin/src/plugin.ts | 4 +-- packages/vite-plugin/src/plugin.ts | 3 +- 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 .changepacks/changepack_log_include_package_match.json diff --git a/.changepacks/changepack_log_include_package_match.json b/.changepacks/changepack_log_include_package_match.json new file mode 100644 index 000000000..dc005e8bd --- /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 `node_modules`: `foo.bar` no longer compiles `fooXbar`, `pkg` no longer compiles `@scope/pkg` or `pkg-extra`, and names with regex characters such as `foo+bar` are compiled. 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/plugin-utils/src/__tests__/shared.test.ts b/packages/plugin-utils/src/__tests__/shared.test.ts index dd119cfd8..4afdd1367 100644 --- a/packages/plugin-utils/src/__tests__/shared.test.ts +++ b/packages/plugin-utils/src/__tests__/shared.test.ts @@ -99,6 +99,38 @@ 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 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..d2a02c0b7 100644 --- a/packages/plugin-utils/src/shared.ts +++ b/packages/plugin-utils/src/shared.ts @@ -47,17 +47,25 @@ export function getFileNumByFilename(filename: string): number | null { * Create a regex that excludes node_modules paths, except for @devup-ui * and any additional included packages. * + * A name matches only as a whole package directory right under a + * `node_modules` directory: `foo.bar` does not match `fooXbar`, `pkg` does not + * match `@scope/pkg` or `pkg-extra`, and a name with regex characters such as + * `foo+bar` matches itself. + * * @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 names = ['@devup-ui', '@devup-editor', ...include] + .map((name) => + name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replaceAll('/', '[\\/\\\\_]'), + ) .join('|') - .replaceAll('/', '[\\/\\\\_]')})([\\/\\\\.]|$))` + const base = `^(?!.*node_modules[\\/\\\\](?:${names})(?:[\\/\\\\]|$)).*node_modules` 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() From bd5fc46cf6e58bb411245f839d4caaceb821ba39 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 09:57:45 +0900 Subject: [PATCH 2/3] test(plugins): expect the exact include matcher in webpack and Turbopack rules Co-Authored-By: Claude Opus 5.5 --- .../next-plugin/src/__tests__/plugin.test.ts | 21 +++---------------- .../src/__tests__/plugin.test.ts | 6 +++--- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 639d7cfe3..ed6209f22 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[\\/\\\\](?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)).*node_modules)|(.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[\\/\\\\](?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)).*node_modules)|(.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[\\/\\\\](?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)).*node_modules)|(.mdx.[tj]sx?$)', ), }, }, diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index f22c489e3..922131fee 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[\\/\\\\](?:@devup-ui|@devup-editor|lib)(?:[\\/\\\\]|$)).*node_modules)|(.mdx.[tj]sx?$)', ), }, { input: [], output: new RegExp( - '(node_modules(?!.*(@devup-ui|@devup-editor)([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)', + '(^(?!.*node_modules[\\/\\\\](?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)).*node_modules)|(.mdx.[tj]sx?$)', ), }, { input: ['lib', 'lib2'], output: new RegExp( - '(node_modules(?!.*(@devup-ui|@devup-editor|lib|lib2)([\\/\\\\.]|$)))|(.mdx.[tj]sx?$)', + '(^(?!.*node_modules[\\/\\\\](?:@devup-ui|@devup-editor|lib|lib2)(?:[\\/\\\\]|$)).*node_modules)|(.mdx.[tj]sx?$)', ), }, ], From df4c019d2217cb674049527b0151e3df73b2b39a Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 09:59:38 +0900 Subject: [PATCH 3/3] fix(plugin-utils): take an included package from the innermost node_modules A dependency nested in an included package's own `node_modules` was compiled with it, because the name could match anywhere in the path. The package a file belongs to is the directory right under its innermost `node_modules` (pnpm and Bun stores included), so match the names only there. Refs #686 (PLG-05) Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_include_package_match.json | 2 +- packages/next-plugin/src/__tests__/plugin.test.ts | 6 +++--- packages/plugin-utils/src/__tests__/shared.test.ts | 10 ++++++++++ packages/plugin-utils/src/shared.ts | 13 ++++++++----- .../webpack-plugin/src/__tests__/plugin.test.ts | 6 +++--- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/.changepacks/changepack_log_include_package_match.json b/.changepacks/changepack_log_include_package_match.json index dc005e8bd..93fb2c1e7 100644 --- a/.changepacks/changepack_log_include_package_match.json +++ b/.changepacks/changepack_log_include_package_match.json @@ -4,6 +4,6 @@ "packages/vite-plugin/package.json": "Patch", "packages/rsbuild-plugin/package.json": "Patch" }, - "note": "`include` package names match only the whole package directory under `node_modules`: `foo.bar` no longer compiles `fooXbar`, `pkg` no longer compiles `@scope/pkg` or `pkg-extra`, and names with regex characters such as `foo+bar` are compiled. Vite and Rsbuild build the matcher once instead of on every transformed module. No new build errors", + "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 ed6209f22..87804ed85 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -761,7 +761,7 @@ describe('DevupUINextPlugin', () => { condition: { not: { path: new RegExp( - '(^(?!.*node_modules[\\/\\\\](?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)).*node_modules)|(.mdx.[tj]sx?$)', + '((?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)))|(.mdx.[tj]sx?$)', ), }, }, @@ -809,7 +809,7 @@ describe('DevupUINextPlugin', () => { condition: { not: { path: new RegExp( - '(^(?!.*node_modules[\\/\\\\](?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)).*node_modules)|(.mdx.[tj]sx?$)', + '((?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)))|(.mdx.[tj]sx?$)', ), }, }, @@ -900,7 +900,7 @@ describe('DevupUINextPlugin', () => { condition: { not: { path: new RegExp( - '(^(?!.*node_modules[\\/\\\\](?:@devup-ui|@devup-editor)(?:[\\/\\\\]|$)).*node_modules)|(.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 4afdd1367..796302786 100644 --- a/packages/plugin-utils/src/__tests__/shared.test.ts +++ b/packages/plugin-utils/src/__tests__/shared.test.ts @@ -131,6 +131,16 @@ describe('createNodeModulesExcludeRegex', () => { ).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 d2a02c0b7..4cba30bb8 100644 --- a/packages/plugin-utils/src/shared.ts +++ b/packages/plugin-utils/src/shared.ts @@ -47,10 +47,12 @@ export function getFileNumByFilename(filename: string): number | null { * Create a regex that excludes node_modules paths, except for @devup-ui * and any additional included packages. * - * A name matches only as a whole package directory right under a - * `node_modules` directory: `foo.bar` does not match `fooXbar`, `pkg` does not - * match `@scope/pkg` or `pkg-extra`, and a name with regex characters such as - * `foo+bar` matches itself. + * 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 @@ -60,12 +62,13 @@ export function createNodeModulesExcludeRegex( include: readonly string[], extraExcludes?: string, ): RegExp { + const separator = '[\\/\\\\]' const names = ['@devup-ui', '@devup-editor', ...include] .map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replaceAll('/', '[\\/\\\\_]'), ) .join('|') - const base = `^(?!.*node_modules[\\/\\\\](?:${names})(?:[\\/\\\\]|$)).*node_modules` + const base = `(?:^|${separator})node_modules${separator}(?!.*${separator}node_modules${separator})(?!(?:${names})(?:${separator}|$))` return new RegExp(extraExcludes ? `(${base})|(${extraExcludes})` : base) } diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index 922131fee..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)(?:[\\/\\\\]|$)).*node_modules)|(.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)(?:[\\/\\\\]|$)).*node_modules)|(.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)(?:[\\/\\\\]|$)).*node_modules)|(.mdx.[tj]sx?$)', + '((?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor|lib|lib2)(?:[\\/\\\\]|$)))|(.mdx.[tj]sx?$)', ), }, ],