webpack 2.1.0-beta.21 → 2.1.0-beta.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/bin/config-yargs.js +6 -5
  2. package/bin/convert-argv.js +59 -31
  3. package/bin/webpack.js +30 -2
  4. package/lib/APIPlugin.js +19 -16
  5. package/lib/Chunk.js +1 -1
  6. package/lib/CommonJsHarmonyMainTemplatePlugin.js +21 -0
  7. package/lib/CompatibilityPlugin.js +24 -17
  8. package/lib/Compilation.js +36 -15
  9. package/lib/Compiler.js +48 -15
  10. package/lib/ConstPlugin.js +40 -37
  11. package/lib/DefinePlugin.js +107 -103
  12. package/lib/DelegatedModule.js +1 -2
  13. package/lib/Dependency.js +5 -0
  14. package/lib/DllReferencePlugin.js +41 -15
  15. package/lib/ExtendedAPIPlugin.js +14 -11
  16. package/lib/FlagDependencyExportsPlugin.js +102 -0
  17. package/lib/FlagInitialModulesAsUsedPlugin.js +23 -23
  18. package/lib/FunctionModuleTemplatePlugin.js +4 -0
  19. package/lib/HotModuleReplacement.runtime.js +1 -1
  20. package/lib/HotModuleReplacementPlugin.js +71 -68
  21. package/lib/LibManifestPlugin.js +1 -2
  22. package/lib/LibraryTemplatePlugin.js +4 -0
  23. package/lib/MainTemplate.js +12 -13
  24. package/lib/Module.js +10 -1
  25. package/lib/MovedToPluginWarningPlugin.js +3 -1
  26. package/lib/MultiCompiler.js +81 -7
  27. package/lib/NodeStuffPlugin.js +97 -86
  28. package/lib/NormalModule.js +6 -0
  29. package/lib/NormalModuleFactory.js +87 -34
  30. package/lib/Parser.js +13 -4
  31. package/lib/ProvidePlugin.js +30 -27
  32. package/lib/RequireJsStuffPlugin.js +27 -21
  33. package/lib/RuleSet.js +369 -0
  34. package/lib/Stats.js +74 -90
  35. package/lib/UseStrictPlugin.js +12 -8
  36. package/lib/WebpackOptionsApply.js +3 -13
  37. package/lib/WebpackOptionsDefaulter.js +0 -1
  38. package/lib/WebpackOptionsValidationError.js +186 -0
  39. package/lib/dependencies/AMDPlugin.js +64 -55
  40. package/lib/dependencies/CommonJsPlugin.js +55 -45
  41. package/lib/dependencies/HarmonyExportExpressionDependency.js +6 -0
  42. package/lib/dependencies/HarmonyExportImportedSpecifierDependency.js +76 -16
  43. package/lib/dependencies/HarmonyExportSpecifierDependency.js +12 -4
  44. package/lib/dependencies/HarmonyImportSpecifierDependency.js +6 -4
  45. package/lib/dependencies/HarmonyModulesHelpers.js +0 -29
  46. package/lib/dependencies/HarmonyModulesPlugin.js +11 -4
  47. package/lib/dependencies/RequireContextPlugin.js +12 -5
  48. package/lib/dependencies/RequireEnsurePlugin.js +17 -10
  49. package/lib/dependencies/RequireIncludePlugin.js +18 -10
  50. package/lib/dependencies/SystemPlugin.js +48 -29
  51. package/lib/node/NodeMainTemplate.runtime.js +2 -2
  52. package/lib/node/NodeMainTemplateAsync.runtime.js +2 -2
  53. package/lib/node/NodeSourcePlugin.js +84 -71
  54. package/lib/optimize/ChunkModuleIdRangePlugin.js +52 -0
  55. package/lib/optimize/EnsureChunkConditionsPlugin.js +7 -2
  56. package/lib/validateWebpackOptions.js +63 -0
  57. package/lib/webpack.js +9 -6
  58. package/lib/webworker/WebWorkerMainTemplate.runtime.js +35 -33
  59. package/package.json +13 -8
  60. package/schemas/webpackOptionsSchema.json +802 -0
  61. package/README.md +0 -315
  62. package/lib/LoadersList.js +0 -110
  63. package/lib/dependencies/LabeledExportsDependency.js +0 -21
  64. package/lib/dependencies/LabeledModuleDependency.js +0 -36
  65. package/lib/dependencies/LabeledModuleDependencyParserPlugin.js +0 -78
  66. package/lib/dependencies/LabeledModulesPlugin.js +0 -26
