Skip to content

Commit

Permalink
fix(angular): fix misc issues with migration replacing the nguniversa…
Browse files Browse the repository at this point in the history
…l usages (#20209)
  • Loading branch information
leosvelperez committed Nov 13, 2023
1 parent 2883fa3 commit b4fbdf1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ describe('replace-nguniversal-engines migration', () => {
};
projectGraph = {
dependencies: {
app1: [{ source: 'app1', target: 'npm:@angular/core', type: 'static' }],
app1: [
{ source: 'app1', target: 'npm:@nguniversal/common', type: 'static' },
],
},
nodes: { app1: { data: project, name: 'app1', type: 'app' } },
};
Expand Down Expand Up @@ -214,6 +216,20 @@ if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
expect(tree.exists('src/express.tokens.ts')).toBe(true);
});

it('should import tokens file correctly in nested paths', async () => {
const filePath = 'src/nested/folder/home/home.component.ts';
tree.write(
filePath,
`import { RESPONSE } from '@nguniversal/express-engine/tokens';`
);

await migration(tree);

expect(tree.read(filePath, 'utf-8')).toContain(
`import { RESPONSE } from '../../../express.tokens';`
);
});

it('should not create tokens file when "@nguniversal/express-engine/tokens" is not used', async () => {
await migration(tree);

Expand All @@ -224,4 +240,13 @@ if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
);
expect(tree.exists('src/express.tokens.ts')).toBe(false);
});

it('should not process non-TypeScript files', async () => {
const content = `import { ngExpressEngine } from '@nguniversal/express-engine';`;
tree.write('src/foo.txt', content);

await migration(tree);

expect(tree.read('src/foo.txt', 'utf-8')).toBe(content);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const UNIVERSAL_PACKAGES = [
**/
const NGUNIVERSAL_PACKAGE_REGEXP =
/@nguniversal\/(common(\/engine)?|express-engine)/g;
const serverExecutors = [
'@angular-devkit/build-angular:server',
'@nx/angular:webpack-server',
];

export default async function (tree: Tree) {
const packageJson = readJson(tree, 'package.json');
Expand All @@ -40,7 +44,8 @@ export default async function (tree: Tree) {
}

const projects = await getProjectsFilteredByDependencies(tree, [
'npm:@angular/core',
'npm:@nguniversal/common',
'npm:@nguniversal/express-engine',
]);
for (const { project } of projects) {
if (project.projectType !== 'application') {
Expand All @@ -52,7 +57,7 @@ export default async function (tree: Tree) {
string /** Output Path */
>();
for (const target of Object.values(project.targets ?? {})) {
if (target.executor !== '@angular-devkit/build-angular:server') {
if (!serverExecutors.includes(target.executor)) {
continue;
}

Expand All @@ -74,33 +79,36 @@ export default async function (tree: Tree) {
const tokensFilePath = `${root}/express.tokens.ts`;

visitNotIgnoredFiles(tree, root, (path) => {
const content = tree.read(path, 'utf8');
let updatedContent = content;
if (!path.endsWith('.ts') || path.endsWith('.d.ts')) {
return;
}

let content = tree.read(path, 'utf8');
if (!content.includes('@nguniversal/')) {
return;
}

// Check if file is importing tokens
if (content.includes('@nguniversal/express-engine/tokens')) {
hasExpressTokens ||= true;

let tokensFileRelativePath: string = relative(
dirname(normalizePath(path)),
normalizePath(tokensFilePath)
let tokensFileRelativePath: string = normalizePath(
relative(dirname(path), tokensFilePath)
);

if (tokensFileRelativePath.charAt(0) !== '.') {
tokensFileRelativePath = './' + tokensFileRelativePath;
}

updatedContent = updatedContent.replaceAll(
content = content.replaceAll(
'@nguniversal/express-engine/tokens',
tokensFileRelativePath.slice(0, -3)
);
}

updatedContent = updatedContent.replaceAll(
NGUNIVERSAL_PACKAGE_REGEXP,
'@angular/ssr'
);
tree.write(path, updatedContent);
content = content.replaceAll(NGUNIVERSAL_PACKAGE_REGEXP, '@angular/ssr');

tree.write(path, content);
});

// Replace server file and add tokens file if needed
Expand Down

0 comments on commit b4fbdf1

Please sign in to comment.