1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const schematics_1 = require("@angular-devkit/schematics");
|
11 | const utility_1 = require("@schematics/angular/utility");
|
12 | const path_1 = require("path");
|
13 | const stream_1 = require("stream");
|
14 | function updateIndexFile(path) {
|
15 | return async (host) => {
|
16 | const buffer = host.read(path);
|
17 | if (buffer === null) {
|
18 | throw new schematics_1.SchematicsException(`Could not read index file: ${path}`);
|
19 | }
|
20 | const { RewritingStream } = await loadEsmModule('parse5-html-rewriting-stream');
|
21 | const rewriter = new RewritingStream();
|
22 | let needsNoScript = true;
|
23 | rewriter.on('startTag', (startTag) => {
|
24 | if (startTag.tagName === 'noscript') {
|
25 | needsNoScript = false;
|
26 | }
|
27 | rewriter.emitStartTag(startTag);
|
28 | });
|
29 | rewriter.on('endTag', (endTag) => {
|
30 | if (endTag.tagName === 'head') {
|
31 | rewriter.emitRaw(' <link rel="manifest" href="manifest.webmanifest">\n');
|
32 | rewriter.emitRaw(' <meta name="theme-color" content="#1976d2">\n');
|
33 | }
|
34 | else if (endTag.tagName === 'body' && needsNoScript) {
|
35 | rewriter.emitRaw(' <noscript>Please enable JavaScript to continue using this application.</noscript>\n');
|
36 | }
|
37 | rewriter.emitEndTag(endTag);
|
38 | });
|
39 | return new Promise((resolve) => {
|
40 | const input = new stream_1.Readable({
|
41 | encoding: 'utf8',
|
42 | read() {
|
43 | this.push(buffer);
|
44 | this.push(null);
|
45 | },
|
46 | });
|
47 | const chunks = [];
|
48 | const output = new stream_1.Writable({
|
49 | write(chunk, encoding, callback) {
|
50 | chunks.push(typeof chunk === 'string' ? Buffer.from(chunk, encoding) : chunk);
|
51 | callback();
|
52 | },
|
53 | final(callback) {
|
54 | const full = Buffer.concat(chunks);
|
55 | host.overwrite(path, full.toString());
|
56 | callback();
|
57 | resolve();
|
58 | },
|
59 | });
|
60 | input.pipe(rewriter).pipe(output);
|
61 | });
|
62 | };
|
63 | }
|
64 | function default_1(options) {
|
65 | return async (host) => {
|
66 | if (!options.title) {
|
67 | options.title = options.project;
|
68 | }
|
69 | const workspace = await (0, utility_1.readWorkspace)(host);
|
70 | if (!options.project) {
|
71 | throw new schematics_1.SchematicsException('Option "project" is required.');
|
72 | }
|
73 | const project = workspace.projects.get(options.project);
|
74 | if (!project) {
|
75 | throw new schematics_1.SchematicsException(`Project is not defined in this workspace.`);
|
76 | }
|
77 | if (project.extensions['projectType'] !== 'application') {
|
78 | throw new schematics_1.SchematicsException(`PWA requires a project type of "application".`);
|
79 | }
|
80 |
|
81 | if (project.targets.size === 0) {
|
82 | throw new schematics_1.SchematicsException(`Targets are not defined for this project.`);
|
83 | }
|
84 | const buildTargets = [];
|
85 | const testTargets = [];
|
86 | for (const target of project.targets.values()) {
|
87 | if (target.builder === '@angular-devkit/build-angular:browser' ||
|
88 | target.builder === '@angular-devkit/build-angular:application') {
|
89 | buildTargets.push(target);
|
90 | }
|
91 | else if (target.builder === '@angular-devkit/build-angular:karma') {
|
92 | testTargets.push(target);
|
93 | }
|
94 | }
|
95 |
|
96 | const assetEntry = path_1.posix.join(project.sourceRoot ?? path_1.posix.join(project.root, 'src'), 'manifest.webmanifest');
|
97 | for (const target of [...buildTargets, ...testTargets]) {
|
98 | if (target.options) {
|
99 | if (Array.isArray(target.options.assets)) {
|
100 | target.options.assets.push(assetEntry);
|
101 | }
|
102 | else {
|
103 | target.options.assets = [assetEntry];
|
104 | }
|
105 | }
|
106 | else {
|
107 | target.options = { assets: [assetEntry] };
|
108 | }
|
109 | }
|
110 |
|
111 | const indexFiles = new Set();
|
112 | for (const target of buildTargets) {
|
113 | if (typeof target.options?.index === 'string') {
|
114 | indexFiles.add(target.options.index);
|
115 | }
|
116 | if (!target.configurations) {
|
117 | continue;
|
118 | }
|
119 | for (const options of Object.values(target.configurations)) {
|
120 | if (typeof options?.index === 'string') {
|
121 | indexFiles.add(options.index);
|
122 | }
|
123 | }
|
124 | }
|
125 |
|
126 | const sourcePath = project.sourceRoot ?? path_1.posix.join(project.root, 'src');
|
127 |
|
128 | const { title, ...swOptions } = options;
|
129 | await (0, utility_1.writeWorkspace)(host, workspace);
|
130 | return (0, schematics_1.chain)([
|
131 | (0, schematics_1.externalSchematic)('@schematics/angular', 'service-worker', swOptions),
|
132 | (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/root'), [(0, schematics_1.template)({ ...options }), (0, schematics_1.move)(sourcePath)])),
|
133 | (0, schematics_1.mergeWith)((0, schematics_1.apply)((0, schematics_1.url)('./files/assets'), [(0, schematics_1.move)(path_1.posix.join(sourcePath, 'assets'))])),
|
134 | ...[...indexFiles].map((path) => updateIndexFile(path)),
|
135 | ]);
|
136 | };
|
137 | }
|
138 | exports.default = default_1;
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 | function loadEsmModule(modulePath) {
|
152 | return new Function('modulePath', `return import(modulePath);`)(modulePath);
|
153 | }
|