Skip to content

Commit e9f7bd6

Browse files
authored
feat: add CJS build output for dual format (#518)
Ideally we want to use ESM, but the reality is that @fluentui/react-theme (and other Fluent packages) ESM lacks Node.js compliance (missing .js extensions) - it works in bundlers but not in Node.js. This means downstream tools are forced to use a CJS bundler, which can `require` Fluent UI from `node_modules` because Fluent has CJS exports as a fallback. However, this package doesn't provide CJS, so such a downstream bundler cannot `require` it and must bundle our source into its own dist instead. By adding CJS here, downstream libraries can externalize us from node_modules, reducing their bundle sizes. Otherwise we become a nested dependency. - Add build:cjs script and lib-commonjs output - Update exports with require condition - Remove "type": "module" for dual-format semantics
1 parent 1051da4 commit e9f7bd6

File tree

4 files changed

+210
-7
lines changed

4 files changed

+210
-7
lines changed

biome.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
"**/*.ts",
1111
"**/*.tsx"
1212
],
13-
"ignore": ["lib/", ".vscode/"]
13+
"ignore": ["lib/", "lib-commonjs/", ".vscode/"]
1414
},
1515
"organizeImports": {
1616
"enabled": true,
17-
"ignore": ["generated/", ".vscode/"]
17+
"ignore": ["generated/", "lib-commonjs/", ".vscode/"]
1818
},
1919
"formatter": {
2020
"enabled": true,
@@ -23,7 +23,7 @@
2323
},
2424
"linter": {
2525
"enabled": true,
26-
"ignore": ["generated/", ".vscode/"],
26+
"ignore": ["generated/", "lib-commonjs/", ".vscode/"],
2727
"rules": {
2828
"recommended": false,
2929
"correctness": {

theme/lib-commonjs/index.js

Lines changed: 195 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

theme/lib-commonjs/index.js.map

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

theme/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@
1010
},
1111
"license": "MIT",
1212
"author": "Axis Communications AB",
13-
"type": "module",
1413
"exports": {
1514
".": {
1615
"types": "./lib/index.d.ts",
17-
"import": "./lib/index.js"
16+
"import": "./lib/index.js",
17+
"require": "./lib-commonjs/index.js"
1818
}
1919
},
2020
"bin": {
2121
"fluent-theme-tokens": "./lib/tokens.cjs"
2222
},
23-
"files": ["lib"],
23+
"files": ["lib", "lib-commonjs"],
2424
"scripts": {
25-
"build": "pnpm build:types && pnpm build:esm && pnpm build:cli",
25+
"build": "pnpm build:types && pnpm build:esm && pnpm build:cjs && pnpm build:cli",
2626
"build:cli": "esbuild --format=cjs --bundle --platform=node --packages=external --outfile=lib/tokens.cjs src/tokens/build.ts",
27+
"build:cjs": "esbuild --format=cjs --bundle --sourcemap --packages=external --outdir=lib-commonjs src/index.ts",
2728
"build:esm": "esbuild --format=esm --bundle --sourcemap --packages=external --outdir=lib src/index.ts",
2829
"build:types": "tsc -p tsconfig.build.json",
2930
"check:unused-deps": "depcheck . --config=depcheck.yml",

0 commit comments

Comments
 (0)