An open source design system that's fully customizable and agent ready
TypeScript
fix(core,cli): fix Node.js ESM resolution for CLI theme build (#2512)
Xinzi Zhouc3d24c97e453
diff --git a/packages/cli/src/commands/build-theme.mjs b/packages/cli/src/commands/build-theme.mjsindex 1c67ca9a93..74614b0557 100644--- a/packages/cli/src/commands/build-theme.mjs+++ b/packages/cli/src/commands/build-theme.mjs@@ -14,7 +14,6 @@import * as fs from 'node:fs';import * as path from 'node:path';-import {createRequire} from 'node:module';import {pathToFileURL, fileURLToPath} from 'node:url';import {createJiti} from 'jiti';import {getRunPrefix} from '../utils/package-manager.mjs';@@ -23,19 +22,18 @@ import {jsonOut, jsonError, humanLog} from '../lib/json.mjs';// Import shared theme processing from core — ensures build and runtime// use the same logic for typography.scale expansion, prose, and component rules.-const _require = createRequire(import.meta.url);let _defineTheme = null;let _generateThemeRules = null;let _generateThemeRulesSplit = null;let _generateOnMediaCSS = null;try {- const coreTheme = _require('@xds/core/theme');+ const coreTheme = await import('@xds/core/theme');_defineTheme = coreTheme.defineTheme;_generateThemeRules = coreTheme.generateThemeRules;_generateThemeRulesSplit = coreTheme.generateThemeRulesSplit;_generateOnMediaCSS = coreTheme.generateOnMediaCSS;-} catch {- // Core not available — fall back to legacy generation+} catch (e) {+ console.warn('Warning: could not load @xds/core/theme:', e.message);}/**diff --git a/packages/core/babel-plugin-add-extensions.cjs b/packages/core/babel-plugin-add-extensions.cjsnew file mode 100644index 0000000000..666d75d2e6--- /dev/null+++ b/packages/core/babel-plugin-add-extensions.cjs@@ -0,0 +1,48 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.+/* eslint-disable @typescript-eslint/no-require-imports, no-undef */++/**+ * Babel plugin that appends .js to relative import/export paths.+ *+ * TypeScript source uses extensionless imports (e.g. './XDSButton').+ * Node.js ESM requires explicit extensions for "type": "module" packages.+ * This plugin rewrites them during compilation so no postbuild step is needed.+ *+ * Handles both file imports ('./XDSButton' → './XDSButton.js') and+ * directory imports ('./domainTokens' → './domainTokens/index.js').+ */+const fs = require('fs');+const nodePath = require('path');++module.exports = function addExtensions() {+ const SKIP = /\.(?:js|mjs|cjs|json|css)$/;++ return {+ visitor: {+ 'ImportDeclaration|ExportNamedDeclaration|ExportAllDeclaration'(+ path,+ state,+ ) {+ const source = path.node.source;+ if (!source) return;+ const value = source.value;+ if (!value.startsWith('./') && !value.startsWith('../')) return;+ if (SKIP.test(value)) return;++ const dir = nodePath.dirname(state.filename);+ const asFile = nodePath.join(dir, value + '.ts');+ const asTsx = nodePath.join(dir, value + '.tsx');+ const asIndex = nodePath.join(dir, value, 'index.ts');+ const asIndexTsx = nodePath.join(dir, value, 'index.tsx');++ if (fs.existsSync(asFile) || fs.existsSync(asTsx)) {+ source.value = value + '.js';+ } else if (fs.existsSync(asIndex) || fs.existsSync(asIndexTsx)) {+ source.value = value + '/index.js';+ } else {+ source.value = value + '.js';+ }+ },+ },+ };+};diff --git a/packages/core/babel.config.json b/packages/core/babel.config.jsonindex a5e6b0b200..b1c7a25273 100644--- a/packages/core/babel.config.json+++ b/packages/core/babel.config.json@@ -4,6 +4,7 @@["@babel/preset-typescript", {"isTSX": true, "allExtensions": true}]],"plugins": [+ "./babel-plugin-add-extensions.cjs",["@stylexjs/babel-plugin",{