@regru/webpack-babel-multi-target-plugin
Version:
A Webpack plugin that works with Babel to allow deployment of ES2015 builds targeted to modern browsers, with an ES5 fallback for legacy browsers.
68 lines • 3.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const babel_multi_target_html_updater_1 = require("./babel.multi.target.html.updater");
const babel_target_1 = require("./babel-target");
const babel_target_entry_option_plugin_1 = require("./babel.target.entry.option.plugin");
const defaults_1 = require("./defaults");
const normalize_css_chunks_plugin_1 = require("./normalize.css.chunks.plugin");
const normalize_module_ids_plugin_1 = require("./normalize.module.ids.plugin");
const safari_nomodule_fix_plugin_1 = require("./safari-nomodule-fix/safari.nomodule.fix.plugin");
const targeting_plugin_1 = require("./targeting.plugin");
exports.BABEL_LOADER = 'babel-loader';
class BabelMultiTargetPlugin {
constructor(options = {}) {
if (!options.babel) {
options.babel = {};
}
if (!options.babel.plugins) {
options.babel.plugins = [];
}
if (!options.babel.presetOptions) {
options.babel.presetOptions = {};
}
if (!options.targets) {
options.targets = defaults_1.DEFAULT_TARGET_INFO;
}
if (!options.exclude) {
options.exclude = [];
}
this.options = options;
const targetFactory = new babel_target_1.BabelTargetFactory(options.babel.presetOptions, options.babel.plugins);
this.targets = Object.keys(options.targets)
.reduce((result, profileName) => {
const targetOptions = options.targets[profileName];
result.push(targetFactory.createBabelTarget(profileName, targetOptions, { cacheDirectory: options.babel.cacheDirectory }));
return result;
}, []);
if (!this.targets.length) {
throw new Error('Must provide at least one target');
}
if (this.targets.filter(target => target.tagAssetsWithKey === false).length > 1) {
throw new Error('Only one target may have the `tagAssetsWithKey` property set to `false`');
}
if (this.targets.filter(target => target.esModule).length > 1) {
throw new Error('Only one target may have the `esModule` property set to `true`');
}
if (this.targets.filter(target => target.noModule).length > 1) {
throw new Error('Only one target may have the `noModule` property set to `true`');
}
}
apply(compiler) {
// magic starts here!
new babel_target_entry_option_plugin_1.BabelTargetEntryOptionPlugin(this.targets).apply(compiler);
new targeting_plugin_1.TargetingPlugin(this.targets, this.options.exclude, this.options.doNotTarget, compiler.options.externals).apply(compiler);
new normalize_css_chunks_plugin_1.NormalizeCssChunksPlugin(this.targets).apply(compiler);
new babel_multi_target_html_updater_1.BabelMultiTargetHtmlUpdater(this.targets).apply(compiler);
if (this.options.safari10NoModuleFix) {
new safari_nomodule_fix_plugin_1.SafariNoModuleFixPlugin(this.options.safari10NoModuleFix).apply(compiler);
}
if (this.options.normalizeModuleIds) {
new normalize_module_ids_plugin_1.NormalizeModuleIdsPlugin().apply(compiler);
}
}
static loader(loader = exports.BABEL_LOADER) {
return targeting_plugin_1.TargetingPlugin.loader(loader);
}
}
exports.BabelMultiTargetPlugin = BabelMultiTargetPlugin;
//# sourceMappingURL=babel.multi.target.plugin.js.map