UNPKG

@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') } } ```

74 lines (72 loc) 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapper = exports.pack = exports.luaEscape = exports.obj = exports.TEXT_MIME_TYPES = exports.COMMA_WITH_NL = void 0; exports.COMMA_WITH_NL = ',\n'; exports.TEXT_MIME_TYPES = [ 'text/plain', 'text/html', 'text/css', 'application/javascript', 'application/json', 'image/svg+xml', ]; const obj = (value) => { return '{\n' + value + '\n}'; }; exports.obj = obj; const luaEscape = (value) => value .replace(/[\\"']/g, '\\$&') // eslint-disable-next-line no-control-regex .replace(/\u0000/g, '\\0') .replace(/\n/g, '\\\n'); exports.luaEscape = luaEscape; // eslint-disable-next-line @typescript-eslint/ban-types const pack = (value) => { if (value === null) { return 'nil'; } if (Array.isArray(value)) { return (0, exports.obj)(value.map(exports.pack).join(exports.COMMA_WITH_NL)); } switch (typeof value) { case 'object': { const data = Object.entries(value) .map(([key, value]) => `[${(0, exports.pack)(key)}] = ${(0, exports.pack)(value)}`) .join(exports.COMMA_WITH_NL); return (0, exports.obj)(data); } case 'string': return `"${(0, exports.luaEscape)(value)}"`; case 'undefined': case 'bigint': case 'boolean': return `${value}`; case 'number': { if (Number.isNaN(value)) { return 'tonumber("NaN")'; } if (value === Infinity) { return 'tonumber("+inf")'; } if (value === -Infinity) { return 'tonumber("-inf")'; } return `${value}`; } default: throw new Error(`Can not pack ${typeof value} ${JSON.stringify(value)}`); } }; exports.pack = pack; const wrapper = (value) => { const data = Array.from(value) .map(([key, item]) => `[${(0, exports.pack)(key)}] = ${(0, exports.pack)(item)}`) .join(exports.COMMA_WITH_NL); return `local data = ${(0, exports.obj)(data)} return { __data = function() return data end }`; }; exports.wrapper = wrapper;