@@ -194,75 +194,78 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
194
194
  ]);
195
195
  });
196
196
 
197
+ params.normalModuleFactory.plugin("parser", function(parser, parserOptions) {
198
+ parser.plugin("expression __webpack_hash__", function(expr) {
199
+ var dep = new ConstDependency("__webpack_require__.h()", expr.range);
200
+ dep.loc = expr.loc;
201
+ this.state.current.addDependency(dep);
202
+ return true;
203
+ });
204
+ parser.plugin("evaluate typeof __webpack_hash__", function(expr) {
205
+ return new BasicEvaluatedExpression().setString("string").setRange(expr.range);
206
+ });
207
+ parser.plugin("evaluate Identifier module.hot", function(expr) {
208
+ return new BasicEvaluatedExpression()
209
+ .setBoolean(!!this.state.compilation.hotUpdateChunkTemplate)
210
+ .setRange(expr.range);
211
+ });
212
+ parser.plugin("call module.hot.accept", function(expr) {
213
+ if(!this.state.compilation.hotUpdateChunkTemplate) return false;
214
+ if(expr.arguments.length >= 1) {
215
+ var arg = this.evaluateExpression(expr.arguments[0]);
216
+ var params = [],
217
+ requests = [];
218
+ if(arg.isString()) {
219
+ params = [arg];
220
+ } else if(arg.isArray()) {
221
+ params = arg.items.filter(function(param) {
222
+ return param.isString();
223
+ });
224
+ }
225
+ if(params.length > 0) {
226
+ params.forEach(function(param, idx) {
227
+ var request = param.string;
228
+ var dep = new ModuleHotAcceptDependency(request, param.range);
229
+ dep.optional = true;
230
+ dep.loc = Object.create(expr.loc);
231
+ dep.loc.index = idx;
232
+ this.state.module.addDependency(dep);
233
+ requests.push(request);
234
+ }.bind(this));
235
+ if(expr.arguments.length > 1)
236
+ this.applyPluginsBailResult("hot accept callback", expr.arguments[1], requests);
237
+ else
238
+ this.applyPluginsBailResult("hot accept without callback", expr, requests);
239
+ }
240
+ }
241
+ });
242
+ parser.plugin("call module.hot.decline", function(expr) {
243
+ if(!this.state.compilation.hotUpdateChunkTemplate) return false;
244
+ if(expr.arguments.length === 1) {
245
+ var arg = this.evaluateExpression(expr.arguments[0]);
246
+ var params = [];
247
+ if(arg.isString()) {
248
+ params = [arg];
249
+ } else if(arg.isArray()) {
250
+ params = arg.items.filter(function(param) {
251
+ return param.isString();
252
+ });
253
+ }
254
+ params.forEach(function(param, idx) {
255
+ var dep = new ModuleHotDeclineDependency(param.string, param.range);
256
+ dep.optional = true;
257
+ dep.loc = Object.create(expr.loc);
258
+ dep.loc.index = idx;
259
+ this.state.module.addDependency(dep);
260
+ }.bind(this));
261
+ }
262
+ });
263
+ parser.plugin("expression module.hot", function() {
264
+ return true;
265
+ });
266
+ });
197
267
  });
