Skip to content
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

feat: support angular build application structure #1216

Merged
merged 1 commit into from
Feb 6, 2024
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
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
"builders": "builders.json",
"homepage": "https://github.com/AmadeusITGroup/ngx-prefetch#readme",
"dependencies": {
"@angular-devkit/architect": "^0.1602.11",
"@angular-devkit/core": "^16.2.11",
"@o3r/schematics": "^9.5.2",
"@angular-devkit/architect": "~0.1700.0",
"@angular-devkit/core": "~17.0.0",
"@o3r/schematics": "^10.0.0-prerelease.61",
"mustache": "^4.2.0",
"typescript": "~5.3.0",
"webpack": "^5.89.0"
},
"devDependencies": {
"@angular-devkit/schematics": "^16.2.11",
"@angular/cli": "^16.2.11",
"@schematics/angular": "^16.2.11",
"@angular-devkit/schematics": "~17.0.0",
"@angular/cli": "~17.0.0",
"@schematics/angular": "~17.0.0",
"@types/jest": "^29.5.11",
"@types/mustache": "^4.2.5",
"@types/node": "^20.10.6",
Expand Down
40 changes: 33 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ export function filterOptions(options: PrefetchBuilderSchema, allowedOptions: st
/** Maximum number of steps */
const STEP_NUMBER = 4;

/**
* Compute the path where to find the swJson file from the ngx-prefetch and the build target options.
* @param targetBuildBuilder
* @param targetOutputPath
* @param outputPathOverride
*/
function computeSwJsonFolderPath<T extends {base: string, browser?: string}>(targetBuildBuilder: string,
targetOutputPath: string | T, outputPathOverride: string | undefined) {
if (typeof outputPathOverride === 'string' && !!outputPathOverride) {
return outputPathOverride;
}
if (typeof targetOutputPath === 'string') {
if (['@nx/angular:application', '@angular-devkit/build-angular:application'].indexOf(targetBuildBuilder) > -1) {
return path.join(targetOutputPath, 'browser');
}
return targetOutputPath;
}
if (typeof targetOutputPath.browser === 'string' && typeof targetOutputPath.base === 'string') {
return path.join(targetOutputPath.base, targetOutputPath.browser);
}
if (typeof targetOutputPath.base === 'string') {
return targetOutputPath.base;
}
return undefined;
}

export default createBuilder<PrefetchBuilderSchema>(async (options, context): Promise<BuilderOutput> => {
context.reportRunning();
Expand All @@ -59,16 +84,17 @@ export default createBuilder<PrefetchBuilderSchema>(async (options, context): Pr
]);
const targetBuildOptions = await context.validateOptions<{ outputPath: string }>(targetBuildRawOptions, targetBuildBuilder);

/** Path to the build output folder */
const prodBuildOutputPath = computeSwJsonFolderPath(targetBuildBuilder, targetBuildOptions.outputPath, options.outputPath);

/** Check the minimum of mandatory options to the builders */
if (typeof targetBuildOptions.outputPath !== 'string') {
if (!prodBuildOutputPath) {
return {
success: false,
error: `The targetBuild ${options.targetBuild} does not provide 'outputPath' option`
error: `Neither the ngx-prefetch task nor targetBuild ${options.targetBuild} provide an 'outputPath' option.`
};
}

/** Path to the build output folder */
const prodBuildOutputPath = targetBuildOptions.outputPath;

context.reportProgress(1, STEP_NUMBER, 'Read ngsw.json');
let swJsonString;
Expand All @@ -78,7 +104,7 @@ export default createBuilder<PrefetchBuilderSchema>(async (options, context): Pr
} catch (error: any) {
return {
success: false,
error
error: error.message
}
}
const swJson = JSON.parse(swJsonString);
Expand Down Expand Up @@ -108,13 +134,13 @@ export default createBuilder<PrefetchBuilderSchema>(async (options, context): Pr

context.reportProgress(4, STEP_NUMBER, 'Webpack');
const compiler = webpack({
mode: options.production ? "production" : "none",
mode: options.production ? 'production' : 'none',
entry: tempOutputPath,
output: {
path: path.join(process.cwd(), prodBuildOutputPath),
filename: 'ngxPrefetch.js'
},
devtool: options.production ? "source-map" : false
devtool: options.production ? 'source-map' : false
});
return new Promise((resolve) => {
compiler.run((err, stats) => {
Expand Down
5 changes: 5 additions & 0 deletions src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
}
},
"additionalProperties": false
},
"outputPath": {
"type": "string",
"description": "Path to the folder of ngsw.json in the production build output.",
"default": ""
}
},
"additionalProperties": false,
Expand Down
6 changes: 6 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ export interface PrefetchBuilderSchema extends JsonObject {

/** List of fallback locales mapping */
fallbackLocalesMap?: {[locale: string]: string};

/**
* Path to the folder of ngsw.json in the production build output.
* If not defined, will try to compute it from the executor options.
*/
outputPath?: string;
}
Loading
Loading