UNPKG

6.5 kBJavaScriptView Raw
1var ConcatSource = require("webpack-sources").ConcatSource;
2var OriginalSource = require("webpack-sources").OriginalSource;
3
4function accessorToObjectAccess(accessor) {
5 return accessor.map(function(a) {
6 return "[" + JSON.stringify(a) + "]";
7 }).join("");
8}
9
10function accessorAccess(base, accessor) {
11 accessor = [].concat(accessor);
12 return accessor.map(function(a, idx) {
13 a = base + accessorToObjectAccess(accessor.slice(0, idx + 1));
14 if(idx === accessor.length - 1) return a;
15 return a + " = " + a + " || {}";
16 }).join(", ");
17}
18
19function FmdMainTemplatePlugin(prefix, name, deps, options) {
20 this.prefix = prefix
21 this.name = name
22 this.deps = deps
23 // this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
24 // this.namedDefine = options.namedDefine;
25}
26module.exports = FmdMainTemplatePlugin;
27
28function generateFmdCode (fmdModuleName, deps) {
29 const source =
30 ' else if (typeof define === \'function\' && define.fmd) {' + '\n' +
31 ` define("${fmdModuleName}", [], factory)` + '\n' +
32 ` define(["${fmdModuleName}"], function (vendors) {` + '\n' +
33 Object.keys(deps).map(depName => {
34 const ver = deps[depName]
35 const name = ver ? depName + '-' + ver : depName
36 return ` define("${name}", [], function () { return vendors["${depName}"] })`
37 }).join('\n') + '\n' +
38 ' })' + '\n' +
39 ' }'
40 return source
41}
42
43FmdMainTemplatePlugin.prototype.apply = function(compilation) {
44 var mainTemplate = compilation.mainTemplate;
45 compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) {
46 var externals = chunk.modules.filter(function(m) {
47 return m.external;
48 });
49 var optionalExternals = [],
50 requiredExternals = [];
51 if(this.optionalAmdExternalAsGlobal) {
52 externals.forEach(function(m) {
53 if(m.optional) {
54 optionalExternals.push(m);
55 } else {
56 requiredExternals.push(m);
57 }
58 });
59 externals = requiredExternals.concat(optionalExternals);
60 } else {
61 requiredExternals = externals;
62 }
63
64 function replaceKeys(str) {
65 return mainTemplate.applyPluginsWaterfall("asset-path", str, {
66 hash: hash,
67 chunk: chunk
68 });
69 }
70
71 function externalsDepsArray(modules) {
72 return "[" + replaceKeys(modules.map(function(m) {
73 return JSON.stringify(typeof m.request === "object" ? m.request.amd : m.request);
74 }).join(", ")) + "]";
75 }
76
77 function externalsRootArray(modules) {
78 return replaceKeys(modules.map(function(m) {
79 var request = m.request;
80 if(typeof request === "object") request = request.root;
81 return "root" + accessorToObjectAccess([].concat(request));
82 }).join(", "));
83 }
84
85 function externalsRequireArray(type) {
86 return replaceKeys(externals.map(function(m) {
87 var expr;
88 var request = m.request;
89 if(typeof request === "object") request = request[type];
90 if(Array.isArray(request)) {
91 expr = "require(" + JSON.stringify(request[0]) + ")" + accessorToObjectAccess(request.slice(1));
92 } else
93 expr = "require(" + JSON.stringify(request) + ")";
94 if(m.optional) {
95 expr = "(function webpackLoadOptionalExternalModule() { try { return " + expr + "; } catch(e) {} }())";
96 }
97 return expr;
98 }).join(", "));
99 }
100
101 function externalsArguments(modules) {
102 return modules.map(function(m) {
103 return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__";
104 }).join(", ");
105 }
106
107 function libraryName(library) {
108 return JSON.stringify(replaceKeys([].concat(library).pop()));
109 }
110
111 var amdFactory;
112 if(optionalExternals.length > 0) {
113 amdFactory = "function webpackLoadOptionalExternalModuleAmd(" + externalsArguments(requiredExternals) + ") {\n" +
114 " return factory(" + (
115 requiredExternals.length > 0 ?
116 externalsArguments(requiredExternals) + ", " + externalsRootArray(optionalExternals) :
117 externalsRootArray(optionalExternals)
118 ) + ");\n" +
119 " }";
120 } else {
121 amdFactory = "factory";
122 }
123
124 var fmdModuleName = this.prefix + this.name
125
126 return new ConcatSource(new OriginalSource(
127 "(function webpackUniversalModuleDefinition(root, factory) {\n" +
128 // commonjs
129 " if(typeof exports === 'object' && typeof module === 'object')\n" +
130 " module.exports = factory(" + externalsRequireArray("commonjs2") + ");\n" +
131 // fmd
132 generateFmdCode(fmdModuleName, this.deps) +
133 // " else if(typeof define === 'function' && define.fmd)\n" +
134 // (requiredExternals.length > 0 ?
135 // (this.name && this.namedDefine === true ?
136 // " define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
137 // " define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n"
138 // ) :
139 // (this.name && this.namedDefine === true ?
140 // " define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" :
141 // " define(\"" + fmdModuleName + "\", [], " + amdFactory + ");\n"
142 // )
143 // ) +
144 // amd
145 " else if(typeof define === 'function' && define.amd)\n" +
146 (requiredExternals.length > 0 ?
147 (this.name && this.namedDefine === true ?
148 " define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
149 " define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n"
150 ) :
151 (this.name && this.namedDefine === true ?
152 " define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" :
153 " define([], " + amdFactory + ");\n"
154 )
155 ) +
156 (this.name ?
157 " else if(typeof exports === 'object')\n" +
158 " exports[" + libraryName(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
159 " else\n" +
160 " " + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" :
161 " else {\n" +
162 (externals.length > 0 ?
163 " var a = typeof exports === 'object' ? factory(" + externalsRequireArray("commonjs") + ") : factory(" + externalsRootArray(externals) + ");\n" :
164 " var a = factory();\n"
165 ) +
166 " for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
167 " }\n"
168 ) +
169 "})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, "\n});\n");
170 }.bind(this));
171 mainTemplate.plugin("global-hash-paths", function(paths) {
172 if(this.name) paths = paths.concat(this.name);
173 return paths;
174 }.bind(this));
175 mainTemplate.plugin("hash", function(hash) {
176 hash.update("fmd");
177 hash.update(this.name + "");
178 }.bind(this));
179};