diff --git a/.changepacks/changepack_log_mdx_compiled_output.json b/.changepacks/changepack_log_mdx_compiled_output.json new file mode 100644 index 000000000..4d969c12a --- /dev/null +++ b/.changepacks/changepack_log_mdx_compiled_output.json @@ -0,0 +1,11 @@ +{ + "changes": { + "bindings/devup-ui-wasm/package.json": "Patch", + "bindings/devup-ui-binding/package.json": "Patch", + "packages/vite-plugin/package.json": "Patch", + "packages/webpack-plugin/package.json": "Patch", + "packages/rsbuild-plugin/package.json": "Patch" + }, + "note": "MDX documents are extracted after the MDX plugin compiles them: the engine reads an `.mdx` filename as the JavaScript module (with or without JSX) an MDX compiler makes of it instead of rejecting the extension, the Vite plugin extracts `.mdx` in a `post` transform, and the webpack and Rsbuild plugins in a `post` loader; Next.js with Turbopack and the Bun plugin still leave MDX untransformed", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/apps/landing/src/app/(detail)/docs/limitations/page.mdx b/apps/landing/src/app/(detail)/docs/limitations/page.mdx index 163d64b62..3850cea33 100644 --- a/apps/landing/src/app/(detail)/docs/limitations/page.mdx +++ b/apps/landing/src/app/(detail)/docs/limitations/page.mdx @@ -95,6 +95,7 @@ A dynamic value such as `width` becomes a CSS variable set in `style`, so a comp - A styled-components or Emotion style function may read the theme only as `theme.a.b` (or `theme.a[2]`). Calls, whole-theme reads and runtime keys are build errors. - 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`. +- An `.mdx` file is extracted after your MDX plugin compiles it to JavaScript, never as raw MDX. The Vite (`@mdx-js/rollup`), webpack and Rsbuild (`@mdx-js/loader`, `@rsbuild/plugin-mdx`) plugins and Next.js with webpack (`@next/mdx`) run their MDX step in a later phase: Vite in a `post` transform, webpack and Rsbuild in a `post` loader. Next.js with Turbopack and the Bun plugin do not extract MDX yet. The scan that reserves class names before a build reads JavaScript and TypeScript modules only, so a document's own classes are named when it compiles. Put components that need Devup UI styles in a `.tsx` module and use them from the document. - 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. - 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. diff --git a/libs/extractor/src/barrel.rs b/libs/extractor/src/barrel.rs index ac89f90cc..7b809711b 100644 --- a/libs/extractor/src/barrel.rs +++ b/libs/extractor/src/barrel.rs @@ -17,7 +17,7 @@ use oxc_ast::ast::{ }; use oxc_parser::{Parser, ParserReturn}; use oxc_semantic::{Semantic, SemanticBuilder}; -use oxc_span::{GetSpan, SourceType}; +use oxc_span::GetSpan; use oxc_syntax::node::NodeId; use oxc_syntax::symbol::SymbolId; use rustc_hash::{FxHashMap, FxHashSet}; @@ -90,7 +90,7 @@ fn analyze(module: &ResolvedModule, package: &str) -> Exports { return exports; } let allocator = Allocator::default(); - let source_type = SourceType::from_path(&module.path).unwrap_or_default(); + let source_type = crate::parsing::path_source_type(&module.path).unwrap_or_default(); let program = Parser::new(&allocator, &module.code, source_type) .parse() .program; @@ -980,7 +980,7 @@ pub(crate) fn rewrite( return Barreled::Unchanged; } let allocator = Allocator::default(); - let source_type = SourceType::from_path(filename).unwrap_or_default(); + let source_type = crate::parsing::path_source_type(filename).unwrap_or_default(); let ParserReturn { program, fatal_error, diff --git a/libs/extractor/src/dead_properties/calls.rs b/libs/extractor/src/dead_properties/calls.rs index 8f5513960..8d72b304b 100644 --- a/libs/extractor/src/dead_properties/calls.rs +++ b/libs/extractor/src/dead_properties/calls.rs @@ -29,7 +29,7 @@ pub(super) fn collect( let parsed = oxc_parser::Parser::new( &allocator, code, - oxc_span::SourceType::from_path(filename) + crate::parsing::path_source_type(filename) .unwrap_or_else(|_| oxc_span::SourceType::ts()) .with_module(true), ) diff --git a/libs/extractor/src/imported_styled.rs b/libs/extractor/src/imported_styled.rs index f9eea26ed..b4a0e1476 100644 --- a/libs/extractor/src/imported_styled.rs +++ b/libs/extractor/src/imported_styled.rs @@ -13,7 +13,7 @@ use oxc_ast::ast::{ use oxc_ast::builder::AstBuilder; use oxc_ast_visit::{Visit, VisitMut, walk}; use oxc_parser::{Parser, ParserReturn}; -use oxc_span::{GetSpan, SourceType, Span}; +use oxc_span::{GetSpan, Span}; use rustc_hash::{FxHashMap, FxHashSet}; use crate::css_prop::{CssProp, CssTakers}; @@ -245,7 +245,12 @@ impl<'a> Reader<'_, 'a> { mut program, fatal_error, .. - } = Parser::new(&sub, &transformed, SourceType::from_path(path).ok()?).parse(); + } = Parser::new( + &sub, + &transformed, + crate::parsing::path_source_type(path).ok()?, + ) + .parse(); if fatal_error { return None; } @@ -325,8 +330,12 @@ impl<'a> Reader<'_, 'a> { &self.option.import_aliases, ); let allocator = Allocator::default(); - let parsed = - Parser::new(&allocator, &transformed, SourceType::from_path(path).ok()?).parse(); + let parsed = Parser::new( + &allocator, + &transformed, + crate::parsing::path_source_type(path).ok()?, + ) + .parse(); if parsed.fatal_error { return None; } @@ -833,7 +842,7 @@ mod tests { let source = format!( "import * as UI from './base'; import {{ Child }} from './base'; import {{ jsx }} from 'react/jsx-runtime'; {body}" ); - let parsed = Parser::new(&allocator, &source, SourceType::tsx()).parse(); + let parsed = Parser::new(&allocator, &source, oxc_span::SourceType::tsx()).parse(); assert_eq!(parsed.diagnostics.len(), 0); let scoping = oxc_semantic::SemanticBuilder::new() .build(&parsed.program) @@ -1778,7 +1787,7 @@ export default styled.span`margin: 1px;`; fn exports_name_what_a_module_gives_other_files() { let code = "import styled from '@emotion/styled';\nexport const A = 1, { b } = {};\nconst C = 2;\nexport { C, C as D };\nexport default C;\nexport function f() {}\nexport type { C as T };"; let allocator = Allocator::default(); - let program = Parser::new(&allocator, code, SourceType::ts()) + let program = Parser::new(&allocator, code, oxc_span::SourceType::ts()) .parse() .program; let exports = Exports::scan(&program); @@ -1789,7 +1798,7 @@ export default styled.span`margin: 1px;`; assert_eq!(exports.index("D"), 2); assert!(!exports.has_anonymous_default()); let code = "export default styled.div`color: red;`;"; - let program = Parser::new(&allocator, code, SourceType::ts()) + let program = Parser::new(&allocator, code, oxc_span::SourceType::ts()) .parse() .program; let exports = Exports::scan(&program); diff --git a/libs/extractor/src/jsx_pragmas.rs b/libs/extractor/src/jsx_pragmas.rs index cf75a5edc..89e84b80e 100644 --- a/libs/extractor/src/jsx_pragmas.rs +++ b/libs/extractor/src/jsx_pragmas.rs @@ -5,7 +5,7 @@ use oxc_ast::{ ast::{Program, Statement}, }; use oxc_parser::Parser; -use oxc_span::{GetSpan, SourceType, Span}; +use oxc_span::{GetSpan, Span}; /// The original header ends at the first body statement, not the first directive. pub(crate) struct HeaderPragmas { @@ -86,7 +86,7 @@ pub(crate) fn stylesheet_source(source: &str, generated: String, filename: &str) let program = Parser::new( &allocator, source, - SourceType::from_path(filename).unwrap_or_default(), + crate::parsing::path_source_type(filename).unwrap_or_default(), ) .parse() .program; diff --git a/libs/extractor/src/module_reference.rs b/libs/extractor/src/module_reference.rs index 170dbfb0b..945d417a7 100644 --- a/libs/extractor/src/module_reference.rs +++ b/libs/extractor/src/module_reference.rs @@ -39,7 +39,8 @@ pub fn original_module_references( source: &str, ) -> HashMap> { let allocator = Allocator::default(); - let source_type = SourceType::from_path(filename).unwrap_or_else(|_| SourceType::ts()); + let source_type = + crate::parsing::path_source_type(filename).unwrap_or_else(|_| SourceType::ts()); let parsed = Parser::new(&allocator, source, source_type).parse(); let program = parsed.program; let mut offsets = Vec::new(); diff --git a/libs/extractor/src/parsing.rs b/libs/extractor/src/parsing.rs index 26f82bb78..f08d1fcd5 100644 --- a/libs/extractor/src/parsing.rs +++ b/libs/extractor/src/parsing.rs @@ -12,8 +12,25 @@ mod hybrid_tests; /// Accept JavaScript JSX without changing the path's TypeScript or module dialect. pub(super) fn source_type(filename: &str) -> Result { - SourceType::from_path(filename) - .map(|source_type| source_type.with_jsx(source_type.is_javascript())) + path_source_type(filename).map(|source_type| source_type.with_jsx(source_type.is_javascript())) +} + +/// How the file at `path` parses, by its extension. +/// +/// An `.mdx` file reaches the extractor as what an MDX compiler +/// (`@mdx-js/loader`, `@mdx-js/rollup`, `@next/mdx`) made of it: an ES +/// module of JavaScript that may keep JSX, never the MDX syntax itself. +pub(super) fn path_source_type(path: &str) -> Result { + if is_mdx(path) { + return Ok(SourceType::jsx()); + } + SourceType::from_path(path) +} + +fn is_mdx(path: &str) -> bool { + std::path::Path::new(path) + .extension() + .is_some_and(|extension| extension.eq_ignore_ascii_case("mdx")) } fn parse_physical<'a>( @@ -203,6 +220,49 @@ mod tests { assert!(has_devup_ui(filename, source, "@devup-ui/react")); } + #[rstest] + #[case("docs/page.mdx")] + #[case("docs/PAGE.MDX")] + fn reads_mdx_as_compiled_javascript(#[case] filename: &str) { + // Given the path of an MDX file, which arrives already compiled. + // When its source type is taken from the path. + let actual = source_type(filename).unwrap(); + // Then it parses as a JavaScript module that may keep JSX. + assert!(actual.is_javascript()); + assert!(actual.is_module()); + assert!(actual.is_jsx()); + } + + #[rstest] + #[case( + "docs/page.mdx", + "import {jsx as _jsx, jsxs as _jsxs} from 'react/jsx-runtime';\nimport { Box } from '@devup-ui/react';\nfunction _createMdxContent(props) {\n const _components = { h1: 'h1', ...props.components };\n return _jsxs(_components.h1, { children: ['Title', _jsx(Box, { bg: 'maroon', p: '13px', children: 'proof' })] });\n}\nexport default function MDXContent(props = {}) {\n return _createMdxContent(props);\n}" + )] + #[case( + "docs/page.mdx", + "import {jsxDEV as _jsxDEV} from 'react/jsx-dev-runtime';\nimport { Box } from '@devup-ui/react';\nexport default function MDXContent() {\n return _jsxDEV(Box, { bg: 'maroon', p: '13px', children: 'proof' }, undefined, false, undefined, this);\n}" + )] + #[case( + "docs/page.mdx", + "import { Box } from '@devup-ui/react';\nexport default function MDXContent() {\n return <>

Title

proof;\n}" + )] + #[serial] + fn extracts_compiled_mdx(#[case] filename: &str, #[case] source: &str) { + // Given what an MDX compiler makes of a document using Box: calls of + // the JSX runtime, or JSX kept with the `jsx` option. + reset_class_map(); + reset_file_map(); + + // When the compiled module is extracted under its `.mdx` filename. + let output = extract(filename, source, ExtractOption::default()).unwrap(); + + // Then Box is compiled to a class like in any other module. + assert_eq!(output.styles.len(), 2, "{}", output.code); + assert!(!output.code.contains("Box"), "{}", output.code); + assert!(output.code.contains("className"), "{}", output.code); + assert!(has_devup_ui(filename, source, "@devup-ui/react")); + } + #[rstest] #[case( "import { Box } from '@devup-ui/react';\nconst view = , ) -> bool { let allocator = Allocator::default(); - let Ok(source_type) = SourceType::from_path(filename) else { + let Ok(source_type) = crate::parsing::path_source_type(filename) else { return false; }; let parsed = Parser::new(&allocator, code, source_type).parse(); @@ -93,7 +93,7 @@ impl<'s> Dispatch<'s> { fn plan(&mut self, code: &str, filename: &str, is_static: &dyn Fn(&str) -> bool) -> Plan { let allocator = Allocator::default(); - let Ok(source_type) = SourceType::from_path(filename) else { + let Ok(source_type) = crate::parsing::path_source_type(filename) else { return Plan::Run; }; let parsed = Parser::new(&allocator, code, source_type).parse(); diff --git a/packages/rsbuild-plugin/src/__tests__/plugin.test.ts b/packages/rsbuild-plugin/src/__tests__/plugin.test.ts index 07834754b..afe9d6b87 100644 --- a/packages/rsbuild-plugin/src/__tests__/plugin.test.ts +++ b/packages/rsbuild-plugin/src/__tests__/plugin.test.ts @@ -370,6 +370,22 @@ describe('DevupUIRsbuildPlugin', () => { getCssSpy.mockRestore() } }) + it('extracts MDX in a post transform, after the MDX loader compiled it', async () => { + const transform = mock() + await DevupUI().setup( + createSetupContext({ transform, modifyRsbuildConfig: mock() }), + ) + const source = transform.mock.calls.find( + ([descriptor]) => + String(descriptor.test) === String(/\.(tsx|ts|js|mjs|jsx)$/), + ) + const mdx = transform.mock.calls.find( + ([descriptor]) => descriptor.order === 'post', + ) + expect(mdx?.[0]).toEqual({ test: /\.mdx$/i, order: 'post' }) + expect(mdx?.[1]).toBe(source?.[1]) + }) + it('should transform code', async () => { const plugin = DevupUI() expect(plugin).toBeDefined() diff --git a/packages/rsbuild-plugin/src/plugin.ts b/packages/rsbuild-plugin/src/plugin.ts index 72905a53b..e01d6df78 100644 --- a/packages/rsbuild-plugin/src/plugin.ts +++ b/packages/rsbuild-plugin/src/plugin.ts @@ -28,7 +28,7 @@ import { tailwindCssFiles, withTailwindCss, } from '@devup-ui/plugin-utils' -import type { RsbuildPlugin, Rspack } from '@rsbuild/core' +import type { RsbuildPlugin, Rspack, TransformHandler } from '@rsbuild/core' import { createReservation, staticEntries } from './reservation' @@ -477,80 +477,83 @@ export const DevupUI = ({ return config }) - api.transform( - { - test: /\.(tsx|ts|js|mjs|jsx)$/, - }, - async ({ code, resourcePath, addDependency }) => { - 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 - // checkout's path into the emitted module, so byte-identical sources - // in two checkouts (git worktrees, a CI matrix, sibling clones) would - // produce different output and any content-addressed or relocated - // build cache would serve the wrong checkout's stylesheet. - // - // Atom mode additionally mirrors vite: the entry CODE imports the - // shared base (import_main_css_in_code=true) so rspack emits - // devup-ui.css once and links it from every entry (hoisted atoms - // shared, not inlined), and the extraction filename is - // POSIX-normalized to match the absolute-keyed canonical map / - // FILE_ROUTES. - let extractCssDir = relative( - dirname(resourcePath), - cssDir, - ).replaceAll('\\', '/') - if (!extractCssDir.startsWith('./')) - extractCssDir = `./${extractCssDir}` - const extractName = atomMode - ? resourcePath.replaceAll('\\', '/') - : resourcePath - const { - code: retCode, - map, - cssFile, - updatedBaseStyle, - dependencies = [], - } = compiler.codeExtract( - extractName, - code, - libPackage, - extractCssDir, - singleCss, - atomMode, - !atomMode, - importAliases, + const extractModule: TransformHandler = async ({ + code, + resourcePath, + addDependency, + }) => { + 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 + // checkout's path into the emitted module, so byte-identical sources + // in two checkouts (git worktrees, a CI matrix, sibling clones) would + // produce different output and any content-addressed or relocated + // build cache would serve the wrong checkout's stylesheet. + // + // Atom mode additionally mirrors vite: the entry CODE imports the + // shared base (import_main_css_in_code=true) so rspack emits + // devup-ui.css once and links it from every entry (hoisted atoms + // shared, not inlined), and the extraction filename is + // POSIX-normalized to match the absolute-keyed canonical map / + // FILE_ROUTES. + let extractCssDir = relative(dirname(resourcePath), cssDir).replaceAll( + '\\', + '/', + ) + if (!extractCssDir.startsWith('./')) + extractCssDir = `./${extractCssDir}` + const extractName = atomMode + ? resourcePath.replaceAll('\\', '/') + : resourcePath + const { + code: retCode, + map, + cssFile, + updatedBaseStyle, + dependencies = [], + } = compiler.codeExtract( + extractName, + code, + libPackage, + extractCssDir, + singleCss, + atomMode, + !atomMode, + importAliases, + ) + for (const dependency of dependencies) addDependency(dependency) + const promises: Promise[] = [] + if (updatedBaseStyle) { + // update base style + promises.push( + stateWriter.write( + join(cssDir, 'devup-ui.css'), + compiler.getCss(null, false), + 'utf-8', + ), ) - for (const dependency of dependencies) addDependency(dependency) - const promises: Promise[] = [] - if (updatedBaseStyle) { - // update base style - promises.push( - stateWriter.write( - join(cssDir, 'devup-ui.css'), - compiler.getCss(null, false), - 'utf-8', - ), - ) - } + } - if (cssFile) { - promises.push( - stateWriter.write( - join(cssDir, basename(cssFile)), - stylesheet(cssFile), - 'utf-8', - ), - ) - } - await Promise.all(promises) - return { - code: retCode, - map, - } - }, - ) + if (cssFile) { + promises.push( + stateWriter.write( + join(cssDir, basename(cssFile)), + stylesheet(cssFile), + 'utf-8', + ), + ) + } + await Promise.all(promises) + return { + code: retCode, + map, + } + } + api.transform({ test: /\.(tsx|ts|js|mjs|jsx)$/ }, extractModule) + // MDX compiles to JavaScript in a normal-phase loader such as + // `@mdx-js/loader`: a `post` transform extracts what it compiled. + api.transform({ test: /\.mdx$/i, order: 'post' }, extractModule) }, } } diff --git a/packages/vite-plugin/src/__tests__/plugin.test.ts b/packages/vite-plugin/src/__tests__/plugin.test.ts index c0205164c..af5acdb1c 100644 --- a/packages/vite-plugin/src/__tests__/plugin.test.ts +++ b/packages/vite-plugin/src/__tests__/plugin.test.ts @@ -1932,6 +1932,38 @@ describe('devupUIVitePlugin', () => { ) }) + it('extracts MDX in a post transform, after the MDX plugin compiled it', async () => { + // Given + codeExtractSpy.mockReturnValue(createCodeExtractResult({ code: 'mdx' })) + const [plugin, , mdxPlugin] = DevupUI({ + engine: 'wasm', + }) as unknown as [ + ViteTestPlugin, + ViteTestRestorePlugin, + Pick & { enforce: 'post' }, + ] + activePlugins.push(plugin) + expect(mdxPlugin).toEqual({ + name: 'devup-ui:mdx', + sharedDuringBuild: true, + enforce: 'post', + transform: expect.any(Function), + }) + // When / Then: only the post transform takes MDX, and it takes only MDX + expect(await plugin.transform('compiled', 'docs/page.mdx')).toBeUndefined() + expect( + await mdxPlugin.transform('compiled', 'docs/page.tsx'), + ).toBeUndefined() + expect( + await mdxPlugin.transform('compiled', 'docs/page.mdx?import'), + ).toEqual({ code: 'mdx' }) + expect(codeExtractSpy).toHaveBeenCalledTimes(1) + expect(codeExtractSpy.mock.calls[0]?.slice(0, 2)).toEqual([ + 'docs/page.mdx', + 'compiled', + ]) + }) + it.each( createTestMatrix({ extractCss: [true, false], diff --git a/packages/vite-plugin/src/plugin.ts b/packages/vite-plugin/src/plugin.ts index 46142b69d..1a29cc3be 100644 --- a/packages/vite-plugin/src/plugin.ts +++ b/packages/vite-plugin/src/plugin.ts @@ -49,6 +49,19 @@ import { createServerWatch } from './server-watch' */ const DEVUP_CSS_FILE_RE = /^devup-ui(-\d+)?\.css$/ +/** Modules the `pre` transform extracts as they are written */ +const SOURCE_FILE_RE = /\.(tsx|ts|js|mjs|jsx)$/i + +/** + * MDX documents, extracted by a `post` transform: the extractor reads what an + * MDX plugin such as `@mdx-js/rollup` compiled them to, never raw MDX + */ +const MDX_FILE_RE = /\.mdx$/i + +type TransformContext = ThisParameterType< + Extract, (...args: never[]) => unknown> +> + const SOURCE_DIR_CANDIDATES = ['src', 'app'] type SourceInput = { readonly code: string } @@ -483,6 +496,124 @@ export function DevupUI({ sheetRevisions.set(fileName, (sheetRevisions.get(fileName) ?? 0) + 1) return stateWriter.write(join(cssDir, fileName), css, 'utf-8') } + /** Extract the module `id` the transform hook accepted */ + async function extractModule( + this: TransformContext, + code: string, + id: string, + ) { + const session = extractionSession + if (!extractCss) return + + const fileName = id.split('?')[0] + if (excludeRegex.test(fileName)) { + return + } + await reservation.reserve() + + const file = resolve(fileName) + let input = latestInputs.get(file) + if (isServe && input?.code !== code) { + input = { code } + latestInputs.set(file, input) + } + const capture = input && { + session, + file, + input, + ownership: getBuildOwnership().generation, + reservation: reservation.revision, + config: configState.generation, + } + const eligible = capture && currentCapture(capture) + const cached = eligible ? session.flights.get(id) : undefined + if (cached && currentCapture(cached.capture)) { + compiledDependencies.update( + this.environment?.name ?? 'client', + id, + cached.dependencies, + ) + for (const dependency of cached.dependencies) + this.addWatchFile(dependency) + await cached.ready + return cached.result + } + + let rel = relative(dirname(id), cssDir).replaceAll('\\', '/') + if (!rel.startsWith('./')) rel = `./${rel}` + + const { + code: extractedCode, + css = '', + map, + cssFile, + updatedBaseStyle, + dependencies = [], + // import main css in code + } = (() => { + using output = compiler.codeExtract( + fileName, + code, + libPackage, + rel, + singleCss, + true, + false, + importAliases, + ) + return { + code: output.code, + css: output.css, + map: output.map, + cssFile: output.cssFile, + updatedBaseStyle: output.updatedBaseStyle, + dependencies: output.dependencies, + } + })() + compiledDependencies.update( + this.environment?.name ?? 'client', + id, + dependencies, + ) + for (const dependency of dependencies) this.addWatchFile(dependency) + const promises: Promise[] = [] + + if (updatedBaseStyle) { + // update base style + promises.push(writeCssFile('devup-ui.css', compiler.getCss(null, false))) + } + + if (cssFile) { + const fileNum = getFileNumByFilename(cssFile) + const prevCss = cssMap.get(fileNum) + if (prevCss && prevCss.length < css.length) cssMap.set(fileNum, css) + // `css` is only set when this transform added styles to the sheet. + if (css) promises.push(writeCssFile(basename(cssFile), css)) + } + const result = { code: extractedCode, map } + if (!eligible) { + await Promise.all(promises) + return result + } + const flight: ExtractionFlight = { + capture, + result, + dependencies: [...dependencies], + ready: Promise.all(promises).then( + () => { + if (session.flights.get(id) === flight && !currentCapture(capture)) + session.flights.delete(id) + }, + (error: unknown) => { + if (session.flights.get(id) === flight) session.flights.delete(id) + throw error + }, + ), + } + session.flights.set(id, flight) + await flight.ready + return result + } // A deleted source file is never transformed again, so its styles stay in // the sheet until the engine forgets the file. Every sheet written so far // is written again; only the ones that changed reach the disk. @@ -763,120 +894,8 @@ export function DevupUI({ }, enforce: 'pre', async transform(code, id) { - const session = extractionSession - if (!extractCss) return - - const fileName = id.split('?')[0] - if (!/\.(tsx|ts|js|mjs|jsx)$/i.test(fileName)) return - if (excludeRegex.test(fileName)) { - return - } - await reservation.reserve() - - const file = resolve(fileName) - let input = latestInputs.get(file) - if (isServe && input?.code !== code) { - input = { code } - latestInputs.set(file, input) - } - const capture = input && { - session, - file, - input, - ownership: getBuildOwnership().generation, - reservation: reservation.revision, - config: configState.generation, - } - const eligible = capture && currentCapture(capture) - const cached = eligible ? session.flights.get(id) : undefined - if (cached && currentCapture(cached.capture)) { - compiledDependencies.update( - this.environment?.name ?? 'client', - id, - cached.dependencies, - ) - for (const dependency of cached.dependencies) - this.addWatchFile(dependency) - await cached.ready - return cached.result - } - - let rel = relative(dirname(id), cssDir).replaceAll('\\', '/') - if (!rel.startsWith('./')) rel = `./${rel}` - - const { - code: extractedCode, - css = '', - map, - cssFile, - updatedBaseStyle, - dependencies = [], - // import main css in code - } = (() => { - using output = compiler.codeExtract( - fileName, - code, - libPackage, - rel, - singleCss, - true, - false, - importAliases, - ) - return { - code: output.code, - css: output.css, - map: output.map, - cssFile: output.cssFile, - updatedBaseStyle: output.updatedBaseStyle, - dependencies: output.dependencies, - } - })() - compiledDependencies.update( - this.environment?.name ?? 'client', - id, - dependencies, - ) - for (const dependency of dependencies) this.addWatchFile(dependency) - const promises: Promise[] = [] - - if (updatedBaseStyle) { - // update base style - promises.push( - writeCssFile('devup-ui.css', compiler.getCss(null, false)), - ) - } - - if (cssFile) { - const fileNum = getFileNumByFilename(cssFile) - const prevCss = cssMap.get(fileNum) - if (prevCss && prevCss.length < css.length) cssMap.set(fileNum, css) - // `css` is only set when this transform added styles to the sheet. - if (css) promises.push(writeCssFile(basename(cssFile), css)) - } - const result = { code: extractedCode, map } - if (!eligible) { - await Promise.all(promises) - return result - } - const flight: ExtractionFlight = { - capture, - result, - dependencies: [...dependencies], - ready: Promise.all(promises).then( - () => { - if (session.flights.get(id) === flight && !currentCapture(capture)) - session.flights.delete(id) - }, - (error: unknown) => { - if (session.flights.get(id) === flight) session.flights.delete(id) - throw error - }, - ), - } - session.flights.set(id, flight) - await flight.ready - return result + if (!SOURCE_FILE_RE.test(id.split('?')[0])) return + return extractModule.call(this, code, id) }, async generateBundle(_options, bundle) { if (!extractCss) return @@ -926,6 +945,16 @@ export function DevupUI({ }, }, } + // An MDX plugin runs in the normal or `pre` phase; this one runs after it. + const mdxPlugin: Plugin = { + name: 'devup-ui:mdx', + sharedDuringBuild: true, + enforce: 'post', + transform: own(async function (this: TransformContext, code, id) { + if (!MDX_FILE_RE.test(id.split('?')[0])) return + return extractModule.call(this, code, id) + }), + } return [ { ...plugin, @@ -934,5 +963,6 @@ export function DevupUI({ transform: own(plugin.transform), }, restorePlugin, + mdxPlugin, ] } diff --git a/packages/webpack-plugin/src/__tests__/extends-theme/loader-identity.mjs b/packages/webpack-plugin/src/__tests__/extends-theme/loader-identity.mjs index a33f8e979..935eb3a91 100644 --- a/packages/webpack-plugin/src/__tests__/extends-theme/loader-identity.mjs +++ b/packages/webpack-plugin/src/__tests__/extends-theme/loader-identity.mjs @@ -34,7 +34,8 @@ const expectedEntry = await realpath( join(packageRoot, manifest.exports['.'][format]), ) const expectedLoaders = await Promise.all( - ['./loader', './css-loader'].map((name) => + // The MDX post rule runs the source loader again + ['./loader', './css-loader', './loader'].map((name) => realpath(join(packageRoot, manifest.exports[name].require)), ), ) @@ -91,7 +92,11 @@ try { ), ].map((path) => realpath(path)), ) - assert.deepEqual(utilsPaths, [utilsPaths[0], utilsPaths[0], utilsPaths[0]]) + assert.deepEqual( + utilsPaths, + utilsPaths.map(() => utilsPaths[0]), + ) + assert.equal(utilsPaths.length, 4) for (const path of loaders) { const fromLoader = createRequire(path)( '@devup-ui/plugin-utils', @@ -176,7 +181,7 @@ try { assert.deepEqual( selectedLoaders, expectedLoaders, - 'both loaders must belong to the installed plugin', + 'every loader must belong to the installed plugin', ) } finally { for (const name of methods) { diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index ba12cb13f..cbd6bc59a 100644 --- a/packages/webpack-plugin/src/__tests__/plugin.test.ts +++ b/packages/webpack-plugin/src/__tests__/plugin.test.ts @@ -601,7 +601,7 @@ describe('devupUIWebpackPlugin', () => { const compiler = createCompiler() await plugin.apply(asCompiler(compiler)) - expect(compiler.options.module.rules.length).toBe(2) + expect(compiler.options.module.rules.length).toBe(3) const sourceRule = compiler.options.module.rules[0] assert( @@ -612,6 +612,28 @@ describe('devupUIWebpackPlugin', () => { expect(sourceRule.exclude).toEqual(options.include.output) }) + it('extracts MDX in a post loader, after the MDX loader compiled it', async () => { + const plugin = new DevupUIWebpackPlugin({ + engine: 'wasm', + include: ['lib'], + }) + existsSyncSpy.mockReturnValue(false) + + const compiler = createCompiler() + await plugin.apply(asCompiler(compiler)) + + const [sourceRule, , mdxRule] = compiler.options.module.rules + assert(typeof sourceRule === 'object' && sourceRule !== null) + expect(mdxRule).toEqual({ + test: /\.mdx$/i, + exclude: new RegExp( + '(?:^|[\\/\\\\])node_modules[\\/\\\\](?!.*[\\/\\\\]node_modules[\\/\\\\])(?!(?:@devup-ui|@devup-editor|lib)(?:[\\/\\\\]|$))', + ), + enforce: 'post', + use: 'use' in sourceRule ? sourceRule.use : undefined, + }) + }) + it.each( createTestMatrix({ debug: [true, false], diff --git a/packages/webpack-plugin/src/plugin.ts b/packages/webpack-plugin/src/plugin.ts index accff8952..70b2e0d30 100644 --- a/packages/webpack-plugin/src/plugin.ts +++ b/packages/webpack-plugin/src/plugin.ts @@ -32,7 +32,7 @@ import { type WasmImportAliases, withTailwindCss, } from '@devup-ui/plugin-utils' -import { type Compiler } from 'webpack' +import { type Compiler, type RuleSetRule } from 'webpack' import { createWebpackReservation } from './reservation' import { servedCss } from './served-css' @@ -576,33 +576,34 @@ export class DevupUIWebpackPlugin { }) } - compiler.options.module.rules.push( - { - test: /\.(tsx|ts|js|mjs|jsx)$/, - exclude: createNodeModulesExcludeRegex( - this.options.include, - '.mdx.[tj]sx?$', - ), - enforce: 'pre', - use: [ - { - loader: createRequire(import.meta.url).resolve( - '@devup-ui/webpack-plugin/loader', - ), - options: { - package: this.options.package, - engine: this.options.engine, - cssDir: this.options.cssDir, - sheetFile: this.sheetFile, - classMapFile: this.classMapFile, - fileMapFile: this.fileMapFile, - watch: this.options.watch, - singleCss: this.options.singleCss, - importAliases: this.importAliases, - }, + const sourceRule = { + test: /\.(tsx|ts|js|mjs|jsx)$/, + exclude: createNodeModulesExcludeRegex( + this.options.include, + '.mdx.[tj]sx?$', + ), + enforce: 'pre', + use: [ + { + loader: createRequire(import.meta.url).resolve( + '@devup-ui/webpack-plugin/loader', + ), + options: { + package: this.options.package, + engine: this.options.engine, + cssDir: this.options.cssDir, + sheetFile: this.sheetFile, + classMapFile: this.classMapFile, + fileMapFile: this.fileMapFile, + watch: this.options.watch, + singleCss: this.options.singleCss, + importAliases: this.importAliases, }, - ], - }, + }, + ], + } satisfies RuleSetRule + compiler.options.module.rules.push( + sourceRule, { test: this.options.cssDir, enforce: 'pre', @@ -618,6 +619,14 @@ export class DevupUIWebpackPlugin { }, ], }, + // MDX compiles to JavaScript in a normal-phase loader such as + // `@mdx-js/loader`: a `post` loader extracts what it compiled. + { + ...sourceRule, + test: /\.mdx$/i, + exclude: createNodeModulesExcludeRegex(this.options.include), + enforce: 'post', + }, ) } }