| 1 |
/* |
| 2 |
MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 |
Author Tobias Koppers @sokra |
| 4 |
*/ |
| 5 |
"use strict"; |
| 6 |
|
| 7 |
const ConcatSource = require("webpack-sources").ConcatSource; |
| 8 |
const OriginalSource = require("webpack-sources").OriginalSource; |
| 9 |
const PrefixSource = require("webpack-sources").PrefixSource; |
| 10 |
const Template = require("./Template"); |
| 11 |
|
| 12 |
// require function shortcuts: |
| 13 |
// __webpack_require__.s = the module id of the entry point |
| 14 |
// __webpack_require__.c = the module cache |
| 15 |
// __webpack_require__.m = the module functions |
| 16 |
// __webpack_require__.p = the bundle public path |
| 17 |
// __webpack_require__.i = the identity function used for harmony imports |
| 18 |
// __webpack_require__.e = the chunk ensure function |
| 19 |
// __webpack_require__.d = the exported propery define getter function |
| 20 |
// __webpack_require__.o = Object.prototype.hasOwnProperty.call |
| 21 |
// __webpack_require__.n = compatibility get default export |
| 22 |
// __webpack_require__.h = the webpack hash |
| 23 |
// __webpack_require__.oe = the uncatched error handler for the webpack runtime |
| 24 |
// __webpack_require__.nc = the script nonce |
| 25 |
|
| 26 |
module.exports = class MainTemplate extends Template { |
| 27 |
constructor(outputOptions) { |
| 28 |
super(outputOptions); |
| 29 |
this.plugin("startup", (source, chunk, hash) => { |
| 30 |
const buf = []; |
| 31 |
if(chunk.entryModule) { |
| 32 |
buf.push("// Load entry module and return exports"); |
| 33 |
buf.push(`return ${this.renderRequireFunctionForModule(hash, chunk, JSON.stringify(chunk.entryModule.id))}(${this.requireFn}.s = ${JSON.stringify(chunk.entryModule.id)});`); |
| 34 |
} |
| 35 |
return this.asString(buf); |
| 36 |
}); |
| 37 |
this.plugin("render", (bootstrapSource, chunk, hash, moduleTemplate, dependencyTemplates) => { |
| 38 |
const source = new ConcatSource(); |
| 39 |
source.add("/******/ (function(modules) { // webpackBootstrap\n"); |
| 40 |
source.add(new PrefixSource("/******/", bootstrapSource)); |
| 41 |
source.add("/******/ })\n"); |
| 42 |
source.add("/************************************************************************/\n"); |
| 43 |
source.add("/******/ ("); |
| 44 |
const modules = this.renderChunkModules(chunk, moduleTemplate, dependencyTemplates, "/******/ "); |
| 45 |
source.add(this.applyPluginsWaterfall("modules", modules, chunk, hash, moduleTemplate, dependencyTemplates)); |
| 46 |
source.add(")"); |
| 47 |
return source; |
| 48 |
}); |
| 49 |
this.plugin("local-vars", (source, chunk, hash) => { |
| 50 |
return this.asString([ |
| 51 |
source, |
| 52 |
"// The module cache", |
| 53 |
"var installedModules = {};" |
| 54 |
]); |
| 55 |
}); |
| 56 |
this.plugin("require", (source, chunk, hash) => { |
| 57 |
return this.asString([ |
| 58 |
source, |
| 59 |
"// Check if module is in cache", |
| 60 |
"if(installedModules[moduleId]) {", |
| 61 |
this.indent("return installedModules[moduleId].exports;"), |
| 62 |
"}", |
| 63 |
"// Create a new module (and put it into the cache)", |
| 64 |
"var module = installedModules[moduleId] = {", |
| 65 |
this.indent(this.applyPluginsWaterfall("module-obj", "", chunk, hash, "moduleId")), |
| 66 |
"};", |
| 67 |
"", |
| 68 |
this.asString(outputOptions.strictModuleExceptionHandling ? [ |
| 69 |
"// Execute the module function", |
| 70 |
"var threw = true;", |
| 71 |
"try {", |
| 72 |
this.indent([ |
| 73 |
`modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(hash, chunk, "moduleId")});`, |
| 74 |
"threw = false;" |
| 75 |
]), |
| 76 |
"} finally {", |
| 77 |
this.indent([ |
| 78 |
"if(threw) delete installedModules[moduleId];" |
| 79 |
]), |
| 80 |
"}" |
| 81 |
] : [ |
| 82 |
"// Execute the module function", |
| 83 |
`modules[moduleId].call(module.exports, module, module.exports, ${this.renderRequireFunctionForModule(hash, chunk, "moduleId")});`, |
| 84 |
]), |
| 85 |
"", |
| 86 |
"// Flag the module as loaded", |
| 87 |
"module.l = true;", |
| 88 |
"", |
| 89 |
"// Return the exports of the module", |
| 90 |
"return module.exports;" |
| 91 |
]); |
| 92 |
}); |
| 93 |
this.plugin("module-obj", (source, chunk, hash, varModuleId) => { |
| 94 |
return this.asString([ |
| 95 |
"i: moduleId,", |
| 96 |
"l: false,", |
| 97 |
"exports: {}" |
| 98 |
]); |
| 99 |
}); |
| 100 |
this.plugin("require-extensions", (source, chunk, hash) => { |
| 101 |
const buf = []; |
| 102 |
if(chunk.chunks.length > 0) { |
| 103 |
buf.push("// This file contains only the entry chunk."); |
| 104 |
buf.push("// The chunk loading function for additional chunks"); |
| 105 |
buf.push(`${this.requireFn}.e = function requireEnsure(chunkId) {`); |
| 106 |
buf.push(this.indent(this.applyPluginsWaterfall("require-ensure", "throw new Error('Not chunk loading available');", chunk, hash, "chunkId"))); |
| 107 |
buf.push("};"); |
| 108 |
} |
| 109 |
buf.push(""); |
| 110 |
buf.push("// expose the modules object (__webpack_modules__)"); |
| 111 |
buf.push(`${this.requireFn}.m = modules;`); |
| 112 |
|
| 113 |
buf.push(""); |
| 114 |
buf.push("// expose the module cache"); |
| 115 |
buf.push(`${this.requireFn}.c = installedModules;`); |
| 116 |
|
| 117 |
buf.push(""); |
| 118 |
buf.push("// identity function for calling harmony imports with the correct context"); |
| 119 |
buf.push(`${this.requireFn}.i = function(value) { return value; };`); |
| 120 |
|
| 121 |
buf.push(""); |
| 122 |
buf.push("// define getter function for harmony exports"); |
| 123 |
buf.push(`${this.requireFn}.d = function(exports, name, getter) {`); |
| 124 |
buf.push(this.indent([ |
| 125 |
`if(!${this.requireFn}.o(exports, name)) {`, |
| 126 |
this.indent([ |
| 127 |
"Object.defineProperty(exports, name, {", |
| 128 |
this.indent([ |
| 129 |
"configurable: false,", |
| 130 |
"enumerable: true,", |
| 131 |
"get: getter" |
| 132 |
]), |
| 133 |
"});" |
| 134 |
]), |
| 135 |
"}" |
| 136 |
])); |
| 137 |
buf.push("};"); |
| 138 |
|
| 139 |
buf.push(""); |
| 140 |
buf.push("// getDefaultExport function for compatibility with non-harmony modules"); |
| 141 |
buf.push(this.requireFn + ".n = function(module) {"); |
| 142 |
buf.push(this.indent([ |
| 143 |
"var getter = module && module.__esModule ?", |
| 144 |
this.indent([ |
| 145 |
"function getDefault() { return module['default']; } :", |
| 146 |
"function getModuleExports() { return module; };" |
| 147 |
]), |
| 148 |
`${this.requireFn}.d(getter, 'a', getter);`, |
| 149 |
"return getter;" |
| 150 |
])); |
| 151 |
buf.push("};"); |
| 152 |
|
| 153 |
buf.push(""); |
| 154 |
buf.push("// Object.prototype.hasOwnProperty.call"); |
| 155 |
buf.push(`${this.requireFn}.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };`); |
| 156 |
|
| 157 |
const publicPath = this.getPublicPath({ |
| 158 |
hash: hash |
| 159 |
}); |
| 160 |
buf.push(""); |
| 161 |
buf.push("// __webpack_public_path__"); |
| 162 |
buf.push(`${this.requireFn}.p = ${JSON.stringify(publicPath)};`); |
| 163 |
return this.asString(buf); |
| 164 |
}); |
| 165 |
|
| 166 |
this.requireFn = "__webpack_require__"; |
| 167 |
} |
| 168 |
|
| 169 |
render(hash, chunk, moduleTemplate, dependencyTemplates) { |
| 170 |
const buf = []; |
| 171 |
buf.push(this.applyPluginsWaterfall("bootstrap", "", chunk, hash, moduleTemplate, dependencyTemplates)); |
| 172 |
buf.push(this.applyPluginsWaterfall("local-vars", "", chunk, hash)); |
| 173 |
buf.push(""); |
| 174 |
buf.push("// The require function"); |
| 175 |
buf.push(`function ${this.requireFn}(moduleId) {`); |
| 176 |
buf.push(this.indent(this.applyPluginsWaterfall("require", "", chunk, hash))); |
| 177 |
buf.push("}"); |
| 178 |
buf.push(""); |
| 179 |
buf.push(this.asString(this.applyPluginsWaterfall("require-extensions", "", chunk, hash))); |
| 180 |
buf.push(""); |
| 181 |
buf.push(this.asString(this.applyPluginsWaterfall("startup", "", chunk, hash))); |
| 182 |
let source = this.applyPluginsWaterfall("render", new OriginalSource(this.prefix(buf, " \t") + "\n", `webpack/bootstrap ${hash}`), chunk, hash, moduleTemplate, dependencyTemplates); |
| 183 |
if(chunk.hasEntryModule()) { |
| 184 |
source = this.applyPluginsWaterfall("render-with-entry", source, chunk, hash); |
| 185 |
} |
| 186 |
if(!source) throw new Error("Compiler error: MainTemplate plugin 'render' should return something"); |
| 187 |
chunk.rendered = true; |
| 188 |
return new ConcatSource(source, ";"); |
| 189 |
} |
| 190 |
|
| 191 |
renderRequireFunctionForModule(hash, chunk, varModuleId) { |
| 192 |
return this.applyPluginsWaterfall("module-require", this.requireFn, chunk, hash, varModuleId); |
| 193 |
} |
| 194 |
|
| 195 |
renderAddModule(hash, chunk, varModuleId, varModule) { |
| 196 |
return this.applyPluginsWaterfall("add-module", `modules[${varModuleId}] = ${varModule};`, chunk, hash, varModuleId, varModule); |
| 197 |
} |
| 198 |
|
| 199 |
renderCurrentHashCode(hash, length) { |
| 200 |
length = length || Infinity; |
| 201 |
return this.applyPluginsWaterfall("current-hash", JSON.stringify(hash.substr(0, length)), length); |
| 202 |
} |
| 203 |
|
| 204 |
entryPointInChildren(chunk) { |
| 205 |
const checkChildren = (chunk, alreadyCheckedChunks) => { |
| 206 |
return chunk.chunks.some((child) => { |
| 207 |
if(alreadyCheckedChunks.indexOf(child) >= 0) return; |
| 208 |
alreadyCheckedChunks.push(child); |
| 209 |
return child.hasEntryModule() || checkChildren(child, alreadyCheckedChunks); |
| 210 |
}); |
| 211 |
}; |
| 212 |
return checkChildren(chunk, []); |
| 213 |
} |
| 214 |
|
| 215 |
getPublicPath(options) { |
| 216 |
return this.applyPluginsWaterfall("asset-path", this.outputOptions.publicPath || "", options); |
| 217 |
} |
| 218 |
|
| 219 |
updateHash(hash) { |
| 220 |
hash.update("maintemplate"); |
| 221 |
hash.update("3"); |
| 222 |
hash.update(this.outputOptions.publicPath + ""); |
| 223 |
this.applyPlugins("hash", hash); |
| 224 |
} |
| 225 |
|
| 226 |
updateHashForChunk(hash, chunk) { |
| 227 |
this.updateHash(hash); |
| 228 |
this.applyPlugins("hash-for-chunk", hash, chunk); |
| 229 |
} |
| 230 |
|
| 231 |
useChunkHash(chunk) { |
| 232 |
const paths = this.applyPluginsWaterfall("global-hash-paths", []); |
| 233 |
return !this.applyPluginsBailResult("global-hash", chunk, paths); |
| 234 |
} |
| 235 |
}; |