Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pink-houses-lose.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@alauda/ui": patch
---

fix: hack for rspack module federation due to `dayjs -> dayjs/esm` alias
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
],
"scripts": {
"build": "node scripts/build.js",
"build:copy": "node scripts/copy-resources",
"build:lib": "ng-packagr -c tsconfig.lib.json",
"build:watch": "node scripts/build.js --watch",
"debug": "node scripts/build.js --debug --watch",
"dev": "yarn start",
Expand Down
20 changes: 16 additions & 4 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const fs = require('node:fs/promises');
const path = require('node:path');

const gulp = require('gulp');
Expand All @@ -13,7 +14,7 @@ const debugNgPackage = '../ng-package.debug.json';

const releaseDest = isDebug ? require(debugNgPackage).dest : 'release';

function copyResources() {
async function afterBuild() {
const themeDest = path.resolve(releaseDest, 'theme');
gulp
.src([
Expand All @@ -29,6 +30,17 @@ function copyResources() {
.src('src/theme/style.scss')
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(themeDest));

// hack for rspack module federation due to `dayjs -> dayjs/esm` alias
const esmEntry = path.resolve(releaseDest, 'fesm2022/alauda-ui.mjs');
const esmEntryContent = await fs.readFile(esmEntry, 'utf-8');
await fs.writeFile(
esmEntry,
esmEntryContent.replace(
"import dayjs from 'dayjs';",
"import dayjs_ from 'dayjs';\nconst dayjs = 'default' in dayjs_ ? dayjs_.default : dayjs_;",
),
);
Comment thread
JounQin marked this conversation as resolved.
Comment on lines +34 to +43

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Fail the build if the dayjs import patch stops matching.

This replacement is exact-string and currently fails open. If ng-packagr changes the emitted import format even slightly, the build will succeed without applying the federation workaround, which is the whole point of this hook.

Suggested fix
   // hack for rspack module federation due to `dayjs -> dayjs/esm` alias
   const esmEntry = path.resolve(releaseDest, 'fesm2022/alauda-ui.mjs');
   const esmEntryContent = await fs.readFile(esmEntry, 'utf-8');
+  const targetImport = "import dayjs from 'dayjs';";
+  const replacement =
+    "import dayjs_ from 'dayjs';\nconst dayjs = 'default' in dayjs_ ? dayjs_.default : dayjs_;";
+  const matches = esmEntryContent.match(/import dayjs from 'dayjs';/g)?.length ?? 0;
+
+  if (matches !== 1) {
+    throw new Error(
+      `Expected exactly one dayjs import to patch in ${esmEntry}, found ${matches}.`,
+    );
+  }
+
   await fs.writeFile(
     esmEntry,
-    esmEntryContent.replace(
-      "import dayjs from 'dayjs';",
-      "import dayjs_ from 'dayjs';\nconst dayjs = 'default' in dayjs_ ? dayjs_.default : dayjs_;",
-    ),
+    esmEntryContent.replace(targetImport, replacement),
   );
 }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@scripts/build.js` around lines 34 - 43, The current exact-string replacement
for dayjs may silently not match and leave the file unchanged; update the logic
around esmEntry/esmEntryContent and the write operation to detect whether the
replacement actually occurred and fail the build if not — after performing the
replace on esmEntryContent, compare the result to the original content and if
identical (no replacement) throw an error or call process.exit(1) with a clear
message (e.g., "dayjs import patch did not match in esmEntry") so the build
fails rather than continuing without the federation workaround.

}

const packagr = ngPackagr
Expand All @@ -40,7 +52,7 @@ const packagr = ngPackagr

if (watch) {
packagr.watch().subscribe(() => {
copyResources();
afterBuild();

if (!isDebug) {
Comment thread
JounQin marked this conversation as resolved.
const src = path.resolve(__dirname, '../release');
Expand All @@ -50,6 +62,6 @@ if (watch) {
}
});
} else {
// eslint-disable-next-line unicorn/prefer-top-level-await
packagr.build().then(copyResources);
packagr.build().then(afterBuild);
}
Comment thread
JounQin marked this conversation as resolved.
23 changes: 0 additions & 23 deletions scripts/copy-resources.js

This file was deleted.

Loading