1 | "use strict";
|
2 | var __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 | };
|
11 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
12 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
13 | };
|
14 | Object.defineProperty(exports, "__esModule", { value: true });
|
15 | const config_1 = require("./config");
|
16 | const createMetatagsFromConfig_1 = __importDefault(require("./createMetatagsFromConfig"));
|
17 | const injector_1 = require("./injector");
|
18 | const TAP_CMD = 'webpack-pwa-manifest-plugin';
|
19 | const TAP = 'WebpackPWAManifestPlugin';
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | class 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 |
|
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 |
|
56 |
|
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 |
|
63 |
|
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 |
|
92 | if (compiler.hooks) {
|
93 | compiler.hooks.compilation.tap(TAP, compilation => {
|
94 |
|
95 |
|
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 | }
|
123 | exports.default = WebpackPWAManifest;
|
124 | exports.WebpackPWAManifest = WebpackPWAManifest;
|
125 |
|
\ | No newline at end of file |