-
Notifications
You must be signed in to change notification settings - Fork 14
fix: hack for rspack module federation due to dayjs -> dayjs/esm alias
#604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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'); | ||
|
|
@@ -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([ | ||
|
|
@@ -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 on lines
+34
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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 |
||
| } | ||
|
|
||
| const packagr = ngPackagr | ||
|
|
@@ -40,7 +52,7 @@ const packagr = ngPackagr | |
|
|
||
| if (watch) { | ||
| packagr.watch().subscribe(() => { | ||
| copyResources(); | ||
| afterBuild(); | ||
|
|
||
| if (!isDebug) { | ||
|
JounQin marked this conversation as resolved.
|
||
| const src = path.resolve(__dirname, '../release'); | ||
|
|
@@ -50,6 +62,6 @@ if (watch) { | |
| } | ||
| }); | ||
| } else { | ||
| // eslint-disable-next-line unicorn/prefer-top-level-await | ||
| packagr.build().then(copyResources); | ||
| packagr.build().then(afterBuild); | ||
| } | ||
|
JounQin marked this conversation as resolved.
|
||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.