webpack/lib/dependencies/LoaderPlugin.js

64 lines
2.1 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const LoaderDependency = require("./LoaderDependency");
class LoaderPlugin {
2015-04-24 05:55:50 +08:00
apply(compiler) {
2017-12-06 22:01:25 +08:00
compiler.hooks.compilation.tap("LoaderPlugin", (compilation, {
normalModuleFactory
}) => {
compilation.dependencyFactories.set(LoaderDependency, normalModuleFactory);
});
2017-12-06 22:01:25 +08:00
compiler.hooks.compilation.tap("LoaderPlugin", (compilation) => {
compilation.hooks.normalModuleLoader.tap("LoaderPlugin", (loaderContext, module) => {
2017-11-08 18:32:05 +08:00
loaderContext.loadModule = (request, callback) => {
const dep = new LoaderDependency(request);
dep.loc = request;
2017-11-19 16:06:40 +08:00
const factory = compilation.dependencyFactories.get(dep.constructor);
if(factory === undefined)
return callback(new Error(`No module factory available for dependency type: ${dep.constructor.name}`));
compilation.addModuleDependencies(module, [{
factory,
dependencies: [dep]
}], true, "lm", false, err => {
if(err) return callback(err);
2013-10-29 21:14:16 +08:00
if(!dep.module) return callback(new Error("Cannot load the module"));
if(dep.module.error) return callback(dep.module.error);
if(!dep.module._source) throw new Error("The module created for a LoaderDependency must have a property _source");
let source, map;
const moduleSource = dep.module._source;
if(moduleSource.sourceAndMap) {
const sourceAndMap = moduleSource.sourceAndMap();
map = sourceAndMap.map;
source = sourceAndMap.source;
} else {
map = moduleSource.map();
source = moduleSource.source();
}
2017-12-13 05:27:49 +08:00
if(dep.module.buildInfo.fileDependencies) {
2018-01-22 20:52:43 +08:00
for(const d of dep.module.buildInfo.fileDependencies) {
loaderContext.addDependency(d);
}
}
2017-12-13 05:27:49 +08:00
if(dep.module.buildInfo.contextDependencies) {
2018-01-22 20:52:43 +08:00
for(const d of dep.module.buildInfo.contextDependencies) {
loaderContext.addContextDependency(d);
}
2013-10-29 21:14:16 +08:00
}
return callback(null, source, map, dep.module);
});
};
});
});
}
}
module.exports = LoaderPlugin;