[code-infra] Generate package.json imports field on build#1533
Conversation
Deploy previewBundle sizeTotal Size Change: 0B(0.00%) - Total Gzip Change: 0B(0.00%) Show details for 64 more bundles@mui/internal-docs-infra/abstractCreateDemo parsed: 0B(0.00%) gzip: 0B(0.00%) PerformanceTotal duration: 17.34 ms -0.62 ms(-3.4%) | Renders: 4 (+0) | Paint: 76.62 ms -2.89 ms(-3.6%) No significant changes — details Check out the code infra dashboard for more information about this PR. |
…finalizeConditions
|
I ran into a few issues: 1. Extra conditions aren't path-rewritten. The { './*': { 'mui-src': './src/*.ts', node: './src/node/*.ts' } }
// →
{ import: { node: './src/node/*.ts', default: './Alert.js' }, ... }That 2. Standard condition objects are rejected outright. 3. Multi-segment globs silently vanish. What does work well: with explicit string keys, the path rewriting + ESM/CJS extensions + Suggestions, roughly in priority order:
|
…pand
Replaces the `mui-src` condition key (which had no consumers) with leaf-based
rewriting that accepts any standard conditions object and rewrites every source
path it contains, including those nested inside sibling conditions. This fixes
the review feedback:
- Sibling conditions are now path-rewritten (conditions stay outer, the
import/require split is injected at each leaf).
- Standard `{ node, default, ... }` conditions objects are accepted (no special
key required).
- Glob enumeration is now controlled by a `--expand` CLI flag (default on),
decoupled from `--flat`; `--no-expand` keeps `*` as a Node runtime subpath
pattern. A zero-match pattern now warns instead of silently dropping.
- Negation (`null`) keys follow Node's most-specific-wins resolution rather than
cascade-subtraction, so a deeper positive pattern still resolves under a
shallower `null`.
Paths that aren't under `src/` (copied assets, build outputs, bare specifiers)
pass through verbatim.
- finalizeConditionValue no longer clobbers a user-authored `default` that sits alongside `import`/`require` in the same conditions object; it only synthesizes `default` when one isn't already present. - Glob expansion of a conditions object now drops a sibling condition whose source file is missing for a given stem (a sibling glob need not match every stem of the primary pattern) instead of failing the build on a non-existent path. Adds regression tests for both.
|
One (non-blocking) issue came out in Base UI: code-infra now validates import targets exist in the build and warns "./test/index.ts" for "#test-utils" was not found. The published imports then include a broken (dev-only) #test-utils entry. Probably needs a code-infra story for dev-only imports, or base-ui dropping #test-utils from published imports. |
|
How do you mark something as dev-only ? |
|
How about we remove the entry from |
|
Personally I'd rather just error when an import/export doesn't resolve, such that CI yells at us when we move something around without updating the import/export, instead of just silently dropping the export. Can't we just keep test-only things out of there and reserve imports/exports for the published shape only? Why is that test import really there? To prevent long backtracking file paths? Maybe an alias in just the test env is better suited for this purpose? |
|
It's just for convenience, to avoid typing long paths with many ".." |
|
Ok, anyway, it's non-blocking for this PR. Another option that I just think about is to {
"#test-utils": { "test": "./test/index.ts" }
}And drop all |
…tories createPackageExports/createPackageImports/createPackageBin now take their subject (exports/imports/bin) as the first positional argument and the rest as an options object, surfacing the primary input. createPackageImports is only called when an imports field is present. No behavior change.
The build tool generates the package.json
exportsfield but was deleting theimportsfield. This regeneratesimportsthe same way, so packages can use internal#-subpath imports pointing at source files.Changes
build.mjs: Extracted the condition-rebuilding logic fromcreatePackageExportsinto a sharedfinalizeConditions()helper. AddedcreatePackageImports(), which mirrorscreatePackageExports(reusingexpandExportGlobs/createExportsFor/finalizeConditions) minus the./package.json/main/typesindex handling that only applies to public exports. Keys are validated to start with#; bare specifiers (e.g. external packages) are passed through unchanged.cmdBuild.mjs:writePackageJsonnow regeneratesimports(in parallel withexports) instead of deleting it.build.test.mjs: Tests for subpath rewrite, condition ordering, bare-specifier passthrough, glob expansion, the#-prefix validation, and the no-imports case.