22 MIT License http://www.opensource.org/licenses/mit-license.php
33 Author Tobias Koppers @sokra
44*/
5- var Template = require ( "../Template" ) ;
5+ "use strict" ;
66
7- function NodeMainTemplatePlugin ( asyncChunkLoading ) {
8- this . asyncChunkLoading = asyncChunkLoading ;
9- }
10- module . exports = NodeMainTemplatePlugin ;
11- NodeMainTemplatePlugin . prototype . apply = function ( mainTemplate ) {
12- var self = this ;
13- mainTemplate . plugin ( "local-vars" , function ( source , chunk ) {
14- if ( chunk . chunks . length > 0 ) {
15- return this . asString ( [
16- source ,
17- "" ,
18- "// object to store loaded chunks" ,
19- "// \"0\" means \"already loaded\"" ,
20- "var installedChunks = {" ,
21- this . indent (
22- chunk . ids . map ( function ( id ) {
23- return id + ": 0" ;
24- } ) . join ( ",\n" )
25- ) ,
26- "};"
27- ] ) ;
28- }
29- return source ;
30- } ) ;
31- mainTemplate . plugin ( "require-extensions" , function ( source , chunk ) {
32- if ( chunk . chunks . length > 0 ) {
33- return this . asString ( [
34- source ,
35- "" ,
36- "// uncatched error handler for webpack runtime" ,
37- this . requireFn + ".oe = function(err) {" ,
38- this . indent ( [
39- "process.nextTick(function() {" ,
40- this . indent ( "throw err; // catch this error by using System.import().catch()" ) ,
41- "});"
42- ] ) ,
43- "};"
44- ] ) ;
45- }
46- return source ;
47- } ) ;
48- mainTemplate . plugin ( "require-ensure" , function ( _ , chunk , hash ) {
49- var chunkFilename = this . outputOptions . chunkFilename ;
50- var chunkMaps = chunk . getChunkMaps ( ) ;
51- var insertMoreModules = [
52- "var moreModules = chunk.modules, chunkIds = chunk.ids;" ,
53- "for(var moduleId in moreModules) {" ,
54- this . indent ( this . renderAddModule ( hash , chunk , "moduleId" , "moreModules[moduleId]" ) ) ,
55- "}"
56- ] ;
57- if ( self . asyncChunkLoading ) {
58- return this . asString ( [
59- "// \"0\" is the signal for \"already loaded\"" ,
60- "if(installedChunks[chunkId] === 0)" ,
61- this . indent ( [
62- "return Promise.resolve();"
63- ] ) ,
64- "// array of [resolve, reject, promise] means \"currently loading\"" ,
65- "if(installedChunks[chunkId])" ,
66- this . indent ( [
67- "return installedChunks[chunkId][2];"
68- ] ) ,
69- "// load the chunk and return promise to it" ,
70- "var promise = new Promise(function(resolve, reject) {" ,
71- this . indent ( [
72- "installedChunks[chunkId] = [resolve, reject];" ,
73- "var filename = __dirname + " + this . applyPluginsWaterfall ( "asset-path" , JSON . stringify ( "/" + chunkFilename ) , {
74- hash : "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" ,
75- hashWithLength : function ( length ) {
76- return "\" + " + this . renderCurrentHashCode ( hash , length ) + " + \"" ;
77- } . bind ( this ) ,
78- chunk : {
79- id : "\" + chunkId + \"" ,
80- hash : "\" + " + JSON . stringify ( chunkMaps . hash ) + "[chunkId] + \"" ,
81- hashWithLength : function ( length ) {
82- var shortChunkHashMap = { } ;
83- Object . keys ( chunkMaps . hash ) . forEach ( function ( chunkId ) {
84- if ( typeof chunkMaps . hash [ chunkId ] === "string" )
85- shortChunkHashMap [ chunkId ] = chunkMaps . hash [ chunkId ] . substr ( 0 , length ) ;
86- } ) ;
87- return "\" + " + JSON . stringify ( shortChunkHashMap ) + "[chunkId] + \"" ;
88- } ,
89- name : "\" + (" + JSON . stringify ( chunkMaps . name ) + "[chunkId]||chunkId) + \""
90- }
91- } ) + ";" ,
92- "require('fs').readFile(filename, 'utf-8', function(err, content) {" ,
7+ const Template = require ( "../Template" ) ;
8+
9+ module . exports = class NodeMainTemplatePlugin {
10+ constructor ( asyncChunkLoading ) {
11+ this . asyncChunkLoading = asyncChunkLoading ;
12+ }
13+
14+ apply ( mainTemplate ) {
15+ const asyncChunkLoading = this . asyncChunkLoading ;
16+ mainTemplate . plugin ( "local-vars" , function ( source , chunk ) {
17+ if ( chunk . chunks . length > 0 ) {
18+ return this . asString ( [
19+ source ,
20+ "" ,
21+ "// object to store loaded chunks" ,
22+ "// \"0\" means \"already loaded\"" ,
23+ "var installedChunks = {" ,
24+ this . indent ( chunk . ids . map ( ( id ) => id + ": 0" ) . join ( ",\n" ) ) ,
25+ "};"
26+ ] ) ;
27+ }
28+ return source ;
29+ } ) ;
30+ mainTemplate . plugin ( "require-extensions" , function ( source , chunk ) {
31+ if ( chunk . chunks . length > 0 ) {
32+ return this . asString ( [
33+ source ,
34+ "" ,
35+ "// uncatched error handler for webpack runtime" ,
36+ this . requireFn + ".oe = function(err) {" ,
9337 this . indent ( [
94- "if(err) return reject(err);" ,
95- "var chunk = {};" ,
96- "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
97- "(chunk, require, require('path').dirname(filename), filename);"
98- ] . concat ( insertMoreModules ) . concat ( [
99- "var callbacks = [];" ,
100- "for(var i = 0; i < chunkIds.length; i++) {" ,
38+ "process.nextTick(function() {" ,
39+ this . indent ( "throw err; // catch this error by using System.import().catch()" ) ,
40+ "});"
41+ ] ) ,
42+ "};"
43+ ] ) ;
44+ }
45+ return source ;
46+ } ) ;
47+ mainTemplate . plugin ( "require-ensure" , function ( _ , chunk , hash ) {
48+ const chunkFilename = this . outputOptions . chunkFilename ;
49+ const chunkMaps = chunk . getChunkMaps ( ) ;
50+ const insertMoreModules = [
51+ "var moreModules = chunk.modules, chunkIds = chunk.ids;" ,
52+ "for(var moduleId in moreModules) {" ,
53+ this . indent ( this . renderAddModule ( hash , chunk , "moduleId" , "moreModules[moduleId]" ) ) ,
54+ "}"
55+ ] ;
56+ if ( asyncChunkLoading ) {
57+ return this . asString ( [
58+ "// \"0\" is the signal for \"already loaded\"" ,
59+ "if(installedChunks[chunkId] === 0)" ,
60+ this . indent ( [
61+ "return Promise.resolve();"
62+ ] ) ,
63+ "// array of [resolve, reject, promise] means \"currently loading\"" ,
64+ "if(installedChunks[chunkId])" ,
65+ this . indent ( [
66+ "return installedChunks[chunkId][2];"
67+ ] ) ,
68+ "// load the chunk and return promise to it" ,
69+ "var promise = new Promise(function(resolve, reject) {" ,
70+ this . indent ( [
71+ "installedChunks[chunkId] = [resolve, reject];" ,
72+ "var filename = __dirname + " + this . applyPluginsWaterfall ( "asset-path" , JSON . stringify ( "/" + chunkFilename ) , {
73+ hash : "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" ,
74+ hashWithLength : function ( length ) {
75+ return "\" + " + this . renderCurrentHashCode ( hash , length ) + " + \"" ;
76+ } . bind ( this ) ,
77+ chunk : {
78+ id : "\" + chunkId + \"" ,
79+ hash : "\" + " + JSON . stringify ( chunkMaps . hash ) + "[chunkId] + \"" ,
80+ hashWithLength : function ( length ) {
81+ const shortChunkHashMap = { } ;
82+ Object . keys ( chunkMaps . hash ) . forEach ( function ( chunkId ) {
83+ if ( typeof chunkMaps . hash [ chunkId ] === "string" )
84+ shortChunkHashMap [ chunkId ] = chunkMaps . hash [ chunkId ] . substr ( 0 , length ) ;
85+ } ) ;
86+ return "\" + " + JSON . stringify ( shortChunkHashMap ) + "[chunkId] + \"" ;
87+ } ,
88+ name : "\" + (" + JSON . stringify ( chunkMaps . name ) + "[chunkId]||chunkId) + \""
89+ }
90+ } ) + ";" ,
91+ "require('fs').readFile(filename, 'utf-8', function(err, content) {" ,
10192 this . indent ( [
102- "if(installedChunks[chunkIds[i]])" ,
93+ "if(err) return reject(err);" ,
94+ "var chunk = {};" ,
95+ "require('vm').runInThisContext('(function(exports, require, __dirname, __filename) {' + content + '\\n})', filename)" +
96+ "(chunk, require, require('path').dirname(filename), filename);"
97+ ] . concat ( insertMoreModules ) . concat ( [
98+ "var callbacks = [];" ,
99+ "for(var i = 0; i < chunkIds.length; i++) {" ,
103100 this . indent ( [
104- "callbacks = callbacks.concat(installedChunks[chunkIds[i]][0]);"
101+ "if(installedChunks[chunkIds[i]])" ,
102+ this . indent ( [
103+ "callbacks = callbacks.concat(installedChunks[chunkIds[i]][0]);"
104+ ] ) ,
105+ "installedChunks[chunkIds[i]] = 0;"
105106 ] ) ,
106- "installedChunks[chunkIds[i]] = 0;"
107- ] ) ,
108- "}" ,
109- "for(i = 0; i < callbacks.length; i++)" ,
110- this . indent ( "callbacks[i]();" )
107+ "}" ,
108+ "for(i = 0; i < callbacks.length; i++)" ,
109+ this . indent ( "callbacks[i]();" )
110+ ] ) ) ,
111+ "});"
112+ ] ) ,
113+ "});" ,
114+ "return installedChunks[chunkId][2] = promise;"
115+ ] ) ;
116+ } else {
117+ const request = this . applyPluginsWaterfall ( "asset-path" , JSON . stringify ( "./" + chunkFilename ) , {
118+ hash : "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" ,
119+ hashWithLength : function ( length ) {
120+ return "\" + " + this . renderCurrentHashCode ( hash , length ) + " + \"" ;
121+ } . bind ( this ) ,
122+ chunk : {
123+ id : "\" + chunkId + \"" ,
124+ hash : "\" + " + JSON . stringify ( chunkMaps . hash ) + "[chunkId] + \"" ,
125+ hashWithLength : function ( length ) {
126+ const shortChunkHashMap = { } ;
127+ Object . keys ( chunkMaps . hash ) . forEach ( function ( chunkId ) {
128+ if ( typeof chunkMaps . hash [ chunkId ] === "string" )
129+ shortChunkHashMap [ chunkId ] = chunkMaps . hash [ chunkId ] . substr ( 0 , length ) ;
130+ } ) ;
131+ return "\" + " + JSON . stringify ( shortChunkHashMap ) + "[chunkId] + \"" ;
132+ } ,
133+ name : "\" + (" + JSON . stringify ( chunkMaps . name ) + "[chunkId]||chunkId) + \""
134+ }
135+ } ) ;
136+ return this . asString ( [
137+ "// \"0\" is the signal for \"already loaded\"" ,
138+ "if(installedChunks[chunkId] !== 0) {" ,
139+ this . indent ( [
140+ "var chunk = require(" + request + ");"
141+ ] . concat ( insertMoreModules ) . concat ( [
142+ "for(var i = 0; i < chunkIds.length; i++)" ,
143+ this . indent ( "installedChunks[chunkIds[i]] = 0;" )
111144 ] ) ) ,
112- "});"
113- ] ) ,
114- "});" ,
115- "return installedChunks[chunkId][2] = promise;"
116- ] ) ;
117- } else {
118- var request = this . applyPluginsWaterfall ( "asset-path" , JSON . stringify ( "./" + chunkFilename ) , {
145+ "}" ,
146+ "return Promise.resolve();"
147+ ] ) ;
148+ }
149+ } ) ;
150+ mainTemplate . plugin ( "hot-bootstrap" , function ( source , chunk , hash ) {
151+ const hotUpdateChunkFilename = this . outputOptions . hotUpdateChunkFilename ;
152+ const hotUpdateMainFilename = this . outputOptions . hotUpdateMainFilename ;
153+ const chunkMaps = chunk . getChunkMaps ( ) ;
154+ const currentHotUpdateChunkFilename = this . applyPluginsWaterfall ( "asset-path" , JSON . stringify ( hotUpdateChunkFilename ) , {
119155 hash : "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" ,
120156 hashWithLength : function ( length ) {
121157 return "\" + " + this . renderCurrentHashCode ( hash , length ) + " + \"" ;
@@ -124,7 +160,7 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) {
124160 id : "\" + chunkId + \"" ,
125161 hash : "\" + " + JSON . stringify ( chunkMaps . hash ) + "[chunkId] + \"" ,
126162 hashWithLength : function ( length ) {
127- var shortChunkHashMap = { } ;
163+ const shortChunkHashMap = { } ;
128164 Object . keys ( chunkMaps . hash ) . forEach ( function ( chunkId ) {
129165 if ( typeof chunkMaps . hash [ chunkId ] === "string" )
130166 shortChunkHashMap [ chunkId ] = chunkMaps . hash [ chunkId ] . substr ( 0 , length ) ;
@@ -134,58 +170,22 @@ NodeMainTemplatePlugin.prototype.apply = function(mainTemplate) {
134170 name : "\" + (" + JSON . stringify ( chunkMaps . name ) + "[chunkId]||chunkId) + \""
135171 }
136172 } ) ;
137- return this . asString ( [
138- "// \"0\" is the signal for \"already loaded\"" ,
139- "if(installedChunks[chunkId] !== 0) {" ,
140- this . indent ( [
141- "var chunk = require(" + request + ");"
142- ] . concat ( insertMoreModules ) . concat ( [
143- "for(var i = 0; i < chunkIds.length; i++)" ,
144- this . indent ( "installedChunks[chunkIds[i]] = 0;" )
145- ] ) ) ,
146- "}" ,
147- "return Promise.resolve();"
148- ] ) ;
149- }
150- } ) ;
151- mainTemplate . plugin ( "hot-bootstrap" , function ( source , chunk , hash ) {
152- var hotUpdateChunkFilename = this . outputOptions . hotUpdateChunkFilename ;
153- var hotUpdateMainFilename = this . outputOptions . hotUpdateMainFilename ;
154- var chunkMaps = chunk . getChunkMaps ( ) ;
155- var currentHotUpdateChunkFilename = this . applyPluginsWaterfall ( "asset-path" , JSON . stringify ( hotUpdateChunkFilename ) , {
156- hash : "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" ,
157- hashWithLength : function ( length ) {
158- return "\" + " + this . renderCurrentHashCode ( hash , length ) + " + \"" ;
159- } . bind ( this ) ,
160- chunk : {
161- id : "\" + chunkId + \"" ,
162- hash : "\" + " + JSON . stringify ( chunkMaps . hash ) + "[chunkId] + \"" ,
173+ const currentHotUpdateMainFilename = this . applyPluginsWaterfall ( "asset-path" , JSON . stringify ( hotUpdateMainFilename ) , {
174+ hash : "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" ,
163175 hashWithLength : function ( length ) {
164- var shortChunkHashMap = { } ;
165- Object . keys ( chunkMaps . hash ) . forEach ( function ( chunkId ) {
166- if ( typeof chunkMaps . hash [ chunkId ] === "string" )
167- shortChunkHashMap [ chunkId ] = chunkMaps . hash [ chunkId ] . substr ( 0 , length ) ;
168- } ) ;
169- return "\" + " + JSON . stringify ( shortChunkHashMap ) + "[chunkId] + \"" ;
170- } ,
171- name : "\" + (" + JSON . stringify ( chunkMaps . name ) + "[chunkId]||chunkId) + \""
172- }
176+ return "\" + " + this . renderCurrentHashCode ( hash , length ) + " + \"" ;
177+ } . bind ( this )
178+ } ) ;
179+ return Template . getFunctionContent ( asyncChunkLoading ? require ( "./NodeMainTemplateAsync.runtime.js" ) : require ( "./NodeMainTemplate.runtime.js" ) )
180+ . replace ( / \$ r e q u i r e \$ / g, this . requireFn )
181+ . replace ( / \$ h o t M a i n F i l e n a m e \$ / g, currentHotUpdateMainFilename )
182+ . replace ( / \$ h o t C h u n k F i l e n a m e \$ / g, currentHotUpdateChunkFilename ) ;
173183 } ) ;
174- var currentHotUpdateMainFilename = this . applyPluginsWaterfall ( "asset-path ", JSON . stringify ( hotUpdateMainFilename ) , {
175- hash : "\" + " + this . renderCurrentHashCode ( hash ) + " + \"" ,
176- hashWithLength : function ( length ) {
177- return "\" + " + this . renderCurrentHashCode ( hash , length ) + " + \"" ;
178- } . bind ( this )
184+ mainTemplate . plugin ( "hash ", function ( hash ) {
185+ hash . update ( "node" ) ;
186+ hash . update ( "3" ) ;
187+ hash . update ( this . outputOptions . filename + "" ) ;
188+ hash . update ( this . outputOptions . chunkFilename + "" ) ;
179189 } ) ;
180- return Template . getFunctionContent ( self . asyncChunkLoading ? require ( "./NodeMainTemplateAsync.runtime.js" ) : require ( "./NodeMainTemplate.runtime.js" ) )
181- . replace ( / \$ r e q u i r e \$ / g, this . requireFn )
182- . replace ( / \$ h o t M a i n F i l e n a m e \$ / g, currentHotUpdateMainFilename )
183- . replace ( / \$ h o t C h u n k F i l e n a m e \$ / g, currentHotUpdateChunkFilename ) ;
184- } ) ;
185- mainTemplate . plugin ( "hash" , function ( hash ) {
186- hash . update ( "node" ) ;
187- hash . update ( "3" ) ;
188- hash . update ( this . outputOptions . filename + "" ) ;
189- hash . update ( this . outputOptions . chunkFilename + "" ) ;
190- } ) ;
190+ }
191191} ;
0 commit comments