198
- compiler.parser.plugin("expression __webpack_hash__", function(expr) {
199
- var dep = new ConstDependency("__webpack_require__.h()", expr.range);
200
- dep.loc = expr.loc;
201
- this.state.current.addDependency(dep);
202
- return true;
203
- });
204
- compiler.parser.plugin("evaluate typeof __webpack_hash__", function(expr) {
205
- return new BasicEvaluatedExpression().setString("string").setRange(expr.range);
206
- });
207
- compiler.parser.plugin("evaluate Identifier module.hot", function(expr) {
208
- return new BasicEvaluatedExpression()
209
- .setBoolean(!!this.state.compilation.hotUpdateChunkTemplate)
210
- .setRange(expr.range);
211
- });
212
- compiler.parser.plugin("call module.hot.accept", function(expr) {
213
- if(!this.state.compilation.hotUpdateChunkTemplate) return false;
214
- if(expr.arguments.length >= 1) {
215
- var arg = this.evaluateExpression(expr.arguments[0]);
216
- var params = [],
217
- requests = [];
218
- if(arg.isString()) {
219
- params = [arg];
220
- } else if(arg.isArray()) {
221
- params = arg.items.filter(function(param) {
222
- return param.isString();
223
- });
224
- }
225
- if(params.length > 0) {
226
- params.forEach(function(param, idx) {
227
- var request = param.string;
228
- var dep = new ModuleHotAcceptDependency(request, param.range);
229
- dep.optional = true;
230
- dep.loc = Object.create(expr.loc);
231
- dep.loc.index = idx;
232
- this.state.module.addDependency(dep);
233
- requests.push(request);
234
- }.bind(this));
235
- if(expr.arguments.length > 1)
236
- this.applyPluginsBailResult("hot accept callback", expr.arguments[1], requests);
237
- else
238
- this.applyPluginsBailResult("hot accept without callback", expr, requests);
239
- }
240
- }
241
- });
242
- compiler.parser.plugin("call module.hot.decline", function(expr) {
243
- if(!this.state.compilation.hotUpdateChunkTemplate) return false;
244
- if(expr.arguments.length === 1) {
245
- var arg = this.evaluateExpression(expr.arguments[0]);
246
- var params = [];
247
- if(arg.isString()) {
248
- params = [arg];
249
- } else if(arg.isArray()) {
250
- params = arg.items.filter(function(param) {
251
- return param.isString();
252
- });
253
- }
254
- params.forEach(function(param, idx) {
255
- var dep = new ModuleHotDeclineDependency(param.string, param.range);
256
- dep.optional = true;
257
- dep.loc = Object.create(expr.loc);
258
- dep.loc.index = idx;
259
- this.state.module.addDependency(dep);
260
- }.bind(this));
261
- }
262
- });
263
- compiler.parser.plugin("expression module.hot", function() {
264
- return true;
265
- });
268
+
266
269
  };
267
270
 
268
271
  var hotInitCode = Template.getFunctionContent(require("./HotModuleReplacement.runtime.js"));
@@ -37,8 +37,7 @@ LibManifestPlugin.prototype.apply = function(compiler) {
37
37
  obj[ident] = {
38
38
  id: module.id,
39
39
  meta: module.meta,
40
- hasStarExport: HarmonyModulesHelpers.hasStarExport(module),
41
- activeExports: HarmonyModulesHelpers.getActiveExports(module)
40
+ exports: Array.isArray(module.providedExports) ? module.providedExports : undefined
42
41
  };
43
42
  }
44
43
  }
@@ -3,6 +3,7 @@
3
3
  Author Tobias Koppers @sokra
4
4
  */
5
5
  var SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
6
+ var CommonJsHarmonyMainTemplatePlugin = require("./CommonJsHarmonyMainTemplatePlugin");
6
7
 
