webpack-genius
Version:
**For chinese developers:** you'd better add taobao mirror before everything start. Or you may fail to install. ```bash yarn config set registry http://registry.npm.taobao.org ```
191 lines (190 loc) • 7.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BabelHandle = void 0;
var tslib_1 = require("tslib");
var RuleHandle_1 = require("./RuleHandle");
var path_1 = tslib_1.__importDefault(require("path"));
var BabelHandle = /** @class */ (function (_super) {
tslib_1.__extends(BabelHandle, _super);
function BabelHandle() {
return _super !== null && _super.apply(this, arguments) || this;
}
BabelHandle.prototype.onInit = function () {
var _this = this;
_super.prototype.onInit.call(this);
['antd', 'antd-mobile'].forEach(function (item) {
if (_this.genius.hasPackage(item)) {
_this.addBabelPlugin([
'babel-plugin-import',
{
libraryName: item,
libraryDirectory: 'lib',
camel2DashComponentName: true,
style: true,
},
item,
]);
}
});
if (this.genius.isHot() && this.genius.hasPackage('react')) {
this.addBabelPlugin(['react-refresh/babel']);
}
if (this.genius.isHot()) {
this.addBabelPlugin([
path_1.default.join(__dirname, '..', 'misc', 'module-hot.js'),
{
entries: [],
},
]);
}
if (this.genius.hasPackage('@ant-design/icons')) {
this.addBabelPlugin([
'babel-plugin-import',
{
libraryName: '@ant-design/icons',
libraryDirectory: 'lib/icons',
camel2DashComponentName: false,
style: false,
},
'@ant-design/icons',
]);
}
if (this.genius.isBuild() && this.genius.hasPackage('lodash')) {
this.addBabelPlugin(['babel-plugin-lodash']);
}
if (this.genius.hasPackage('react')) {
this.addBabelPreset(['@babel/preset-react', {
runtime: 'automatic'
}]);
}
};
BabelHandle.prototype.addBabelPlugin = function (plugin) {
this.setOptions('babel-loader', function (loader) {
var _a;
(_a = loader.plugins) === null || _a === void 0 ? void 0 : _a.push(plugin);
});
return this;
};
BabelHandle.prototype.addBabelPreset = function (preset) {
this.setOptions('babel-loader', function (loader) {
var _a;
(_a = loader.presets) === null || _a === void 0 ? void 0 : _a.push(preset);
});
return this;
};
BabelHandle.prototype.resetEntries = function () {
var list = [];
var entries = this.genius.getOriginalEntry();
if (entries) {
if (typeof entries === 'object') {
Object.keys(entries).forEach(function (key) {
if (Array.isArray(entries[key])) {
list.push.apply(list, tslib_1.__spread(entries[key]));
}
else {
list.push(entries[key]);
}
});
}
else if (typeof entries === 'string') {
list.push(entries);
}
else if (typeof entries === 'function') {
// TODO implements
}
list = list.map(function (item) { return path_1.default.resolve(item); });
}
this.setOptions('babel-loader', function (loader) {
var _a;
var plugin = (_a = loader.plugins) === null || _a === void 0 ? void 0 : _a.find(function (item) { return item[0].indexOf('module-hot.js') >= 0; });
if (plugin && plugin[1]) {
plugin[1].entries = list;
}
});
};
BabelHandle.prototype.loaders = function () {
return [
{
loader: 'thread-loader',
options: {
workerParallelJobs: 50,
poolTimeout: 2000,
},
},
{
loader: 'babel-loader',
options: {
// Disable file babel.config.js
configFile: false,
// Disable file .babelrc ??
babelrc: false,
cacheCompression: false,
cacheDirectory: this.genius.isHot(),
plugins: [
[
'@babel/plugin-proposal-decorators',
{
decoratorsBeforeExport: true,
},
],
[
'@babel/plugin-proposal-class-properties',
{
loose: false,
},
],
[
'@babel/plugin-proposal-optional-chaining',
{
loose: false,
},
],
[
'@babel/plugin-proposal-nullish-coalescing-operator',
{
loose: false,
},
],
[
'@babel/plugin-transform-runtime',
{
corejs: { version: 3, proposals: true },
helpers: this.genius.isBuild(),
regenerator: true,
useESModules: false,
},
]
],
presets: [
[
'@babel/preset-env',
{
// Use @babel/plugin-transform-runtime instead
useBuiltIns: false,
// Warn: only has an effect when used alongside useBuiltIns: 'usage' or useBuiltIns: 'entry'
// corejs: 3,
loose: true,
modules: false,
},
],
],
},
},
];
};
BabelHandle.prototype.collect = function () {
this.setOptions('babel-loader', function (options) {
var _a, _b;
(_a = options.plugins) === null || _a === void 0 ? void 0 : _a.forEach(function (item) {
item[0] = require.resolve(item[0]);
});
(_b = options.presets) === null || _b === void 0 ? void 0 : _b.forEach(function (item) {
item[0] = require.resolve(item[0]);
});
});
this.resetEntries();
return _super.prototype.collect.call(this);
};
return BabelHandle;
}(RuleHandle_1.RuleHandle));
exports.BabelHandle = BabelHandle;