pilet-webpack-plugin
Version:
Webpack plugin for generating a valid pilet bundle.
58 lines • 2.14 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getVariables = getVariables;
exports.setEnvironment = setEnvironment;
exports.getDefineVariables = getDefineVariables;
exports.getDependencies = getDependencies;
exports.withExternals = withExternals;
function getVariables(name, version, env) {
return {
NODE_ENV: env,
BUILD_TIME: new Date().toDateString(),
BUILD_TIME_FULL: new Date().toISOString(),
BUILD_PCKG_VERSION: version,
BUILD_PCKG_NAME: name,
};
}
function setEnvironment(variables) {
Object.keys(variables).forEach(key => (process.env[key] = variables[key]));
}
function getDefineVariables(variables) {
return Object.entries(variables).reduce((obj, [name, value]) => {
obj[`process.env.${name}`] = JSON.stringify(value);
return obj;
}, {});
}
function getDependencies(importmap, compilerOptions) {
const dependencies = {};
const { entry, externals } = compilerOptions;
if (typeof entry === 'object' && entry && Array.isArray(externals) && typeof externals[0] === 'object') {
for (const dep of importmap) {
dependencies[dep.id] = dep.ref;
externals[0][dep.name] = dep.requireId || dep.id;
if (dep.type === 'local') {
entry[dep.ref.replace(/\.js$/, '')] = dep.entry;
}
}
}
return dependencies;
}
function withExternals(compilerOptions, externals) {
const current = compilerOptions.externals || [];
const arrayExternals = Array.isArray(current) ? current : [current];
const objectExternal = externals.reduce((external, dep) => {
external[dep] = dep;
return external;
}, {});
const newExternals = arrayExternals.filter(external => {
if (typeof external === 'object' && Object.keys(external).length) {
for (const dep in external) {
objectExternal[dep] = external[dep];
}
return false;
}
return true;
});
compilerOptions.externals = [objectExternal, ...newExternals];
}
//# sourceMappingURL=helpers.js.map
;