Skip to content

Commit c2f4243

Browse files
fix build for demos bundles
replace antiforgery before bundle
1 parent 894f1d6 commit c2f4243

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

apps/demos/utils/create-bundles/Angular/bundler.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import { exec } from 'child_process';
22
import { BuildOptions } from 'esbuild';
3-
import { existsSync, mkdirSync, removeSync } from 'fs-extra';
3+
import {
4+
existsSync,
5+
mkdirSync,
6+
readFileSync,
7+
readdirSync,
8+
removeSync,
9+
statSync,
10+
writeFileSync,
11+
} from 'fs-extra';
12+
import { dirname, join, relative } from 'path';
413
import { Demo, Framework } from '../helper/types';
514
import { createDemoLayout, getDestinationPathByDemo, getSourcePathByDemo } from '../helper';
615
import { getIndexHtmlPath, getProjectNameByDemo } from './utils';
@@ -19,6 +28,44 @@ export default class AngularBundler implements Bundler {
1928

2029
getBuildOptions = (): BuildOptions => ({});
2130

31+
private updateAntiForgeryImport = (sourceDemoPath: string) => {
32+
const angularAppPath = join(sourceDemoPath, 'app');
33+
if (!existsSync(angularAppPath)) {
34+
return;
35+
}
36+
37+
const oldImport = "import 'anti-forgery';";
38+
const antiForgeryFilePath = join(__dirname, '..', '..', '..', 'shared', 'anti-forgery', 'fetch-override.js');
39+
40+
const replaceRecursively = (directoryPath: string) => {
41+
const entries = readdirSync(directoryPath);
42+
entries.forEach((entryName) => {
43+
const entryPath = join(directoryPath, entryName);
44+
const isDirectory = statSync(entryPath).isDirectory();
45+
if (isDirectory) {
46+
replaceRecursively(entryPath);
47+
return;
48+
}
49+
50+
if (!entryPath.endsWith('.ts')) {
51+
return;
52+
}
53+
54+
const content = readFileSync(entryPath, 'utf8');
55+
if (!content.includes(oldImport)) {
56+
return;
57+
}
58+
59+
const relativeImportPath = relative(dirname(entryPath), antiForgeryFilePath).split('\\').join('/');
60+
const normalizedImportPath = relativeImportPath.startsWith('.') ? relativeImportPath : `./${relativeImportPath}`;
61+
const newImport = `import '${normalizedImportPath}';`;
62+
writeFileSync(entryPath, content.split(oldImport).join(newImport), 'utf8');
63+
});
64+
};
65+
66+
replaceRecursively(angularAppPath);
67+
};
68+
2269
buildDemo = (demo: Demo, res): Promise<void> => {
2370
const sourceDemoPath = getSourcePathByDemo(demo, this.framework);
2471
if (!existsSync(sourceDemoPath)) {
@@ -41,6 +88,7 @@ export default class AngularBundler implements Bundler {
4188
mkdirSync(indexHtmlPath, { recursive: true });
4289

4390
createDemoLayout(demo, this.framework);
91+
this.updateAntiForgeryImport(sourceDemoPath);
4492

4593
const ngBuildCommand = `npm run build-angular -- ${getProjectNameByDemo(demo)}`;
4694
const ngBuildProcess = exec(ngBuildCommand);

0 commit comments

Comments
 (0)