webpack/lib/MultiModule.js

73 lines
1.7 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2017-03-10 19:19:33 +08:00
"use strict";
2017-03-10 19:19:33 +08:00
const Module = require("./Module");
const Template = require("./Template");
2017-03-10 19:19:33 +08:00
const RawSource = require("webpack-sources").RawSource;
class MultiModule extends Module {
2017-03-10 19:19:33 +08:00
constructor(context, dependencies, name) {
2017-10-30 20:56:57 +08:00
super("javascript/dynamic");
// Info from Factory
2017-03-10 19:19:33 +08:00
this.context = context;
this.dependencies = dependencies;
this.name = name;
}
2017-03-10 19:19:33 +08:00
identifier() {
return `multi ${this.dependencies.map((d) => d.request).join(" ")}`;
}
2017-03-10 19:19:33 +08:00
readableIdentifier(requestShortener) {
return `multi ${this.dependencies.map((d) => requestShortener.shorten(d.request)).join(" ")}`;
}
2017-03-10 19:19:33 +08:00
build(options, compilation, resolver, fs, callback) {
this.built = true;
return callback();
}
2017-03-10 19:19:33 +08:00
needRebuild() {
return false;
}
2017-03-10 19:19:33 +08:00
size() {
return 16 + this.dependencies.length * 12;
}
2017-03-10 19:19:33 +08:00
updateHash(hash) {
hash.update("multi module");
hash.update(this.name || "");
super.updateHash(hash);
}
2017-03-10 19:19:33 +08:00
source(dependencyTemplates, outputOptions) {
const str = [];
2017-11-08 18:32:05 +08:00
this.dependencies.forEach((dep, idx) => {
2017-03-10 19:19:33 +08:00
if(dep.module) {
if(idx === this.dependencies.length - 1)
str.push("module.exports = ");
str.push("__webpack_require__(");
if(outputOptions.pathinfo)
str.push(Template.toComment(dep.request));
2017-03-10 19:19:33 +08:00
str.push(`${JSON.stringify(dep.module.id)}`);
str.push(")");
} else {
2017-11-08 18:32:05 +08:00
// TODO replace with WebpackMissingModule module
2017-03-10 19:19:33 +08:00
str.push("(function webpackMissingModule() { throw new Error(");
str.push(JSON.stringify(`Cannot find module "${dep.request}"`));
str.push("); }())");
}
str.push(";\n");
});
2017-03-10 19:19:33 +08:00
return new RawSource(str.join(""));
}
}
module.exports = MultiModule;