From ed601f47ae6de0e85de278f97d25f67f91298756 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 14:00:50 +0900 Subject: [PATCH 1/3] fix(plugins): compile modern modules, resolve bundler conditions and hoist over every source root Carries the plugin core fixes of #755 that main still lacked onto the native-engine main. Module resolution is JavaScript shared by both engines, so the NAPI and WASM builds get them alike. - PLG-06 / NXT-20: one SOURCE_EXTENSIONS list (.ts .tsx .mts .cts .js .jsx .mjs .cjs) drives the import graph, the module resolver and the Vite, webpack, Rsbuild, Bun and Turbopack filters, and the Next prewarm. An import of ./a.js, ./a.mjs or ./a.cjs also resolves to the a.ts, a.mts or a.cts beside it. Bun bundles the project's own .js and .cjs modules and leaves those of packages to Bun. - PLG-12: package exports take the first branch in the package's own order whose condition applies, with the conditions of the bundler: Vite's resolve.conditions, the ESM conditions of webpack and Rspack (per environment in Rsbuild) and Bun's target and conditions. A package exported only to require still resolves. - PLG-13: webpack and Rsbuild plan collapse and atom hoisting over one graph of the src and app directories of the working directory and of the bundler root, instead of src alone. - PLG-20: Bun appends a bare import of each module whose values a file inlines, so its module graph and watcher keep them. Refs #686 Co-Authored-By: Claude Opus 5.5 --- .changepacks/changepack_log_plugin_core.json | 12 ++ e2e/plugins/engine-parity.mjs | 2 +- .../bun-plugin/src/__tests__/plugin.test.ts | 76 ++++++++++- packages/bun-plugin/src/plugin.ts | 94 ++++++++++--- .../src/__tests__/atom-hoist.test.ts | 2 +- .../src/__tests__/availability-adapters.mjs | 3 +- .../next-plugin/src/__tests__/plugin.test.ts | 19 ++- .../next-plugin/src/__tests__/prewarm.test.ts | 3 +- packages/next-plugin/src/plugin.ts | 11 +- packages/next-plugin/src/prewarm.ts | 5 +- .../src/__tests__/bundler-conditions.test.ts | 127 ++++++++++++++++++ .../__tests__/reservation-resolution.test.ts | 21 +++ .../src/__tests__/source-extensions.test.ts | 113 ++++++++++++++++ .../src/__tests__/source-roots.test.ts | 73 ++++++++++ .../plugin-utils/src/bundler-conditions.ts | 46 +++++++ packages/plugin-utils/src/import-graph.ts | 118 ++++++++++++---- packages/plugin-utils/src/index.ts | 8 ++ .../plugin-utils/src/reservation-discovery.ts | 3 + .../plugin-utils/src/source-extensions.ts | 49 +++++++ .../src/__tests__/atom-hoist.test.ts | 2 +- .../src/__tests__/checkout-isolation.test.ts | 1 + .../src/__tests__/plugin.test.ts | 105 ++++++++++++++- packages/rsbuild-plugin/src/plugin.ts | 59 +++++--- .../vite-plugin/src/__tests__/plugin.test.ts | 47 +++++++ .../__tests__/provenance-relevance.test.ts | 22 ++- packages/vite-plugin/src/plugin.ts | 5 +- .../vite-plugin/src/reservation-inputs.ts | 24 ++-- packages/vite-plugin/src/reservation.ts | 8 +- .../src/__tests__/loader.test.ts | 58 +++++++- .../src/__tests__/plugin.test.ts | 40 +++++- packages/webpack-plugin/src/loader.ts | 33 ++++- packages/webpack-plugin/src/plugin.ts | 60 ++++++--- packages/webpack-plugin/src/reservation.ts | 9 +- 33 files changed, 1132 insertions(+), 126 deletions(-) create mode 100644 .changepacks/changepack_log_plugin_core.json create mode 100644 packages/plugin-utils/src/__tests__/bundler-conditions.test.ts create mode 100644 packages/plugin-utils/src/__tests__/source-extensions.test.ts create mode 100644 packages/plugin-utils/src/__tests__/source-roots.test.ts create mode 100644 packages/plugin-utils/src/bundler-conditions.ts create mode 100644 packages/plugin-utils/src/source-extensions.ts diff --git a/.changepacks/changepack_log_plugin_core.json b/.changepacks/changepack_log_plugin_core.json new file mode 100644 index 000000000..5a3d13085 --- /dev/null +++ b/.changepacks/changepack_log_plugin_core.json @@ -0,0 +1,12 @@ +{ + "changes": { + "packages/plugin-utils/package.json": "Patch", + "packages/vite-plugin/package.json": "Patch", + "packages/webpack-plugin/package.json": "Patch", + "packages/rsbuild-plugin/package.json": "Patch", + "packages/bun-plugin/package.json": "Patch", + "packages/next-plugin/package.json": "Patch" + }, + "note": "Compile `.mts`, `.cts` and `.cjs` modules in every plugin, Turbopack and the import graph, and resolve `./a.js`, `./a.mjs` and `./a.cjs` imports to their TypeScript sources. Resolve package `exports` in the package's own order with the conditions of the bundler: Vite's `resolve.conditions`, the ESM conditions of webpack and Rspack per environment, and the target and `conditions` of Bun. Atom hoisting in webpack and Rsbuild plans over the `src` and `app` directories of the working directory and of the bundler root, not `src` alone. Bun keeps the modules whose values a file inlines in its module graph, so changing one rebuilds the file, and bundles the project's own `.js` and `.cjs` modules. No new build errors.", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/e2e/plugins/engine-parity.mjs b/e2e/plugins/engine-parity.mjs index 188db91a8..618d67edb 100644 --- a/e2e/plugins/engine-parity.mjs +++ b/e2e/plugins/engine-parity.mjs @@ -398,7 +398,7 @@ function nextSelectors(value, engine, kind) { ? ['engine'] : [ `${prefix}./df/devup-ui/*.css/0/options/engine`, - `${prefix}*.{tsx,ts,jsx,js,mjs}/loaders/0/options/engine`, + `${prefix}*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}/loaders/0/options/engine`, ].sort(), ) return value diff --git a/packages/bun-plugin/src/__tests__/plugin.test.ts b/packages/bun-plugin/src/__tests__/plugin.test.ts index 6e2ea54a2..ff5d39e21 100644 --- a/packages/bun-plugin/src/__tests__/plugin.test.ts +++ b/packages/bun-plugin/src/__tests__/plugin.test.ts @@ -16,7 +16,7 @@ import { } from 'bun:test' import { bunRegistration } from '../../../../bun.setup' -import { DevupUI, register } from '../plugin' +import { bunConditions, DevupUI, register } from '../plugin' import { captureBunHooks } from './captured-hooks.test-support' const selectCompiler = pluginUtils.loadCompiler @@ -117,6 +117,32 @@ async function load(path: string, namespace = 'file') { }) } +describe('bunConditions', () => { + it('resolves with the conditions of the target and the configured ones', () => { + expect(bunConditions(undefined)).toEqual([ + 'import', + 'module', + 'bun', + 'node', + ]) + expect(bunConditions({})).toEqual(['import', 'module', 'browser']) + expect(bunConditions({ target: 'node', conditions: 'worker' })).toEqual([ + 'import', + 'module', + 'node', + 'worker', + ]) + expect(bunConditions({ target: 'bun', conditions: ['a', 'b'] })).toEqual([ + 'import', + 'module', + 'bun', + 'node', + 'a', + 'b', + ]) + }) +}) + describe('registered Bun hooks', () => { it('compiles default runtime input into native state without mutating WASM', async () => { const native = pluginUtils.loadCompiler() @@ -261,6 +287,52 @@ describe('registered Bun hooks', () => { expect(writeSync).not.toHaveBeenCalled() }) + it('bundles the project .js and .cjs modules and leaves packages to Bun', async () => { + const dir = join(root, 'local-scripts') + await Bun.write( + join(dir, 'entry.ts'), + "import { view } from './view.js'; import { lib } from 'lib'; export { view, lib }", + ) + await Bun.write( + join(dir, 'view.js'), + "import { css } from '@devup-ui/react'; export const view = css({ color: 'orange' })", + ) + await Bun.write( + join(dir, 'node_modules/lib/package.json'), + JSON.stringify({ name: 'lib', main: 'index.js' }), + ) + await Bun.write( + join(dir, 'node_modules/lib/index.js'), + 'export const lib = 1', + ) + const result = await Bun.build({ + entrypoints: [join(dir, 'entry.ts')], + target: 'bun', + outdir: join(dir, 'out'), + plugins: [DevupUI({ engine: 'wasm' })], + }) + expect(result.success).toBe(true) + const outputs = await Promise.all( + result.outputs.map((output) => output.text()), + ) + expect(outputs.join('')).toMatch(/color:\s*orange/) + expect(outputs.join('')).not.toContain('css(') + }) + + it('keeps the build-time dependencies of a source in its module graph', async () => { + await Bun.write(join(root, 'dep-tokens.ts'), "export const color = 'red'") + const path = join(root, 'dep-source.ts') + await Bun.write( + path, + "import { css } from '@devup-ui/react'; import { color } from './dep-tokens'; export const c = css({ color })", + ) + await register({ engine: 'wasm' }) + const result = await load(path) + if (!result || !('contents' in result)) + throw new Error('Expected transformed source contents') + expect(result.contents).toEndWith('\nimport "./dep-tokens.ts";') + }) + it('creates cold directories before writing configuration and CSS', async () => { await register({ engine: 'wasm' }) expect(mkdir).toHaveBeenCalledWith('df', { recursive: true }) @@ -314,6 +386,8 @@ describe('registered Bun hooks', () => { for (const [extension, loader] of [ ['tsx', 'tsx'], ['ts', 'ts'], + ['mts', 'ts'], + ['cts', 'ts'], ['jsx', 'jsx'], ['mjs', 'js'], ['js', 'js'], diff --git a/packages/bun-plugin/src/plugin.ts b/packages/bun-plugin/src/plugin.ts index 284772623..c79f02476 100644 --- a/packages/bun-plugin/src/plugin.ts +++ b/packages/bun-plugin/src/plugin.ts @@ -54,6 +54,51 @@ export interface DevupUIBunPluginOptions { type SourceLoader = 'tsx' | 'ts' | 'jsx' | 'js' +/** + * The `exports` conditions Bun resolves an import with: those of its target + * (`browser` by default for `Bun.build`, `bun` under the runtime) and the + * configured `conditions` + */ +export function bunConditions( + config: Pick | undefined, +): string[] { + const target = config ? (config.target ?? 'browser') : 'bun' + return [ + 'import', + 'module', + ...{ browser: ['browser'], node: ['node'], bun: ['bun', 'node'] }[target], + ...[config?.conditions ?? []].flat(), + ] +} + +/** + * `code` followed by a bare import of each build-time dependency, so that + * Bun's module graph, and with it its watcher, keeps the modules whose values + * the extractor inlined. Appended, the imports move no line or column of the + * extracted code. + */ +export function withDependencyImports( + code: string, + filePath: string, + dependencies: readonly string[], +): string { + return [ + code, + ...[...new Set(dependencies)].map((dependency) => { + const path = relative(dirname(filePath), dependency).replaceAll('\\', '/') + return `import ${JSON.stringify(path.startsWith('.') ? path : `./${path}`)};` + }), + ].join('\n') +} + +/** Bun's loader for a source file */ +function sourceLoader(filePath: string): SourceLoader { + if (filePath.endsWith('.tsx')) return 'tsx' + if (/\.[mc]?ts$/.test(filePath)) return 'ts' + if (filePath.endsWith('.jsx')) return 'jsx' + return 'js' +} + async function writeDataFiles(compiler: DevupCompiler): Promise { let theme = {} let tailwindFiles: string[] = [] @@ -88,10 +133,11 @@ async function initialize( compiler: DevupCompiler, shorthands: Record, watchTailwindCss: ReturnType, + conditions: readonly string[], ) { compiler.registerShorthands(shorthands) compiler.setPrefix(null) - compiler.setModuleResolver(createModuleResolver()) + compiler.setModuleResolver(createModuleResolver({ conditions })) if (!existsSync(distDir)) await mkdir(distDir, { recursive: true }) await writeFile(join(distDir, '.gitignore'), '*', 'utf-8') await writeFile( @@ -130,13 +176,7 @@ async function loadSourceFile( filePath: string, bundling: boolean, ) { - const loader: SourceLoader = filePath.endsWith('.tsx') - ? 'tsx' - : filePath.endsWith('.ts') - ? 'ts' - : filePath.endsWith('.jsx') - ? 'jsx' - : 'js' + const loader = sourceLoader(filePath) const contents = await Bun.file(filePath).text() if ( @@ -163,7 +203,10 @@ async function loadSourceFile( compiler.getCss(null, false), 'utf-8', ) - return { contents: code.code, loader } + return { + contents: withDependencyImports(code.code, filePath, code.dependencies), + loader, + } } return { contents, loader } } @@ -188,7 +231,12 @@ function DevupUI(options: DevupUIBunPluginOptions = {}) { // this process left in the engine const endBuild = beginBuild(compiler) build.onEnd?.(endBuild) - await initialize(compiler, shorthands, watchTailwindCss) + await initialize( + compiler, + shorthands, + watchTailwindCss, + bunConditions(build.config), + ) const debug = options.debug ?? !bundling compiler.setDebug(debug) const slots = createSharedSlotReservation(compiler) @@ -260,19 +308,29 @@ function DevupUI(options: DevupUIBunPluginOptions = {}) { }, ) + const loadSource = ({ path }: { readonly path: string }) => { + if (reservationPending) { + reserveFiles() + reservationPending = false + } + return loadSourceFile(compiler, path, bundling) + } // Load source files from packages directory (file namespace) build.onLoad( { - filter: /\.(?:tsx?|jsx|mjs)$|[\\/]@devup-ui[\\/].*\.js$/, - }, - ({ path }) => { - if (reservationPending) { - reserveFiles() - reservationPending = false - } - return loadSourceFile(compiler, path, bundling) + filter: /\.(?:[mc]?ts|tsx|jsx|mjs)$|[\\/]@devup-ui[\\/].*\.js$/, }, + loadSource, ) + // A bundle also takes the project's own `.js` and `.cjs` modules. Bun + // loads one itself when the hook returns nothing, which the runtime + // does not allow, so dependencies are left to Bun here. + if (bundling) + build.onLoad({ filter: /\.c?js$/ }, (args) => + !/[\\/]node_modules[\\/]/.test(args.path) + ? loadSource(args) + : undefined, + ) }, } satisfies BunPlugin } diff --git a/packages/next-plugin/src/__tests__/atom-hoist.test.ts b/packages/next-plugin/src/__tests__/atom-hoist.test.ts index 001706d41..62dc27d7a 100644 --- a/packages/next-plugin/src/__tests__/atom-hoist.test.ts +++ b/packages/next-plugin/src/__tests__/atom-hoist.test.ts @@ -20,7 +20,7 @@ const { default: loader } = await import(${builtPluginEntry('next-plugin', 'load const { default: cssLoader } = await import(${builtPluginEntry('next-plugin', 'css-loader.mjs')}); const config = DevupUI({}, { atomHoist: 2, cssDir, distDir: join(root, 'df') }); const rules = config.turbopack.rules; -const jsOptions = rules['*.{tsx,ts,jsx,js,mjs}'].loaders[0].options; +const jsOptions = rules['*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}'].loaders[0].options; const cssOptions = rules['./df/devup-ui/*.css'][0].options; const invoke = (fn, options, request) => new Promise((resolve, reject) => { fn.call({ resourcePath: request.resourcePath, getOptions: () => options, addDependency() {}, diff --git a/packages/next-plugin/src/__tests__/availability-adapters.mjs b/packages/next-plugin/src/__tests__/availability-adapters.mjs index 001b1d0ef..1f92c6429 100644 --- a/packages/next-plugin/src/__tests__/availability-adapters.mjs +++ b/packages/next-plugin/src/__tests__/availability-adapters.mjs @@ -107,7 +107,8 @@ export async function openAdapter({ adapter, worktree, root, files }) { assert.fail('must observe one real coordinator') } const rules = config.turbopack.rules - const jsOptions = rules['*.{tsx,ts,jsx,js,mjs}'].loaders[0].options + const jsOptions = + rules['*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}'].loaders[0].options const cssOptions = rules['./df/devup-ui/*.css'][0].options const invoke = (fn, opts, input) => new Promise((resolve, reject) => { diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 639d7cfe3..a942cfd44 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -283,7 +283,7 @@ describe('DevupUINextPlugin', () => { withModuleResolver(primedWasm); // Match normal fixed-cwd supplier resolution after fixture setup. const before = { native: native.getCss(null, false), wasm: wasm.getCss(null, false) }; const config = DevupUI({}, { singleCss: true, debug: false }); - const options = config.turbopack.rules['*.{tsx,ts,jsx,js,mjs}'].loaders[0].options; + const options = config.turbopack.rules['*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}'].loaders[0].options; const code = await new Promise((resolve, reject) => loader.call({ resourcePath, getOptions: () => options, addDependency() {}, async: () => (error, code) => error ? reject(error) : resolve(code), @@ -685,6 +685,17 @@ describe('DevupUINextPlugin', () => { } }) + it('compiles every source extension the import graph lists under Turbopack', () => { + process.env.TURBOPACK = '1' + const rules = Object.keys(DevupUI({}).turbopack?.rules ?? {}) + const glob = rules.find((rule) => rule.startsWith('*.{')) + expect(glob?.slice(3, -1).split(',').sort()).toEqual( + importGraphModule.SOURCE_EXTENSIONS.map((extension) => + extension.slice(1), + ).sort(), + ) + }) + it('should apply turbo config', async () => { process.env.TURBOPACK = '1' existsSyncSpy @@ -722,7 +733,7 @@ describe('DevupUINextPlugin', () => { }, }, ], - '*.{tsx,ts,jsx,js,mjs}': { + '*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}': { loaders: [ { loader: '@devup-ui/next-plugin/loader', @@ -810,7 +821,7 @@ describe('DevupUINextPlugin', () => { }, }, ], - '*.{tsx,ts,jsx,js,mjs}': { + '*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}': { condition: { not: { path: new RegExp( @@ -906,7 +917,7 @@ describe('DevupUINextPlugin', () => { }, }, ], - '*.{tsx,ts,jsx,js,mjs}': { + '*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}': { condition: { not: { path: new RegExp( diff --git a/packages/next-plugin/src/__tests__/prewarm.test.ts b/packages/next-plugin/src/__tests__/prewarm.test.ts index 1c2a5da48..0d4d2b9cb 100644 --- a/packages/next-plugin/src/__tests__/prewarm.test.ts +++ b/packages/next-plugin/src/__tests__/prewarm.test.ts @@ -50,7 +50,7 @@ describe('collectProductionPrewarmFiles', () => { } } - it('includes all source candidates and ESM entries accepted by the loader', () => { + it('includes all source candidates and the ESM or CommonJS entries the loader accepts', () => { writePackage( '@devup-ui/reset-css', { @@ -98,6 +98,7 @@ describe('collectProductionPrewarmFiles', () => { [ 'node_modules/@acme/ui/index.mjs', 'node_modules/@devup-editor/editor/index.js', + 'node_modules/@devup-ui/cjs-only/index.cjs', 'node_modules/@devup-ui/reset-css/dist/index.mjs', 'node_modules/design-system/index.js', 'src/app/page.tsx', diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index aa92f663e..94b20ffcd 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -583,11 +583,12 @@ export function DevupUI( }, ], // Must cover every extension the import-graph pre-pass lists AND the - // webpack rule matches (tsx|ts|jsx|js|mjs). Omitting one (e.g. jsx) - // means those files are never extracted: their Box/styled markup hits - // the runtime stubs ("Cannot run on the runtime") and the coordinator - // waits on graph members that never POST /extract. - '*.{tsx,ts,jsx,js,mjs}': { + // webpack rule matches (SOURCE_EXTENSIONS of plugin-utils). Omitting + // one (e.g. mts) means those files are never extracted: their + // Box/styled markup hits the runtime stubs ("Cannot run on the + // runtime") and the coordinator waits on graph members that never + // POST /extract. + '*.{tsx,ts,mts,cts,jsx,js,mjs,cjs}': { loaders: [ { loader: '@devup-ui/next-plugin/loader', diff --git a/packages/next-plugin/src/prewarm.ts b/packages/next-plugin/src/prewarm.ts index 04a9cfd38..8564ed492 100644 --- a/packages/next-plugin/src/prewarm.ts +++ b/packages/next-plugin/src/prewarm.ts @@ -4,11 +4,10 @@ import { extname, isAbsolute, relative, resolve } from 'node:path' import { createModuleResolver, parseImports, + SOURCE_FILE_REGEX, type StaticImportGraph, } from '@devup-ui/plugin-utils' -const EXTRACTABLE_EXTENSION = /\.(?:tsx?|jsx?|mjs)$/ - function packageNameFromSpecifier(specifier: string): string | undefined { if (specifier.startsWith('#') || specifier.startsWith('node:')) { return undefined @@ -98,7 +97,7 @@ export function collectProductionPrewarmFiles({ const module = resolveModule(specifier, importer) if (!module) continue const filename = preferEsmFile(module.path) - if (!EXTRACTABLE_EXTENSION.test(filename) || visited.has(filename)) continue + if (!SOURCE_FILE_REGEX.test(filename) || visited.has(filename)) continue visited.add(filename) files.add(toKey(resolvedCwd, filename)) const source = diff --git a/packages/plugin-utils/src/__tests__/bundler-conditions.test.ts b/packages/plugin-utils/src/__tests__/bundler-conditions.test.ts new file mode 100644 index 000000000..11ebd2752 --- /dev/null +++ b/packages/plugin-utils/src/__tests__/bundler-conditions.test.ts @@ -0,0 +1,127 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { dirname, join } from 'node:path' + +import { afterEach, beforeEach, describe, expect, it } from 'bun:test' + +import { + esmImportConditions, + viteImportConditions, +} from '../bundler-conditions' +import { createModuleResolver } from '../import-graph' + +describe('esmImportConditions', () => { + it('expands the ESM conditions with the general ones', () => { + expect( + esmImportConditions({ + conditionNames: ['webpack', 'production', 'browser'], + byDependency: { + esm: { conditionNames: ['import', 'module', '...'] }, + commonjs: { conditionNames: ['require', '...'] }, + }, + }), + ).toEqual(['import', 'module', 'webpack', 'production', 'browser']) + }) + + it('falls back to the general conditions and then to none', () => { + expect(esmImportConditions({ conditionNames: ['browser'] })).toEqual([ + 'browser', + ]) + expect( + esmImportConditions({ + byDependency: { esm: { conditionNames: ['...'] } }, + }), + ).toEqual([]) + expect(esmImportConditions({ byDependency: {} })).toBeUndefined() + expect(esmImportConditions(undefined)).toBeUndefined() + }) +}) + +describe('viteImportConditions', () => { + it('adds import and resolves the mode placeholder', () => { + const conditions = ['module', 'browser', 'development|production'] + expect( + viteImportConditions({ isProduction: true, resolve: { conditions } }), + ).toEqual(['import', 'module', 'browser', 'production']) + expect(viteImportConditions({ resolve: { conditions } })).toEqual([ + 'import', + 'module', + 'browser', + 'development', + ]) + }) + + it('names none when the config does not', () => { + expect(viteImportConditions({ resolve: {} })).toBeUndefined() + expect(viteImportConditions(undefined)).toBeUndefined() + }) +}) + +describe('createModuleResolver conditions', () => { + let root: string + + beforeEach(() => { + root = mkdtempSync(join(tmpdir(), 'devup-ui-conditions-')) + const files: Record = { + 'node_modules/split/package.json': JSON.stringify({ + exports: { + '.': { + browser: './browser.js', + import: './import.js', + default: './default.js', + }, + './nested': { + node: { import: './node-import.js', default: './node.js' }, + default: './fallback.js', + }, + './empty': { browser: {}, default: './empty-fallback.js' }, + }, + }), + 'node_modules/split/browser.js': 'browser', + 'node_modules/split/import.js': 'import', + 'node_modules/split/default.js': 'default', + 'node_modules/split/node-import.js': 'node-import', + 'node_modules/split/node.js': 'node', + 'node_modules/split/fallback.js': 'fallback', + 'node_modules/split/empty-fallback.js': 'empty-fallback', + 'node_modules/required/package.json': JSON.stringify({ + exports: { require: './index.cjs', default: null }, + }), + 'node_modules/required/index.cjs': 'required', + } + for (const [path, code] of Object.entries(files)) { + mkdirSync(dirname(join(root, path)), { recursive: true }) + writeFileSync(join(root, path), code) + } + }) + + afterEach(() => { + rmSync(root, { recursive: true, force: true }) + }) + + function code(specifier: string, conditions?: readonly string[]) { + return createModuleResolver({ cwd: root, conditions })( + specifier, + 'src/App.tsx', + )?.code + } + + it('takes the first matching branch in the order the package lists them', () => { + expect(code('split', ['import', 'browser'])).toBe('browser') + expect(code('split', ['import'])).toBe('import') + expect(code('split', [])).toBe('default') + expect(code('split/nested', ['import', 'node'])).toBe('node-import') + expect(code('split/nested', ['node'])).toBe('node') + expect(code('split/nested', ['browser'])).toBe('fallback') + expect(code('split/empty', ['browser'])).toBe('empty-fallback') + }) + + it('resolves import, module and node conditions by default', () => { + expect(code('split')).toBe('import') + expect(code('split/nested')).toBe('node-import') + }) + + it('resolves a package exported only to require', () => { + expect(code('required', ['import', 'browser'])).toBe('required') + }) +}) diff --git a/packages/plugin-utils/src/__tests__/reservation-resolution.test.ts b/packages/plugin-utils/src/__tests__/reservation-resolution.test.ts index fb3d7fc68..a77450887 100644 --- a/packages/plugin-utils/src/__tests__/reservation-resolution.test.ts +++ b/packages/plugin-utils/src/__tests__/reservation-resolution.test.ts @@ -157,3 +157,24 @@ it('detects exchanged specifier targets even when the importer target set stays expect(recorded.packets).toHaveLength(2) expect(recorded.packets[1]).toEqual(recorded.packets[0]) }) + +it('resolves package imports with the bundler conditions', () => { + // Given + using fs = tokens() + fs.write( + 'node_modules/@devup-ui/tokens/package.json', + JSON.stringify({ exports: { browser: './blue.js', default: './red.js' } }), + ) + const imported = (conditions?: readonly string[]) => + [ + ...collectReservationInputs({ + ...fs.options, + conditions, + }).resolutions.imports.values(), + ] + .map((targets) => targets.get('@devup-ui/tokens')) + .find(Boolean) + // When / Then + expect(imported(['browser'])).toEndWith('blue.js') + expect(imported()).toEndWith('red.js') +}) diff --git a/packages/plugin-utils/src/__tests__/source-extensions.test.ts b/packages/plugin-utils/src/__tests__/source-extensions.test.ts new file mode 100644 index 000000000..4cc5ce907 --- /dev/null +++ b/packages/plugin-utils/src/__tests__/source-extensions.test.ts @@ -0,0 +1,113 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { dirname, join } from 'node:path' + +import { afterEach, beforeEach, describe, expect, it } from 'bun:test' + +import { + buildStaticImportGraph, + createModuleResolver, + listSourceFiles, +} from '../import-graph' +import { + SOURCE_EXTENSIONS, + SOURCE_FILE_REGEX, + sourceFileCandidates, +} from '../source-extensions' + +let root: string + +beforeEach(() => { + root = mkdtempSync(join(tmpdir(), 'devup-ui-source-extensions-')) +}) + +afterEach(() => { + rmSync(root, { recursive: true, force: true }) +}) + +function write(files: Record) { + for (const [path, code] of Object.entries(files)) { + mkdirSync(dirname(join(root, path)), { recursive: true }) + writeFileSync(join(root, path), code) + } +} + +describe('SOURCE_FILE_REGEX', () => { + it('matches every source extension and nothing else', () => { + for (const extension of SOURCE_EXTENSIONS) + expect(SOURCE_FILE_REGEX.test(`a/b${extension}`)).toBe(true) + for (const path of ['a.css', 'a.json', 'a.mdx', 'a.d', 'a.mtsx', 'ts']) + expect(SOURCE_FILE_REGEX.test(path)).toBe(false) + }) +}) + +describe('sourceFileCandidates', () => { + it('adds the TypeScript files a JavaScript extension stands for', () => { + expect(sourceFileCandidates('a.js')).toEqual(['a.js', 'a.ts', 'a.tsx']) + expect(sourceFileCandidates('a.jsx')).toEqual(['a.jsx', 'a.tsx']) + expect(sourceFileCandidates('a.mjs')).toEqual(['a.mjs', 'a.mts']) + expect(sourceFileCandidates('a.cjs')).toEqual(['a.cjs', 'a.cts']) + expect(sourceFileCandidates('a.mts')).toEqual(['a.mts']) + }) +}) + +describe('modern module extensions', () => { + it('lists, parses and links ESM and CommonJS TypeScript modules', () => { + write({ + 'src/index.tsx': + "import './esm.mjs'\nimport './cjs.cjs'\nimport './plain.js'\nimport './script.cjs'\n", + 'src/esm.mts': "import { b } from './b.js'\nexport const a = b\n", + 'src/cjs.cts': 'export const c = 1\n', + 'src/plain.ts': 'export const p = 1\n', + 'src/b.ts': 'export const b = 1\n', + 'src/script.cjs': 'module.exports = 1\n', + 'src/types.d.mts': 'export {}\n', + 'src/style.css': 'a {}\n', + }) + const srcDir = join(root, 'src') + expect(listSourceFiles(srcDir)).toEqual( + [ + 'b.ts', + 'cjs.cts', + 'esm.mts', + 'index.tsx', + 'plain.ts', + 'script.cjs', + 'types.d.mts', + ].map((file) => join(srcDir, file)), + ) + const graph = buildStaticImportGraph(srcDir) + expect([ + ...(graph.staticImports.get(join(srcDir, 'index.tsx')) ?? []), + ]).toEqual( + ['esm.mts', 'cjs.cts', 'plain.ts', 'script.cjs'].map((file) => + join(srcDir, file), + ), + ) + expect([ + ...(graph.staticImports.get(join(srcDir, 'esm.mts')) ?? []), + ]).toEqual([join(srcDir, 'b.ts')]) + }) + + it('resolves the TypeScript source of a JavaScript import', () => { + write({ + 'src/tokens.mts': 'export const a = 1', + 'src/colors.cts': 'export const b = 2', + 'src/theme.ts': 'export const c = 3', + }) + const resolveModule = createModuleResolver({ cwd: root }) + expect(resolveModule('./tokens.mjs', 'src/App.tsx')?.code).toBe( + 'export const a = 1', + ) + expect(resolveModule('./colors.cjs', 'src/App.tsx')?.code).toBe( + 'export const b = 2', + ) + expect(resolveModule('./theme.js', 'src/App.tsx')?.code).toBe( + 'export const c = 3', + ) + expect(resolveModule('./tokens.mts', 'src/App.tsx')?.code).toBe( + 'export const a = 1', + ) + expect(resolveModule('./missing.mjs', 'src/App.tsx')).toBeUndefined() + }) +}) diff --git a/packages/plugin-utils/src/__tests__/source-roots.test.ts b/packages/plugin-utils/src/__tests__/source-roots.test.ts new file mode 100644 index 000000000..205892014 --- /dev/null +++ b/packages/plugin-utils/src/__tests__/source-roots.test.ts @@ -0,0 +1,73 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { dirname, join } from 'node:path' + +import { afterEach, beforeEach, describe, expect, it } from 'bun:test' + +import { + buildCanonicalMap, + buildStaticImportGraph, + computeFileReach, + conventionalSourceRoots, + planAtomHoist, +} from '../import-graph' + +let root: string + +beforeEach(() => { + root = mkdtempSync(join(tmpdir(), 'devup-ui-source-roots-')) +}) + +afterEach(() => { + rmSync(root, { recursive: true, force: true }) +}) + +function write(files: Record) { + for (const [path, code] of Object.entries(files)) { + mkdirSync(dirname(join(root, path)), { recursive: true }) + writeFileSync(join(root, path), code) + } +} + +describe('conventionalSourceRoots', () => { + it('lists src and app of every project root once', () => { + expect(conventionalSourceRoots(root, root, join(root, 'web'))).toEqual( + ['src', 'app', 'web/src', 'web/app'].map((dir) => join(root, dir)), + ) + }) +}) + +describe('buildStaticImportGraph roots', () => { + it('links the files of several roots under one source directory', () => { + write({ + 'app/one/page.tsx': "import '../../src/shared'\n", + 'app/two/page.tsx': "import '../../src/shared'\n", + 'src/shared.tsx': 'export const shared = 1\n', + 'scripts/tool.ts': "import '../src/shared'\n", + }) + const graph = buildStaticImportGraph( + root, + undefined, + conventionalSourceRoots(root, join(root, 'src')), + ) + expect(graph.files).toEqual( + ['app/one/page.tsx', 'app/two/page.tsx', 'src/shared.tsx'].map((file) => + join(root, file), + ), + ) + expect([ + ...(graph.staticImporters.get(join(root, 'src/shared.tsx')) ?? []), + ]).toEqual( + ['app/one/page.tsx', 'app/two/page.tsx'].map((file) => join(root, file)), + ) + + // Both routes of `app` reach the shared module of `src`, so it hoists + const options = { srcDir: root, cwd: root, graph } + const plan = planAtomHoist( + buildCanonicalMap(options), + computeFileReach(options), + 2, + ) + expect(plan?.reachByBucket['src/shared.tsx']).toEqual([0, 1]) + }) +}) diff --git a/packages/plugin-utils/src/bundler-conditions.ts b/packages/plugin-utils/src/bundler-conditions.ts new file mode 100644 index 000000000..251b9a275 --- /dev/null +++ b/packages/plugin-utils/src/bundler-conditions.ts @@ -0,0 +1,46 @@ +/** The part of webpack's and Rspack's `resolve` options naming `exports` conditions. */ +export interface ConditionResolveOptions { + readonly conditionNames?: readonly string[] + readonly byDependency?: Readonly< + Record + > +} + +/** + * The `exports` conditions webpack and Rspack resolve an ESM import with: + * those of `byDependency.esm`, where `...` stands for the general + * `conditionNames`. `undefined` when the options name none. + */ +export function esmImportConditions( + resolve: ConditionResolveOptions | undefined, +): string[] | undefined { + const general = resolve?.conditionNames ?? [] + return ( + resolve?.byDependency?.esm?.conditionNames ?? resolve?.conditionNames + )?.flatMap((condition) => (condition === '...' ? general : [condition])) +} + +/** The `resolve` options of Vite naming `exports` conditions. */ +export interface ViteConditionConfig { + readonly isProduction?: boolean + readonly resolve?: { readonly conditions?: readonly string[] } +} + +/** + * The `exports` conditions Vite resolves an import with: `import` and the + * configured `resolve.conditions`, with `development|production` standing for + * the current mode. `undefined` when the config names none. + */ +export function viteImportConditions( + config: ViteConditionConfig | undefined, +): string[] | undefined { + const mode = config?.isProduction ? 'production' : 'development' + return ( + config?.resolve?.conditions && [ + 'import', + ...config.resolve.conditions.map((condition) => + condition === 'development|production' ? mode : condition, + ), + ] + ) +} diff --git a/packages/plugin-utils/src/import-graph.ts b/packages/plugin-utils/src/import-graph.ts index 9bf15167e..0a2a08e87 100644 --- a/packages/plugin-utils/src/import-graph.ts +++ b/packages/plugin-utils/src/import-graph.ts @@ -18,6 +18,11 @@ import { } from 'node:path' import { PackageManifestShapeError } from './package-manifest' +import { + SOURCE_EXTENSIONS, + SOURCE_FILE_REGEX, + sourceFileCandidates, +} from './source-extensions' import { decodeStringLiteral } from './string-literal' /** @@ -69,11 +74,11 @@ interface ResolveContext { aliases: PathAlias[] aliasBaseDir: string files: Set - srcDir: string } +/** The extensions an extensionless import is completed with, like a bundler. */ const jsExtensions = ['.ts', '.tsx', '.js', '.jsx', '.mjs'] -const jsFileRegex = /\.(?:tsx?|jsx?|mjs)$/ +const sourceExtensions: readonly string[] = SOURCE_EXTENSIONS const testFileRegex = /\.(?:test|spec)\.[mc]?[jt]sx?$/ const routeFileRegex = /(^|\/)(page|layout|template|default|loading|error|not-found|global-error)\.(tsx|ts|jsx|js)$/ @@ -117,20 +122,31 @@ export interface StaticImportGraph { * Scan `srcDir` once (`listSourceFiles` -> read -> parse imports -> resolve) * and return the full static import graph. All three graph consumers accept * the result, so the file I/O and parsing cost is paid a single time. + * + * `roots` lists the directories to scan instead of all of `srcDir`, such as + * both `src` and `app` of a project whose `srcDir` is the project root. Files + * stay ordered by their path relative to `srcDir`. */ export function buildStaticImportGraph( srcDir: string, tsconfigPath?: string, + roots: readonly string[] = [srcDir], ): StaticImportGraph { const resolvedSrcDir = resolve(srcDir) - const files = listSourceFiles(resolvedSrcDir) + const files = [ + ...new Set(roots.flatMap((root) => listSourceFiles(resolve(root)))), + ].sort((a, b) => + compareCodePoints( + toPosixRelative(resolvedSrcDir, a), + toPosixRelative(resolvedSrcDir, b), + ), + ) const fileSet = new Set(files) const aliases = readPathAliases(tsconfigPath) const context: ResolveContext = { aliases: aliases.aliases, aliasBaseDir: aliases.baseDir, files: fileSet, - srcDir: resolvedSrcDir, } const staticImporters = new Map>() const staticImports = new Map>() @@ -180,6 +196,22 @@ export function buildStaticImportGraph( } } +/** + * The `src` and `app` directories of each project root: where a project keeps + * its sources by convention. Missing ones list no files. + */ +export function conventionalSourceRoots( + ...projectRoots: readonly string[] +): string[] { + return [ + ...new Set( + projectRoots.flatMap((root) => + ['src', 'app'].map((name) => resolve(root, name)), + ), + ), + ] +} + export function buildCanonicalMap( opts: BuildCanonicalMapOptions, ): Record { @@ -654,7 +686,7 @@ export function listSourceFiles( } export function isSourceFile(filename: string): boolean { - return jsFileRegex.test(filename) && !testFileRegex.test(filename) + return SOURCE_FILE_REGEX.test(filename) && !testFileRegex.test(filename) } export function importedSpecifiers( @@ -1040,7 +1072,6 @@ function resolveImport( const resolvedFile = resolveFile(candidateBase) if (!resolvedFile) continue if (!context.files.has(resolvedFile)) continue - if (!isInsideDir(context.srcDir, resolvedFile)) continue return resolvedFile } @@ -1064,8 +1095,18 @@ export interface CreateModuleResolverOptions { toId?: (path: string) => string readonly allowPackage?: (specifier: string) => boolean readonly onPackageManifest?: (path: string) => void + /** + * The package `exports` conditions the bundler resolves an import with, + * such as `browser` or `development`. `default` always applies. Where a + * package lists several that apply, its own order decides, as in Node and + * every bundler. + */ + readonly conditions?: readonly string[] } +/** The conditions of an import when the bundler does not say which */ +const DEFAULT_CONDITIONS = ['import', 'module', 'node'] + /** * Resolve the imports of extracted files like the bundler: relative paths, * tsconfig `paths`, then packages in `node_modules` (`exports` conditions, @@ -1077,6 +1118,7 @@ export function createModuleResolver({ toId = (path) => path, allowPackage, onPackageManifest, + conditions = DEFAULT_CONDITIONS, }: CreateModuleResolverOptions = {}): ( specifier: string, importer: string, @@ -1101,7 +1143,7 @@ export function createModuleResolver({ return cached.path } let manifest = '' - const path = resolvePackage(specifier, from, (found) => { + const path = resolvePackage(specifier, from, conditions, (found) => { manifest = found onPackageManifest?.(found) }) @@ -1159,12 +1201,10 @@ function fileStamp(path: string): string | undefined { } } -const sourceExtensions = [...jsExtensions, '.cjs'] - /** `resolveFile`, also completing a name like `theme.css` to `theme.css.ts`. */ function resolveSourceFile(base: string): string | undefined { const candidates = sourceExtensions.includes(extname(base)) - ? [base] + ? sourceFileCandidates(base) : [ ...jsExtensions.map((extension) => `${base}${extension}`), ...jsExtensions.map((extension) => join(base, `index${extension}`)), @@ -1173,12 +1213,10 @@ function resolveSourceFile(base: string): string | undefined { return found && resolve(found) } -/** Conditions of package `exports`, in the order a bundler prefers them */ -const packageConditions = ['import', 'module', 'default', 'require', 'node'] - function resolvePackage( specifier: string, importer: string, + conditions: readonly string[], onPackageManifest?: (path: string) => void, ): string | undefined { const parts = specifier.split('/') @@ -1196,6 +1234,7 @@ function resolvePackage( packageDir, manifest, ['.', ...parts.slice(nameLength)].join('/'), + conditions, ) return found && realpathSync(found) } @@ -1210,9 +1249,14 @@ function resolvePackageEntry( dir: string, manifest: Record, subpath: string, + conditions: readonly string[], ): string | undefined { if (manifest.exports !== undefined) { - const target = exportsTarget(manifest.exports, subpath) + // The resolver cannot tell an import from a `require`, so a package that + // exports only to `require` still resolves for a CommonJS source. + const target = + exportsTarget(manifest.exports, subpath, conditions) ?? + exportsTarget(manifest.exports, subpath, requireConditions(conditions)) return target === undefined ? undefined : resolveSourceFile(join(dir, target)) @@ -1224,16 +1268,27 @@ function resolvePackageEntry( return resolveSourceFile(join(dir, main ?? 'index')) } -function exportsTarget(exports: unknown, subpath: string): string | undefined { +/** `conditions` as a `require` call resolves them */ +function requireConditions(conditions: readonly string[]): string[] { + return conditions.map((condition) => + condition === 'import' ? 'require' : condition, + ) +} + +function exportsTarget( + exports: unknown, + subpath: string, + conditions: readonly string[], +): string | undefined { const subpaths = isRecord(exports) && !Array.isArray(exports) ? Object.keys(exports).filter((key) => key.startsWith('.')) : [] if (subpaths.length === 0) { - return subpath === '.' ? conditionTarget(exports) : undefined + return subpath === '.' ? conditionTarget(exports, conditions) : undefined } const map = exports as Record - if (subpath in map) return conditionTarget(map[subpath]) + if (subpath in map) return conditionTarget(map[subpath], conditions) for (const key of subpaths) { const [prefix, suffix = ''] = key.split('*') if ( @@ -1246,21 +1301,33 @@ function exportsTarget(exports: unknown, subpath: string): string | undefined { prefix.length, subpath.length - suffix.length, ) - return conditionTarget(map[key])?.replaceAll('*', matched) + return conditionTarget(map[key], conditions)?.replaceAll('*', matched) } } return undefined } -function conditionTarget(value: unknown): string | undefined { +/** + * The target of the first branch, in the package's own order, whose + * condition is `default` or one of `conditions` and that leads to a target + */ +function conditionTarget( + value: unknown, + conditions: readonly string[], +): string | undefined { if (typeof value === 'string') return value if (Array.isArray(value)) { - return value.map(conditionTarget).find(Boolean) + return value + .map((entry) => conditionTarget(entry, conditions)) + .find(Boolean) } if (!isRecord(value)) return undefined - return packageConditions - .filter((condition) => condition in value) - .map((condition) => conditionTarget(value[condition])) + return Object.entries(value) + .filter( + ([condition]) => + condition === 'default' || conditions.includes(condition), + ) + .map(([, branch]) => conditionTarget(branch, conditions)) .find(Boolean) } function resolveAliasCandidates( @@ -1291,8 +1358,9 @@ function resolveAliasCandidates( function resolveFile(candidateBase: string): string | undefined { const ext = extname(candidateBase) if (ext) { - if (!jsExtensions.includes(ext)) return undefined - return isFile(candidateBase) ? resolve(candidateBase) : undefined + if (!sourceExtensions.includes(ext)) return undefined + const found = sourceFileCandidates(candidateBase).find(isFile) + return found && resolve(found) } for (const jsExtension of jsExtensions) { diff --git a/packages/plugin-utils/src/index.ts b/packages/plugin-utils/src/index.ts index bfc7b1e1b..078e44eda 100644 --- a/packages/plugin-utils/src/index.ts +++ b/packages/plugin-utils/src/index.ts @@ -3,6 +3,12 @@ export { getBuildOwnership, type ResettableEngine, } from './build-session' +export { + type ConditionResolveOptions, + esmImportConditions, + viteImportConditions, + type ViteConditionConfig, +} from './bundler-conditions' export { loadCompiler } from './compiler' export type { DevupCompiler, @@ -22,6 +28,7 @@ export { type ComputeFileRoutesOptions, computeReachableFiles, type ComputeReachableFilesOptions, + conventionalSourceRoots, createModuleResolver, type CreateModuleResolverOptions, listSourceFiles, @@ -71,6 +78,7 @@ export { ShorthandConfigError, type ShorthandErrorLocation, } from './shorthands' +export { SOURCE_EXTENSIONS, SOURCE_FILE_REGEX } from './source-extensions' export { createStateWriter, type StateWriter, diff --git a/packages/plugin-utils/src/reservation-discovery.ts b/packages/plugin-utils/src/reservation-discovery.ts index abb5a4e81..9f65f4d21 100644 --- a/packages/plugin-utils/src/reservation-discovery.ts +++ b/packages/plugin-utils/src/reservation-discovery.ts @@ -36,6 +36,8 @@ export type ReservationDiscoveryOptions = { readonly importAliases: Readonly readonly toId?: (physicalPath: string) => string readonly prepared?: readonly PreparedReservationInput[] + /** The package `exports` conditions the bundler resolves imports with. */ + readonly conditions?: readonly string[] } export type ReservationInventory = { @@ -111,6 +113,7 @@ export function collectReservationInputs( ...(options.tsconfigPath === undefined ? {} : { tsconfigPath: resolve(cwd, options.tsconfigPath) }), + conditions: options.conditions, } const resolver = createModuleResolver({ ...resolverOptions, diff --git a/packages/plugin-utils/src/source-extensions.ts b/packages/plugin-utils/src/source-extensions.ts new file mode 100644 index 000000000..76b72594e --- /dev/null +++ b/packages/plugin-utils/src/source-extensions.ts @@ -0,0 +1,49 @@ +import { extname } from 'node:path' + +/** + * The extensions of the JavaScript and TypeScript modules the extractor + * compiles, ESM (`.mjs`, `.mts`) and CommonJS (`.cjs`, `.cts`) ones included. + * Every plugin filter, the import graph and the module resolver take files + * by this one list, so a file the graph reaches is also one the bundler + * transforms. + */ +export const SOURCE_EXTENSIONS = [ + '.ts', + '.tsx', + '.mts', + '.cts', + '.js', + '.jsx', + '.mjs', + '.cjs', +] as const + +/** Whether a path ends in one of {@link SOURCE_EXTENSIONS}. */ +export const SOURCE_FILE_REGEX = /\.(?:[mc]?[jt]s|[jt]sx)$/ + +/** + * The TypeScript files an import of a JavaScript path can name, in the order + * TypeScript and bundlers try them: ESM TypeScript is imported as `./a.js`, + * `./a.mjs` or `./a.cjs` while the file on disk is `a.ts`, `a.mts` or `a.cts`. + */ +const TYPESCRIPT_FOR_JAVASCRIPT: Readonly> = { + '.js': ['.ts', '.tsx'], + '.jsx': ['.tsx'], + '.mjs': ['.mts'], + '.cjs': ['.cts'], +} + +/** + * The files an import of `path` can load: `path` itself, then the TypeScript + * sources a JavaScript extension stands for. + */ +export function sourceFileCandidates(path: string): string[] { + const extension = extname(path) + const base = path.slice(0, path.length - extension.length) + return [ + path, + ...(TYPESCRIPT_FOR_JAVASCRIPT[extension] ?? []).map( + (typescript) => `${base}${typescript}`, + ), + ] +} diff --git a/packages/rsbuild-plugin/src/__tests__/atom-hoist.test.ts b/packages/rsbuild-plugin/src/__tests__/atom-hoist.test.ts index a11716515..819ae049a 100644 --- a/packages/rsbuild-plugin/src/__tests__/atom-hoist.test.ts +++ b/packages/rsbuild-plugin/src/__tests__/atom-hoist.test.ts @@ -52,7 +52,7 @@ compiler.hooks.thisCompilation.call(compilation); const jsTransform = transforms.find(item => item.options.test instanceof RegExp).handler; const cssTransform = transforms.find(item => item.options.test === cssDir).handler; const transform = async (file, code) => { - const result = await jsTransform({ code, resourcePath: join(root, file), addDependency() {} }); + const result = await jsTransform({ code, resourcePath: join(root, file), addDependency() {}, environment: { name: 'web' } }); return result.code; }; const loadCss = name => cssTransform({ resourcePath: join(cssDir, name), environment: { name: 'web' } }); diff --git a/packages/rsbuild-plugin/src/__tests__/checkout-isolation.test.ts b/packages/rsbuild-plugin/src/__tests__/checkout-isolation.test.ts index c85723550..f0d575d0e 100644 --- a/packages/rsbuild-plugin/src/__tests__/checkout-isolation.test.ts +++ b/packages/rsbuild-plugin/src/__tests__/checkout-isolation.test.ts @@ -79,6 +79,7 @@ async function extractedCssDirIn(checkout: string) { codeExtractSpy.mockClear() await transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: source, resourcePath: join(checkout, 'src', 'App.tsx'), }) diff --git a/packages/rsbuild-plugin/src/__tests__/plugin.test.ts b/packages/rsbuild-plugin/src/__tests__/plugin.test.ts index 15afbd3ad..dc5c66be0 100644 --- a/packages/rsbuild-plugin/src/__tests__/plugin.test.ts +++ b/packages/rsbuild-plugin/src/__tests__/plugin.test.ts @@ -1,6 +1,7 @@ import assert from 'node:assert/strict' import * as fs from 'node:fs' import * as fsPromises from 'node:fs/promises' +import { tmpdir } from 'node:os' import { join, resolve } from 'node:path' import * as pluginUtils from '@devup-ui/plugin-utils' @@ -17,6 +18,7 @@ import { import { DevupUI as createPlugin } from '../plugin' +const realExistsSync = fs.existsSync const wasm = pluginUtils.loadCompiler('wasm') const DevupUI = (options: Parameters[0] = {}) => createPlugin({ ...options, engine: 'wasm' }) @@ -212,6 +214,7 @@ describe('DevupUIRsbuildPlugin', () => { ) const addDependency = mock() await transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: "import { Box } from '@devup-ui/react'", resourcePath: 'src/App.tsx', addDependency, @@ -222,6 +225,57 @@ describe('DevupUIRsbuildPlugin', () => { } }) + it('resolves imports with the conditions of the environment it transforms for', async () => { + const root = fs.mkdtempSync(join(tmpdir(), 'devup-ui-rsbuild-conditions-')) + const setModuleResolverSpy = spyOn( + wasm, + 'setModuleResolver', + ).mockReturnValue(undefined) + try { + await Bun.write( + join(root, 'node_modules/split/package.json'), + JSON.stringify({ + exports: { browser: './browser.js', node: './node.js' }, + }), + ) + await Bun.write(join(root, 'node_modules/split/browser.js'), 'browser') + await Bun.write(join(root, 'node_modules/split/node.js'), 'node') + const transform = mock() + const modifyRspackConfig = mock() + await DevupUI({ atomHoist: 0 }).setup( + createSetupContext({ transform, modifyRspackConfig }), + ) + for (const [name, condition] of [ + ['web', 'browser'], + ['server', 'node'], + ]) + for (const [modify] of modifyRspackConfig.mock.calls) + modify( + { resolve: { conditionNames: [condition] } }, + { environment: { name, entry: {} } }, + ) + codeExtractSpy.mockReturnValue(createCodeExtractResult()) + const resolved = async (name: string) => { + await transform.mock.calls[1][1]({ + environment: { name }, + code: '', + resourcePath: join(root, 'App.tsx'), + addDependency: mock(), + }) + const resolveModule = setModuleResolverSpy.mock.calls.at(-1)?.[0] as ( + specifier: string, + importer: string, + ) => { code: string } | undefined + return resolveModule('split', join(root, 'App.tsx'))?.code + } + expect(await resolved('web')).toBe('browser') + expect(await resolved('server')).toBe('node') + } finally { + setModuleResolverSpy.mockRestore() + fs.rmSync(root, { recursive: true, force: true }) + } + }) + it('should return a plugin object with correct name', async () => { const plugin = DevupUI() expect(plugin).toBeDefined() @@ -350,7 +404,7 @@ describe('DevupUIRsbuildPlugin', () => { expect(transform).toHaveBeenCalled() expect(transform).toHaveBeenCalledWith( { - test: /\.(tsx|ts|js|mjs|jsx)$/, + test: pluginUtils.SOURCE_FILE_REGEX, }, expect.any(Function), ) @@ -385,7 +439,7 @@ describe('DevupUIRsbuildPlugin', () => { expect(transform).toHaveBeenCalled() expect(transform).toHaveBeenCalledWith( { - test: /\.(tsx|ts|js|mjs|jsx)$/, + test: pluginUtils.SOURCE_FILE_REGEX, }, expect.any(Function), ) @@ -399,6 +453,7 @@ describe('DevupUIRsbuildPlugin', () => { ) await expect( transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: `import { Box } from '@devup-ui/react' const App = () => `, resourcePath: 'src/App.tsx', @@ -409,6 +464,7 @@ const App = () => `, }) await expect( transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: `import { Box } from '@devup-ui/react' const App = () => `, resourcePath: 'node_modules/@wrong-ui/react/index.tsx', @@ -438,6 +494,7 @@ export const a = ` ) await expect( transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: source, resourcePath: 'src/App.tsx', }), @@ -468,6 +525,7 @@ export const a =
` ) await expect( transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: source, resourcePath: 'src/App.tsx', }), @@ -497,7 +555,7 @@ export const a =
` expect(transform).toHaveBeenCalled() expect(transform).toHaveBeenCalledWith( { - test: /\.(tsx|ts|js|mjs|jsx)$/, + test: pluginUtils.SOURCE_FILE_REGEX, }, expect.any(Function), ) @@ -511,6 +569,7 @@ export const a =
` }), ) const ret = await transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: `import { Box } from '@devup-ui/react' const App = () => `, resourcePath: 'src/App.tsx', @@ -527,6 +586,7 @@ const App = () => `, ) const ret1 = await transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: `import { Box } from '@devup-ui/react' const App = () => `, resourcePath: 'node_modules/@devup-ui/react/index.tsx', @@ -578,6 +638,7 @@ const App = () => `, codeExtractSpy.mockReturnValue(createCodeExtractResult()) const addDependency = mock() await transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: "import { Box } from '@devup-ui/react'", resourcePath: 'src/App.tsx', addDependency, @@ -839,6 +900,7 @@ const App = () => `, const userRspack = config.tools.rspack modifyRsbuildConfig.mock.calls[0][0](config) await transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: 'source', resourcePath, addDependency: mock(), @@ -877,6 +939,37 @@ const App = () => `, ) }) + it('plans hoisting over one graph of the src and app roots', async () => { + spies() + const project = fs.mkdtempSync(join(tmpdir(), 'devup-ui-rsbuild-roots-')) + try { + for (const path of ['app/page.tsx', 'src/a.tsx', 'web/app/b.tsx']) + await Bun.write(join(project, path), 'export {}') + existsSyncSpy.mockImplementation((path) => realExistsSync(String(path))) + const cwd = spyOn(process, 'cwd').mockReturnValue(project) + try { + await DevupUI({ atomHoist: 2 }).setup( + createSetupContext({ context: { rootPath: join(project, 'web') } }), + ) + } finally { + cwd.mockRestore() + } + const [{ graph }] = buildCanonicalMapSpy.mock.calls.at(-1) as [ + { graph: { files: string[] } }, + ] + expect(graph.files).toEqual( + ['app/page.tsx', 'src/a.tsx', 'web/app/b.tsx'].map((path) => + join(project, path), + ), + ) + expect(computeFileReachSpy).toHaveBeenCalledWith( + expect.objectContaining({ graph, srcDir: project }), + ) + } finally { + fs.rmSync(project, { recursive: true, force: true }) + } + }) + it('composes collapse + hoist and folds reach onto the canonical bucket', async () => { spies() buildCanonicalMapSpy.mockReturnValue({ @@ -983,6 +1076,7 @@ const App = () => `, // calls[1] is the source transform; atom mode posix-normalizes the // filename and passes a relative cssDir + import_main_css_in_code=true. await transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: `import { Box } from '@devup-ui/react'\nconst A = () => `, resourcePath: 'src/App.tsx', }) @@ -1146,6 +1240,7 @@ const App = () => `, // When for (const name of [first, second]) { await transform.mock.calls[1][1]({ + environment: { name: 'web' }, code: 'source', resourcePath: resolve('src', `${name}.tsx`), addDependency: mock(), @@ -1176,11 +1271,11 @@ const App = () => `, }, }) expect(computeReachableFilesSpy).toHaveBeenCalledWith({ - srcDir: resolve(process.cwd(), 'src'), - tsconfigPath: resolve(process.cwd(), 'tsconfig.json'), + srcDir: process.cwd(), entries: ['a', 'b', 'c', 'd'].map((name) => resolve(process.cwd(), `./src/${name}.tsx`), ), + graph: expect.objectContaining({ files: expect.any(Array) }), }) expect(codeExtractSpy).toHaveBeenCalledWith( resolve('src', 'App.tsx'), diff --git a/packages/rsbuild-plugin/src/plugin.ts b/packages/rsbuild-plugin/src/plugin.ts index 91dae66c8..d3162db4d 100644 --- a/packages/rsbuild-plugin/src/plugin.ts +++ b/packages/rsbuild-plugin/src/plugin.ts @@ -5,8 +5,10 @@ import { basename, dirname, join, relative, resolve } from 'node:path' import { beginBuild, buildCanonicalMap, + buildStaticImportGraph, computeFileReach, computeReachableFiles, + conventionalSourceRoots, createCompatTypes, createModuleResolver, createNodeModulesExcludeRegex, @@ -15,6 +17,7 @@ import { type CustomShorthands, type DevupCompiler, type DevupUIBasePluginOptions, + esmImportConditions, getFileNumByFilename, type ImportAliases, loadCompiler, @@ -23,6 +26,7 @@ import { normalizeShorthands, planAtomHoist, readJsxImportSource, + SOURCE_FILE_REGEX, tailwindCssFiles, withTailwindCss, } from '@devup-ui/plugin-utils' @@ -233,28 +237,41 @@ export const DevupUI = ({ // absolute keys (keyBy: 'absolute') and the extraction filename is // POSIX-normalized to match. const atomMode = !singleCss && Number.isFinite(atomHoist) && atomHoist > 0 - compiler.setModuleResolver( + const moduleResolver = (conditions?: readonly string[]) => createModuleResolver({ toId: (path) => (atomMode ? path.replaceAll('\\', '/') : path), - }), - ) + conditions, + }) + compiler.setModuleResolver(moduleResolver()) + // Each environment resolves imports with its own `exports` conditions + const environmentResolvers = new Map< + string, + ReturnType + >() + // The `src` and `app` directories of the working directory and of the + // Rsbuild root, scanned once for the canonical map and the routes + const sourceGraph = (srcDir: string) => + buildStaticImportGraph( + srcDir, + resolve(srcDir, 'tsconfig.json'), + conventionalSourceRoots(process.cwd(), api.context.rootPath), + ) if (atomMode) { try { const root = process.cwd() - const srcDir = resolve(root, 'src') - const tsconfigPath = resolve(root, 'tsconfig.json') + const graph = sourceGraph(root) const canonicalMap = buildCanonicalMap({ - srcDir, - tsconfigPath, + srcDir: root, cwd: root, keyBy: 'absolute', + graph, }) compiler.importCanonicalMap(canonicalMap) const fileReach = computeFileReach({ - srcDir, - tsconfigPath, + srcDir: root, cwd: root, keyBy: 'absolute', + graph, }) const plan = planAtomHoist(canonicalMap, fileReach, atomHoist) if (plan) { @@ -283,17 +300,18 @@ export const DevupUI = ({ compiler, ) const entryFiles = new Set() - // Extract the source files under `src` that the entries reach, in path - // order, the same way the transform does, so that a stylesheet built on - // its first import already holds the styles of every one. Best-effort: - // a stylesheet still missing styles is rebuilt by another pass. + // Extract the source files under `src` and `app` that the entries + // reach, in path order, the same way the transform does, so that a + // stylesheet built on its first import already holds the styles of every + // one. Best-effort: a stylesheet still missing styles is rebuilt by + // another pass. const prewarm = () => { try { const root = api.context.rootPath for (const file of computeReachableFiles({ - srcDir: resolve(root, 'src'), - tsconfigPath: resolve(root, 'tsconfig.json'), + srcDir: root, entries: [...entryFiles], + graph: sourceGraph(root), })) { let extractCssDir = relative(dirname(file), cssDir).replaceAll( '\\', @@ -351,6 +369,10 @@ export const DevupUI = ({ api.modifyRspackConfig((config, { environment }) => { for (const request of staticEntries(environment.entry)) entryFiles.add(resolve(api.context.rootPath, request)) + environmentResolvers.set( + environment.name, + moduleResolver(esmImportConditions(config.resolve)), + ) config.plugins ??= [] config.plugins.push({ apply(compiler: Rspack.Compiler) { @@ -460,9 +482,9 @@ export const DevupUI = ({ api.transform( { - test: /\.(tsx|ts|js|mjs|jsx)$/, + test: SOURCE_FILE_REGEX, }, - async ({ code, resourcePath, addDependency }) => { + async ({ code, resourcePath, addDependency, environment }) => { if (createNodeModulesExcludeRegex(include).test(resourcePath)) return code for (const file of tailwindFiles) addDependency(file) @@ -488,6 +510,9 @@ export const DevupUI = ({ const extractName = atomMode ? resourcePath.replaceAll('\\', '/') : resourcePath + const environmentResolver = environmentResolvers.get(environment.name) + if (environmentResolver) + compiler.setModuleResolver(environmentResolver) const { code: retCode, map, diff --git a/packages/vite-plugin/src/__tests__/plugin.test.ts b/packages/vite-plugin/src/__tests__/plugin.test.ts index cbc3643da..b19048495 100644 --- a/packages/vite-plugin/src/__tests__/plugin.test.ts +++ b/packages/vite-plugin/src/__tests__/plugin.test.ts @@ -2149,4 +2149,51 @@ describe('module resolver', () => { setModuleResolverSpy.mockRestore() } }) + + it('resolves packages with the conditions Vite resolves them with', async () => { + const root = fs.mkdtempSync(join(os.tmpdir(), 'devup-vite-conditions-')) + const setModuleResolverSpy = spyOn( + wasm, + 'setModuleResolver', + ).mockReturnValue(undefined) + try { + await Bun.write( + join(root, 'node_modules/split/package.json'), + JSON.stringify({ + exports: { + node: './node.js', + browser: './browser.js', + production: './production.js', + }, + }), + ) + await Bun.write(join(root, 'node_modules/split/browser.js'), 'browser') + await Bun.write( + join(root, 'node_modules/split/production.js'), + 'production', + ) + const resolved = async (config: object) => { + await createPlugin({}).configResolved({ root, ...config }) + const resolveModule = setModuleResolverSpy.mock.calls.at(-1)?.[0] as ( + specifier: string, + importer: string, + ) => { code: string } | undefined + return resolveModule('split', join(root, 'src/App.tsx'))?.code + } + expect( + await resolved({ + resolve: { conditions: ['module', 'browser'] }, + }), + ).toBe('browser') + expect( + await resolved({ + isProduction: true, + resolve: { conditions: ['development|production'] }, + }), + ).toBe('production') + } finally { + setModuleResolverSpy.mockRestore() + fs.rmSync(root, { recursive: true, force: true }) + } + }) }) diff --git a/packages/vite-plugin/src/__tests__/provenance-relevance.test.ts b/packages/vite-plugin/src/__tests__/provenance-relevance.test.ts index 1028a4325..c32b941b9 100644 --- a/packages/vite-plugin/src/__tests__/provenance-relevance.test.ts +++ b/packages/vite-plugin/src/__tests__/provenance-relevance.test.ts @@ -205,7 +205,27 @@ for (const operation of ['create', 'delete', 'recreate']) { }) } -for (const suffix of ['json', 'css', 'cjs']) { +for (const suffix of ['mts', 'cts', 'cjs']) { + it(`admits a changed ${suffix} module as a source`, () => { + // Given + using project = fixture() + const reservation = createViteReservation(wasm, { + package: '@devup-ui/react', + include: [], + importAliases: {}, + singleCss: true, + cssDir: join(project.root, 'df/css'), + distDir: join(project.root, 'df'), + }) + const revision = reservation.revision + // When + reservation.invalidate(join(project.root, `arbitrary.${suffix}`), 9) + // Then + expect(reservation.revision).toBe(revision + 1) + }) +} + +for (const suffix of ['json', 'css']) { it(`admits cross-environment compiled ${suffix} supplier without blanket suffix admission`, () => { // Given using project = fixture() diff --git a/packages/vite-plugin/src/plugin.ts b/packages/vite-plugin/src/plugin.ts index f9ce736fc..3b46b828d 100644 --- a/packages/vite-plugin/src/plugin.ts +++ b/packages/vite-plugin/src/plugin.ts @@ -23,7 +23,9 @@ import { normalizeShorthands, readJsxImportSource, seedFileNumbers, + SOURCE_FILE_REGEX, tailwindCssFiles, + viteImportConditions, withTailwindCss, } from '@devup-ui/plugin-utils' import type { @@ -501,6 +503,7 @@ export function DevupUI({ compiler.setModuleResolver( createModuleResolver({ cwd: projectRoot, + conditions: viteImportConditions(config), toId: (path) => path.replaceAll('\\', '/'), }), ) @@ -747,7 +750,7 @@ export function DevupUI({ if (!extractCss) return const fileName = id.split('?')[0] - if (!/\.(tsx|ts|js|mjs|jsx)$/i.test(fileName)) return + if (!SOURCE_FILE_REGEX.test(fileName.toLowerCase())) return if (createNodeModulesExcludeRegex(include).test(fileName)) { return } diff --git a/packages/vite-plugin/src/reservation-inputs.ts b/packages/vite-plugin/src/reservation-inputs.ts index 802dde59f..5a5a5592e 100644 --- a/packages/vite-plugin/src/reservation-inputs.ts +++ b/packages/vite-plugin/src/reservation-inputs.ts @@ -1,6 +1,7 @@ import { readFileSync } from 'node:fs' import { resolve } from 'node:path' +import type { ViteConditionConfig } from '@devup-ui/plugin-utils' import type { BuildOptions } from 'vite' import { @@ -16,16 +17,19 @@ export type ReservationEnvironment = { > } -export type ReservationConfig = ReservationEnvironment & { - readonly command?: 'build' | 'serve' - readonly base?: string - readonly root?: string - readonly publicDir?: string | false - readonly env?: Readonly> - readonly define?: Readonly> - readonly environments?: Readonly> - readonly configFileDependencies?: readonly string[] -} +export type ReservationConfig = ReservationEnvironment & + ViteConditionConfig & { + readonly command?: 'build' | 'serve' + readonly base?: string + readonly root?: string + readonly publicDir?: string | false + readonly env?: Readonly< + Record + > + readonly define?: Readonly> + readonly environments?: Readonly> + readonly configFileDependencies?: readonly string[] + } export function environmentInputs( environment: ReservationEnvironment, diff --git a/packages/vite-plugin/src/reservation.ts b/packages/vite-plugin/src/reservation.ts index c767e8fc4..73db38ac9 100644 --- a/packages/vite-plugin/src/reservation.ts +++ b/packages/vite-plugin/src/reservation.ts @@ -10,6 +10,8 @@ import { type DevupCompiler, planAtomHoist, reservationGraph, + SOURCE_FILE_REGEX, + viteImportConditions, type WasmImportAliases, } from '@devup-ui/plugin-utils' @@ -59,7 +61,7 @@ export function createViteReservation( ) return if ( - !/\.(tsx|ts|js|mjs|jsx)$/i.test(path) && + !SOURCE_FILE_REGEX.test(path.toLowerCase()) && !explicitInputs.has(path) && !dependencies.some((dependency) => resolve(dependency) === path) && !options.relevant?.(path) @@ -80,9 +82,11 @@ export function createViteReservation( const watched = new Set() const roots = new Set() let cwd: string | undefined + let conditions: readonly string[] | undefined for (const { config, environment } of environments.values()) { const root = config.root ?? process.cwd() cwd ??= root + conditions ??= viteImportConditions(config) for (const name of ['src', 'app']) { const path = resolve(root, name) watched.add(path) @@ -105,6 +109,7 @@ export function createViteReservation( createModuleResolver({ cwd: cwd ?? process.cwd(), toId: (path) => path.replaceAll('\\', '/'), + conditions, }), ) slots.reserve( @@ -116,6 +121,7 @@ export function createViteReservation( package: options.package, importAliases: options.importAliases, toId: (path) => path.replaceAll('\\', '/'), + conditions, }, { beforeReserve(inventory) { diff --git a/packages/webpack-plugin/src/__tests__/loader.test.ts b/packages/webpack-plugin/src/__tests__/loader.test.ts index 4da6cbe60..fe8cd872f 100644 --- a/packages/webpack-plugin/src/__tests__/loader.test.ts +++ b/packages/webpack-plugin/src/__tests__/loader.test.ts @@ -1,4 +1,6 @@ +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' import * as fsPromises from 'node:fs/promises' +import { tmpdir } from 'node:os' import * as nodePath from 'node:path' import { join } from 'node:path' @@ -26,7 +28,10 @@ interface TestLoaderContext extends Pick< 'async' | 'resourcePath' | 'addDependency' > { getOptions: () => Partial - _compiler?: { __DEVUP_CACHE: string } + _compiler?: { + __DEVUP_CACHE: string + options?: { resolve: { conditionNames?: string[] } } + } } function createCodeExtractResult( @@ -212,6 +217,56 @@ describe('devupUILoader', () => { setModuleResolverSpy.mockRestore() } }) + + it('resolves packages with the conditions of the compiler and keeps one resolver per set', async () => { + const root = mkdtempSync(join(tmpdir(), 'devup-ui-loader-conditions-')) + const setModuleResolverSpy = spyOn( + wasm, + 'setModuleResolver', + ).mockReturnValue(undefined) + try { + mkdirSync(join(root, 'node_modules/split'), { recursive: true }) + writeFileSync( + join(root, 'node_modules/split/package.json'), + JSON.stringify({ + exports: { browser: './browser.js', node: './node.js' }, + }), + ) + writeFileSync(join(root, 'node_modules/split/browser.js'), 'browser') + writeFileSync(join(root, 'node_modules/split/node.js'), 'node') + codeExtractSpy.mockReturnValue(createCodeExtractResult()) + const load = async (conditionNames: string[]) => { + const callback = mock() + devupUILoader.bind( + createLoaderContext( + { package: 'package', cssDir: 'cssDir', singleCss: true }, + callback, + 'index.tsx', + { + _compiler: { + __DEVUP_CACHE: '', + options: { resolve: { conditionNames } }, + }, + }, + ), + )(Buffer.from('code'), 'index.tsx') + await waitFor(() => expect(callback).toHaveBeenCalled()) + const resolveModule = setModuleResolverSpy.mock.calls.at(-1)?.[0] as ( + specifier: string, + importer: string, + ) => { code: string } | undefined + return resolveModule + } + const browser = await load(['browser']) + expect(browser('split', join(root, 'App.tsx'))?.code).toBe('browser') + expect(await load(['browser'])).toBe(browser) + const node = await load(['node']) + expect(node('split', join(root, 'App.tsx'))?.code).toBe('node') + } finally { + setModuleResolverSpy.mockRestore() + rmSync(root, { recursive: true, force: true }) + } + }) it.each( createTestMatrix({ updatedBaseStyle: [true, false], @@ -219,6 +274,7 @@ describe('devupUILoader', () => { )('should extract code with css', async (options) => { const _compiler = { __DEVUP_CACHE: '', + options: { resolve: {} }, } const asyncCallback = mock() const t = createLoaderContext( diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index f22c489e3..de24d64ad 100644 --- a/packages/webpack-plugin/src/__tests__/plugin.test.ts +++ b/packages/webpack-plugin/src/__tests__/plugin.test.ts @@ -2,6 +2,7 @@ import assert from 'node:assert/strict' import type { Stats } from 'node:fs' import * as fs from 'node:fs' import * as fsPromises from 'node:fs/promises' +import { tmpdir } from 'node:os' import { join, relative, resolve } from 'node:path' import * as pluginUtils from '@devup-ui/plugin-utils' @@ -19,6 +20,7 @@ import type { Compiler } from 'webpack' import { DevupUIWebpackPlugin } from '../plugin' import { servedCss } from '../served-css' +const realExistsSync = fs.existsSync const native = pluginUtils.loadCompiler('native') const wasm = pluginUtils.loadCompiler('wasm') @@ -859,6 +861,40 @@ describe('devupUIWebpackPlugin', () => { computeReachableFilesSpy.mockRestore() }) + it('plans collapse and hoisting over one graph of every src and app root', async () => { + const project = fs.mkdtempSync(join(tmpdir(), 'devup-ui-webpack-roots-')) + try { + for (const path of ['app/page.tsx', 'src/a.tsx', 'web/src/b.tsx']) + await Bun.write(join(project, path), 'export {}') + existsSyncSpy.mockImplementation((path) => realExistsSync(String(path))) + const plugin = new DevupUIWebpackPlugin({ + engine: 'wasm', + atomHoist: 2, + }) + const compiler = createCompiler() + compiler.options.context = join(project, 'web') + const cwd = spyOn(process, 'cwd').mockReturnValue(project) + try { + plugin.apply(asCompiler(compiler)) + } finally { + cwd.mockRestore() + } + const [{ graph }] = buildCanonicalMapSpy.mock.calls.at(-1) as [ + { graph: { files: string[] } }, + ] + expect(graph.files).toEqual( + ['app/page.tsx', 'src/a.tsx', 'web/src/b.tsx'].map((path) => + join(project, path), + ), + ) + expect(computeFileReachSpy).toHaveBeenCalledWith( + expect.objectContaining({ graph, srcDir: project }), + ) + } finally { + fs.rmSync(project, { recursive: true, force: true }) + } + }) + it('runs single-importer collapse when atomHoist is explicitly zero, without hoisting', () => { // Constraint: single-importer collapse must ALWAYS be on. The canonical // map is built + imported unconditionally; only atom HOISTING is gated. @@ -1031,12 +1067,12 @@ describe('devupUIWebpackPlugin', () => { plugin.apply(asCompiler(compiler)) compiler.hooks.run.tap.mock.calls[0][1]() expect(computeReachableFilesSpy).toHaveBeenCalledWith({ - srcDir: resolve(process.cwd(), 'src'), - tsconfigPath: resolve(process.cwd(), 'tsconfig.json'), + srcDir: process.cwd(), entries: [ resolve('/project', './src/parent'), resolve('/project', './src/other'), ], + graph: expect.objectContaining({ files: expect.any(Array) }), }) // The loader's resolver is registered before the first extraction, so // imported constants inline as they do in the loader diff --git a/packages/webpack-plugin/src/loader.ts b/packages/webpack-plugin/src/loader.ts index ee4db747f..8c6ae7176 100644 --- a/packages/webpack-plugin/src/loader.ts +++ b/packages/webpack-plugin/src/loader.ts @@ -7,6 +7,7 @@ import { type DevupCompiler, type DevupCompilerOutput, type DevupEngine, + esmImportConditions, loadCompiler, } from '@devup-ui/plugin-utils' import type { LoaderContext, RawLoaderDefinitionFunction } from 'webpack' @@ -41,16 +42,31 @@ function parseSourceMap(sourceMap: string | undefined): LoaderSourceMap { const stateWriter = createStateWriter((path, content, encoding) => encoding ? writeFile(path, content, encoding) : writeFile(path, content), ) -let moduleResolver: ReturnType | undefined +let moduleResolver: + | { + readonly conditions: string + readonly resolve: ReturnType + } + | undefined -/** Resolve imports to the cwd-relative ids this loader extracts files under */ +/** + * Resolve imports to the cwd-relative ids this loader extracts files under, + * with the `exports` conditions the bundler resolves the same imports with + */ function setCwdModuleResolver( compiler: Pick, + conditions: readonly string[] | undefined, ): void { - moduleResolver ??= createModuleResolver({ - toId: (path) => relative(process.cwd(), path).replaceAll('\\', '/'), - }) - compiler.setModuleResolver(moduleResolver) + const key = JSON.stringify(conditions ?? null) + if (moduleResolver?.conditions !== key) + moduleResolver = { + conditions: key, + resolve: createModuleResolver({ + conditions, + toId: (path) => relative(process.cwd(), path).replaceAll('\\', '/'), + }), + } + compiler.setModuleResolver(moduleResolver.resolve) } const devupUILoader: RawLoaderDefinitionFunction = @@ -86,7 +102,10 @@ const devupUILoader: RawLoaderDefinitionFunction = const relativePath = relative(process.cwd(), id).replaceAll('\\', '/') if (!relCssDir.startsWith('./')) relCssDir = `./${relCssDir}` - setCwdModuleResolver(compiler) + setCwdModuleResolver( + compiler, + esmImportConditions(this._compiler?.options.resolve), + ) const output: DevupCompilerOutput = compiler.codeExtract( relativePath, source.toString(), diff --git a/packages/webpack-plugin/src/plugin.ts b/packages/webpack-plugin/src/plugin.ts index 2c5bd2ed0..9b83afd81 100644 --- a/packages/webpack-plugin/src/plugin.ts +++ b/packages/webpack-plugin/src/plugin.ts @@ -6,9 +6,11 @@ import { dirname, join, relative, resolve } from 'node:path' import { beginBuild, buildCanonicalMap, + buildStaticImportGraph, collectNumberedFiles, computeFileReach, computeReachableFiles, + conventionalSourceRoots, createCompatTypes, createModuleResolver, createNodeModulesExcludeRegex, @@ -16,6 +18,7 @@ import { type CustomShorthands, type DevupCompiler, type DevupUIBasePluginOptions, + esmImportConditions, extractedNeedles, getFileNumByFilename, type ImportAliases, @@ -26,6 +29,7 @@ import { planAtomHoist, readJsxImportSource, seedFileNumbers, + SOURCE_FILE_REGEX, tailwindCssFiles, type WasmImportAliases, withTailwindCss, @@ -171,16 +175,33 @@ export class DevupUIWebpackPlugin { } /** - * Extract the source files under `src` that `entries` reach into the shared - * compiler sheet, in path order, so that a stylesheet built on its first import - * holds the styles of every one (all collapsed members of a bucket, and the - * shared base), not just those of the modules webpack happened to build - * first. Mirrors the loader's `codeExtract` call (same filename keying + - * options) so re-extraction during compilation is idempotent. Generic entry - * discovery remains best-effort; an adapter's complete set is authoritative - * and its read/extraction failures must fail the build. + * The import graph of the project's `src` and `app` directories, under both + * the working directory and webpack's `context` */ - private prewarmExtractor(entries: string[]) { + private sourceGraph(context: string | undefined) { + const cwd = process.cwd() + return buildStaticImportGraph( + cwd, + resolve(cwd, 'tsconfig.json'), + conventionalSourceRoots(cwd, context ?? cwd), + ) + } + + /** + * Extract the source files under `src` and `app` that `entries` reach into + * the shared compiler sheet, in path order, so that a stylesheet built on + * its first import holds the styles of every one (all collapsed members of a + * bucket, and the shared base), not just those of the modules webpack + * happened to build first. Mirrors the loader's `codeExtract` call (same + * filename keying + options) so re-extraction during compilation is + * idempotent. Generic entry discovery remains best-effort; an adapter's + * complete set is authoritative and its read/extraction failures must fail + * the build. + */ + private prewarmExtractor( + entries: string[], + { context, resolve: resolveOptions }: Compiler['options'], + ) { try { const cwd = process.cwd() // The same resolver as the loader's, so imported constants and @@ -188,6 +209,7 @@ export class DevupUIWebpackPlugin { this.compiler.setModuleResolver( createModuleResolver({ toId: (path) => relative(cwd, path).replaceAll('\\', '/'), + conditions: esmImportConditions(resolveOptions), }), ) const files = @@ -195,9 +217,9 @@ export class DevupUIWebpackPlugin { resolve(cwd, file), ) ?? computeReachableFiles({ - srcDir: resolve(cwd, 'src'), - tsconfigPath: resolve(cwd, 'tsconfig.json'), + srcDir: cwd, entries, + graph: this.sourceGraph(context), }) if (this.options.productionPrewarmFiles !== undefined) { seedFileNumbers(this.compiler, [...this.options.productionPrewarmFiles]) @@ -299,23 +321,24 @@ export class DevupUIWebpackPlugin { // The canonical map is built + imported unconditionally; only atom HOISTING // composes on top in effective atom mode. Mirrors next-plugin's pre-pass. try { - const srcDir = resolve(process.cwd(), 'src') - const tsconfigPath = resolve(process.cwd(), 'tsconfig.json') const cwd = process.cwd() + // One graph over every source root serves both the canonical map and + // the routes, so an `app` directory is hoisted like `src` + const graph = this.sourceGraph(compiler.options.context) const canonicalMap = buildCanonicalMap({ - srcDir, - tsconfigPath, + srcDir: cwd, cwd, keyBy: 'cwd-relative', + graph, }) this.compiler.importCanonicalMap(canonicalMap) if (atomMode) { const fileReach = computeFileReach({ - srcDir, - tsconfigPath, + srcDir: cwd, cwd, keyBy: 'cwd-relative', + graph, }) const plan = planAtomHoist(canonicalMap, fileReach, atomHoist) if (plan) { @@ -386,6 +409,7 @@ export class DevupUIWebpackPlugin { : Object.values(entry ?? {}).flatMap(({ import: requests = [] }) => requests.map((request) => resolve(context, request)), ), + compiler.options, ) } compiler.hooks.run.tap( @@ -532,7 +556,7 @@ export class DevupUIWebpackPlugin { compiler.options.module.rules.push( { - test: /\.(tsx|ts|js|mjs|jsx)$/, + test: SOURCE_FILE_REGEX, exclude: createNodeModulesExcludeRegex( this.options.include, '.mdx.[tj]sx?$', diff --git a/packages/webpack-plugin/src/reservation.ts b/packages/webpack-plugin/src/reservation.ts index 7caebdc1b..e5f1b97df 100644 --- a/packages/webpack-plugin/src/reservation.ts +++ b/packages/webpack-plugin/src/reservation.ts @@ -4,6 +4,7 @@ import { createModuleResolver, createSharedSlotReservation, type DevupCompiler, + esmImportConditions, type WasmImportAliases, } from '@devup-ui/plugin-utils' import type { Compiler } from 'webpack' @@ -19,11 +20,14 @@ export function createWebpackReservation( return ({ context, entry, - }: Pick) => { + resolve: resolveOptions, + }: Pick & + Partial>) => { const cwd = process.cwd() const root = resolve(context ?? cwd) const toId = (path: string) => relative(cwd, path).replaceAll('\\', '/') - compiler.setModuleResolver(createModuleResolver({ toId })) + const conditions = esmImportConditions(resolveOptions) + compiler.setModuleResolver(createModuleResolver({ toId, conditions })) slots.invalidate() slots.reserve( { @@ -47,6 +51,7 @@ export function createWebpackReservation( package: options.package, importAliases, toId, + conditions, }, { singleCss: options.singleCss, From 9a7999045aa0c6798032b67146c7e0ddd99acf5d Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 20:24:56 +0900 Subject: [PATCH 2/3] style(plugin-utils): sort the bundler-conditions exports Co-Authored-By: Claude Opus 5.5 --- packages/plugin-utils/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/plugin-utils/src/index.ts b/packages/plugin-utils/src/index.ts index 078e44eda..781563b58 100644 --- a/packages/plugin-utils/src/index.ts +++ b/packages/plugin-utils/src/index.ts @@ -6,8 +6,8 @@ export { export { type ConditionResolveOptions, esmImportConditions, - viteImportConditions, type ViteConditionConfig, + viteImportConditions, } from './bundler-conditions' export { loadCompiler } from './compiler' export type { From 3bee18465393a9905c58654c87ae121c0100d960 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Tue, 17 Nov 2026 11:50:18 +0900 Subject: [PATCH 3/3] docs(bun-plugin): describe which files the runtime and Bun.build compile The runtime filter matches `.ts`, `.mts`, `.cts`, `.tsx`, `.jsx` and `.mjs` files anywhere, `node_modules` included, and `.js` files of `@devup-ui` packages. `Bun.build` adds every `.js` and `.cjs` file whose path is outside `node_modules`, which includes linked workspace packages resolved to their real path. Bun reads the source a runtime plugin returns as an ES module, so a `.cts` file now matched under the runtime loses the exports of `module.exports` or `export =`; `Bun.build` reads it as CommonJS. Say so in the README and the limitations page, which still listed the old extensions. Co-Authored-By: Claude Opus 5.5 --- .../app/(detail)/docs/limitations/page.mdx | 2 +- packages/bun-plugin/README.md | 22 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/apps/landing/src/app/(detail)/docs/limitations/page.mdx b/apps/landing/src/app/(detail)/docs/limitations/page.mdx index 163d64b62..85be95b4e 100644 --- a/apps/landing/src/app/(detail)/docs/limitations/page.mdx +++ b/apps/landing/src/app/(detail)/docs/limitations/page.mdx @@ -96,7 +96,7 @@ A dynamic value such as `width` becomes a CSS variable set in `style`, so a comp - Inside ``, `cx(getClass())` is a build error; put the result in a variable first, which then joins as a class. - `withComponent` takes a tag name or a component written as a name or `a.b`. A target only the runtime knows, as `withComponent(make())`, is a build error: the styled component compiles to a plain function with no `withComponent`. Pass the target directly, or render with `as`. - The plugins read `jsxImportSource` from the `tsconfig.json` (or `jsconfig.json`) in the working directory. Opt out with `importAliases: { '@emotion/react/jsx-runtime': false }`. -- The Bun plugin compiles `.ts`, `.tsx`, `.jsx` and `.mjs` files, but `.js` files only inside `@devup-ui` packages: Devup UI code in a `.js` file of your own project reaches the runtime placeholders. Use `.jsx`, `.tsx` or `.mjs` for it. A Bun runtime plugin must return the source of every file it matches, and Bun reads that source as an ES module, so matching `.js` files would break CommonJS ones. +- The Bun plugin compiles `.ts`, `.mts`, `.cts`, `.tsx`, `.jsx` and `.mjs` files, and `.js` files inside `@devup-ui` packages; `Bun.build` also compiles every `.js` and `.cjs` file outside `node_modules`. Under the Bun runtime, Devup UI code in a `.js` or `.cjs` file of your own project reaches the runtime placeholders. Use `.jsx`, `.tsx` or `.mjs` for it. A Bun runtime plugin must return the source of every file it matches, and Bun reads that source as an ES module, so matching `.js` files would break CommonJS ones. For the same reason, a `.cts` file loaded under the Bun runtime must use `import` and `export`, not `module.exports` or `export =`. - Code that only the runtime can compute — other globals, `Date`, `Math.random`, `new`, classes, async code — is never run by the build; on an element it stays a CSS variable, elsewhere it is a build error. ## Build errors diff --git a/packages/bun-plugin/README.md b/packages/bun-plugin/README.md index 76adef852..c6448d06e 100644 --- a/packages/bun-plugin/README.md +++ b/packages/bun-plugin/README.md @@ -63,17 +63,27 @@ Class names are short in `Bun.build` and readable under the runtime; pass ## Compiled Files `.ts`, `.mts`, `.cts`, `.tsx`, `.jsx` and `.mjs` files are compiled wherever -they are, and `.js` files inside `@devup-ui` packages. `Bun.build` also -compiles the `.js` and `.cjs` files of your own project outside -`node_modules`. Under the runtime, Devup UI code in a `.js` file of your own -project is not compiled and reaches the runtime placeholders +they are, `node_modules` included, and `.js` files inside `@devup-ui` +packages. `.mts` and `.cts` files are read as TypeScript. + +`Bun.build` also compiles every `.js` and `.cjs` file outside `node_modules`: +those of your own project, and those of a linked workspace package Bun resolves +to its real path. A `.js` or `.cjs` file inside `node_modules` is left to Bun, +except the `.js` files of `@devup-ui` packages. + +Under the runtime, the `.js` and `.cjs` files of your own project are not +compiled: Devup UI code in one reaches the runtime placeholders (`Cannot run on the runtime`). Give such files a `.jsx`, `.tsx` or `.mjs` extension. The runtime cannot take those files on. A Bun runtime plugin must return the source of every file its filter matches, it cannot hand a file back to Bun, and -Bun reads returned source as an ES module, so a CommonJS `.js` file -(`require`, `module.exports`) it matched would stop loading. +Bun reads returned source as an ES module, so a CommonJS `.js` or `.cjs` file +(`require`, `module.exports`) it matched would stop loading. For the same +reason, a `.cts` file loaded under the runtime must be written with `import` +and `export`: one written with `module.exports` or `export =` loses its +exports. `Bun.build` reads returned CommonJS as CommonJS, so this applies to +the runtime only. ## Compiler Engine