vue-di-loader
Version:
Vue Dependency Injection Webpack Loader
167 lines • 6.9 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var path_1 = require("path");
var upath_1 = require("upath");
var fs_1 = require("fs");
var argumenter_1 = require("@joejukan/argumenter");
var web_kit_1 = require("@joejukan/web-kit");
var globalization_1 = require("../globalization");
var _1 = require(".");
var function_1 = require("../function");
var VueDIPlugin = /** @class */ (function () {
function VueDIPlugin() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var argue = new argumenter_1.Argumenter(args);
this.options = argue.object;
if (typeof web_kit_1.access(this, 'options.debug') === 'boolean') {
globalization_1.configuration.verbose = this.options.debug;
}
if (typeof web_kit_1.access(this, 'options.components.deep') === 'boolean') {
globalization_1.configuration.deep = this.options.components.deep;
}
if (typeof web_kit_1.access(this, 'options.components.hot') === 'boolean') {
globalization_1.configuration.hot = this.options.components.hot;
}
}
VueDIPlugin.prototype.apply = function (compiler) {
var _this = this;
compiler.hooks.beforeCompile.tap('VueDIPlugin', function (compilation) {
if (globalization_1.configuration.applied) {
return;
}
else {
globalization_1.configuration.applied = true;
}
_this.log('beforeCompile');
// set dom parsing.
if (web_kit_1.access(_this, 'options.parsers.dom')) {
globalization_1.configuration.domParsing = _this.options.parsers.dom;
}
// inject external pre-compiled components.
if (web_kit_1.access(_this, 'options.externals')) {
_this.external();
}
if (web_kit_1.access(_this, 'options.components.path')) {
var path = _this.options.components.path;
if (Array.isArray(path)) {
path.forEach(function (p) { return _this.pitch(p); });
}
else if (typeof path === 'string') {
_this.pitch(path);
}
}
var entries = new Array();
if (web_kit_1.access(_this, 'options.components.entry')) {
entries = _this.options.components.entry;
}
if (web_kit_1.access(compiler, 'options.entry')) {
entries = compiler.options.entry;
}
// TODO: support Entry and EntryFunc
if (Array.isArray(entries)) {
entries.forEach(function (entry) { return globalization_1.configuration.entries.push(path_1.resolve(entry)); });
}
else if (typeof entries === 'string') {
globalization_1.configuration.entries.push(path_1.resolve(entries));
}
if (web_kit_1.access(_this, 'options.loaders.file')) {
var path = _this.options.loaders.file.path;
if (Array.isArray(path)) {
path.forEach(function (path) { return _this.fileLoader(path); });
}
else if (typeof path === 'string') {
_this.fileLoader(path);
}
}
if (web_kit_1.access(_this, 'options.loaders.sass')) {
var paths = _this.options.loaders.sass.path || [];
paths.forEach(function (path) { return globalization_1.sass.path.push(path); });
}
for (var k in globalization_1.dependencies) {
_this.log("identified dependency: " + k);
}
});
};
VueDIPlugin.prototype.external = function () {
var externals = this.options.externals;
for (var path in externals) {
var components = externals[path];
for (var symbol in components) {
var name_1 = web_kit_1.kebab(symbol);
this.log("injecting component [ name: " + name_1 + ", symbol: " + symbol + ", module: " + path + " ]");
globalization_1.dependencies[name_1] = new _1.DependencyClass(name_1, symbol, path, true);
}
}
};
VueDIPlugin.prototype.pitch = function (path) {
var _this = this;
path = path_1.resolve(path);
var stats = fs_1.statSync(path);
if (stats.isDirectory()) {
var files = fs_1.readdirSync(path);
if (globalization_1.configuration.deep) {
files.forEach(function (file) { return _this.pitch("" + path + path_1.sep + file); });
}
else {
files.forEach(function (file) {
var filePath = "" + path + path_1.sep + file;
var fileStats = fs_1.statSync(filePath);
if (fileStats.isFile()) {
_this.pitch(filePath);
}
});
}
}
else if (stats.isFile()) {
if (/\.vue$/i.test(path)) {
/* set pitched boolean */
if (!globalization_1.configuration.pitched) {
globalization_1.configuration.pitched = true;
}
var ast = new _1.ASTClass();
ast.pitch(path);
}
}
};
VueDIPlugin.prototype.entry = function (path) {
path = path_1.resolve(path);
};
VueDIPlugin.prototype.fileLoader = function (path) {
var _this = this;
var types = web_kit_1.access(this, 'options.loaders.file.type') || [];
if (typeof path !== 'string') {
return;
}
path = path_1.resolve(path);
var files = fs_1.readdirSync(path);
files.forEach(function (file) {
var filePath = "" + path + path_1.sep + file;
var stats = fs_1.statSync(filePath);
if (stats.isDirectory()) {
_this.fileLoader(filePath);
}
else if (stats.isFile()) {
if (Array.isArray(types) && types.length > 0) {
types.forEach(function (type) {
if (new RegExp(type + "$").test(upath_1.basename(filePath))) {
_1.ASTClass.addFile(filePath);
return;
}
});
}
else {
_1.ASTClass.addFile(filePath);
}
}
});
};
VueDIPlugin.prototype.log = function (value) {
function_1.log("[vue-di-plugin] " + value);
};
return VueDIPlugin;
}());
exports.VueDIPlugin = VueDIPlugin;
//# sourceMappingURL=vue.di.plugin.js.map
;