@tarantool.io/lua-bundler-webpack-plugin
Version:
Bundle to single Lua file with content of bundle as big Lua table with next semantic: ``` { { is_entry: Boolean, body: String(content of file), mime: String(mime-type of file), mode: String('plain' or 'base64') } } ```
49 lines (48 loc) • 2.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LuaBundleWebpackPlugin = void 0;
const tslib_1 = require("tslib");
const mime_types_1 = (0, tslib_1.__importDefault)(require("mime-types"));
const webpack_1 = require("webpack");
const helpers_1 = require("./helpers");
const { RawSource } = webpack_1.sources;
const tapFn = (compilation, { bundleName, namespace, entryRegExp }) => (assets) => {
const map = Object.entries(assets).reduce((acc, [fileName, source]) => {
var _a;
const parts = namespace ? fileName.split(namespace + '/') : ['', fileName];
const namespacedFileName = (_a = parts[parts.length - 1]) !== null && _a !== void 0 ? _a : '';
if (parts.length > 1 && namespacedFileName) {
const contentType = mime_types_1.default.lookup(namespacedFileName) || 'application/octet-stream';
const mode = helpers_1.TEXT_MIME_TYPES.includes(contentType) || contentType.startsWith('text/') ? 'plain' : 'base64';
acc.set(namespacedFileName, {
is_entry: entryRegExp.test(namespacedFileName),
body: source.source().toString(mode === 'base64' ? 'base64' : undefined),
mime: contentType,
mode,
});
}
return acc;
}, new Map());
if (map.size > 0) {
compilation.emitAsset(bundleName, new RawSource((0, helpers_1.wrapper)(map)));
}
};
class LuaBundleWebpackPlugin {
constructor(options) {
this.options = Object.assign({
bundleName: 'bundle.lua',
namespace: '',
entryRegExp: /main.+js$/,
}, options);
}
apply(compiler) {
const pluginName = LuaBundleWebpackPlugin.name;
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
compilation.hooks.processAssets.tap({
name: pluginName,
stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
}, tapFn(compilation, this.options));
});
}
}
exports.LuaBundleWebpackPlugin = LuaBundleWebpackPlugin;