@builder/webpack-plugin-swc
Version:
swc module loader for webpack
165 lines • 9.23 kB
JavaScript
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SWCMinifyPlugin = void 0;
var swc_1 = require("@builder/swc");
var webpack_sources_1 = require("webpack-sources");
var version = require('../package.json').version;
var isWebpack5 = function (compilation) { return 'processAssets' in compilation.hooks; };
var isJsFile = /\.[cm]?js(\?.*)?$/i;
var pluginName = 'swc-minify';
var SWCMinifyPlugin = /** @class */ (function () {
function SWCMinifyPlugin(options) {
if (options === void 0) { options = {}; }
var sync = options.sync, restOptions = __rest(options, ["sync"]);
this.sync = sync;
this.options = restOptions;
if (typeof options.minify === 'undefined') {
this.options.minify = true;
}
}
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
SWCMinifyPlugin.prototype.apply = function (compiler) {
var _this = this;
var meta = JSON.stringify({ name: pluginName, version: version, options: this.options });
compiler.hooks.compilation.tap(pluginName, function (compilation) {
compilation.hooks.chunkHash.tap(pluginName, function (_, hash) { return hash.update(meta); });
var tapMethod = _this.sync ? 'tap' : 'tapPromise';
if (isWebpack5(compilation)) {
compilation.hooks.processAssets[tapMethod]({
name: pluginName,
stage: compilation.constructor.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
additionalAssets: true,
}, function () { return _this.transformAssets(compilation); });
compilation.hooks.statsPrinter.tap(pluginName, function (statsPrinter) {
statsPrinter.hooks.print
.for('asset.info.minimized')
.tap(pluginName, function (minimized, _a) {
var green = _a.green, formatFlag = _a.formatFlag;
return minimized ? green(formatFlag('minimized')) : undefined;
});
});
}
else {
compilation.hooks.optimizeChunkAssets[tapMethod](pluginName, function () { return _this.transformAssets(compilation); });
}
});
};
SWCMinifyPlugin.prototype.transformAssets = function (compilation) {
return __awaiter(this, void 0, void 0, function () {
var devtool, sourcemap, assets;
return __generator(this, function (_a) {
devtool = compilation.compiler.options.devtool;
sourcemap = !!devtool;
assets = compilation.getAssets().filter(function (asset) { return !asset.info.minimized && isJsFile.test(asset.name); });
if (this.sync) {
return [2 /*return*/, this.processAssetsSync(assets, sourcemap, compilation)];
}
else {
return [2 /*return*/, this.processAssets(assets, sourcemap, compilation)];
}
return [2 /*return*/];
});
});
};
SWCMinifyPlugin.prototype.processAssetsSync = function (assets, sourcemap, compilation) {
var _this = this;
assets.forEach(function (asset) {
var _a = asset.source.sourceAndMap(), source = _a.source, map = _a.map;
var sourceAsString = source.toString();
var result = (0, swc_1.transformSync)(sourceAsString, __assign(__assign({}, _this.options), { sourceMaps: sourcemap, sourceFileName: asset.name }));
compilation.updateAsset(asset.name, sourcemap
? new webpack_sources_1.SourceMapSource(result.code, asset.name, result.map, sourceAsString, map, true)
: new webpack_sources_1.RawSource(result.code), __assign(__assign({}, asset.info), { minimized: true }));
});
};
SWCMinifyPlugin.prototype.processAssets = function (assets, sourcemap, compilation) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.all(assets.map(function (asset) { return __awaiter(_this, void 0, void 0, function () {
var _a, source, map, sourceAsString, result;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = asset.source.sourceAndMap(), source = _a.source, map = _a.map;
sourceAsString = source.toString();
return [4 /*yield*/, (0, swc_1.transform)(sourceAsString, __assign(__assign({}, this.options), { sourceMaps: sourcemap, sourceFileName: asset.name }))];
case 1:
result = _b.sent();
compilation.updateAsset(asset.name, sourcemap
? new webpack_sources_1.SourceMapSource(result.code, asset.name, result.map, sourceAsString, map, true)
: new webpack_sources_1.RawSource(result.code), __assign(__assign({}, asset.info), { minimized: true }));
return [2 /*return*/];
}
});
}); }))];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
return SWCMinifyPlugin;
}());
exports.SWCMinifyPlugin = SWCMinifyPlugin;
//# sourceMappingURL=index.js.map
;