Skip to content

feat(@schematics/angular): generate ngsw-config.json under the project #13346

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

Merged
merged 1 commit into from
Jan 8, 2019
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
15 changes: 7 additions & 8 deletions packages/schematics/angular/service-worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
SchematicContext,
SchematicsException,
Tree,
UpdateRecorder,
apply,
chain,
mergeWith,
Expand Down Expand Up @@ -54,13 +53,14 @@ function getProjectConfiguration(
return applyTo;
}

function updateConfigFile(options: ServiceWorkerOptions): Rule {
function updateConfigFile(options: ServiceWorkerOptions, root: string): Rule {
return (host: Tree, context: SchematicContext) => {
context.logger.debug('updating config file.');
const workspace = getWorkspace(host);

const config = getProjectConfiguration(workspace, options);
config.serviceWorker = true;
config.ngswConfigPath = `${root.endsWith('/') ? root : root + '/'}ngsw-config.json`;

return updateWorkspace(workspace);
};
Expand Down Expand Up @@ -103,8 +103,7 @@ function updateAppModule(options: ServiceWorkerOptions): Rule {
let importModule = 'ServiceWorkerModule';
let importPath = '@angular/service-worker';
if (!isImported(moduleSource, importModule, importPath)) {
const change = insertImport
(moduleSource, modulePath, importModule, importPath);
const change = insertImport(moduleSource, modulePath, importModule, importPath);
if (change) {
const recorder = host.beginUpdate(modulePath);
recorder.insertLeft((change as InsertChange).pos, (change as InsertChange).toAdd);
Expand All @@ -119,8 +118,7 @@ function updateAppModule(options: ServiceWorkerOptions): Rule {
// TODO: dynamically find environments relative path
importPath = '../environments/environment';
if (!isImported(moduleSource, importModule, importPath)) {
const change = insertImport
(moduleSource, modulePath, importModule, importPath);
const change = insertImport(moduleSource, modulePath, importModule, importPath);
if (change) {
const recorder = host.beginUpdate(modulePath);
recorder.insertLeft((change as InsertChange).pos, (change as InsertChange).toAdd);
Expand Down Expand Up @@ -176,16 +174,17 @@ export default function (options: ServiceWorkerOptions): Rule {
resourcesOutputPath = '/' + resourcesOutputPath.split('/').filter(x => !!x).join('/');
}

const root = project.root || project.sourceRoot || '';
const templateSource = apply(url('./files'), [
template({ ...options, resourcesOutputPath }),
move(project.root),
move(root),
]);

context.addTask(new NodePackageInstallTask());

return chain([
mergeWith(templateSource),
updateConfigFile(options),
updateConfigFile(options, root),
addDependencies(),
updateAppModule(options),
]);
Expand Down
25 changes: 25 additions & 0 deletions packages/schematics/angular/service-worker/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ describe('Service Worker Schematic', () => {
const tree = schematicRunner.runSchematic('service-worker', defaultOptions, appTree);
const path = '/projects/bar/ngsw-config.json';
expect(tree.exists(path)).toEqual(true);

const { projects } = JSON.parse(tree.readContent('/angular.json'));
expect(projects.bar.architect.build.configurations.production.ngswConfigPath)
.toBe('projects/bar/ngsw-config.json');
});

it('should add root assets RegExp', () => {
Expand All @@ -117,4 +121,25 @@ describe('Service Worker Schematic', () => {
.toContain('/outDir/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)');
});

it('should generate ngsw-config.json in src when the application is at root level', () => {
const name = 'foo';
const rootAppOptions: ApplicationOptions = {
...appOptions,
name,
projectRoot: '',
};
const rootSWOptions: ServiceWorkerOptions = {
...defaultOptions,
project: name,
};

let tree = schematicRunner.runSchematic('application', rootAppOptions, appTree);
tree = schematicRunner.runSchematic('service-worker', rootSWOptions, tree);
expect(tree.exists('/src/ngsw-config.json')).toBe(true);

const { projects } = JSON.parse(tree.readContent('/angular.json'));
expect(projects.foo.architect.build.configurations.production.ngswConfigPath)
.toBe('src/ngsw-config.json');
});

});
1 change: 1 addition & 0 deletions packages/schematics/angular/utility/workspace-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface BrowserBuilderOptions extends BrowserBuilderBaseOptions {
extractLicenses?: boolean;
vendorChunk?: boolean;
buildOptimizer?: boolean;
ngswConfigPath?: string;
budgets?: {
type: string;
maximumWarning?: string;
Expand Down