diff --git a/.changepacks/changepack_log_cross_file_component_markers.json b/.changepacks/changepack_log_cross_file_component_markers.json new file mode 100644 index 000000000..f33da7d8d --- /dev/null +++ b/.changepacks/changepack_log_cross_file_component_markers.json @@ -0,0 +1,12 @@ +{ + "changes": { + "bindings/devup-ui-wasm/package.json": "Patch", + "bindings/devup-ui-binding/package.json": "Patch", + "packages/plugin-utils/package.json": "Patch", + "packages/webpack-plugin/package.json": "Patch", + "packages/next-plugin/package.json": "Patch", + "packages/vite-plugin/package.json": "Patch" + }, + "note": "One-shot production builds with the Vite and webpack plugins (Next.js without Turbopack included) drop the component-selector markers no module selects. Each transform and the start-of-build scan report the markers they give and select; once every module is compiled, a marker no module selects or writes leaves the output chunks. When a module selects a component but the scan never read it (virtual module, a file compiled by another tool such as MDX, require.context / import.meta.glob / computed import(), or a file outside the scanned roots and entries), the build keeps every marker and warns with the module and the reason. The scan now also follows require.context and import.meta.glob calls whose arguments are written out. Warm webpack builds read the reports of modules restored from the persistent cache. Dev servers, watch builds, Turbopack and the Rsbuild and Bun plugins keep every marker.", + "date": "2026-11-19T00: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 1bb497d12..743902340 100644 --- a/apps/landing/src/app/(detail)/docs/limitations/page.mdx +++ b/apps/landing/src/app/(detail)/docs/limitations/page.mdx @@ -90,7 +90,7 @@ A dynamic value such as `width` becomes a CSS variable set in `style`, so a comp ## Known limitations -- An exported styled component always carries one short marker class (`a--b`), even when no selector in its own file names it. A module importing it may select it (`${Child} { ... }`) or extend it, and each file is built without knowing which files import it, so the class has to be on the element from the start. Leaving it off needs the whole build to know, before it compiles the component, that no module selects it, which per-file compilation cannot show yet. A styled component the file keeps to itself (not exported) carries a marker only when a selector in that file names it, so a private component that is only rendered or extended there carries none. +- An exported styled component carries one short marker class (`a--b`) when it is compiled, even when no selector in its own file names it. A module importing it may select it (`${Child} { ... }`) or extend it, and each file is built without knowing which files import it, so the class has to be on the element from the start. A one-shot production build with the Vite or webpack plugin (including Next.js without Turbopack) takes it off again once every module is compiled: the start-of-build scan reports what each file it reads selects, each transform reports what its module selects, and a marker that neither lists, and that no module wrote itself, leaves the output chunks. When a module selects a component but the scan never read it (a virtual module; a file another tool compiles first, such as MDX; a file loaded through `require.context`, `import.meta.glob` or a computed `import()` whose arguments the scan cannot read; or a file outside the scanned roots and entries, such as one only a function `entry` reaches), the build keeps every marker and warns, naming each such module and the reason. Dev servers, watch builds, Turbopack and the Rsbuild and Bun plugins keep every marker. A styled component the file keeps to itself (not exported) carries a marker only when a selector in that file names it, so a private component that is only rendered or extended there carries none. - A class, `keyframes` name or styled component defined in another module is not read as a build-time value: its name is only known once that file is built. Declare it in the file that uses it, or read it from a style prop. - `css()`, `globalCss()` and `keyframes()` have no element to set a CSS variable on, so a runtime value there is a build error. Move it to a style prop. - 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. diff --git a/apps/landing/src/app/(detail)/docs/migration/styled-components/page.ko.mdx b/apps/landing/src/app/(detail)/docs/migration/styled-components/page.ko.mdx index 28f7425be..da0f5ddb7 100644 --- a/apps/landing/src/app/(detail)/docs/migration/styled-components/page.ko.mdx +++ b/apps/landing/src/app/(detail)/docs/migration/styled-components/page.ko.mdx @@ -193,6 +193,7 @@ styled 컴포넌트는 함수 컴포넌트로 컴파일되며 React 19는 함수 | `css` prop, `jsx`, JSX runtime pragma | `className`과 `style`로 컴파일됨 | | `ClassNames` | child가 렌더링하는 결과로 대체되며, 각 `css` / `cx` 호출은 클래스로 컴파일됨 | | 컴포넌트 선택자(`${Child}`) | marker 클래스. Emotion의 확장 컴포넌트는 Emotion에서처럼 이를 제외하고 렌더링함 | +| 어느 모듈도 선택하지 않는 marker | Vite·webpack 일회성 production 빌드에서 제거됨 | | `.attrs()`, `.withConfig()` | attrs는 props 위에 병합됨. 빌드 타임 `shouldForwardProp`. `componentId` 클래스 | ## 제한 사항 diff --git a/apps/landing/src/app/(detail)/docs/migration/styled-components/page.mdx b/apps/landing/src/app/(detail)/docs/migration/styled-components/page.mdx index f82a6b76c..ae6d1c344 100644 --- a/apps/landing/src/app/(detail)/docs/migration/styled-components/page.mdx +++ b/apps/landing/src/app/(detail)/docs/migration/styled-components/page.mdx @@ -193,6 +193,7 @@ A styled component compiles to a function component, whose `defaultProps` React | `css` prop, `jsx`, JSX runtime pragmas | compiled to `className` and `style` | | `ClassNames` | replaced by what its child renders, each `css` / `cx` call compiled to classes | | Component selectors (`${Child}`) | a marker class; Emotion extensions render without it, as Emotion's do | +| Markers no module selects | dropped by one-shot Vite and webpack production builds | | `.attrs()`, `.withConfig()` | attrs merged over props; build-time `shouldForwardProp`; `componentId` classes | ## Limitations diff --git a/benchmark-record.js b/benchmark-record.js index 2a25c9309..7e5f1e776 100644 --- a/benchmark-record.js +++ b/benchmark-record.js @@ -188,10 +188,11 @@ export function formatJson(value) { return ( JSON.stringify(value, null, 2).replace( /^( *)(.*)\[\n((?:\1 {2}(?:-?[\d.e+-]+|"[^"\n]*"|null),?\n)+)\1\]/gm, - (whole, indent, key, items) => { + (whole, indent, key, items, offset, json) => { const values = items.trim().split(/,\n */) const line = `${indent}${key}[${values.join(', ')}]` - if (line.length + 1 <= PRINT_WIDTH) return line + const comma = json[offset + whole.length] === ',' ? 1 : 0 + if (line.length + comma <= PRINT_WIDTH) return line return values.every((item) => /^-?[\d.e+-]+$/.test(item)) ? fillNumbers(indent, key, values) : whole diff --git a/benchmark/history.json b/benchmark/history.json index db1fcffce..79cc5f333 100644 --- a/benchmark/history.json +++ b/benchmark/history.json @@ -292,9 +292,7 @@ "devup-ui-large": [15.099, 15.029, 15.117, 88882994, 1388, 934329], "devup-ui-large-wasm": [16.233, 16.017, 16.28, 89000421, 1388, 934329], "devup-ui-large-turbo": [6.037, 5.938, 6.244, 38676786, 1512, 571629], - "devup-ui-large-turbo-wasm": [ - 6.5, 6.483, 6.542, 38662174, 1512, 571629 - ] + "devup-ui-large-turbo-wasm": [6.5, 6.483, 6.542, 38662174, 1512, 571629] } }, { diff --git a/bindings/devup-ui-binding/index.d.cts b/bindings/devup-ui-binding/index.d.cts index 3dd2009c8..9d6cb8957 100644 --- a/bindings/devup-ui-binding/index.d.cts +++ b/bindings/devup-ui-binding/index.d.cts @@ -7,6 +7,10 @@ export class Output { readonly css: string | undefined /** Each access returns an independent array; mutating it does not change native state. */ readonly dependencies: string[] + /** Marker classes the source's components render with, for component selectors. */ + readonly givenMarkers: string[] + /** Marker classes the source's styles select components by. */ + readonly selectedMarkers: string[] readonly map: string | undefined readonly updatedBaseStyle: boolean } @@ -80,7 +84,7 @@ export function reserveClassSlots( importMainCssInCode: boolean, importMainCssInCss: boolean, importAliases: unknown, -): void +): string[] export function resetBuildState(): void export function resetStateForTesting(): void export function seedFileMap(files: string[]): void diff --git a/bindings/devup-ui-binding/src/host.rs b/bindings/devup-ui-binding/src/host.rs index 50a782fed..afdfb63fc 100644 --- a/bindings/devup-ui-binding/src/host.rs +++ b/bindings/devup-ui-binding/src/host.rs @@ -238,7 +238,7 @@ pub fn reserve<'env>( callback: AccessCallback<'env>, schema_token: Unknown<'env>, access_token: Unknown<'env>, -) -> Result<()> { +) -> Result> { let access = Access { env, callback, @@ -272,7 +272,8 @@ pub fn reserve<'env>( diagnostics.add_source(&input.filename, &input.code); } with_resolver(env, diagnostics, |resolver, _check| { - core::reserve_class_slots_internal(&inputs, option, resolver); - Ok(()) + Ok(core::reserve_class_slots_selecting( + &inputs, option, resolver, + )) }) } diff --git a/bindings/devup-ui-binding/src/output.rs b/bindings/devup-ui-binding/src/output.rs index 2c46a7c42..672c6e295 100644 --- a/bindings/devup-ui-binding/src/output.rs +++ b/bindings/devup-ui-binding/src/output.rs @@ -60,4 +60,14 @@ impl NativeOutput { pub fn dependencies(&self, env: Env) -> Result> { Ok(self.live(env)?.dependencies.clone()) } + /// Copy the marker classes the source's components render with. + #[napi(getter, js_name = "givenMarkers")] + pub fn given_markers(&self, env: Env) -> Result> { + Ok(self.live(env)?.component_markers.given.clone()) + } + /// Copy the marker classes the source's styles select components by. + #[napi(getter, js_name = "selectedMarkers")] + pub fn selected_markers(&self, env: Env) -> Result> { + Ok(self.live(env)?.component_markers.selected.clone()) + } } diff --git a/bindings/devup-ui-wasm/src/lib.rs b/bindings/devup-ui-wasm/src/lib.rs index f61e9fb96..628fe2b8e 100644 --- a/bindings/devup-ui-wasm/src/lib.rs +++ b/bindings/devup-ui-wasm/src/lib.rs @@ -58,6 +58,7 @@ pub struct Output { updated_base_style: bool, css: Option, dependencies: Vec, + component_markers: extractor::ComponentMarkers, } impl From for Output { @@ -69,6 +70,7 @@ impl From for Output { updated_base_style: output.updated_base_style, css: output.css, dependencies: output.dependencies, + component_markers: output.component_markers, } } } @@ -94,6 +96,7 @@ impl Output { map, css_file, dependencies, + component_markers: extractor::ComponentMarkers::default(), }, filename, binding_core::CssOptions { @@ -143,6 +146,20 @@ impl Output { pub fn dependencies(&self) -> Vec { self.dependencies.clone() } + + /// Marker classes the source's components render with, for selectors + #[wasm_bindgen(getter, js_name = "givenMarkers")] + #[must_use] + pub fn given_markers(&self) -> Vec { + self.component_markers.given.clone() + } + + /// Marker classes the source's styles select components by + #[wasm_bindgen(getter, js_name = "selectedMarkers")] + #[must_use] + pub fn selected_markers(&self) -> Vec { + self.component_markers.selected.clone() + } } #[wasm_bindgen(js_name = "setDebug")] diff --git a/bindings/devup-ui-wasm/src/reservation.rs b/bindings/devup-ui-wasm/src/reservation.rs index baf3cb9b4..1c7732a1c 100644 --- a/bindings/devup-ui-wasm/src/reservation.rs +++ b/bindings/devup-ui-wasm/src/reservation.rs @@ -7,7 +7,9 @@ use wasm_bindgen::prelude::*; #[cfg(test)] use crate::with_style_sheet_mut; -pub use devup_ui_binding_core::{ReservationInput, reserve_class_slots_internal}; +pub use devup_ui_binding_core::{ + ReservationInput, reserve_class_slots_internal, reserve_class_slots_selecting, +}; mod rows; #[cfg(not(tarpaulin_include))] use crate::{ @@ -17,7 +19,8 @@ use crate::{ #[cfg(not(tarpaulin_include))] use std::{cell::RefCell, rc::Rc}; -/// Reserve `[filename, code, cssDir]` rows using the current resolver and configuration. +/// Reserve `[filename, code, cssDir]` rows using the current resolver and +/// configuration; the component-selector markers the rows' styles select. #[wasm_bindgen(js_name = "reserveClassSlots")] #[cfg(not(tarpaulin_include))] pub fn reserve_class_slots( @@ -27,7 +30,7 @@ pub fn reserve_class_slots( import_main_css_in_code: bool, _import_main_css_in_css: bool, import_aliases: JsValue, -) -> Result<(), JsValue> { +) -> Result, JsValue> { let rows: Vec> = const { boundary::wasm() }.decode(rows, 0)?; let inputs = rows::parse_rows(rows).map_err(|_| boundary::fail(0))?; let option = ExtractOption { @@ -49,14 +52,13 @@ pub fn reserve_class_slots( call_module_resolver(&resolver, request, &state) } }); - reserve_class_slots_internal( + Ok(reserve_class_slots_selecting( &inputs, option, resolver .as_ref() .map(|resolver| resolver as &ModuleResolver), - ); - Ok(()) + )) } /// New live class and file slots since this build's first reservation; repeats do not count. diff --git a/bindings/devup-ui-wasm/src/reservation/tests.rs b/bindings/devup-ui-wasm/src/reservation/tests.rs index 3741b4178..29aab1594 100644 --- a/bindings/devup-ui-wasm/src/reservation/tests.rs +++ b/bindings/devup-ui-wasm/src/reservation/tests.rs @@ -452,3 +452,46 @@ fn supplied_physical_order_is_not_resorted_by_engine_id() -> TestResult { assert_eq!(get_unreserved_class_slot_count(), 0); Ok(()) } + +const CHILD: &str = "import {styled} from '@devup-ui/react'; export const Child = styled.div`color: red;`; export const Other = styled.span`color: blue;`;"; +const PARENT: &str = "import {styled} from '@devup-ui/react'; import {Child} from './child'; export const Parent = styled.div`${Child} { color: green; }`;"; + +fn child_module(specifier: &str, _: &str) -> Option { + (specifier == "./child").then(|| extractor::ResolvedModule { + path: "child.tsx".into(), + code: CHILD.into(), + }) +} + +#[test] +#[serial] +fn reservation_returns_the_markers_the_inputs_select() -> TestResult { + // Given + let (_reset, option) = setup(true); + let inputs = [input("child.tsx", CHILD), input("parent.tsx", PARENT)]; + // When + let selected = reserve_class_slots_selecting(&inputs, option.clone(), Some(&child_module)); + // Then + let child = transform(&inputs[0], &option)?; + let parent = code_extract_with_modules_internal( + "parent.tsx", + PARENT, + &option.package, + "./css".into(), + option.single_css, + option.import_main_css, + true, + option.import_aliases.clone(), + &child_module, + )?; + assert_eq!(selected, parent.selected_markers()); + assert_eq!(child.given_markers().len(), 2); + assert!(child.given_markers().contains(&selected[0])); + assert!( + parent + .given_markers() + .iter() + .all(|marker| !selected.contains(marker)) + ); + Ok(()) +} diff --git a/e2e/binding/diagnostics.mjs b/e2e/binding/diagnostics.mjs index 6bf5d208b..2b55a047c 100644 --- a/e2e/binding/diagnostics.mjs +++ b/e2e/binding/diagnostics.mjs @@ -224,6 +224,8 @@ export function runDiagnostics(w) { 'cssFile', 'updatedBaseStyle', 'dependencies', + 'givenMarkers', + 'selectedMarkers', 'free', Symbol.dispose, ]) { diff --git a/e2e/binding/host.mjs b/e2e/binding/host.mjs index 1fdfa1c5a..983a462e2 100644 --- a/e2e/binding/host.mjs +++ b/e2e/binding/host.mjs @@ -93,6 +93,8 @@ const fields = [ 'cssFile', 'updatedBaseStyle', 'dependencies', + 'givenMarkers', + 'selectedMarkers', ] const exports4 = [ 'exportSheet', diff --git a/libs/binding/src/lib.rs b/libs/binding/src/lib.rs index 3f6ad22a1..ed443c077 100644 --- a/libs/binding/src/lib.rs +++ b/libs/binding/src/lib.rs @@ -16,7 +16,9 @@ pub use css_cache::{SheetCell, SheetGuard}; pub use extractor::{has_devup_ui, has_devup_ui_with}; pub use lazy_css::LazyCss; pub use output::{CssOptions, ExtractionMode, Output, SourceMapMode, code_extract}; -pub use reservation::{ReservationInput, reserve_class_slots_internal}; +pub use reservation::{ + ReservationInput, reserve_class_slots_internal, reserve_class_slots_selecting, +}; pub use resolver_state::{ResolverRequest, ResolverState, check_resolved_path, resolved_module}; pub use session::{ PerSession, create_session, drop_session, live_session, select_session, with_session, diff --git a/libs/binding/src/output.rs b/libs/binding/src/output.rs index 8533eb798..b7aa86510 100644 --- a/libs/binding/src/output.rs +++ b/libs/binding/src/output.rs @@ -52,6 +52,8 @@ pub struct Output { pub css: Option, /// Files read through the module resolver. pub dependencies: Vec, + /// The component-selector markers the source gives and selects by. + pub component_markers: extractor::ComponentMarkers, } impl Output { @@ -88,6 +90,7 @@ impl Output { map: output.map, css_file: output.css_file, dependencies: output.dependencies, + component_markers: output.component_markers, updated_base_style: updated_base_style || default_collected, css: { if !collected && !default_collected { diff --git a/libs/binding/src/reservation.rs b/libs/binding/src/reservation.rs index 75ee97e2f..f2566c1c9 100644 --- a/libs/binding/src/reservation.rs +++ b/libs/binding/src/reservation.rs @@ -51,6 +51,19 @@ pub fn reserve_class_slots_internal( option: ExtractOption, resolver: Option<&ModuleResolver>, ) { + let _ = reserve_class_slots_selecting(inputs, option, resolver); +} + +/// [`reserve_class_slots_internal`], returning the component-selector +/// markers the inputs' styles select, sorted, once each: those a build may +/// keep when it drops the markers no module reads. +#[must_use] +pub fn reserve_class_slots_selecting( + inputs: &[ReservationInput], + option: ExtractOption, + resolver: Option<&ModuleResolver>, +) -> Vec { + let mut selected = std::collections::BTreeSet::new(); let _atom_mode = css::atom_hoist::AtomModeGuard::new(option.single_css); let slots = ScratchSlots::default(); let files: Vec = inputs.iter().map(|input| input.filename.clone()).collect(); @@ -72,6 +85,7 @@ pub fn reserve_class_slots_internal( None => extract_without_source_map(&input.filename, &input.code, file_option), }; if let Ok(output) = extracted { + selected.extend(output.component_markers.selected); let bucket = file_map::canonical(&input.filename); let global = option.single_css || file_map::is_global(&input.filename); with_style_sheet_mut(|sheet| { @@ -81,4 +95,5 @@ pub fn reserve_class_slots_internal( } } slots.commit(); + selected.into_iter().collect() } diff --git a/libs/extractor/src/class_selection_support.rs b/libs/extractor/src/class_selection_support.rs index 13c86e143..42518839a 100644 --- a/libs/extractor/src/class_selection_support.rs +++ b/libs/extractor/src/class_selection_support.rs @@ -78,6 +78,7 @@ pub fn observe(outputs: &Modules, observer: &str, globals: &str) -> serde_json:: map: output.map.clone(), css_file: output.css_file.clone(), dependencies: output.dependencies.clone(), + component_markers: crate::ComponentMarkers::default(), }, ) })); diff --git a/libs/extractor/src/component_markers.rs b/libs/extractor/src/component_markers.rs new file mode 100644 index 000000000..a28b9b1e9 --- /dev/null +++ b/libs/extractor/src/component_markers.rs @@ -0,0 +1,54 @@ +//! The component-selector markers a file gives and the ones its styles read. +//! +//! A styled component carries a marker class when a selector may pick it out +//! (`${Child} { ... }`, `[Child]`): one in its own file, or, as it is +//! exported, one in a module importing it. A build that has seen every +//! module can drop each marker no module's styles read; these lists are what +//! it needs to know which. + +/// The marker classes of one extraction +#[derive(Debug, Default, Clone, PartialEq, Eq)] +pub struct ComponentMarkers { + /// Markers the file's components render with, in order, once each + pub given: Vec, + /// Markers the file's styles select a component by, in order, once each + pub selected: Vec, +} + +impl ComponentMarkers { + /// Record `marker` as one a component of the file renders with + pub(crate) fn give(&mut self, marker: &str) { + push_once(&mut self.given, marker); + } + + /// Record `marker` as one a selector of the file reads + pub(crate) fn select(&mut self, marker: &str) { + push_once(&mut self.selected, marker); + } +} + +fn push_once(list: &mut Vec, marker: &str) { + if !list.iter().any(|known| known == marker) { + list.push(marker.to_string()); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn each_marker_is_recorded_once_in_order() { + // Given + let mut markers = ComponentMarkers::default(); + // When + markers.give("a--a"); + markers.give("a--b"); + markers.give("a--a"); + markers.select("b--a"); + markers.select("b--a"); + // Then + assert_eq!(markers.given, ["a--a", "a--b"]); + assert_eq!(markers.selected, ["b--a"]); + } +} diff --git a/libs/extractor/src/css_composition_rest_tests.rs b/libs/extractor/src/css_composition_rest_tests.rs index 5f401c713..dc7f53c89 100644 --- a/libs/extractor/src/css_composition_rest_tests.rs +++ b/libs/extractor/src/css_composition_rest_tests.rs @@ -307,6 +307,7 @@ fn layers_stay_distinct_when_nested_spreads_override_matching_rules() { map: None, css_file: None, dependencies: vec![], + component_markers: crate::ComponentMarkers::default(), }; let styles = result_styles(&output, true); assert_eq!( diff --git a/libs/extractor/src/emotion_namespace/output.rs b/libs/extractor/src/emotion_namespace/output.rs index 6cf4ee0e7..6d92330c9 100644 --- a/libs/extractor/src/emotion_namespace/output.rs +++ b/libs/extractor/src/emotion_namespace/output.rs @@ -40,6 +40,7 @@ impl Rewritten<'_> { }), css_file: None, dependencies: Vec::new(), + component_markers: crate::ComponentMarkers::default(), } } } diff --git a/libs/extractor/src/factory_dynamic_value_tests.rs b/libs/extractor/src/factory_dynamic_value_tests.rs index 1a775811c..815bf47cd 100644 --- a/libs/extractor/src/factory_dynamic_value_tests.rs +++ b/libs/extractor/src/factory_dynamic_value_tests.rs @@ -112,6 +112,7 @@ fn render_observation(output: &ExtractOutput, invocation: &str, filename: Option map: None, css_file: output.css_file.clone(), dependencies: output.dependencies.clone(), + component_markers: crate::ComponentMarkers::default(), }; super::render_observation(&javascript, invocation, filename) } diff --git a/libs/extractor/src/lib.rs b/libs/extractor/src/lib.rs index d489b0918..a93391b1e 100644 --- a/libs/extractor/src/lib.rs +++ b/libs/extractor/src/lib.rs @@ -21,6 +21,7 @@ mod class_selection_support; #[cfg(test)] mod class_selection_tests; mod component; +mod component_markers; mod composition; #[cfg(test)] mod composition_shorthand_tests; @@ -39,6 +40,7 @@ mod emotion_namespace; mod emotion_retargets; mod evaluation_sandbox; pub mod extract_style; +pub use component_markers::ComponentMarkers; mod extractor; mod fresh_name; mod gen_class_name; @@ -399,6 +401,9 @@ pub struct ExtractOutput { /// Files read through the module resolver, for the bundler to watch pub dependencies: Vec, + + /// The component-selector markers the file gives and selects by + pub component_markers: ComponentMarkers, } /// A module the bundler resolved an import to @@ -602,6 +607,7 @@ fn extract_source( map: None, css_file: None, dependencies: Vec::new(), + component_markers: crate::ComponentMarkers::default(), }; let pragma_rewritten = alias_edits @@ -620,6 +626,7 @@ fn extract_source( map: None, css_file: None, dependencies: Vec::new(), + component_markers: crate::ComponentMarkers::default(), }); } let (source, earlier_edits) = evaluated.unwrap_or((code, &[])); @@ -809,6 +816,7 @@ fn extract_source( map: None, css_file: None, dependencies: dependencies.into_iter().collect(), + component_markers: crate::ComponentMarkers::default(), }); } @@ -1053,6 +1061,7 @@ fn extract_source( map, css_file: Some(css_file), dependencies: dependencies.into_iter().collect(), + component_markers: visitor.component_markers.into_inner(), }) } diff --git a/libs/extractor/src/module_loader/script/coverage_tests.rs b/libs/extractor/src/module_loader/script/coverage_tests.rs index 1d9f041c2..a59f1d70a 100644 --- a/libs/extractor/src/module_loader/script/coverage_tests.rs +++ b/libs/extractor/src/module_loader/script/coverage_tests.rs @@ -27,6 +27,7 @@ fn retained_code_rejects_malformed_source_map_instead_of_inventing_a_trace() -> map: Some("{invalid".to_string()), css_file: None, dependencies: Vec::new(), + component_markers: crate::ComponentMarkers::default(), }; // When let error = Unit::retained("retained.ts", &output, "const width: number = 4;") @@ -48,6 +49,7 @@ fn retained_code_uses_written_trace_when_extraction_leaves_the_source_unchanged( map: None, css_file: None, dependencies: Vec::new(), + component_markers: crate::ComponentMarkers::default(), }; // When let unit = Unit::retained("retained.ts", &output, code)?; @@ -72,6 +74,7 @@ fn retained_code_rejects_missing_original_source_instead_of_inventing_a_trace( map: map.map(str::to_string), css_file: None, dependencies: Vec::new(), + component_markers: crate::ComponentMarkers::default(), }; // When let error = Unit::retained("retained.ts", &output, "const width: number = 4;") diff --git a/libs/extractor/src/styled_source_tests/captured_render.rs b/libs/extractor/src/styled_source_tests/captured_render.rs index 209616386..f63afbea4 100644 --- a/libs/extractor/src/styled_source_tests/captured_render.rs +++ b/libs/extractor/src/styled_source_tests/captured_render.rs @@ -36,6 +36,7 @@ pub(crate) fn execute( map: None, css_file: output.css_file.clone(), dependencies: output.dependencies.clone(), + component_markers: crate::ComponentMarkers::default(), }; super::css_prop_execution::execute_extracted(location, &executable, "") } diff --git a/libs/extractor/src/styled_source_tests/component_selector_markers.rs b/libs/extractor/src/styled_source_tests/component_selector_markers.rs index f596daf57..dfb57658c 100644 --- a/libs/extractor/src/styled_source_tests/component_selector_markers.rs +++ b/libs/extractor/src/styled_source_tests/component_selector_markers.rs @@ -42,7 +42,6 @@ fn markers(code: &str) -> Vec { } #[rstest] -#[serial] #[case::private_rendered( "const Child = styled.div`color: red;`;\nexport const App = () => ;", &[] @@ -67,6 +66,7 @@ fn markers(code: &str) -> Vec { "const Child = styled.div`color: red;`;\nexport default Child;", &[("default", 0)] )] +#[serial] fn markers_go_only_where_a_selector_can_read_them( #[values(EMOTION, STYLED_COMPONENTS)] import: &str, #[case] source: &str, @@ -87,9 +87,9 @@ fn markers_go_only_where_a_selector_can_read_them( } #[rstest] -#[serial] #[case::private("const Other = Child.withComponent('a');\nexport const App = () => <>;", &[])] #[case::exported("export const Other = Child.withComponent('a');", &[("Other", 1)])] +#[serial] fn emotion_with_component_results_share_a_marker_only_when_they_can_be_selected( #[case] source: &str, #[case] expected: &[(&str, usize)], @@ -113,3 +113,96 @@ fn expected_markers(expected: &[(&str, usize)]) -> Vec { .map(|(name, index)| css::component_marker(name, *index, "test.tsx")) .collect() } + +const CHILD_MODULE: &str = "import styled from '@emotion/styled';\nexport const Child = styled.div`color: red;`;\nexport const Other = styled.span`color: blue;`;"; + +fn child_module(specifier: &str, _: &str) -> Option { + (specifier == "./child").then(|| crate::ResolvedModule { + path: "/src/child.tsx".to_string(), + code: CHILD_MODULE.to_string(), + }) +} + +#[rstest] +#[case::exported("export const Child = styled.div`color: red;`;", &[("Child", 0)], &[])] +#[case::exported_anonymous_default("export default styled.div`color: red;`;", &[("default", 0)], &[])] +#[case::exported_and_selected_here( + "export const Child = styled.div`color: red;`;\nexport const Parent = styled.div`${Child} { color: blue; }`;", + &[("Child", 0), ("Parent", 1)], + &[("Child", 0)] +)] +#[case::private_selected( + "const Child = styled.div`color: red;`;\nconst Parent = styled.div`${Child} { color: blue; }`;\nexport const App = () => ;", + &[("Child", 1)], + &[("Child", 1)] +)] +#[case::shared_with_component_target( + "const Child = styled.div`color: red;`;\nconst Link = Child.withComponent('a');\nexport const Parent = styled.div`${Link} { color: blue; }`;\nexport const App = () => ;", + &[("Parent", 1), ("Child", 2)], + &[("Child", 2)] +)] +#[serial] +fn extraction_lists_the_markers_it_gives_and_selects_by( + #[case] source: &str, + #[case] given: &[(&str, usize)], + #[case] selected: &[(&str, usize)], +) { + // Given + reset_class_map(); + reset_file_map(); + let code = format!("{EMOTION}{source}"); + // When + let output = + extract("test.tsx", &code, option()).unwrap_or_else(|error| panic!("{code}\n{error}")); + // Then + assert_eq!( + sorted(output.component_markers.given), + sorted(expected_markers(given)), + "{code}" + ); + assert_eq!( + sorted(output.component_markers.selected), + sorted(expected_markers(selected)), + "{code}" + ); +} + +fn sorted(mut markers: Vec) -> Vec { + markers.sort(); + markers +} + +#[rstest] +#[case::named( + "import { Child } from './child';\nexport const Parent = styled.div`${Child} { color: green; }`;", + 1 +)] +#[case::namespace( + "import * as kit from './child';\nexport const Parent = styled.div`${kit.Child} { color: green; }`;", + 1 +)] +#[serial] +fn markers_of_imported_components_are_selected_by_the_importer( + #[case] source: &str, + #[case] selected: usize, +) { + // Given + reset_class_map(); + reset_file_map(); + let code = format!("{EMOTION}{source}"); + // When + let output = + crate::extract_with_modules("/src/parent.tsx", &code, option(), false, &child_module) + .unwrap_or_else(|error| panic!("{code}\n{error}")); + // Then + let markers = &output.component_markers; + assert_eq!(markers.selected.len(), selected, "{code}\n{markers:?}"); + assert_eq!(markers.given.len(), 1, "{code}\n{markers:?}"); + assert!( + markers + .selected + .iter() + .all(|marker| !markers.given.contains(marker)), + "{code}\n{markers:?}" + ); +} diff --git a/libs/extractor/src/visit.rs b/libs/extractor/src/visit.rs index 063d8757c..0f53c9ac0 100644 --- a/libs/extractor/src/visit.rs +++ b/libs/extractor/src/visit.rs @@ -328,6 +328,9 @@ pub struct DevupVisitor<'a> { /// The marker classes of the exported or selected styled component the /// declaration being visited defines, with where its definition starts pending_marker: Option<(u32, Vec<(String, usize)>)>, + /// The markers the file gives exported components and the ones its + /// selectors read + pub component_markers: std::cell::RefCell, /// Where the styled components start that no other module nor `css` /// reaches: the module exports none, and renders each only as a tag private_styled: FxHashSet, @@ -1417,6 +1420,7 @@ impl<'a> DevupVisitor<'a> { devup_styled: FxHashSet::default(), selected_components: FxHashSet::default(), pending_marker: None, + component_markers: std::cell::RefCell::default(), private_styled: FxHashSet::default(), rendered: FxHashSet::default(), exports: Exports::default(), @@ -1598,10 +1602,15 @@ impl<'a> DevupVisitor<'a> { /// The classes of markers a declaration is known to give, named only once /// it becomes a component so that an export that is not one numbers no file fn marker_classes(&self, markers: &[(String, usize)]) -> Vec { - markers + let classes: Vec = markers .iter() .map(|(name, index)| self.marker(name, *index)) - .collect() + .collect(); + let mut recorded = self.component_markers.borrow_mut(); + for class in &classes { + recorded.give(class); + } + classes } /// The styled components the file exports, by the names it exports them @@ -3703,6 +3712,7 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> { && self.style_values.import_name(symbol) == Some(namespace) && let Some(marker) = component.markers.first() { + self.component_markers.borrow_mut().select(marker); self.style_values .insert_member(symbol, member, format!(".{marker}")); } @@ -3726,6 +3736,7 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> { }) .collect(); for (symbol, marker) in imported { + self.component_markers.borrow_mut().select(&marker); self.style_values.insert( symbol, crate::style_values::StyleValue::Component(format!(".{marker}")), @@ -5513,6 +5524,7 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> { .filter(|symbol| self.selected_components.contains(symbol)) .zip(shared.iter().flatten().next()) { + self.component_markers.borrow_mut().select(class); self.style_values.insert( symbol, crate::style_values::StyleValue::Component(format!(".{class}")), @@ -5531,6 +5543,7 @@ impl<'a> VisitMut<'a> for DevupVisitor<'a> { if let Some((symbol, start, markers, selected)) = &marked { if *selected { let class = self.marker(&markers[0].0, markers[0].1); + self.component_markers.borrow_mut().select(&class); self.style_values.insert( *symbol, crate::style_values::StyleValue::Component(format!(".{class}")), diff --git a/packages/plugin-utils/src/__tests__/__snapshots__/component-markers.test.ts.snap b/packages/plugin-utils/src/__tests__/__snapshots__/component-markers.test.ts.snap new file mode 100644 index 000000000..b90bb0d7c --- /dev/null +++ b/packages/plugin-utils/src/__tests__/__snapshots__/component-markers.test.ts.snap @@ -0,0 +1,10 @@ +// Bun Snapshot v1, https://bun.sh/docs/test/snapshots + +exports[`markerWarning lists each unscanned module with its cause and markers 1`] = ` +"[devup-ui] Kept every component-selector marker in this build: the start-of-build scan did not see these modules select components from other modules. + - virtual:x [virtual-module]: a virtual module no file backs; selects a--a + - /a.mdx [compiled-source]: compiled by another tool before Devup UI read it; selects a--b, a--c + - /ctx.tsx [context-module]: loaded through require.context, import.meta.glob or a computed import(); selects a--d + - /out.tsx [outside-scan]: not reachable from the scanned roots and entries; selects a--e + - /fail.tsx [scan-failed]: the start-of-build scan could not extract it; selects a--f" +`; diff --git a/packages/plugin-utils/src/__tests__/benchmark-record.test.ts b/packages/plugin-utils/src/__tests__/benchmark-record.test.ts index e7075a0e5..42d011ee8 100644 --- a/packages/plugin-utils/src/__tests__/benchmark-record.test.ts +++ b/packages/plugin-utils/src/__tests__/benchmark-record.test.ts @@ -411,6 +411,18 @@ describe('benchmark JSON', () => { ) }) + it('fits a last array of 80 columns on one line, as it takes no comma', () => { + // Exactly 80 columns, 81 when a comma follows + const values = [1.5, 2.25, 3.125, 38662174, 1512, 571629, 12345678, 1234] + const fits = { aaaaaaaaaaaaaaaa: values } + const split = { aaaaaaaaaaaaaaaa: values, b: 1 } + const line = ` "aaaaaaaaaaaaaaaa": [${values.join(', ')}]` + + expect(line).toHaveLength(80) + expect(formatJson(fits)).toBe(`{\n${line}\n}\n`) + expect(formatJson(split).split('\n')[1]).toBe(' "aaaaaaaaaaaaaaaa": [') + }) + it('formats the checked files exactly as it writes them', () => { for (const file of ['benchmark-results.json', 'benchmark/history.json']) { const text = readFileSync(join(ROOT, file), 'utf8').replaceAll( diff --git a/packages/plugin-utils/src/__tests__/component-markers.test.ts b/packages/plugin-utils/src/__tests__/component-markers.test.ts new file mode 100644 index 000000000..b8bac8542 --- /dev/null +++ b/packages/plugin-utils/src/__tests__/component-markers.test.ts @@ -0,0 +1,280 @@ +import { beforeEach, describe, expect, it } from 'bun:test' + +import { + applyMarkerEdits, + ComponentMarkerLedger, + markerEditMap, + markerEdits, + markerLedgerOf, + markerLikeTokens, + markerWarning, + registerMarkerLedger, + resetMarkerProcessState, + unscannedCause, +} from '../component-markers' + +beforeEach(() => { + resetMarkerProcessState() +}) + +function transform( + id: string, + given: string[], + selected: string[] = [], + input = '', +) { + return { id, input, givenMarkers: given, selectedMarkers: selected } +} + +describe('ComponentMarkerLedger', () => { + it('keeps every marker until a scan reports its selections', () => { + // Given + const ledger = new ComponentMarkerLedger() + ledger.record(transform('/a.tsx', ['a--a'])) + // When + const plan = ledger.plan() + // Then + expect(plan).toEqual({ keep: { kind: 'no-scan' } }) + ledger.scanned(undefined, []) + expect(ledger.plan()).toEqual({ keep: { kind: 'no-scan' } }) + }) + + it('keeps markers when a transform selects one the scan missed', () => { + // Given + const ledger = new ComponentMarkerLedger() + ledger.scanned(['a--b'], ['/a.tsx', '/b.tsx']) + ledger.record(transform('/a.tsx', ['a--a', 'a--b', 'a--c', 'a--d'])) + ledger.record(transform('/b.tsx', ['b--a'], ['a--c'], 'className="a--d"')) + // When + const plan = ledger.plan() + // Then + expect(plan).toEqual({ keep: expect.anything() }) + }) + + it('names the modules the scan did not see select a marker', () => { + // Given + const ledger = new ComponentMarkerLedger() + ledger.scanned(['a--b'], ['/a.tsx', '/c.tsx']) + ledger.record(transform('/a.tsx', ['a--a', 'a--b'])) + ledger.record(transform('/z.tsx', [], ['a--a'])) + ledger.record({ + ...transform('/v.tsx', [], ['a--a']), + cause: 'virtual-module', + }) + ledger.record(transform('/c.tsx', [], ['a--b', 'a--c'])) + // When + const plan = ledger.plan() + // Then + expect(plan).toEqual({ + keep: { + kind: 'unscanned', + modules: [ + { id: '/c.tsx', cause: 'scan-failed', markers: ['a--c'] }, + { id: '/v.tsx', cause: 'virtual-module', markers: ['a--a'] }, + { id: '/z.tsx', cause: 'outside-scan', markers: ['a--a'] }, + ], + }, + }) + // A later compilation of the same process keeps its markers too + const later = new ComponentMarkerLedger() + later.scanned([], []) + expect(later.plan()).toEqual({ keep: { kind: 'earlier-compilation' } }) + }) + + it('strips what is given and selected by no one, keeping written tokens', () => { + // Given + const ledger = new ComponentMarkerLedger() + ledger.scanned(['a--b'], ['/a.tsx', '/b.tsx']) + ledger.record(transform('/a.tsx', ['a--a', 'a--b', 'a--c', 'a--d'])) + ledger.record(transform('/b.tsx', ['b--a'], ['a--b'], 'className="a--d"')) + ledger.record(transform('/b.tsx', ['b--a'], ['a--b'], 'className="a--d"')) + // When + const plan = ledger.plan() + // Then + expect(plan).toEqual({ strip: new Set(['a--a', 'a--c', 'b--a']) }) + expect([ledger.recorded('/b.tsx'), ledger.recorded('/c.tsx')]).toEqual([ + true, + false, + ]) + ledger.clear() + expect(ledger.plan()).toEqual({ strip: new Set() }) + }) +}) + +describe('markerWarning', () => { + it('says nothing for reasons that are not missing certainty', () => { + expect(markerWarning({ kind: 'no-scan' })).toBeUndefined() + expect(markerWarning({ kind: 'watch' })).toBeUndefined() + expect(markerWarning({ kind: 'earlier-compilation' })).toBeUndefined() + }) + + it('lists each unscanned module with its cause and markers', () => { + // Given + const reason = { + kind: 'unscanned' as const, + modules: [ + { + id: 'virtual:x', + cause: 'virtual-module' as const, + markers: ['a--a'], + }, + { + id: '/a.mdx', + cause: 'compiled-source' as const, + markers: ['a--b', 'a--c'], + }, + { id: '/ctx.tsx', cause: 'context-module' as const, markers: ['a--d'] }, + { id: '/out.tsx', cause: 'outside-scan' as const, markers: ['a--e'] }, + { id: '/fail.tsx', cause: 'scan-failed' as const, markers: ['a--f'] }, + ], + } + // When + const warning = markerWarning(reason) + // Then + expect(warning).toMatchSnapshot() + }) + + it('says when an earlier compilation already dropped a marker', () => { + // Given + const first = new ComponentMarkerLedger() + first.scanned([], ['/a.tsx']) + first.record(transform('/a.tsx', ['a--a'])) + expect(first.plan()).toEqual({ strip: new Set(['a--a']) }) + const second = new ComponentMarkerLedger() + second.scanned([], []) + second.record(transform('/late.tsx', [], ['a--a'])) + const plan = second.plan() + // When + const warning = 'keep' in plan ? markerWarning(plan.keep) : undefined + // Then + expect(warning).toContain('already dropped a--a') + }) +}) + +describe('unscannedCause', () => { + it.each([ + ['\0virtual', 'x', 'x', 'virtual-module'], + ['virtual:routes', 'x', 'x', 'virtual-module'], + ['/gone.tsx', 'x', undefined, 'virtual-module'], + ['/a.mdx', 'x', 'x', 'compiled-source'], + ['/a.tsx', 'compiled', 'source', 'compiled-source'], + ['/a.tsx?query', 'x', 'x', 'outside-scan'], + ['/a.tsx', 'x', 'x', 'outside-scan'], + ] as const)('reads %s', (id, input, onDisk, cause) => { + expect(unscannedCause(id, input, onDisk)).toBe(cause) + }) +}) + +describe('markerLikeTokens', () => { + it('finds tokens written like markers', () => { + expect(markerLikeTokens('a b--c "d--e" `--f` x')).toEqual( + new Set(['b--c', 'd--e']), + ) + expect(markerLikeTokens('')).toEqual(new Set()) + }) +}) + +describe('markerEdits', () => { + const strip = new Set(['a--a', 'a--b']) + + it.each([ + ['"a--a b"', '"b"'], + ['"b a--a"', '"b"'], + ['"a--a"', '""'], + ['`a--a b ${c}`', '`b ${c}`'], + ['`a--a${c}`', '`${c}`'], + ["'a--b c a--a'", "'c'"], + ['"xa--a b"', '"xa--a b"'], + ['"a--ax b"', '"a--ax b"'], + ['"a--c b"', '"a--c b"'], + ['i--a', 'i--a'], + ])('rewrites %s', (code, expected) => { + expect(applyMarkerEdits(code, markerEdits(code, strip))).toBe(expected) + }) + + it('makes no edits without markers to drop', () => { + expect(markerEdits('"a--a"', new Set())).toEqual([]) + }) +}) + +const BASE64 = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + +/** Each segment as `[generated line, column, original line, column]` */ +function decode(mappings: string): [number, number, number, number][] { + const result: [number, number, number, number][] = [] + let fields: number[] = [0, 0, 0, 0] + for (const [generatedLine, group] of mappings.split(';').entries()) { + fields[0] = 0 + for (const segment of group ? group.split(',') : []) { + const values: number[] = [] + let value = 0 + let shift = 0 + for (const char of segment) { + const digit = BASE64.indexOf(char) + value += (digit & 31) << shift + if (digit & 32) shift += 5 + else { + values.push(value & 1 ? -(value >>> 1) : value >>> 1) + value = 0 + shift = 0 + } + } + fields = fields.map((field, index) => field + (values[index] ?? 0)) + result.push([ + generatedLine, + fields[0] ?? 0, + fields[2] ?? 0, + fields[3] ?? 0, + ]) + } + } + return result +} + +describe('markerEditMap', () => { + it('maps each word of the rewritten code back to where it was', () => { + // Given + const code = 'const x = "a--a b";\nf(`a--b c ${d}`, -1);' + const edits = markerEdits(code, strip()) + const rewritten = applyMarkerEdits(code, edits) + // When + const map = markerEditMap(code, edits) + // Then + expect(rewritten).toBe('const x = "b";\nf(`c ${d}`, -1);') + const lines = rewritten.split('\n') + const original = code.split('\n') + const segments = decode(map.mappings) + let words = 0 + for (const [generatedLine, column, line, originalColumn] of segments) { + expect(line).toBe(generatedLine) + const word = /^[\w$]+/.exec(lines[generatedLine]?.slice(column) ?? '') + if (!word) continue + words++ + expect(original[line]?.slice(originalColumn).startsWith(word[0])).toBe( + true, + ) + } + expect(words).toBe(8) + expect(map.mappings.split(';')).toHaveLength(2) + expect(map.version).toBe(3) + }) + + function strip() { + return new Set(['a--a', 'a--b']) + } +}) + +describe('marker ledger registry', () => { + it('finds the ledger registered for an owner', () => { + // Given + const owner = {} + const ledger = new ComponentMarkerLedger() + // When + registerMarkerLedger(owner, ledger) + // Then + expect(markerLedgerOf(owner)).toBe(ledger) + expect(markerLedgerOf({})).toBeUndefined() + expect(markerLedgerOf(undefined)).toBeUndefined() + }) +}) diff --git a/packages/plugin-utils/src/__tests__/context-imports.test.ts b/packages/plugin-utils/src/__tests__/context-imports.test.ts new file mode 100644 index 000000000..508b58807 --- /dev/null +++ b/packages/plugin-utils/src/__tests__/context-imports.test.ts @@ -0,0 +1,99 @@ +import { join, relative } from 'node:path' + +import { expect, it } from 'bun:test' + +import { contextImports } from '../context-imports' +import { collectReservationInputs } from '../reservation-discovery' +import { fixture, STYLE } from './reservation-fixture' + +function listed(root: string, files: readonly string[]): string[] { + return files.map((file) => relative(root, file).replaceAll('\\', '/')) +} + +it('reads the directories require.context names with its recursion and pattern', () => { + // Given + using fs = fixture() + for (const path of [ + 'pages/a.tsx', + 'pages/b.md', + 'pages/deep/c.tsx', + 'pages/node_modules/x.tsx', + 'pages/.hidden/y.tsx', + 'flat/a.tsx', + 'flat/deep/b.tsx', + 'all/a.tsx', + ]) + fs.write(path, STYLE) + const source = [ + "require.context('./pages', true, /\\.tsx$/)", + "require.context('./flat', false, /\\.tsx$/g, 'lazy')", + "require.context('./all')", + "require.context('./pages', true, /[/)", + "require.context('./missing', true, /x/)", + 'require.context(dir, true, /x/)', + ].join('\n') + // When + const files = contextImports(join(fs.root, 'entry.ts'), source, fs.root) + // Then + expect(listed(fs.root, files)).toEqual([ + 'all/a.tsx', + 'flat/a.tsx', + 'pages/a.tsx', + 'pages/deep/c.tsx', + ]) +}) + +it('reads the patterns import.meta.glob names, relative or from the root', () => { + // Given + using fs = fixture() + for (const path of [ + 'src/routes/a.tsx', + 'src/routes/deep/b.tsx', + 'src/routes/c.ts', + 'src/routes/skip.css', + 'widgets/one.tsx', + 'widgets/two.jsx', + 'single/x.tsx', + ]) + fs.write(path, STYLE) + const source = [ + "import.meta.glob('./routes/**/*.tsx')", + "import.meta.glob<{ default: unknown }>(['/widgets/*.{tsx,jsx}', '!/widgets/two.jsx'], { eager: true })", + "import.meta.globEager('./routes/?.ts')", + 'import.meta.glob("/single/x.tsx")', + "import.meta.glob('/widgets/**')", + 'import.meta.glob(pattern)', + ].join('\n') + // When + const files = contextImports(join(fs.root, 'src/main.ts'), source, fs.root) + // Then + expect(listed(fs.root, files)).toEqual([ + 'single/x.tsx', + 'src/routes/a.tsx', + 'src/routes/c.ts', + 'src/routes/deep/b.tsx', + 'widgets/one.tsx', + 'widgets/two.jsx', + ]) +}) + +it('reserves the files a directory import loads', () => { + // Given + using fs = fixture() + fs.write('entry.ts', "require.context('./pages', true, /\\.tsx$/)") + fs.write('pages/Home.tsx', STYLE) + fs.write('pages/notes.md', STYLE) + // When + const inventory = collectReservationInputs({ + roots: [], + entries: [{ request: './entry.ts', context: fs.root }], + cwd: fs.root, + package: '@devup-ui/react', + importAliases: {}, + toId: (path) => relative(fs.root, path).replaceAll('\\', '/'), + }) + // Then + expect(inventory.inputs.map(({ filename }) => filename)).toEqual([ + 'pages/Home.tsx', + ]) +}) diff --git a/packages/plugin-utils/src/__tests__/shared-slots.test.ts b/packages/plugin-utils/src/__tests__/shared-slots.test.ts index 24f2ad4d7..23a416b83 100644 --- a/packages/plugin-utils/src/__tests__/shared-slots.test.ts +++ b/packages/plugin-utils/src/__tests__/shared-slots.test.ts @@ -282,3 +282,28 @@ it('isolates a broken allowed package and reserves the valid suffix before retry ['src/A.tsx', STYLE, '../df/devup-ui'], ]) }) + +it('keeps the marker selections and ids of the last reservation', () => { + // Given + using fs = fixture() + fs.write('src/A.tsx', STYLE) + const recorded = recorder() + let selections: readonly string[] | undefined = ['a--a'] + const manager = createSharedSlotReservation({ + reserveClassSlots(...args) { + recorded.engine.reserveClassSlots(...args) + return selections + }, + }) + // When + const before = [manager.markerSelections, manager.reservedIds] + manager.reserve(discovery(fs.root), flags) + const reported = [manager.markerSelections, manager.reservedIds] + selections = undefined + manager.invalidate() + manager.reserve(discovery(fs.root), flags) + // Then + expect(before).toEqual([undefined, []]) + expect(reported).toEqual([['a--a'], ['src/A.tsx']]) + expect(manager.markerSelections).toBeUndefined() +}) diff --git a/packages/plugin-utils/src/compiler-types.ts b/packages/plugin-utils/src/compiler-types.ts index 4db11c43f..791fd1b6b 100644 --- a/packages/plugin-utils/src/compiler-types.ts +++ b/packages/plugin-utils/src/compiler-types.ts @@ -16,6 +16,8 @@ export type DevupCompilerOutput = Pick< | 'cssFile' | 'map' | 'dependencies' + | 'givenMarkers' + | 'selectedMarkers' | 'updatedBaseStyle' | 'free' | typeof Symbol.dispose diff --git a/packages/plugin-utils/src/component-markers.ts b/packages/plugin-utils/src/component-markers.ts new file mode 100644 index 000000000..5b261eb9c --- /dev/null +++ b/packages/plugin-utils/src/component-markers.ts @@ -0,0 +1,381 @@ +/** + * Component-selector markers a build can drop. + * + * A styled component that a module exports carries a marker class, because a + * module importing it may select it (`${Child} { ... }`, `[Child]`). Only a + * build that has seen every module can know that none does. The start-of-build + * scan extracts the inventory and reports the markers its styles select; each + * transform reports the markers its module gives and selects. A one-shot + * production build drops a marker from its output chunks only when: + * + * - the scan ran, and every transformed module selects only markers the scan + * saw, so every compilation of the build, in this process or another one, + * reads the same list; + * - no module selects it, in the scan or in a transform; and + * - no module wrote the class itself, so removing it touches only Devup UI's + * own class lists. + * + * Otherwise it keeps every marker and says why, naming each module the scan + * did not cover and the reason, so the cases can be closed one by one. + */ + +/** Why the start-of-build scan did not see a module's selectors */ +export type UnscannedCause = + /** A module no file backs, made by a plugin (`virtual:`, `\0` ids) */ + | 'virtual-module' + /** A file another tool compiles first (MDX, Vue, Svelte, Astro) */ + | 'compiled-source' + /** Loaded through `require.context`, `import.meta.glob` or a computed `import()` the scan cannot follow */ + | 'context-module' + /** A source file the scan never reached from its roots and entries */ + | 'outside-scan' + /** A file the scan read but could not extract */ + | 'scan-failed' + +/** A module whose selectors the scan did not see */ +export interface UnscannedModule { + readonly id: string + readonly cause: UnscannedCause + /** The markers it selects that the scan did not report */ + readonly markers: readonly string[] +} + +/** Why a build keeps every component-selector marker */ +export type MarkerKeepReason = + /** Dev servers and watch builds: modules change after their markers are given */ + | { readonly kind: 'watch' } + /** No start-of-build scan reported its selections */ + | { readonly kind: 'no-scan' } + /** Modules selected markers the scan did not see */ + | { + readonly kind: 'unscanned' + readonly modules: readonly UnscannedModule[] + } + /** An earlier compilation in this process found modules the scan missed */ + | { readonly kind: 'earlier-compilation' } + +/** What a build does with the markers its modules give */ +export type MarkerPlan = + { readonly strip: ReadonlySet } | { readonly keep: MarkerKeepReason } + +/** What one transform reports */ +export interface MarkerTransform { + /** The id the bundler knows the module by */ + readonly id: string + /** The code the transform read */ + readonly input: string + readonly givenMarkers: readonly string[] + readonly selectedMarkers: readonly string[] + /** Why the scan may not have seen this module, as the bundler knows it */ + readonly cause?: UnscannedCause +} + +interface Recorded { + readonly given: readonly string[] + readonly selected: readonly string[] + /** Class-like tokens the module wrote itself */ + readonly authored: ReadonlySet + readonly cause: UnscannedCause +} + +/** The classes a build may never touch, across the compilations of a process */ +const processState: { + uncertain: boolean + stripped: Set +} = { uncertain: false, stripped: new Set() } + +/** @internal Forget what earlier compilations of this process found */ +export function resetMarkerProcessState(): void { + processState.uncertain = false + processState.stripped = new Set() +} + +const TOKEN = /[^\s"'`${}]+--[^\s"'`${}]*/g + +/** The tokens of `code` written like a marker class (`a--b`) */ +export function markerLikeTokens(code: string): Set { + return new Set(code.match(TOKEN)) +} + +const SOURCE_SCRIPT = /\.[cm]?[jt]sx?$/i + +/** Why the scan may have missed module `id`, from what the bundler read */ +export function unscannedCause( + id: string, + input: string, + onDisk: string | undefined, +): UnscannedCause { + if (id.includes('\0') || id.startsWith('virtual:') || onDisk === undefined) + return 'virtual-module' + if (!SOURCE_SCRIPT.test(id.split('?')[0] ?? id) || onDisk !== input) + return 'compiled-source' + return 'outside-scan' +} + +/** Gathers what the scan and the transforms of one build report */ +export class ComponentMarkerLedger { + private scan: ReadonlySet | undefined + private scannedIds: ReadonlySet = new Set() + private readonly modules = new Map() + + /** + * Take the markers the scan's styles select, and the ids of the modules it + * extracted; `undefined` when no scan reported them. + */ + scanned( + selections: readonly string[] | void, + modules: Iterable, + ): void { + this.scan = selections ? new Set(selections) : undefined + this.scannedIds = new Set(modules) + } + + /** Take what one transform reports, replacing an earlier one of the module */ + record(transform: MarkerTransform): void { + this.modules.set(transform.id, { + given: transform.givenMarkers, + selected: transform.selectedMarkers, + authored: markerLikeTokens(transform.input), + cause: + transform.cause ?? + (this.scannedIds.has(transform.id) ? 'scan-failed' : 'outside-scan'), + }) + } + + /** Whether a transform of module `id` reported to this ledger */ + recorded(id: string): boolean { + return this.modules.has(id) + } + + /** Forget every transform, as a new build begins */ + clear(): void { + this.modules.clear() + } + + /** What the build does with its markers, once every module is transformed */ + plan(): MarkerPlan { + if (processState.uncertain) return { keep: { kind: 'earlier-compilation' } } + const scan = this.scan + if (!scan) return { keep: { kind: 'no-scan' } } + const unscanned = this.unscanned(scan) + if (unscanned.length) { + processState.uncertain = true + return { keep: { kind: 'unscanned', modules: unscanned } } + } + const strip = this.unselected(scan) + for (const marker of strip) processState.stripped.add(marker) + return { strip } + } + + /** Markers an earlier compilation of this process already dropped */ + static strippedEarlier(markers: readonly string[]): string[] { + return markers.filter((marker) => processState.stripped.has(marker)) + } + + private unscanned(scan: ReadonlySet): UnscannedModule[] { + const modules: UnscannedModule[] = [] + for (const [id, recorded] of this.modules) { + const markers = recorded.selected.filter((marker) => !scan.has(marker)) + if (markers.length) modules.push({ id, cause: recorded.cause, markers }) + } + return modules.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)) + } + + private unselected(scan: ReadonlySet): Set { + const kept = new Set(scan) + for (const recorded of this.modules.values()) { + for (const marker of recorded.selected) kept.add(marker) + for (const token of recorded.authored) kept.add(token) + } + const strip = new Set() + for (const recorded of this.modules.values()) + for (const marker of recorded.given) + if (!kept.has(marker)) strip.add(marker) + return strip + } +} + +const CAUSES: Record = { + 'virtual-module': 'a virtual module no file backs', + 'compiled-source': 'compiled by another tool before Devup UI read it', + 'context-module': + 'loaded through require.context, import.meta.glob or a computed import()', + 'outside-scan': 'not reachable from the scanned roots and entries', + 'scan-failed': 'the start-of-build scan could not extract it', +} + +/** + * The warning for a build that keeps its markers because certainty is + * missing; `undefined` when there is nothing to say. + */ +export function markerWarning(reason: MarkerKeepReason): string | undefined { + if (reason.kind !== 'unscanned') return undefined + const lines = reason.modules.map( + (module) => + ` - ${module.id} [${module.cause}]: ${CAUSES[module.cause]}; selects ${module.markers.join(', ')}`, + ) + const earlier = ComponentMarkerLedger.strippedEarlier( + reason.modules.flatMap((module) => module.markers), + ) + if (earlier.length) + lines.push( + ` An earlier compilation of this build already dropped ${earlier.join(', ')}; rebuild to keep them there too.`, + ) + return [ + '[devup-ui] Kept every component-selector marker in this build: the start-of-build scan did not see these modules select components from other modules.', + ...lines, + ].join('\n') +} + +/** A removal from a chunk, as `[start, end)` offsets into its code */ +export type MarkerEdit = readonly [start: number, end: number] + +const QUOTE = new Set(['"', "'", '`']) + +function isClassBoundary(char: string | undefined): boolean { + return char === undefined || char === ' ' || QUOTE.has(char) +} + +/** + * The removals that drop `markers` from the class lists in `code`: each one + * standing alone between quotes or spaces, with the space that separates it + */ +export function markerEdits( + code: string, + markers: ReadonlySet, +): MarkerEdit[] { + const edits: MarkerEdit[] = [] + if (!markers.size) return edits + for (const match of code.matchAll(TOKEN)) { + const start = match.index + const end = start + match[0].length + if ( + !markers.has(match[0]) || + !isClassBoundary(code[start - 1]) || + !(isClassBoundary(code[end]) || code.startsWith('${', end)) + ) + continue + if (code[end] === ' ') edits.push([start, end + 1]) + else if (code[start - 1] === ' ') edits.push([start - 1, end]) + else edits.push([start, end]) + } + return edits +} + +/** `code` with `edits` removed */ +export function applyMarkerEdits( + code: string, + edits: readonly MarkerEdit[], +): string { + let result = '' + let at = 0 + for (const [start, end] of edits) { + result += code.slice(at, start) + at = end + } + return result + code.slice(at) +} + +const BASE64 = + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + +function vlq(value: number): string { + let rest = value < 0 ? (-value << 1) | 1 : value << 1 + let encoded = '' + do { + let digit = rest & 31 + rest >>>= 5 + if (rest > 0) digit |= 32 + encoded += BASE64[digit] + } while (rest > 0) + return encoded +} + +const WORD = /[\w$]/ + +/** + * A source map from `code` with `edits` removed back to `code`, with a + * segment at every word boundary so that the map it is composed with keeps + * its precision + */ +export function markerEditMap( + code: string, + edits: readonly MarkerEdit[], +): { version: 3; sources: string[]; names: string[]; mappings: string } { + const lines: string[] = [] + let segments: string[] = [] + let generated = 0 + let previousGenerated = 0 + let previousLine = 0 + let previousColumn = 0 + let line = 0 + let column = 0 + let edit = 0 + const segment = () => { + segments.push( + vlq(generated - previousGenerated) + + 'A' + + vlq(line - previousLine) + + vlq(column - previousColumn), + ) + previousGenerated = generated + previousLine = line + previousColumn = column + } + for (let index = 0; index < code.length; index++) { + const current = edits[edit] + if (current && index === current[0]) { + column += current[1] - current[0] + index = current[1] - 1 + edit++ + continue + } + const char = code[index] + if ( + column === 0 || + WORD.test(char ?? '') !== WORD.test(code[index - 1] ?? '') + ) + segment() + if (char === '\n') { + lines.push(segments.join(',')) + segments = [] + generated = 0 + previousGenerated = 0 + line++ + column = 0 + } else { + generated++ + column++ + } + } + lines.push(segments.join(',')) + return { version: 3, sources: [''], names: [], mappings: lines.join(';') } +} + +const LEDGERS = Symbol.for('devup-ui.componentMarkerLedgers') + +function ledgers(): WeakMap { + const scope = globalThis as typeof globalThis & { + [LEDGERS]?: WeakMap + } + return (scope[LEDGERS] ??= new WeakMap()) +} + +/** + * The ledger of the build `owner` stands for, as the plugin registered it; + * shared by every copy of this module a process loads, so that a loader + * bundled apart from its plugin reports to the same ledger + */ +export function markerLedgerOf( + owner: object | undefined, +): ComponentMarkerLedger | undefined { + return owner ? ledgers().get(owner) : undefined +} + +/** Register `ledger` as the one of the build `owner` stands for */ +export function registerMarkerLedger( + owner: object, + ledger: ComponentMarkerLedger, +): void { + ledgers().set(owner, ledger) +} diff --git a/packages/plugin-utils/src/context-imports.ts b/packages/plugin-utils/src/context-imports.ts new file mode 100644 index 000000000..17a1dd400 --- /dev/null +++ b/packages/plugin-utils/src/context-imports.ts @@ -0,0 +1,111 @@ +import { readdirSync } from 'node:fs' +import { dirname, join, relative, resolve } from 'node:path' + +/** + * The files a module loads through a bundler's directory import, when its + * arguments are written out: webpack's `require.context(dir, recursive, + * /regex/)` and Vite's `import.meta.glob('pattern' | ['pattern', ...])`. + * Without them, the start-of-build scan would never reach those files. + */ +export function contextImports( + filename: string, + source: string, + root: string, +): string[] { + const directory = dirname(filename) + const files = new Set() + for (const match of source.matchAll(REQUIRE_CONTEXT)) { + const [, , request = '', recursive, pattern, flags = ''] = match + const base = resolve(directory, request) + const test = pattern === undefined ? /^\.\/.*$/ : regex(pattern, flags) + for (const file of listFiles(base, recursive !== 'false')) { + const path = `./${relative(base, file).replaceAll('\\', '/')}` + if (test?.test(path)) files.add(file) + } + } + for (const match of source.matchAll(IMPORT_META_GLOB)) { + for (const pattern of globPatterns(match[1] ?? '')) { + const base = pattern.startsWith('/') + ? resolve(root, `.${pattern}`) + : resolve(directory, pattern) + const start = staticPrefix(base) + const test = globRegex(base.replaceAll('\\', '/')) + for (const file of listFiles(start, true)) + if (test.test(file.replaceAll('\\', '/'))) files.add(file) + } + } + return [...files].sort() +} + +const REQUIRE_CONTEXT = + /\brequire\.context\(\s*(['"])([^'"]+)\1\s*(?:,\s*(true|false)\s*)?(?:,\s*\/((?:\\.|[^/\\\n])+)\/([a-z]*)\s*)?(?:,\s*['"][^'"]*['"]\s*)?\)/g + +const IMPORT_META_GLOB = + /\bimport\.meta\.glob(?:Eager)?\s*(?:<[^>()]*>)?\(\s*((?:'[^']*'|"[^"]*")|\[\s*(?:(?:'[^']*'|"[^"]*")\s*,?\s*)*\])/g + +function regex(pattern: string, flags: string): RegExp | undefined { + try { + return new RegExp(pattern, flags.replaceAll('g', '')) + } catch { + return undefined + } +} + +/** The positive patterns of a glob argument, a string or an array of them */ +function globPatterns(argument: string): string[] { + return [...argument.matchAll(/'([^']*)'|"([^"]*)"/g)] + .map((match) => match[1] ?? match[2] ?? '') + .filter((pattern) => pattern && !pattern.startsWith('!')) +} + +const GLOB_CHARS = /[*?{[]/ + +/** The deepest directory of `pattern` before its first wildcard */ +function staticPrefix(pattern: string): string { + const index = pattern.search(GLOB_CHARS) + return index < 0 ? dirname(pattern) : dirname(pattern.slice(0, index + 1)) +} + +/** A regular expression matching the paths `glob` matches */ +function globRegex(glob: string): RegExp { + let source = '' + for (let index = 0; index < glob.length; index++) { + const char = glob[index] ?? '' + if (glob.startsWith('**/', index)) { + source += '(?:.*/)?' + index += 2 + } else if (glob.startsWith('**', index)) { + source += '.*' + index += 1 + } else if (char === '*') source += '[^/]*' + else if (char === '?') source += '[^/]' + else if (char === '{') source += '(?:' + else if (char === '}') source += ')' + else if (char === ',') source += '|' + else source += char.replace(/[.+^$()|[\]\\]/g, '\\$&') + } + return new RegExp(`^${source}$`) +} + +/** The files under `directory`, skipping dependencies and hidden folders */ +function listFiles(directory: string, recursive: boolean): string[] { + let entries + try { + entries = readdirSync(directory, { withFileTypes: true }) + } catch { + return [] + } + const files: string[] = [] + for (const entry of entries) { + const path = join(directory, entry.name) + if (entry.isFile()) files.push(path) + else if ( + recursive && + entry.isDirectory() && + entry.name !== 'node_modules' && + !entry.name.startsWith('.') + ) + files.push(...listFiles(path, true)) + } + return files +} diff --git a/packages/plugin-utils/src/index.ts b/packages/plugin-utils/src/index.ts index 66487196f..469daa733 100644 --- a/packages/plugin-utils/src/index.ts +++ b/packages/plugin-utils/src/index.ts @@ -16,6 +16,24 @@ export type { DevupCompilerOutput, DevupEngine, } from './compiler-types' +export { + applyMarkerEdits, + ComponentMarkerLedger, + type MarkerEdit, + markerEditMap, + markerEdits, + type MarkerKeepReason, + markerLedgerOf, + markerLikeTokens, + type MarkerPlan, + type MarkerTransform, + markerWarning, + registerMarkerLedger, + resetMarkerProcessState, + type UnscannedCause, + unscannedCause, + type UnscannedModule, +} from './component-markers' export { type AtomHoistPlan, buildCanonicalMap, diff --git a/packages/plugin-utils/src/reservation-discovery.ts b/packages/plugin-utils/src/reservation-discovery.ts index 9f65f4d21..f043bee3b 100644 --- a/packages/plugin-utils/src/reservation-discovery.ts +++ b/packages/plugin-utils/src/reservation-discovery.ts @@ -1,6 +1,7 @@ import { readFileSync } from 'node:fs' import { resolve } from 'node:path' +import { contextImports } from './context-imports' import { createModuleResolver, importedSpecifiers, @@ -182,6 +183,12 @@ export function collectReservationInputs( if (!sources.has(target)) sources.set(target, module.code) pending.push(module.path) } + // Directories `require.context` or `import.meta.glob` load, written out + for (const file of contextImports(physicalPath, code, cwd)) { + if (!isSourceFile(file)) continue + targets.add(physicalKey(file)) + pending.push(file) + } } const selected = new Set() const closure = [...seeds] diff --git a/packages/plugin-utils/src/shared-slots.ts b/packages/plugin-utils/src/shared-slots.ts index ea2080f49..c61c4767c 100644 --- a/packages/plugin-utils/src/shared-slots.ts +++ b/packages/plugin-utils/src/shared-slots.ts @@ -21,7 +21,7 @@ export interface ReservationEngine { importMainCssInCode: boolean, importMainCssInCss: boolean, importAliases: Readonly, - ): void + ): readonly string[] | void } export type ReservationBatchOptions = { @@ -36,6 +36,13 @@ export type ReservationBatchOptions = { export interface SharedSlotReservation { readonly dependencies: readonly string[] readonly packageManifests: readonly string[] + /** + * The component-selector markers the last reservation's styles select, or + * `undefined` until an engine reports them + */ + readonly markerSelections: readonly string[] | undefined + /** The ids the last reservation extracted */ + readonly reservedIds: readonly string[] invalidate(): void reserve( discovery: ReservationDiscoveryOptions, @@ -46,6 +53,7 @@ export interface SharedSlotReservation { type Committed = { readonly inventory: ReservationInventory readonly rows: readonly ReservationRow[] + readonly selections: readonly string[] | undefined readonly packageName: string readonly aliases: Readonly readonly singleCss: boolean @@ -116,6 +124,12 @@ export function createSharedSlotReservation( get packageManifests() { return committed?.inventory.packageManifests ?? [] }, + get markerSelections() { + return committed?.selections + }, + get reservedIds() { + return committed?.rows.map(([filename]) => filename) ?? [] + }, invalidate() { dirty = true }, @@ -146,7 +160,7 @@ export function createSharedSlotReservation( ) return false options.beforeReserve?.(inventory) - engine.reserveClassSlots( + const selections = engine.reserveClassSlots( rows, discovery.package, options.singleCss, @@ -157,6 +171,7 @@ export function createSharedSlotReservation( committed = { inventory, rows, + selections: selections ?? undefined, packageName: discovery.package, aliases: { ...discovery.importAliases }, singleCss: options.singleCss, diff --git a/packages/vite-plugin/src/__tests__/component-markers.test.ts b/packages/vite-plugin/src/__tests__/component-markers.test.ts new file mode 100644 index 000000000..09dc833a4 --- /dev/null +++ b/packages/vite-plugin/src/__tests__/component-markers.test.ts @@ -0,0 +1,82 @@ +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' + +import { resetMarkerProcessState } from '@devup-ui/plugin-utils' +import { afterEach, beforeEach, expect, it, mock } from 'bun:test' + +import { createViteMarkers } from '../component-markers' + +let root: string +beforeEach(() => { + resetMarkerProcessState() + root = mkdtempSync(join(tmpdir(), 'devup-vite-markers-')) +}) +afterEach(() => { + rmSync(root, { recursive: true, force: true }) +}) + +const silent = { warn: mock() } + +it('drops the markers no module selects from each chunk, with a map', () => { + // Given + const markers = createViteMarkers() + markers.reset() + markers.scanned(['a--b'], ['/src/A.tsx']) + markers.record('/src/A.tsx', '', { givenMarkers: ['a--a', 'a--b', 'a--c'] }) + markers.record('C:\\src\\B.tsx', '', {}) + const chunk = { + modules: { + '/src/A.tsx': { code: 'unread' }, + 'C:\\src\\B.tsx': { code: 'f("a--c")' }, + '/vendor.js': { code: 'f("a--c")' }, + '/empty.js': { code: null }, + }, + } + // When + const result = markers.renderChunk( + 'f("a--a x"),f("a--b"),f("a--c")', + chunk, + silent, + ) + const untouched = markers.renderChunk('f("y")', chunk, silent) + // Then + expect(result?.code).toBe('f("x"),f("a--b"),f("a--c")') + expect(result?.map.version).toBe(3) + expect(untouched).toBeNull() + expect(silent.warn).not.toHaveBeenCalled() +}) + +it('keeps every marker and warns once when the scan missed a selecting module', () => { + // Given + const markers = createViteMarkers() + markers.scanned([], []) + writeFileSync(join(root, 'Late.tsx'), 'on disk') + markers.record(join(root, 'Late.tsx'), 'on disk', { + givenMarkers: [], + selectedMarkers: ['a--a'], + }) + markers.record(join(root, 'Gone.tsx'), 'x', { selectedMarkers: ['a--b'] }) + const context = { warn: mock() } + // When + const first = markers.renderChunk('f("a--a")', { modules: {} }, context) + const second = markers.renderChunk('f("a--a")', { modules: {} }, context) + // Then + expect([first, second]).toEqual([null, null]) + expect(context.warn).toHaveBeenCalledTimes(1) + const [warning] = context.warn.mock.calls[0] as [string] + expect(warning).toContain('Late.tsx [outside-scan]') + expect(warning).toContain('Gone.tsx [virtual-module]') +}) + +it('keeps every marker quietly without a scan', () => { + // Given + const markers = createViteMarkers() + markers.record('/a.tsx', '', { givenMarkers: ['a--a'] }) + const context = { warn: mock() } + // When + const result = markers.renderChunk('f("a--a")', { modules: {} }, context) + // Then + expect(result).toBeNull() + expect(context.warn).not.toHaveBeenCalled() +}) diff --git a/packages/vite-plugin/src/__tests__/plugin.test.ts b/packages/vite-plugin/src/__tests__/plugin.test.ts index 0f73792cc..2fbc01e8d 100644 --- a/packages/vite-plugin/src/__tests__/plugin.test.ts +++ b/packages/vite-plugin/src/__tests__/plugin.test.ts @@ -155,6 +155,7 @@ interface ViteTestPlugin { configResolved: (config?: { command?: 'serve' | 'build' build?: { watch?: object | null } + isProduction?: boolean root?: string plugins?: readonly object[] }) => Promise @@ -177,6 +178,11 @@ interface ViteTestPlugin { timestamp: number }) => Promise load: (id: string) => string | undefined + renderChunk: ( + this: { warn(message: string): void }, + code: string, + chunk: { modules: Record }, + ) => { code: string } | null transform: ( this: { environment?: { @@ -424,6 +430,35 @@ describe('devupUIVitePlugin', () => { expect(output.free).toHaveBeenCalledTimes(1) }) + it('drops unselected component markers only from one-shot production chunks', async () => { + // Given + const plugin = createPlugin() + codeExtractSpy.mockReturnValueOnce( + createCodeExtractResult({ givenMarkers: ['a--a'], selectedMarkers: [] }), + ) + const context = { warn: mock() } + const chunk = { modules: {} } + // When + await plugin.configResolved({ command: 'build', isProduction: true }) + await plugin.buildStart.call({ addWatchFile: mock() } as never) + await plugin.transform('source', 'source.tsx') + const production = plugin.renderChunk.call(context, 'f("a--a")', chunk) + await plugin.configResolved({ command: 'build', isProduction: false }) + const development = plugin.renderChunk.call(context, 'f("a--a")', chunk) + // Then + expect(production?.code).toBe('f("")') + expect(development).toBeNull() + expect(context.warn).not.toHaveBeenCalled() + }) + + it('renders chunks untouched without CSS extraction', async () => { + const plugin = createPlugin({ extractCss: false }) + await plugin.configResolved({ command: 'build', isProduction: true }) + expect( + plugin.renderChunk.call({ warn: mock() }, 'f("a--a")', { modules: {} }), + ).toBeNull() + }) + it('should apply default options', () => { const plugin = createPlugin({}) expect(plugin).toEqual({ @@ -437,6 +472,7 @@ describe('devupUIVitePlugin', () => { hotUpdate: expect.any(Function), enforce: 'pre', transform: expect.any(Function), + renderChunk: expect.any(Function), apply: expect.any(Function), outputOptions: expect.any(Function), generateBundle: expect.any(Function), diff --git a/packages/vite-plugin/src/component-markers.ts b/packages/vite-plugin/src/component-markers.ts new file mode 100644 index 000000000..f76a8d474 --- /dev/null +++ b/packages/vite-plugin/src/component-markers.ts @@ -0,0 +1,101 @@ +import { readFileSync } from 'node:fs' + +import { + applyMarkerEdits, + ComponentMarkerLedger, + markerEditMap, + markerEdits, + markerLikeTokens, + markerWarning, + unscannedCause, +} from '@devup-ui/plugin-utils' + +interface MarkerOutput { + readonly givenMarkers?: readonly string[] + readonly selectedMarkers?: readonly string[] +} + +interface RenderedChunk { + /** The rendered code of each module in the chunk, by id */ + readonly modules: Readonly> +} + +function readSource(file: string): string | undefined { + try { + return readFileSync(file, 'utf-8') + } catch { + return undefined + } +} + +/** + * Component-selector markers in a Vite build: what the scan and the + * transforms report, and the chunks rewritten without the markers no module + * selects once every module is transformed + */ +export function createViteMarkers() { + const ledger = new ComponentMarkerLedger() + let warned = false + let plan: ReturnType | undefined + return { + /** A new build begins */ + reset() { + ledger.clear() + plan = undefined + warned = false + }, + /** + * An environment's build begins: its chunks follow the plan made once its + * own modules are transformed + */ + scanned(selections: readonly string[] | undefined, ids: readonly string[]) { + ledger.scanned(selections, ids) + plan = undefined + warned = false + }, + /** What the transform of module `file` read and gave */ + record(file: string, input: string, output: MarkerOutput) { + const id = file.replaceAll('\\', '/') + const selectedMarkers = output.selectedMarkers ?? [] + ledger.record({ + id, + input, + givenMarkers: output.givenMarkers ?? [], + selectedMarkers, + cause: selectedMarkers.length + ? unscannedCause(id, input, readSource(file)) + : undefined, + }) + }, + /** + * `code` without the markers no module selects, with its map; `null` when + * nothing changes. `context` hears once why a build keeps them. + */ + renderChunk( + code: string, + chunk: RenderedChunk, + context: { warn(message: string): void }, + ) { + plan ??= ledger.plan() + if ('keep' in plan) { + const warning = markerWarning(plan.keep) + if (warning && !warned) context.warn(warning) + warned = true + return null + } + const foreign = new Set() + for (const [id, module] of Object.entries(chunk.modules)) + if (!ledger.recorded(id.replaceAll('\\', '/')) && module.code) + for (const token of markerLikeTokens(module.code)) foreign.add(token) + const strip = new Set( + [...plan.strip].filter((marker) => !foreign.has(marker)), + ) + const edits = markerEdits(code, strip) + if (!edits.length) return null + return { + code: applyMarkerEdits(code, edits), + map: markerEditMap(code, edits), + } + }, + } +} diff --git a/packages/vite-plugin/src/plugin.ts b/packages/vite-plugin/src/plugin.ts index a77dcdd0b..50b91b6ae 100644 --- a/packages/vite-plugin/src/plugin.ts +++ b/packages/vite-plugin/src/plugin.ts @@ -43,6 +43,7 @@ import type { import { assetNames, hashSheetAssetNames } from './asset-names' import { createCompiledDependencies } from './compiled-dependencies' +import { createViteMarkers } from './component-markers' import { createEnvironmentSheets } from './environment-sheets' import { createViteReservation } from './reservation' import { createServerWatch } from './server-watch' @@ -459,6 +460,9 @@ export function DevupUI({ compiler.getCss(fileNum, false), ) const compiledDependencies = createCompiledDependencies() + const markers = createViteMarkers() + /** Whether this is a one-shot production build, which drops unread markers */ + let dropsMarkers = false let stopWatching: (() => void) | undefined let watcherSession: ExtractionSession | undefined let watchConfigInputs: (() => void) | undefined @@ -621,6 +625,8 @@ export function DevupUI({ cssFile, updatedBaseStyle, dependencies = [], + givenMarkers, + selectedMarkers, // import main css in code } = (() => { using output = compiler.codeExtract( @@ -640,8 +646,11 @@ export function DevupUI({ cssFile: output.cssFile, updatedBaseStyle: output.updatedBaseStyle, dependencies: output.dependencies, + givenMarkers: output.givenMarkers, + selectedMarkers: output.selectedMarkers, } })() + markers.record(fileName, code, { givenMarkers, selectedMarkers }) compiledDependencies.update( this.environment?.name ?? 'client', id, @@ -711,6 +720,9 @@ export function DevupUI({ if (!ownsEngine) configureEngine() resolvedConfig = config isServe = config?.command === 'serve' + dropsMarkers = + !isServe && !config?.build?.watch && config?.isProduction === true + markers.reset() const projectRoot = config?.root ?? process.cwd() locate(projectRoot) // Vite ids are POSIX absolute paths @@ -803,6 +815,7 @@ export function DevupUI({ async buildStart() { if (!extractCss) return await reservation.reserve() + markers.scanned(reservation.markerSelections, reservation.reservedIds) for (const dependency of reservation.dependencies) this.addWatchFile(dependency) }, @@ -986,6 +999,10 @@ export function DevupUI({ if (!SOURCE_FILE_REGEX.test(id.split('?')[0].toLowerCase())) return return extractModule.call(this, code, id) }, + renderChunk(code, chunk) { + if (!extractCss || !dropsMarkers) return null + return markers.renderChunk(code, chunk, this) + }, async generateBundle(_options, bundle) { if (!extractCss) return const writesOutput = this.environment?.config.build?.write !== false diff --git a/packages/vite-plugin/src/reservation.ts b/packages/vite-plugin/src/reservation.ts index 73db38ac9..29cc6b306 100644 --- a/packages/vite-plugin/src/reservation.ts +++ b/packages/vite-plugin/src/reservation.ts @@ -234,6 +234,14 @@ export function createViteReservation( get dependencies() { return dependencies }, + /** The markers the last scan's styles select; `undefined` before one ran */ + get markerSelections() { + return slots.markerSelections + }, + /** The ids the last scan extracted */ + get reservedIds() { + return slots.reservedIds + }, reserve(): Promise { if (!environments.size || prepared === revision) return Promise.resolve() if (!pending) diff --git a/packages/webpack-plugin/src/__tests__/component-markers.test.ts b/packages/webpack-plugin/src/__tests__/component-markers.test.ts new file mode 100644 index 000000000..149f792db --- /dev/null +++ b/packages/webpack-plugin/src/__tests__/component-markers.test.ts @@ -0,0 +1,185 @@ +import { relative } from 'node:path' + +import { + ComponentMarkerLedger, + resetMarkerProcessState, +} from '@devup-ui/plugin-utils' +import { beforeEach, expect, it } from 'bun:test' +import type { Compilation, Compiler } from 'webpack' + +import { stripComponentMarkers } from '../component-markers' + +beforeEach(() => { + resetMarkerProcessState() +}) + +class RawSource { + constructor(private readonly code: string) {} + source(): string { + return this.code + } +} + +/** Webpack's ReplaceSource, as far as the plugin uses it */ +class ReplaceSource { + private readonly edits: [number, number][] = [] + constructor(private readonly original: RawSource) {} + replace(start: number, end: number, text: string): void { + expect(text).toBe('') + this.edits.push([start, end]) + } + source(): string { + const code = this.original.source() + let result = '' + let at = 0 + for (const [start, end] of this.edits) { + result += code.slice(at, start) + at = end + 1 + } + return result + code.slice(at) + } +} + +const webpack = { + webpack: { WebpackError: Error, sources: { ReplaceSource } }, +} as unknown as Pick + +type Asset = { name: string; source: RawSource } + +function compilation( + assets: Record, + modules: object[] = [], +): { compilation: Compilation; assets: Map; warnings: Error[] } { + const stored = new Map( + Object.entries(assets).map(([name, code]) => [ + name, + { name, source: new RawSource(code) }, + ]), + ) + const warnings: Error[] = [] + const value = { + modules: new Set(modules), + warnings, + getAssets: () => [...stored.values()], + updateAsset: (name: string, source: RawSource) => { + stored.set(name, { name, source }) + }, + } + return { + compilation: value as unknown as Compilation, + assets: stored, + warnings, + } +} + +const root = process.cwd() +const module = (resource: string | undefined, code: string | undefined) => ({ + ...(resource === undefined ? {} : { resource }), + originalSource: () => (code === undefined ? null : new RawSource(code)), +}) + +it('drops markers no module selects from script assets', () => { + // Given + const ledger = new ComponentMarkerLedger() + ledger.scanned(['a--b'], ['src/A.tsx']) + ledger.record({ + id: 'src/A.tsx', + input: '', + givenMarkers: ['a--a', 'a--b', 'a--c', 'a--d'], + selectedMarkers: [], + }) + const { compilation: build, assets } = compilation( + { + 'main.js': 'f("a--a x"),f("a--b y"),f("a--c"),f("a--d"),f("a--e")', + 'other.mjs': 'f("y")', + 'main.css': '.a--a{}', + }, + [ + module(`${root}/src/A.tsx`, 'unread'), + module(`${root}/vendor.js`, 'f("a--c")'), + module(undefined, 'f("a--d")'), + module(`${root}/raw.bin`, undefined), + { + ...module(`${root}/src/Cached.tsx`, 'f("a--e")'), + buildInfo: { + devupComponentMarkers: { + id: 'src/Cached.tsx', + input: '', + givenMarkers: ['a--e'], + selectedMarkers: [], + }, + }, + }, + { ...module(`${root}/src/A.tsx`, 'unread'), buildInfo: {} }, + ], + ) + // When + stripComponentMarkers(webpack, build, ledger, root, relative) + // Then + expect(assets.get('main.js')?.source.source()).toBe( + 'f("x"),f("a--b y"),f("a--c"),f("a--d"),f("")', + ) + expect(assets.get('main.css')?.source.source()).toBe('.a--a{}') + expect(assets.get('other.mjs')?.source.source()).toBe('f("y")') +}) + +it('leaves the assets alone when nothing is left to drop', () => { + // Given + const ledger = new ComponentMarkerLedger() + ledger.scanned([], []) + const { + compilation: build, + assets, + warnings, + } = compilation({ + 'main.js': 'f("a--a")', + }) + // When + stripComponentMarkers(webpack, build, ledger, root, relative) + // Then + expect(assets.get('main.js')?.source.source()).toBe('f("a--a")') + expect(warnings).toEqual([]) +}) + +it('keeps every marker and warns when the scan missed a selecting module', () => { + // Given + const ledger = new ComponentMarkerLedger() + ledger.scanned([], []) + ledger.record({ + id: 'src/A.tsx', + input: '', + givenMarkers: ['a--a'], + selectedMarkers: [], + }) + ledger.record({ + id: 'late.tsx', + input: '', + givenMarkers: [], + selectedMarkers: ['a--a'], + cause: 'context-module', + }) + const { + compilation: build, + assets, + warnings, + } = compilation({ + 'main.js': 'f("a--a")', + }) + // When + stripComponentMarkers(webpack, build, ledger, root, relative) + // Then + expect(assets.get('main.js')?.source.source()).toBe('f("a--a")') + expect(warnings.map(({ message }) => message)).toEqual([ + expect.stringContaining('late.tsx [context-module]'), + ]) +}) + +it('keeps every marker quietly without a scan', () => { + // Given + const ledger = new ComponentMarkerLedger() + const { compilation: build, warnings } = compilation({ 'main.js': '' }) + // When + stripComponentMarkers(webpack, build, ledger, root, relative) + // Then + expect(warnings).toEqual([]) +}) diff --git a/packages/webpack-plugin/src/__tests__/loader.test.ts b/packages/webpack-plugin/src/__tests__/loader.test.ts index 5d2fa6e6d..38b30ca49 100644 --- a/packages/webpack-plugin/src/__tests__/loader.test.ts +++ b/packages/webpack-plugin/src/__tests__/loader.test.ts @@ -4,7 +4,13 @@ import { tmpdir } from 'node:os' import * as nodePath from 'node:path' import { join } from 'node:path' -import { type DevupCompilerOutput, loadCompiler } from '@devup-ui/plugin-utils' +import { + ComponentMarkerLedger, + type DevupCompilerOutput, + loadCompiler, + registerMarkerLedger, + resetMarkerProcessState, +} from '@devup-ui/plugin-utils' import { afterEach, beforeEach, @@ -44,6 +50,8 @@ function createCodeExtractResult( updatedBaseStyle: false, map: undefined, dependencies: [], + givenMarkers: [], + selectedMarkers: [], free: () => {}, [Symbol.dispose]: () => {}, ...overrides, @@ -717,3 +725,120 @@ export const a =
` } }) }) + +describe('component-selector markers', () => { + const options = { + package: 'package', + cssDir: 'df/devup-ui', + sheetFile: 's', + classMapFile: 'c', + fileMapFile: 'f', + watch: false, + singleCss: true, + } + let root: string + beforeEach(() => { + resetMarkerProcessState() + root = mkdtempSync(join(tmpdir(), 'devup-loader-markers-')) + }) + afterEach(() => { + rmSync(root, { recursive: true, force: true }) + }) + + async function load( + file: string, + output: Partial, + context: Record, + ) { + const callback = mock() + codeExtractSpy.mockReturnValue(createCodeExtractResult(output)) + const t = createLoaderContext( + { ...options, root }, + callback, + join(root, file), + context, + ) + devupUILoader.bind(t)(Buffer.from('source'), join(root, file)) + await waitFor(() => expect(callback).toHaveBeenCalled()) + } + + it('reports what each module gives and selects to the build ledger', async () => { + // Given + const compiler = { __DEVUP_CACHE: '', options: { resolve: {} } } + const ledger = new ComponentMarkerLedger() + registerMarkerLedger(compiler, ledger) + ledger.scanned([], ['src/A.tsx']) + writeFileSync(join(root, 'disk.tsx'), 'source') + writeFileSync(join(root, 'compiled.tsx'), 'other') + const cached = { buildInfo: {} as Record } + // When + await load( + 'src/A.tsx', + { givenMarkers: ['a--a'] }, + { _compiler: compiler, _module: cached }, + ) + await load( + 'ctx.tsx', + { selectedMarkers: ['a--b'] }, + { + _compiler: { root: compiler, options: { resolve: {} } }, + _module: {}, + _compilation: { + moduleGraph: { + getIncomingConnections: () => [ + { dependency: null }, + { dependency: { type: 'require.context' } }, + ], + }, + }, + }, + ) + await load( + 'disk.tsx', + { selectedMarkers: ['a--c'] }, + { + _compiler: compiler, + _module: {}, + _compilation: { + moduleGraph: { + getIncomingConnections: () => [{ dependency: { type: 'esm' } }], + }, + }, + }, + ) + await load( + 'compiled.tsx', + { selectedMarkers: ['a--d'] }, + { + _compiler: compiler, + }, + ) + await load( + 'gone.tsx', + { selectedMarkers: ['a--e'] }, + { + _compiler: compiler, + }, + ) + // Then + expect(ledger.recorded('src/A.tsx')).toBe(true) + expect(cached.buildInfo['devupComponentMarkers']).toEqual({ + id: 'src/A.tsx', + input: '', + givenMarkers: ['a--a'], + selectedMarkers: [], + cause: undefined, + }) + expect(ledger.plan()).toEqual({ + keep: { + kind: 'unscanned', + modules: [ + { id: 'compiled.tsx', cause: 'compiled-source', markers: ['a--d'] }, + { id: 'ctx.tsx', cause: 'context-module', markers: ['a--b'] }, + { id: 'disk.tsx', cause: 'outside-scan', markers: ['a--c'] }, + { id: 'gone.tsx', cause: 'virtual-module', markers: ['a--e'] }, + ], + }, + }) + }) +}) diff --git a/packages/webpack-plugin/src/__tests__/plugin.test.ts b/packages/webpack-plugin/src/__tests__/plugin.test.ts index b7d210a68..ca2499b9d 100644 --- a/packages/webpack-plugin/src/__tests__/plugin.test.ts +++ b/packages/webpack-plugin/src/__tests__/plugin.test.ts @@ -183,7 +183,10 @@ interface MockCompiler { webpack: { DefinePlugin: ReturnType WebpackError: typeof Error - Compilation: { PROCESS_ASSETS_STAGE_REPORT: number } + Compilation: { + PROCESS_ASSETS_STAGE_REPORT: number + PROCESS_ASSETS_STAGE_OPTIMIZE?: number + } } hooks: { watchRun: { tapPromise: ReturnType } @@ -1803,6 +1806,52 @@ describe('devupUIWebpackPlugin', () => { expect(rebuild).toHaveBeenCalledWith(module, expect.any(Function)) }) + it('drops unselected component markers only in a one-shot production build', () => { + const { compiler } = compile() + compiler.options.mode = 'production' + const run = compiler.hooks.run.tap.mock.calls.find( + ([name]) => name === 'DevupUIWebpackPluginReservation', + )![1] as () => void + compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE = 700 + const ledger = pluginUtils.markerLedgerOf(compiler) + assert(ledger) + const scanned = spyOn(ledger, 'scanned') + run() + expect(scanned).toHaveBeenCalledTimes(1) + const plan = spyOn(ledger, 'plan') + const stages: Record void]> = {} + const compilation = { + modules: [], + warnings: [], + getAssets: () => [], + hooks: { + finishModules: { tap: mock() }, + needAdditionalPass: { tap: mock() }, + processAssets: { + tap: ( + { name, stage }: { name: string; stage: number }, + fn: () => void, + ) => { + stages[name] = [stage, fn] + }, + }, + }, + } + ;( + compiler.hooks.thisCompilation.tap.mock.calls[0]![1] as ( + compilation: object, + ) => void + )(compilation) + const [stage, strip] = stages['DevupUIWebpackPluginComponentMarkers']! + expect(stage).toBe(700) + compiler.watchMode = true + strip() + expect(plan).not.toHaveBeenCalled() + compiler.watchMode = false + strip() + expect(plan).toHaveBeenCalledTimes(1) + }) + it('writes the shared base to disk when it does not hold it yet', () => { getCssSpy.mockReturnValue('base') existsSyncSpy.mockReturnValue(true) diff --git a/packages/webpack-plugin/src/component-markers.ts b/packages/webpack-plugin/src/component-markers.ts new file mode 100644 index 000000000..11cb142fc --- /dev/null +++ b/packages/webpack-plugin/src/component-markers.ts @@ -0,0 +1,82 @@ +import { + type ComponentMarkerLedger, + markerEdits, + markerLikeTokens, + type MarkerTransform, + markerWarning, +} from '@devup-ui/plugin-utils' +import type { Compilation, Compiler } from 'webpack' + +const SCRIPT = /\.[cm]?js$/ + +/** Where the loader keeps a module's report, which webpack's cache restores */ +export const MARKER_BUILD_INFO = 'devupComponentMarkers' + +/** Take the reports of the modules webpack restored instead of loading them */ +function restoreReports( + compilation: Compilation, + ledger: ComponentMarkerLedger, +): void { + for (const module of compilation.modules) { + const report = module.buildInfo?.[MARKER_BUILD_INFO] as + MarkerTransform | undefined + if (report && !ledger.recorded(report.id)) ledger.record(report) + } +} + +/** The marker-like classes modules the loader did not transform wrote */ +function foreignTokens( + compilation: Compilation, + ledger: ComponentMarkerLedger, + root: string, + relative: (from: string, to: string) => string, +): Set { + const tokens = new Set() + for (const module of compilation.modules) { + const resource = + 'resource' in module && typeof module.resource === 'string' + ? relative(root, module.resource).replaceAll('\\', '/') + : undefined + if (resource !== undefined && ledger.recorded(resource)) continue + const source = module.originalSource()?.source() + if (typeof source === 'string') + for (const token of markerLikeTokens(source)) tokens.add(token) + } + return tokens +} + +/** + * Drop the component-selector markers no module of the build selects from + * its script chunks, before they are minified; or, when the build cannot be + * certain, keep them all and warn which modules made it so + */ +export function stripComponentMarkers( + compiler: Pick, + compilation: Compilation, + ledger: ComponentMarkerLedger, + root: string, + relative: (from: string, to: string) => string, +): void { + restoreReports(compilation, ledger) + const plan = ledger.plan() + if ('keep' in plan) { + const warning = markerWarning(plan.keep) + if (warning) + compilation.warnings.push(new compiler.webpack.WebpackError(warning)) + return + } + if (!plan.strip.size) return + const foreign = foreignTokens(compilation, ledger, root, relative) + const strip = new Set( + [...plan.strip].filter((marker) => !foreign.has(marker)), + ) + for (const asset of compilation.getAssets()) { + if (!SCRIPT.test(asset.name)) continue + const code = asset.source.source().toString() + const edits = markerEdits(code, strip) + if (!edits.length) continue + const source = new compiler.webpack.sources.ReplaceSource(asset.source) + for (const [start, end] of edits) source.replace(start, end - 1, '') + compilation.updateAsset(asset.name, source) + } +} diff --git a/packages/webpack-plugin/src/loader.ts b/packages/webpack-plugin/src/loader.ts index e8c71c836..bb92a3de3 100644 --- a/packages/webpack-plugin/src/loader.ts +++ b/packages/webpack-plugin/src/loader.ts @@ -1,3 +1,4 @@ +import { readFileSync } from 'node:fs' import { writeFile } from 'node:fs/promises' import { basename, dirname, join, relative, resolve } from 'node:path' @@ -9,9 +10,16 @@ import { type DevupEngine, esmImportConditions, loadCompiler, + markerLedgerOf, + markerLikeTokens, + type MarkerTransform, + type UnscannedCause, + unscannedCause, } from '@devup-ui/plugin-utils' import type { LoaderContext, RawLoaderDefinitionFunction } from 'webpack' +import { MARKER_BUILD_INFO } from './component-markers' + export interface DevupUILoaderOptions { readonly engine?: DevupEngine /** The project root extraction ids are relative to; the cwd when absent */ @@ -70,6 +78,60 @@ function setRootModuleResolver( compiler.setModuleResolver(moduleResolver) } +/** + * Tell the build's marker ledger which component-selector markers the module + * gives and selects, and, for one that selects any, why the start-of-build + * scan may have missed it + */ +function reportMarkers( + context: LoaderContext, + id: string, + input: string, + output: Pick, +): void { + const owner = context._compiler + const ledger = markerLedgerOf(owner?.root ?? owner) + if (!ledger) return + const selectedMarkers = output.selectedMarkers + const transform: MarkerTransform = { + id, + // Of the code, the ledger reads only the tokens written like a marker + input: [...markerLikeTokens(input)].join(' '), + givenMarkers: [...output.givenMarkers], + selectedMarkers: [...selectedMarkers], + cause: selectedMarkers.length ? markerCause(context, id, input) : undefined, + } + ledger.record(transform) + // A build that restores the module from webpack's cache runs no loader for + // it; the plugin reads the report from the module instead + const buildInfo = context._module?.buildInfo + if (buildInfo) buildInfo[MARKER_BUILD_INFO] = transform +} + +function markerCause( + context: LoaderContext, + id: string, + input: string, +): UnscannedCause { + const graph = context._compilation?.moduleGraph + const module = context._module + if ( + graph && + module && + [...graph.getIncomingConnections(module)].some((connection) => + connection.dependency?.type.includes('context'), + ) + ) + return 'context-module' + let onDisk: string | undefined + try { + onDisk = readFileSync(context.resourcePath, 'utf-8') + } catch { + onDisk = undefined + } + return unscannedCause(id, input, onDisk) +} + const devupUILoader: RawLoaderDefinitionFunction = function (source) { const { @@ -124,6 +186,7 @@ const devupUILoader: RawLoaderDefinitionFunction = 'code' | 'css' | 'map' | 'cssFile' | 'updatedBaseStyle' | 'dependencies' > try { + reportMarkers(this, relativePath, source.toString(), output) const cssFile = output.cssFile snapshot = { code: output.code, diff --git a/packages/webpack-plugin/src/plugin.ts b/packages/webpack-plugin/src/plugin.ts index 1ad96fcff..7874c3b83 100644 --- a/packages/webpack-plugin/src/plugin.ts +++ b/packages/webpack-plugin/src/plugin.ts @@ -8,6 +8,7 @@ import { buildCanonicalMap, buildStaticImportGraph, collectNumberedFiles, + ComponentMarkerLedger, computeFileReach, computeReachableFiles, conventionalSourceRoots, @@ -30,6 +31,7 @@ import { normalizeStylexOptions, planAtomHoist, readJsxImportSource, + registerMarkerLedger, resolveDevupFile, resolveProjectPath, seedFileNumbers, @@ -42,6 +44,7 @@ import { } from '@devup-ui/plugin-utils' import { type Compiler, type RuleSetRule } from 'webpack' +import { stripComponentMarkers } from './component-markers' import { createWebpackReservation } from './reservation' import { servedCss } from './served-css' import { reconcileWatchCss } from './watch-css' @@ -98,6 +101,8 @@ export class DevupUIWebpackPlugin { > & { readonly productionPrewarmFiles?: readonly string[] } /** Webpack's `context` once applied; the working directory before */ private root = process.cwd() + /** What the scan and the loaders report of component-selector markers */ + private readonly markers = new ComponentMarkerLedger() constructor({ package: libPackage = '@devup-ui/react', @@ -519,8 +524,14 @@ export class DevupUIWebpackPlugin { this.importAliases, this.compiler, ) + registerMarkerLedger(compiler, this.markers) const reserveBeforeCompile = () => { + this.markers.clear() reserve(compiler.options) + this.markers.scanned( + reserve.slots.markerSelections, + reserve.slots.reservedIds, + ) if (this.options.watch) return const { entry, context = this.root } = compiler.options this.prewarmExtractor( @@ -611,6 +622,25 @@ export class DevupUIWebpackPlugin { // must hold every style extracted so far if (!existsSync(basePath) || readFileSync(basePath, 'utf-8') !== base) writeFileSync(basePath, base, 'utf-8') + // A one-shot production build drops the markers no module selects + if (compiler.options.mode === 'production') + compilation.hooks.processAssets.tap( + { + name: 'DevupUIWebpackPluginComponentMarkers', + stage: + compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE, + }, + () => { + if (compiler.watchMode) return + stripComponentMarkers( + compiler, + compilation, + this.markers, + this.root, + relative, + ) + }, + ) let stale: string[] = [] compilation.hooks.finishModules.tap('DevupUIWebpackPlugin', () => { if (compiler.watchMode) return diff --git a/packages/webpack-plugin/src/reservation.ts b/packages/webpack-plugin/src/reservation.ts index 691527ac8..daec365b6 100644 --- a/packages/webpack-plugin/src/reservation.ts +++ b/packages/webpack-plugin/src/reservation.ts @@ -17,12 +17,13 @@ export function createWebpackReservation( compiler: Pick, ) { const slots = createSharedSlotReservation(compiler) - return ({ + return Object.assign(reserve, { slots }) + function reserve({ context, entry, resolve: resolveOptions, }: Pick & - Partial>) => { + Partial>) { const cwd = process.cwd() const root = resolve(context ?? cwd) // The loaders extract files under ids relative to the project root