|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { workspaces } from '@angular-devkit/core'; |
| 10 | +import { |
| 11 | + Rule, |
| 12 | + SchematicContext, |
| 13 | + SchematicsException, |
| 14 | + chain, |
| 15 | + externalSchematic, |
| 16 | +} from '@angular-devkit/schematics'; |
| 17 | +import { dirname } from 'node:path'; |
| 18 | +import { JSONFile } from '../../utility/json-file'; |
| 19 | +import { TreeWorkspaceHost, allTargetOptions, getWorkspace } from '../../utility/workspace'; |
| 20 | +import { Builders, ProjectType } from '../../utility/workspace-models'; |
| 21 | + |
| 22 | +export default function (): Rule { |
| 23 | + return async (tree, context) => { |
| 24 | + const rules: Rule[] = []; |
| 25 | + const workspace = await getWorkspace(tree); |
| 26 | + |
| 27 | + for (const [name, project] of workspace.projects) { |
| 28 | + if (project.extensions.projectType !== ProjectType.Application) { |
| 29 | + // Only interested in application projects since these changes only effects application builders |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + const buildTarget = project.targets.get('build'); |
| 34 | + if (!buildTarget || buildTarget.builder === Builders.Application) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + if ( |
| 39 | + buildTarget.builder !== Builders.BrowserEsbuild && |
| 40 | + buildTarget.builder !== Builders.Browser |
| 41 | + ) { |
| 42 | + context.logger.error( |
| 43 | + `Cannot update project "${name}" to use the application builder.` + |
| 44 | + ` Only "${Builders.BrowserEsbuild}" and "${Builders.Browser}" can be automatically migrated.`, |
| 45 | + ); |
| 46 | + |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + // Update builder target and options |
| 51 | + buildTarget.builder = Builders.Application; |
| 52 | + const hasServerTarget = project.targets.has('server'); |
| 53 | + |
| 54 | + for (const [, options] of allTargetOptions(buildTarget, false)) { |
| 55 | + // Show warnings for using no longer supported options |
| 56 | + if (usesNoLongerSupportedOptions(options, context, name)) { |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + // Rename and transform options |
| 61 | + options['browser'] = options['main']; |
| 62 | + if (hasServerTarget && typeof options['browser'] === 'string') { |
| 63 | + options['server'] = dirname(options['browser']) + '/main.server.ts'; |
| 64 | + } |
| 65 | + options['serviceWorker'] = options['ngswConfigPath'] ?? options['serviceWorker']; |
| 66 | + |
| 67 | + if (typeof options['polyfills'] === 'string') { |
| 68 | + options['polyfills'] = [options['polyfills']]; |
| 69 | + } |
| 70 | + |
| 71 | + if (typeof options['outputPath'] === 'string') { |
| 72 | + options['outputPath'] = options['outputPath']?.replace(/\/browser\/?$/, ''); |
| 73 | + } |
| 74 | + |
| 75 | + // Delete removed options |
| 76 | + delete options['deployUrl']; |
| 77 | + delete options['vendorChunk']; |
| 78 | + delete options['commonChunk']; |
| 79 | + delete options['resourcesOutputPath']; |
| 80 | + delete options['buildOptimizer']; |
| 81 | + delete options['main']; |
| 82 | + delete options['ngswConfigPath']; |
| 83 | + } |
| 84 | + |
| 85 | + // Merge browser and server tsconfig |
| 86 | + if (hasServerTarget) { |
| 87 | + const browserTsConfig = buildTarget?.options?.tsConfig; |
| 88 | + const serverTsConfig = project.targets.get('server')?.options?.tsConfig; |
| 89 | + |
| 90 | + if (typeof browserTsConfig !== 'string') { |
| 91 | + throw new SchematicsException( |
| 92 | + `Cannot update project "${name}" to use the application builder` + |
| 93 | + ` as the browser tsconfig cannot be located.`, |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + if (typeof serverTsConfig !== 'string') { |
| 98 | + throw new SchematicsException( |
| 99 | + `Cannot update project "${name}" to use the application builder` + |
| 100 | + ` as the server tsconfig cannot be located.`, |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + const browserJson = new JSONFile(tree, browserTsConfig); |
| 105 | + const serverJson = new JSONFile(tree, serverTsConfig); |
| 106 | + |
| 107 | + const filesPath = ['files']; |
| 108 | + |
| 109 | + const files = new Set([ |
| 110 | + ...((browserJson.get(filesPath) as string[] | undefined) ?? []), |
| 111 | + ...((serverJson.get(filesPath) as string[] | undefined) ?? []), |
| 112 | + ]); |
| 113 | + |
| 114 | + // Server file will be added later by the means of the ssr schematic. |
| 115 | + files.delete('server.ts'); |
| 116 | + |
| 117 | + browserJson.modify(filesPath, Array.from(files)); |
| 118 | + |
| 119 | + const typesPath = ['compilerOptions', 'types']; |
| 120 | + browserJson.modify( |
| 121 | + typesPath, |
| 122 | + Array.from( |
| 123 | + new Set([ |
| 124 | + ...((browserJson.get(typesPath) as string[] | undefined) ?? []), |
| 125 | + ...((serverJson.get(typesPath) as string[] | undefined) ?? []), |
| 126 | + ]), |
| 127 | + ), |
| 128 | + ); |
| 129 | + |
| 130 | + // Delete server tsconfig |
| 131 | + tree.delete(serverTsConfig); |
| 132 | + } |
| 133 | + |
| 134 | + // Update main tsconfig |
| 135 | + const rootJson = new JSONFile(tree, 'tsconfig.json'); |
| 136 | + rootJson.modify(['compilerOptions', 'esModuleInterop'], true); |
| 137 | + rootJson.modify(['compilerOptions', 'downlevelIteration'], undefined); |
| 138 | + rootJson.modify(['compilerOptions', 'allowSyntheticDefaultImports'], undefined); |
| 139 | + |
| 140 | + // Update server file |
| 141 | + const ssrMainFile = project.targets.get('server')?.options?.['main']; |
| 142 | + if (typeof ssrMainFile === 'string') { |
| 143 | + tree.delete(ssrMainFile); |
| 144 | + |
| 145 | + rules.push( |
| 146 | + externalSchematic('@schematics/angular', 'ssr', { |
| 147 | + project: name, |
| 148 | + skipInstall: true, |
| 149 | + }), |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | + // Delete package.json helper scripts |
| 154 | + const pkgJson = new JSONFile(tree, 'package.json'); |
| 155 | + ['build:ssr', 'dev:ssr', 'serve:ssr', 'prerender'].forEach((s) => |
| 156 | + pkgJson.remove(['scripts', s]), |
| 157 | + ); |
| 158 | + |
| 159 | + // Delete all redundant targets |
| 160 | + for (const [key, target] of project.targets) { |
| 161 | + switch (target.builder) { |
| 162 | + case Builders.Server: |
| 163 | + case Builders.Prerender: |
| 164 | + case Builders.AppShell: |
| 165 | + case Builders.SsrDevServer: |
| 166 | + project.targets.delete(key); |
| 167 | + break; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + // Save workspace changes |
| 173 | + await workspaces.writeWorkspace(workspace, new TreeWorkspaceHost(tree)); |
| 174 | + |
| 175 | + return chain(rules); |
| 176 | + }; |
| 177 | +} |
| 178 | + |
| 179 | +function usesNoLongerSupportedOptions( |
| 180 | + { deployUrl, resourcesOutputPath }: Record<string, unknown>, |
| 181 | + context: SchematicContext, |
| 182 | + projectName: string, |
| 183 | +): boolean { |
| 184 | + let hasUsage = false; |
| 185 | + if (typeof deployUrl === 'string') { |
| 186 | + hasUsage = true; |
| 187 | + context.logger.warn( |
| 188 | + `Skipping migration for project "${projectName}". "deployUrl" option is not available in the application builder.`, |
| 189 | + ); |
| 190 | + } |
| 191 | + |
| 192 | + if (typeof resourcesOutputPath === 'string' && /^\/?media\/?$/.test(resourcesOutputPath)) { |
| 193 | + hasUsage = true; |
| 194 | + context.logger.warn( |
| 195 | + `Skipping migration for project "${projectName}". "resourcesOutputPath" option is not available in the application builder.` + |
| 196 | + `Media files will be output into a "media" directory within the output location.`, |
| 197 | + ); |
| 198 | + } |
| 199 | + |
| 200 | + return hasUsage; |
| 201 | +} |
0 commit comments