7
8
  function accessorToObjectAccess(accessor) {
8
9
  return accessor.map(function(a) {
@@ -55,6 +56,9 @@ LibraryTemplatePlugin.prototype.apply = function(compiler) {
55
56
  case "commonjs2":
56
57
  compilation.apply(new SetVarMainTemplatePlugin("module.exports"));
57
58
  break;
59
+ case "commonjs-module":
60
+ compilation.apply(new CommonJsHarmonyMainTemplatePlugin());
61
+ break;
58
62
  case "amd":
59
63
  var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
60
64
  compilation.apply(new AmdMainTemplatePlugin(this.name));
@@ -7,19 +7,18 @@ var OriginalSource = require("webpack-sources").OriginalSource;
7
7
  var PrefixSource = require("webpack-sources").PrefixSource;
8
8
  var Template = require("./Template");
9
9
 
10
- /* require function shortcuts:
11
- * __webpack_require__.s = the module id of the entry point
12
- * __webpack_require__.c = the module cache
13
- * __webpack_require__.m = the module functions
14
- * __webpack_require__.p = the bundle public path
15
- * __webpack_require__.i = the identity function used for harmony imports
16
- * __webpack_require__.e = the chunk ensure function
17
- * __webpack_require__.d = the exported propery define getter function
18
- * __webpack_require__.o = Object.prototype.hasOwnProperty.call
19
- * __webpack_require__.n = compatibility get default export
20
- * __webpack_require__.h = the webpack hash
21
- * __webpack_require__.oe = the uncatched error handler for the webpack runtime
22
- */
10
+ // require function shortcuts:
11
+ // __webpack_require__.s = the module id of the entry point
12
+ // __webpack_require__.c = the module cache
13
+ // __webpack_require__.m = the module functions
14
+ // __webpack_require__.p = the bundle public path
15
+ // __webpack_require__.i = the identity function used for harmony imports
16
+ // __webpack_require__.e = the chunk ensure function
17
+ // __webpack_require__.d = the exported propery define getter function
18
+ // __webpack_require__.o = Object.prototype.hasOwnProperty.call
19
+ // __webpack_require__.n = compatibility get default export
20
+ // __webpack_require__.h = the webpack hash
21
+ // __webpack_require__.oe = the uncatched error handler for the webpack runtime
23
22
 
24
23
  function MainTemplate(outputOptions) {
25
24
  Template.call(this, outputOptions);
package/lib/Module.js CHANGED
@@ -20,6 +20,7 @@ function Module() {
20
20
  this.index2 = null;
21
21
  this.used = null;
22
22
  this.usedExports = null;
23
+ this.providedExports = null;
23
24
  this.chunks = [];
24
25
  this.warnings = [];
25
26
  this.dependenciesWarnings = [];
@@ -51,6 +52,7 @@ Module.prototype.disconnect = function() {
51
52
  this.index2 = null;
52
53
  this.used = null;
53
54
  this.usedExports = null;
55
+ this.providedExports = null;
54
56
  this.chunks.length = 0;
55
57
  DependenciesBlock.prototype.disconnect.call(this);
56
58
  };
@@ -132,11 +134,17 @@ Module.prototype.isUsed = function(exportName) {
132
134
  if(this.usedExports === true) return exportName;
133
135
  var idx = this.usedExports.indexOf(exportName);
134
136
  if(idx < 0) return false;
135
- if(HarmonyModulesHelpers.isExportedByHarmony(this, exportName))
137
+ if(this.isProvided(exportName))
136
138
  return Template.numberToIdentifer(idx);
137
139
  return exportName;
138
140
  };
139
141
 
142
+ Module.prototype.isProvided = function(exportName) {
143
+ if(!Array.isArray(this.providedExports))
144
+ return null;
145
+ return this.providedExports.indexOf(exportName) >= 0;
146
+ };
147
+
140
148
  Module.prototype.toString = function() {
141
149
  return "Module[" + (this.id || this.debugId) + "]";
142
150
  };
@@ -168,3 +176,4 @@ Module.prototype.readableIdentifier = null;
168
176
  Module.prototype.build = null;
169
177
  Module.prototype.source = null;
170
178
  Module.prototype.size = null;
179
+ Module.prototype.nameForCondition = null;
@@ -12,6 +12,8 @@ MovedToPluginWarningPlugin.prototype.apply = function(compiler) {
12
12
  var optionName = this.optionName;
13
13
  var pluginName = this.pluginName;
14
14
  compiler.plugin("compilation", function(compilation) {
15
- compilation.warnings.push(new Error("webpack options:\nDEPRECATED option '" + optionName + "' will be moved to the " + pluginName + ". Use this instead.\nFor more info about the usage of the " + pluginName + " see https://webpack.github.io/docs/list-of-plugins.html"));
15
+ compilation.warnings.push(new Error("webpack options:\nDEPRECATED option '" + optionName + "' will be moved to the " + pluginName + ". " +
16
+ "Use this instead.\n" +
17
+ "For more info about the usage of the " + pluginName + " see https://webpack.github.io/docs/list-of-plugins.html"));
16
18
  });
17
19
  };
@@ -90,19 +90,93 @@ module.exports = MultiCompiler;
90
90
  MultiCompiler.prototype = Object.create(Tapable.prototype);
91
91
  MultiCompiler.prototype.constructor = MultiCompiler;
92
92
 
93
- MultiCompiler.prototype.watch = function(watchDelay, handler) {
94
- var watchings = this.compilers.map(function(compiler) {
95
- return compiler.watch(watchDelay, handler);
93
+ function runWithDependencies(compilers, fn, callback) {
94
+ var fulfilledNames = {};
95
+ var remainingCompilers = compilers;
96
+
97
+ function isDependencyFulfilled(d) {
98
+ return fulfilledNames[d];
99
+ }
100
+
101
+ function getReadyCompilers() {
102
+ var readyCompilers = [];
103
+ var list = remainingCompilers;
104
+ remainingCompilers = [];
105
+ for(var i = 0; i < list.length; i++) {
106
+ var c = list[i];
107
+ var ready = !c.dependencies || c.dependencies.every(isDependencyFulfilled);
108
+ if(ready)
109
+ readyCompilers.push(c);
110
+ else
111
+ remainingCompilers.push(c);
112
+ }
113
+ return readyCompilers;
114
+ }
115
+
116
+ function runCompilers(callback) {
117
+ if(remainingCompilers.length === 0) return callback();
118
+ async.map(getReadyCompilers(), function(compiler, callback) {
119
+ fn(compiler, function(err) {
120
+ if(err) return callback(err);
121
+ fulfilledNames[compiler.name] = true;
122
+ runCompilers(callback);
123
+ });
124
+ }, callback);
125
+ }
126
+ runCompilers(callback);
127
+ }
128
+
129
+ MultiCompiler.prototype.watch = function(watchOptions, handler) {
130
+ var watchings = [];
131
+ var allStats = this.compilers.map(function() {
132
+ return null;
133
+ });
134
+ var compilerStatus = this.compilers.map(function() {
135
+ return false;
96
136
  });
137
+
138
+ runWithDependencies(this.compilers, function(compiler, callback) {
139
+ var compilerIdx = this.compilers.indexOf(compiler);
140
+ var firstRun = true;
141
+ var watching = compiler.watch(watchOptions, function(err, stats) {
142
+ if(err)
143
+ handler(err);
144
+ if(stats) {
145
+ allStats[compilerIdx] = stats;
146
+ compilerStatus[compilerIdx] = true;
147
+ if(compilerStatus.every(Boolean)) {
148
+ var multiStats = new MultiStats(allStats);
149
+ handler(null, multiStats)
150
+ }
151
+ }
152
+ if(firstRun && !err) {
153
+ firstRun = false;
154
+ callback();
155
+ }
156
+ });
157
+ watchings.push(watching);
158
+ }.bind(this), function() {
159
+ // ignore
160
+ });
161
+
97
162
  return new MultiWatching(watchings);
98
163
  };
99
164
 
100
165
  MultiCompiler.prototype.run = function(callback) {
101
- async.map(this.compilers, function(compiler, callback) {
102
- compiler.run(callback);
103
- }, function(err, stats) {
166
+ var allStats = this.compilers.map(function() {
167
+ return null;
168
+ });
169
+
170
+ runWithDependencies(this.compilers, function(compiler, callback) {
171
+ var compilerIdx = this.compilers.indexOf(compiler);
172
+ compiler.run(function(err, stats) {
173
+ if(err) return callback(err);
174
+ allStats[compilerIdx] = stats;
175
+ callback();
176
+ });
177
+ }.bind(this), function(err) {
104
178
  if(err) return callback(err);
105
- callback(null, new MultiStats(stats));
179
+ callback(null, new MultiStats(allStats));
106
180
  });
107
181
  };
108
182
 
@@ -15,97 +15,108 @@ function NodeStuffPlugin(options) {
15
15
  }
16
16
  module.exports = NodeStuffPlugin;
17
17
  NodeStuffPlugin.prototype.apply = function(compiler) {
18
- compiler.plugin("compilation", function(compilation) {
18
+ var options = this.options;
19
+ compiler.plugin("compilation", function(compilation, params) {
19
20
  compilation.dependencyFactories.set(ConstDependency, new NullFactory());
20
21
  compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
21
- });
22
22
 
23
- function ignore() {
24
- return true;
25
- }
23
+ params.normalModuleFactory.plugin("parser", function(parser, parserOptions) {
26
24
 
27
- function setConstant(expressionName, value) {
28
- compiler.parser.plugin("expression " + expressionName, function() {
29
- this.state.current.addVariable(expressionName, JSON.stringify(value));
30
- return true;
31
- });
32
- }
25
+ if(parserOptions.node === false)
26
+ return;
33
27
 
34
- function setModuleConstant(expressionName, fn) {
35
- compiler.parser.plugin("expression " + expressionName, function() {
36
- this.state.current.addVariable(expressionName, JSON.stringify(fn(this.state.module)));
37
- return true;
38
- });
39
- }
40
- var context = compiler.context;
41
- if(this.options.__filename === "mock") {
42
- setConstant("__filename", "/index.js");
43
- } else if(this.options.__filename) {
44
- setModuleConstant("__filename", function(module) {
45
- return path.relative(context, module.resource);
46
- });
47
- }
48
- compiler.parser.plugin("evaluate Identifier __filename", function(expr) {
49
- if(!this.state.module) return;
50
- var res = new BasicEvaluatedExpression();
51
- var resource = this.state.module.resource;
52
- var i = resource.indexOf("?");
53
- res.setString(i < 0 ? resource : resource.substr(0, i));
54
- res.setRange(expr.range);
55
- return res;
56
- });
57
- if(this.options.__dirname === "mock") {
58
- setConstant("__dirname", "/");
59
- } else if(this.options.__dirname) {
60
- setModuleConstant("__dirname", function(module) {
61
- return path.relative(context, module.context);
62
- });
63
- }
64
- compiler.parser.plugin("evaluate Identifier __dirname", function(expr) {
65
- if(!this.state.module) return;
66
- var res = new BasicEvaluatedExpression();
67
- res.setString(this.state.module.context);
68
- res.setRange(expr.range);
69
- return res;
70
- });
71
- compiler.parser.plugin("expression require.main", function(expr) {
72
- var dep = new ConstDependency("__webpack_require__.c[__webpack_require__.s]", expr.range);
73
- dep.loc = expr.loc;
74
- this.state.current.addDependency(dep);
75
- return true;
76
- });
77
- compiler.parser.plugin("expression require.extensions", function(expr) {
78
- var dep = new ConstDependency("(void 0)", expr.range);
79
- dep.loc = expr.loc;
80
- this.state.current.addDependency(dep);
81
- if(!this.state.module) return;
82
- this.state.module.warnings.push(new UnsupportedFeatureWarning(this.state.module, "require.extensions is not supported by webpack. Use a loader instead."));
83
- return true;
84
- });
85
- compiler.parser.plugin("expression module.loaded", function(expr) {
86
- var dep = new ConstDependency("module.l", expr.range);
87
- dep.loc = expr.loc;
88
- this.state.current.addDependency(dep);
89
- return true;
90
- });
91
- compiler.parser.plugin("expression module.id", function(expr) {
92
- var dep = new ConstDependency("module.i", expr.range);
93
- dep.loc = expr.loc;
94
- this.state.current.addDependency(dep);
95
- return true;
96
- });
97
- compiler.parser.plugin("expression module.exports", ignore);
98
- compiler.parser.plugin("evaluate Identifier module.hot", function(expr) {
99
- return new BasicEvaluatedExpression().setBoolean(false).setRange(expr.range);
100
- });
101
- compiler.parser.plugin("expression module", function() {
102
- var moduleJsPath = path.join(__dirname, "..", "buildin", "module.js");
103
- if(this.state.module.context) {
104
- moduleJsPath = path.relative(this.state.module.context, moduleJsPath);
105
- if(!/^[A-Z]:/i.test(moduleJsPath)) {
106
- moduleJsPath = "./" + moduleJsPath.replace(/\\/g, "/");
28
+ var localOptions = options;
29
+ if(parserOptions.node)
30
+ localOptions = Object.assign({}, localOptions, parserOptions.node);
31
+
32
+ function ignore() {
33
+ return true;
34
+ }
35
+
36
+ function setConstant(expressionName, value) {
37
+ parser.plugin("expression " + expressionName, function() {
38
+ this.state.current.addVariable(expressionName, JSON.stringify(value));
39
+ return true;
40
+ });
107
41
  }
108
- }
109
- return ModuleParserHelpers.addParsedVariable(this, "module", "require(" + JSON.stringify(moduleJsPath) + ")(module)");
42
+
43
+ function setModuleConstant(expressionName, fn) {
44
+ parser.plugin("expression " + expressionName, function() {
45
+ this.state.current.addVariable(expressionName, JSON.stringify(fn(this.state.module)));
46
+ return true;
47
+ });
48
+ }
49
+ var context = compiler.context;
50
+ if(localOptions.__filename === "mock") {
51
+ setConstant("__filename", "/index.js");
52
+ } else if(localOptions.__filename) {
53
+ setModuleConstant("__filename", function(module) {
54
+ return path.relative(context, module.resource);
55
+ });
56
+ }
57
+ parser.plugin("evaluate Identifier __filename", function(expr) {
58
+ if(!this.state.module) return;
59
+ var res = new BasicEvaluatedExpression();
60
+ var resource = this.state.module.resource;
61
+ var i = resource.indexOf("?");
62
+ res.setString(i < 0 ? resource : resource.substr(0, i));
63
+ res.setRange(expr.range);
64
+ return res;
65
+ });
66
+ if(localOptions.__dirname === "mock") {
67
+ setConstant("__dirname", "/");
68
+ } else if(localOptions.__dirname) {
69
+ setModuleConstant("__dirname", function(module) {
70
+ return path.relative(context, module.context);
71
+ });
72
+ }
73
+ parser.plugin("evaluate Identifier __dirname", function(expr) {
74
+ if(!this.state.module) return;
75
+ var res = new BasicEvaluatedExpression();
76
+ res.setString(this.state.module.context);
77
+ res.setRange(expr.range);
78
+ return res;
79
+ });
80
+ parser.plugin("expression require.main", function(expr) {
81
+ var dep = new ConstDependency("__webpack_require__.c[__webpack_require__.s]", expr.range);
82
+ dep.loc = expr.loc;
83
+ this.state.current.addDependency(dep);
84
+ return true;
85
+ });
86
+ parser.plugin("expression require.extensions", function(expr) {
87
+ var dep = new ConstDependency("(void 0)", expr.range);
88
+ dep.loc = expr.loc;
89
+ this.state.current.addDependency(dep);
90
+ if(!this.state.module) return;
91
+ this.state.module.warnings.push(new UnsupportedFeatureWarning(this.state.module, "require.extensions is not supported by webpack. Use a loader instead."));
92
+ return true;
93
+ });
94
+ parser.plugin("expression module.loaded", function(expr) {
95
+ var dep = new ConstDependency("module.l", expr.range);
96
+ dep.loc = expr.loc;
97
+ this.state.current.addDependency(dep);
98
+ return true;
99
+ });
100
+ parser.plugin("expression module.id", function(expr) {
101
+ var dep = new ConstDependency("module.i", expr.range);
102
+ dep.loc = expr.loc;
103
+ this.state.current.addDependency(dep);
104
+ return true;
105
+ });
106
+ parser.plugin("expression module.exports", ignore);
107
+ parser.plugin("evaluate Identifier module.hot", function(expr) {
108
+ return new BasicEvaluatedExpression().setBoolean(false).setRange(expr.range);
109
+ });
110
+ parser.plugin("expression module", function() {
111
+ var moduleJsPath = path.join(__dirname, "..", "buildin", "module.js");
112
+ if(this.state.module.context) {
113
+ moduleJsPath = path.relative(this.state.module.context, moduleJsPath);
114
+ if(!/^[A-Z]:/i.test(moduleJsPath)) {
115
+ moduleJsPath = "./" + moduleJsPath.replace(/\\/g, "/");
116
+ }
117
+ }
118
+ return ModuleParserHelpers.addParsedVariable(this, "module", "require(" + JSON.stringify(moduleJsPath) + ")(module)");
119
+ });
120
+ });
110
121
  });
111
122
  };
@@ -75,6 +75,12 @@ NormalModule.prototype.libIdent = function(options) {
75
75
  return contextify(options, this.userRequest);
76
76
  };
77
77
 
78
+ NormalModule.prototype.nameForCondition = function() {
79
+ var idx = this.resource.indexOf("?");
80
+ if(idx >= 0) return this.resource.substr(0, idx);
81
+ return this.resource;
82
+ };
83
+
78
84
  NormalModule.prototype.doBuild = function doBuild(options, compilation, resolver, fs, callback) {
79
85
  this.cacheable = false;
80
86
  var module = this;