source: trip-planner-front/node_modules/webpack-subresource-integrity/jmtp.js@ 8d391a1

Last change on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/**
2 * Copyright (c) 2015-present, Waysact Pty Ltd
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8var Template = require('webpack/lib/Template');
9var util = require('./util');
10var webpackVersionComponents = require('webpack/package.json').version.split(
11 '.'
12);
13var webpackVersionMajor = Number(webpackVersionComponents[0]);
14
15function WebIntegrityJsonpMainTemplatePlugin(sriPlugin, compilation) {
16 this.sriPlugin = sriPlugin;
17 this.compilation = compilation;
18}
19
20WebIntegrityJsonpMainTemplatePlugin.prototype.addSriHashes =
21 function addSriHashes(mainTemplate, source, chunk) {
22 var allChunks = util.findChunks(chunk);
23 var includedChunks = chunk.getChunkMaps().hash;
24 var hashFuncNames = this.sriPlugin.options.hashFuncNames;
25
26 if (Object.keys(includedChunks).length > 0) {
27 return (Template.asString || mainTemplate.asString)([
28 source,
29 '__webpack_require__.sriHashes = ' +
30 JSON.stringify(
31 Array.from(allChunks).reduce(function chunkIdReducer(
32 sriHashes,
33 depChunk
34 ) {
35 if (includedChunks[depChunk.id.toString()]) {
36 // eslint-disable-next-line no-param-reassign
37 sriHashes[depChunk.id] = util.makePlaceholder(hashFuncNames, depChunk.id);
38 }
39 return sriHashes;
40 },
41 {})
42 ) +
43 ';'
44 ]);
45 }
46
47 return source;
48 };
49
50/*
51 * Patch jsonp-script code to add the integrity attribute.
52 */
53WebIntegrityJsonpMainTemplatePlugin.prototype.addAttribute =
54 function addAttribute(mainTemplate, elName, source) {
55 const outputOptions = this.compilation.outputOptions || mainTemplate.outputOptions;
56 if (!outputOptions.crossOriginLoading) {
57 this.sriPlugin.errorOnce(
58 this.compilation,
59 'webpack option output.crossOriginLoading not set, code splitting will not work!'
60 );
61 }
62
63 return (Template.asString || mainTemplate.asString)([
64 source,
65 elName + '.integrity = __webpack_require__.sriHashes[' +
66 ((webpackVersionMajor >= 5 && elName === 'script') ? 'key.match(/^chunk-(.+)$/)[1]' : 'chunkId') +
67 '];',
68 elName + '.crossOrigin = ' + JSON.stringify(outputOptions.crossOriginLoading) + ';',
69 ]);
70 };
71
72WebIntegrityJsonpMainTemplatePlugin.prototype.apply = function apply(
73 mainTemplate
74) {
75 var jsonpScriptPlugin = this.addAttribute.bind(this, mainTemplate, "script");
76 var linkPreloadPlugin = this.addAttribute.bind(this, mainTemplate, "link");
77 var addSriHashes = this.addSriHashes.bind(this, mainTemplate);
78
79 if (
80 this.compilation.outputOptions.chunkLoading
81 ? ['require', 'async-node'].includes(
82 this.compilation.outputOptions.chunkLoading
83 )
84 : this.compilation.compiler.options.target !== 'web'
85 ) {
86 this.sriPlugin.warnOnce(
87 this.compilation,
88 'This plugin is not useful for non-web targets.'
89 );
90 return;
91 }
92
93 if (mainTemplate.hooks) {
94 mainTemplate.hooks.jsonpScript.tap('SriPlugin', jsonpScriptPlugin);
95 mainTemplate.hooks.linkPreload.tap('SriPlugin', linkPreloadPlugin);
96 mainTemplate.hooks.localVars.tap('SriPlugin', addSriHashes);
97 } else {
98 mainTemplate.plugin('jsonp-script', jsonpScriptPlugin);
99 mainTemplate.plugin('local-vars', addSriHashes);
100 }
101};
102
103module.exports = WebIntegrityJsonpMainTemplatePlugin;
Note: See TracBrowser for help on using the repository browser.