UNPKG

2.26 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5'use strict';
6
7const Template = require('./Template');
8
9class JsonpMainTemplatePlugin {
10 apply(mainTemplate) {
11 mainTemplate.plugin('rax-hot-bootstrap', function(source, chunk, hash) {
12 const hotUpdateChunkFilename = this.outputOptions.hotUpdateChunkFilename;
13 const hotUpdateMainFilename = this.outputOptions.hotUpdateMainFilename;
14 const hotUpdateFunction = this.outputOptions.hotUpdateFunction;
15 const currentHotUpdateChunkFilename = this.applyPluginsWaterfall(
16 'asset-path',
17 JSON.stringify(hotUpdateChunkFilename),
18 {
19 hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
20 hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`,
21 chunk: {
22 id: '" + chunkId + "'
23 }
24 }
25 );
26 const currentHotUpdateMainFilename = this.applyPluginsWaterfall(
27 'asset-path',
28 JSON.stringify(hotUpdateMainFilename),
29 {
30 hash: `" + ${this.renderCurrentHashCode(hash)} + "`,
31 hashWithLength: length => `" + ${this.renderCurrentHashCode(hash, length)} + "`
32 }
33 );
34 const runtimeSource = Template.getFunctionContent(require('./RaxJsonpMainTemplate.runtime.js'))
35 .replace(/\/\/\$semicolon/g, ';')
36 .replace(/\$require\$/g, this.requireFn)
37 .replace(/\$hotMainFilename\$/g, currentHotUpdateMainFilename)
38 .replace(/\$hotChunkFilename\$/g, currentHotUpdateChunkFilename)
39 .replace(/\$hash\$/g, JSON.stringify(hash));
40 return `${source}
41function hotDisposeChunk(chunkId) {
42 delete installedChunks[chunkId];
43}
44var parentHotUpdateCallback = global[${JSON.stringify(hotUpdateFunction)}];
45global[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
46 });
47 mainTemplate.plugin('hash', function(hash) {
48 hash.update('jsonp');
49 hash.update('4');
50 hash.update(`${this.outputOptions.filename}`);
51 hash.update(`${this.outputOptions.chunkFilename}`);
52 hash.update(`${this.outputOptions.jsonpFunction}`);
53 hash.update(`${this.outputOptions.hotUpdateFunction}`);
54 });
55 }
56}
57module.exports = JsonpMainTemplatePlugin;