|
| 1 | +/* |
| 2 | + MIT License http://www.opensource.org/licenses/mit-license.php |
| 3 | + Author Tobias Koppers @sokra |
| 4 | +*/ |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +const RawSource = require("webpack-sources").RawSource; |
| 8 | +const ReplaceSource = require("webpack-sources").ReplaceSource; |
| 9 | + |
| 10 | +// TODO: clean up this file |
| 11 | +// replace with newer constructs |
| 12 | + |
| 13 | +// TODO: remove DependencyVariables and replace them with something better |
| 14 | + |
| 15 | +class JavascriptGenerator { |
| 16 | + |
| 17 | + generate(module, dependencyTemplates, runtimeTemplate) { |
| 18 | + const originalSource = module.originalSource(); |
| 19 | + if(!originalSource) { |
| 20 | + return new RawSource("throw new Error('No source available');"); |
| 21 | + } |
| 22 | + |
| 23 | + const source = new ReplaceSource(originalSource); |
| 24 | + |
| 25 | + this.sourceBlock(module, module, [], dependencyTemplates, source, runtimeTemplate); |
| 26 | + |
| 27 | + return source; |
| 28 | + } |
| 29 | + |
| 30 | + sourceBlock(module, block, availableVars, dependencyTemplates, source, runtimeTemplate) { |
| 31 | + for(const dependency of block.dependencies) { |
| 32 | + this.sourceDependency(dependency, dependencyTemplates, source, runtimeTemplate); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the variables of all blocks that we need to inject. |
| 37 | + * These will contain the variable name and its expression. |
| 38 | + * The name will be added as a paramter in a IIFE the expression as its value. |
| 39 | + */ |
| 40 | + const vars = block.variables.reduce((result, value) => { |
| 41 | + const variable = this.sourceVariables( |
| 42 | + value, availableVars, dependencyTemplates, runtimeTemplate); |
| 43 | + |
| 44 | + if(variable) { |
| 45 | + result.push(variable); |
| 46 | + } |
| 47 | + |
| 48 | + return result; |
| 49 | + }, []); |
| 50 | + |
| 51 | + /** |
| 52 | + * if we actually have variables |
| 53 | + * this is important as how #splitVariablesInUniqueNamedChunks works |
| 54 | + * it will always return an array in an array which would lead to a IIFE wrapper around |
| 55 | + * a module if we do this with an empty vars array. |
| 56 | + */ |
| 57 | + if(vars.length > 0) { |
| 58 | + /** |
| 59 | + * Split all variables up into chunks of unique names. |
| 60 | + * e.g. imagine you have the following variable names that need to be injected: |
| 61 | + * [foo, bar, baz, foo, some, more] |
| 62 | + * we can not inject "foo" twice, therefore we just make two IIFEs like so: |
| 63 | + * (function(foo, bar, baz){ |
| 64 | + * (function(foo, some, more){ |
| 65 | + * ... |
| 66 | + * }(...)); |
| 67 | + * }(...)); |
| 68 | + * |
| 69 | + * "splitVariablesInUniqueNamedChunks" splits the variables shown above up to this: |
| 70 | + * [[foo, bar, baz], [foo, some, more]] |
| 71 | + */ |
| 72 | + const injectionVariableChunks = this.splitVariablesInUniqueNamedChunks(vars); |
| 73 | + |
| 74 | + // create all the beginnings of IIFEs |
| 75 | + const functionWrapperStarts = injectionVariableChunks.map((variableChunk) => { |
| 76 | + return this.variableInjectionFunctionWrapperStartCode( |
| 77 | + variableChunk.map(variable => variable.name) |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + // and all the ends |
| 82 | + const functionWrapperEnds = injectionVariableChunks.map((variableChunk) => { |
| 83 | + return this.variableInjectionFunctionWrapperEndCode( |
| 84 | + module, |
| 85 | + variableChunk.map(variable => variable.expression), block |
| 86 | + ); |
| 87 | + }); |
| 88 | + |
| 89 | + // join them to one big string |
| 90 | + const varStartCode = functionWrapperStarts.join(""); |
| 91 | + |
| 92 | + // reverse the ends first before joining them, as the last added must be the inner most |
| 93 | + const varEndCode = functionWrapperEnds.reverse().join(""); |
| 94 | + |
| 95 | + // if we have anything, add it to the source |
| 96 | + if(varStartCode && varEndCode) { |
| 97 | + const start = block.range ? block.range[0] : -10; |
| 98 | + const end = block.range ? block.range[1] : (module.originalSource().size() + 1); |
| 99 | + source.insert(start + 0.5, varStartCode); |
| 100 | + source.insert(end + 0.5, "\n/* WEBPACK VAR INJECTION */" + varEndCode); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + for(const childBlock of block.blocks) { |
| 105 | + this.sourceBlock( |
| 106 | + module, |
| 107 | + childBlock, |
| 108 | + availableVars.concat(vars), |
| 109 | + dependencyTemplates, |
| 110 | + source, |
| 111 | + runtimeTemplate |
| 112 | + ); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + sourceDependency(dependency, dependencyTemplates, source, runtimeTemplate) { |
| 117 | + const template = dependencyTemplates.get(dependency.constructor); |
| 118 | + if(!template) throw new Error("No template for dependency: " + dependency.constructor.name); |
| 119 | + template.apply(dependency, source, runtimeTemplate, dependencyTemplates); |
| 120 | + } |
| 121 | + |
| 122 | + sourceVariables(variable, availableVars, dependencyTemplates, runtimeTemplate) { |
| 123 | + const name = variable.name; |
| 124 | + const expr = variable.expressionSource(dependencyTemplates, runtimeTemplate); |
| 125 | + |
| 126 | + if(availableVars.some(v => v.name === name && v.expression.source() === expr.source())) { |
| 127 | + return; |
| 128 | + } |
| 129 | + return { |
| 130 | + name: name, |
| 131 | + expression: expr |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + /* |
| 136 | + * creates the start part of a IIFE around the module to inject a variable name |
| 137 | + * (function(...){ <- this part |
| 138 | + * }.call(...)) |
| 139 | + */ |
| 140 | + variableInjectionFunctionWrapperStartCode(varNames) { |
| 141 | + const args = varNames.join(", "); |
| 142 | + return `/* WEBPACK VAR INJECTION */(function(${args}) {`; |
| 143 | + } |
| 144 | + |
| 145 | + contextArgument(module, block) { |
| 146 | + if(this === block) { |
| 147 | + return module.exportsArgument; |
| 148 | + } |
| 149 | + return "this"; |
| 150 | + } |
| 151 | + |
| 152 | + /* |
| 153 | + * creates the end part of a IIFE around the module to inject a variable name |
| 154 | + * (function(...){ |
| 155 | + * }.call(...)) <- this part |
| 156 | + */ |
| 157 | + variableInjectionFunctionWrapperEndCode(module, varExpressions, block) { |
| 158 | + const firstParam = this.contextArgument(module, block); |
| 159 | + const furtherParams = varExpressions.map(e => e.source()).join(", "); |
| 160 | + return `}.call(${firstParam}, ${furtherParams}))`; |
| 161 | + } |
| 162 | + |
| 163 | + splitVariablesInUniqueNamedChunks(vars) { |
| 164 | + const startState = [ |
| 165 | + [] |
| 166 | + ]; |
| 167 | + return vars.reduce((chunks, variable) => { |
| 168 | + const current = chunks[chunks.length - 1]; |
| 169 | + // check if variable with same name exists already |
| 170 | + // if so create a new chunk of variables. |
| 171 | + const variableNameAlreadyExists = current.some(v => v.name === variable.name); |
| 172 | + |
| 173 | + if(variableNameAlreadyExists) { |
| 174 | + // start new chunk with current variable |
| 175 | + chunks.push([variable]); |
| 176 | + } else { |
| 177 | + // else add it to current chunk |
| 178 | + current.push(variable); |
| 179 | + } |
| 180 | + return chunks; |
| 181 | + }, startState); |
| 182 | + } |
| 183 | + |
| 184 | +} |
| 185 | + |
| 186 | +module.exports = JavascriptGenerator; |
0 commit comments