|
2 | 2 | MIT License http://www.opensource.org/licenses/mit-license.php |
3 | 3 | Author Tobias Koppers @sokra |
4 | 4 | */ |
5 | | -var LoaderDependency = require("./LoaderDependency"); |
| 5 | +"use strict"; |
6 | 6 |
|
7 | | -function LoaderPlugin() {} |
8 | | -module.exports = LoaderPlugin; |
9 | | - |
10 | | -LoaderPlugin.prototype.apply = function(compiler) { |
11 | | - compiler.plugin("compilation", function(compilation, params) { |
12 | | - var normalModuleFactory = params.normalModuleFactory; |
| 7 | +const LoaderDependency = require("./LoaderDependency"); |
13 | 8 |
|
14 | | - compilation.dependencyFactories.set(LoaderDependency, normalModuleFactory); |
15 | | - }); |
16 | | - compiler.plugin("compilation", function(compilation) { |
17 | | - compilation.plugin("normal-module-loader", function(loaderContext, module) { |
18 | | - loaderContext.loadModule = function loadModule(request, callback) { |
19 | | - var dep = new LoaderDependency(request); |
20 | | - dep.loc = request; |
21 | | - compilation.addModuleDependencies(module, [ |
22 | | - [dep] |
23 | | - ], true, "lm", false, function(err) { |
24 | | - if(err) return callback(err); |
| 9 | +class LoaderPlugin { |
25 | 10 |
|
26 | | - if(!dep.module) return callback(new Error("Cannot load the module")); |
27 | | - if(dep.module.building) dep.module.building.push(next); |
28 | | - else next(); |
| 11 | + apply(compiler) { |
| 12 | + compiler.plugin("compilation", (compilation, params) => { |
| 13 | + const normalModuleFactory = params.normalModuleFactory; |
29 | 14 |
|
30 | | - function next(err) { |
| 15 | + compilation.dependencyFactories.set(LoaderDependency, normalModuleFactory); |
| 16 | + }); |
| 17 | + compiler.plugin("compilation", (compilation) => { |
| 18 | + compilation.plugin("normal-module-loader", (loaderContext, module) => { |
| 19 | + loaderContext.loadModule = function loadModule(request, callback) { |
| 20 | + const dep = new LoaderDependency(request); |
| 21 | + dep.loc = request; |
| 22 | + compilation.addModuleDependencies(module, [ |
| 23 | + [dep] |
| 24 | + ], true, "lm", false, (err) => { |
31 | 25 | if(err) return callback(err); |
32 | 26 |
|
33 | | - if(dep.module.error) return callback(dep.module.error); |
34 | | - if(!dep.module._source) throw new Error("The module created for a LoaderDependency must have a property _source"); |
35 | | - var source, map; |
36 | | - var moduleSource = dep.module._source; |
37 | | - if(moduleSource.sourceAndMap) { |
38 | | - var sourceAndMap = moduleSource.sourceAndMap(); |
39 | | - map = sourceAndMap.map; |
40 | | - source = sourceAndMap.source; |
41 | | - } else { |
42 | | - map = moduleSource.map(); |
43 | | - source = moduleSource.source(); |
44 | | - } |
45 | | - if(dep.module.fileDependencies) { |
46 | | - dep.module.fileDependencies.forEach(function(dep) { |
47 | | - loaderContext.addDependency(dep); |
48 | | - }); |
49 | | - } |
50 | | - if(dep.module.contextDependencies) { |
51 | | - dep.module.contextDependencies.forEach(function(dep) { |
52 | | - loaderContext.addContextDependency(dep); |
53 | | - }); |
| 27 | + if(!dep.module) return callback(new Error("Cannot load the module")); |
| 28 | + if(dep.module.building) dep.module.building.push(next); |
| 29 | + else next(); |
| 30 | + |
| 31 | + function next(err) { |
| 32 | + if(err) return callback(err); |
| 33 | + |
| 34 | + if(dep.module.error) return callback(dep.module.error); |
| 35 | + if(!dep.module._source) throw new Error("The module created for a LoaderDependency must have a property _source"); |
| 36 | + let source, map; |
| 37 | + const moduleSource = dep.module._source; |
| 38 | + if(moduleSource.sourceAndMap) { |
| 39 | + const sourceAndMap = moduleSource.sourceAndMap(); |
| 40 | + map = sourceAndMap.map; |
| 41 | + source = sourceAndMap.source; |
| 42 | + } else { |
| 43 | + map = moduleSource.map(); |
| 44 | + source = moduleSource.source(); |
| 45 | + } |
| 46 | + if(dep.module.fileDependencies) { |
| 47 | + dep.module.fileDependencies.forEach((dep) => loaderContext.addDependency(dep)); |
| 48 | + } |
| 49 | + if(dep.module.contextDependencies) { |
| 50 | + dep.module.contextDependencies.forEach((dep) => loaderContext.addContextDependency(dep)); |
| 51 | + } |
| 52 | + return callback(null, source, map, dep.module); |
54 | 53 | } |
55 | | - return callback(null, source, map, dep.module); |
56 | | - } |
57 | | - }); |
58 | | - }; |
| 54 | + }); |
| 55 | + }; |
| 56 | + }); |
59 | 57 | }); |
60 | | - }); |
61 | | -}; |
| 58 | + } |
| 59 | +} |
| 60 | +module.exports = LoaderPlugin; |
0 commit comments