Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changepacks/changepack_log_include_package_match.json
Original file line number Diff line number Diff line change
@@ -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"
}
21 changes: 3 additions & 18 deletions packages/next-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?$)',
),
},
},
Expand Down Expand Up @@ -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?$)',
),
},
},
Expand Down Expand Up @@ -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?$)',
),
},
},
Expand Down
42 changes: 42 additions & 0 deletions packages/plugin-utils/src/__tests__/shared.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions packages/plugin-utils/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/rsbuild-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export const DevupUI = ({
importAliases: userImportAliases,
}: Partial<DevupUIRsbuildPluginOptions> = {}): RsbuildPlugin => {
const normalizedShorthands = normalizeShorthands(shorthands ?? {})
const excludeRegex = createNodeModulesExcludeRegex(include)
const compiler = loadCompiler(engine)
compiler.registerShorthands(normalizedShorthands)
const importAliases = mergeImportAliases(
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/vite-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ export function DevupUI({
importAliases: userImportAliases,
}: Partial<DevupUIPluginOptions> = {}): 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,
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions packages/webpack-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?$)',
),
},
],
Expand Down
Loading