UNPKG

5.72 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15const config_1 = require("./config");
16const createMetatagsFromConfig_1 = __importDefault(require("./createMetatagsFromConfig"));
17const injector_1 = require("./injector");
18const TAP_CMD = 'webpack-pwa-manifest-plugin';
19const TAP = 'WebpackPWAManifestPlugin';
20/**
21 * Generate a `manifest.json` for your PWA based on the `app.json`.
22 * This plugin must be **after HtmlWebpackPlugin**.
23 *
24 * To test PWAs in chrome visit `chrome://flags#enable-desktop-pwas`
25 */
26class WebpackPWAManifest {
27 constructor(appJson, { noResources, filename, publicPath, HtmlWebpackPlugin, projectRoot }) {
28 this.assets = null;
29 this.hasHTMLPlugin = false;
30 this.projectRoot = projectRoot || process.cwd();
31 this.HtmlWebpackPlugin = HtmlWebpackPlugin;
32 this.manifest = config_1.createPWAManifestFromExpoConfig(appJson);
33 this.expoConfig = appJson;
34 this.options = {
35 fingerprints: true,
36 inject: true,
37 ios: false,
38 publicPath,
39 // filename: options.fingerprints ? '[name].[hash].[ext]' : '[name].[ext]',
40 noResources,
41 filename,
42 includeDirectory: false,
43 metatags: createMetatagsFromConfig_1.default(appJson),
44 };
45 if (noResources) {
46 delete this.manifest.startupImages;
47 delete this.manifest.icons;
48 }
49 config_1.validateManifest(this.manifest);
50 }
51 getManifest() {
52 return this.manifest;
53 }
54 apply(compiler) {
55 // Hook into the html-webpack-plugin processing
56 // and add the html
57 const injectToHTMLAsync = (htmlPluginData, compilation, callback) => __awaiter(this, void 0, void 0, function* () {
58 if (!this.hasHTMLPlugin) {
59 this.hasHTMLPlugin = true;
60 }
61 const publicPath = this.options.publicPath || compilation.outputOptions.publicPath;
62 // The manifest (this.manifest) should be ready by this point.
63 // It will be written to disk here.
64 const manifestFile = yield injector_1.buildResourcesAsync(this, publicPath);
65 if (!this.options.inject) {
66 callback(null, htmlPluginData);
67 return;
68 }
69 let tags = injector_1.generateAppleSplashAndIconTags(this.assets);
70 for (const metatagName of Object.keys(this.options.metatags)) {
71 const content = this.options.metatags[metatagName];
72 tags = injector_1.applyTag(tags, 'meta', {
73 name: metatagName,
74 content,
75 });
76 }
77 if (manifestFile) {
78 const manifestLink = {
79 rel: 'manifest',
80 href: manifestFile.url,
81 };
82 if (this.manifest.crossorigin) {
83 manifestLink.crossorigin = this.manifest.crossorigin;
84 }
85 tags = injector_1.applyTag(tags, 'link', manifestLink);
86 }
87 const tagsHTML = injector_1.generateHtmlTags(tags);
88 htmlPluginData.html = htmlPluginData.html.replace(/(<\/head>)/i, `${tagsHTML}</head>`);
89 callback(null, htmlPluginData);
90 });
91 // webpack 4
92 if (compiler.hooks) {
93 compiler.hooks.compilation.tap(TAP, compilation => {
94 // This is set in html-webpack-plugin pre-v4.
95 // @ts-ignore
96 let hook = compilation.hooks.htmlWebpackPluginAfterHtmlProcessing;
97 if (!hook) {
98 const HtmlWebpackPlugin = this.HtmlWebpackPlugin || require('html-webpack-plugin');
99 hook = HtmlWebpackPlugin.getHooks(compilation).beforeEmit;
100 }
101 hook.tapAsync(TAP_CMD, (htmlPluginData, callback) => {
102 injectToHTMLAsync(htmlPluginData, compilation, () => {
103 if (this.assets) {
104 for (let asset of this.assets) {
105 compilation.assets[asset.output] = {
106 source: () => asset.source,
107 size: () => asset.size,
108 };
109 }
110 }
111 callback();
112 });
113 });
114 });
115 }
116 else {
117 compiler.plugin('compilation', compilation => {
118 compilation.plugin('html-webpack-plugin-before-html-processing', (htmlPluginData, callback) => injectToHTMLAsync(htmlPluginData, compilation, callback));
119 });
120 }
121 }
122}
123exports.default = WebpackPWAManifest;
124exports.WebpackPWAManifest = WebpackPWAManifest;
125//# sourceMappingURL=WebpackPWAManifestPlugin.js.map
\No newline at end of file