[cleanup]

This commit is contained in:
2025-08-07 15:20:34 +01:00
parent 5105214a1f
commit 91bf93e3f6

View File

@@ -1,53 +1,25 @@
import wp from 'webpack'; import wp from 'webpack';
import path from 'node:path' import path from 'node:path'
import {createRequire,builtinModules} from 'node:module'
/**
if (process.argv[1] === import.meta.filename) { *
console.log("[SafePack] started"); * @param {string} l library name
main(); * @param {string} moduleLocation module location
console.log("done"); * @param {string} outputPath
} * @returns
*/
function main() { export function wpCompress(l, moduleLocation,outputPath = path.resolve('./output/')) {
const ls = [
'classnames',
'semver',
'ansi-styles',
// 'debug',
// 'supports-color',
'chalk',
'ms',
'minimatch',
'strip-ansi',
'tslib',
'has-flag',
'ansi-regex',
'color-convert',
'color-name',
// 'type-fest',
'string-width'
];
ls.forEach(l => {
wpCompress(l).then(outputFileLocation => {
console.log("[wp] success", outputFileLocation);
}).catch(err => {
console.error("[failed wp]", l);
console.error("[wp] error");
});
});
}
export function wpCompress(l, outputPath = path.resolve('./output/')) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const libraryLocation = import.meta.resolve(l);
const libraryLocation = extractFunctionForModule(l, moduleLocation);
console.log(libraryLocation); console.log(libraryLocation);
const outputFile = l + '.bundle.cjs'; const outputFile = l + '.bundle.cjs';
// throw Error("5"); console.log(`[WebPack] Compressing ${l} in ${moduleLocation} to ${outputFile}`);
const moduleFallbackMap = builtinModules.reduce((prev, current) => {
prev[current] = false;
return prev;
}, {});
wp({ wp({
entry: libraryLocation, entry: libraryLocation,
mode: 'production', mode: 'production',
@@ -55,12 +27,13 @@ export function wpCompress(l, outputPath = path.resolve('./output/')) {
mangleExports: false, mangleExports: false,
avoidEntryIife: true, avoidEntryIife: true,
minimize: false, minimize: false,
moduleIds: 'named',
concatenateModules: true,
}, },
resolve:{ resolve:{
fallback:{ modules: [path.join(moduleLocation,'./node_modules')],
"path": false fallback:moduleFallbackMap
}
}, },
output: { output: {
path: outputPath, path: outputPath,
@@ -84,4 +57,10 @@ export function wpCompress(l, outputPath = path.resolve('./output/')) {
}); });
}); });
} }
function extractFunctionForModule(l, moduleLocation) {
const moduleLocationPath = path.resolve(moduleLocation,'package.json');
const require = createRequire(moduleLocationPath);
const resolved = require.resolve(l)
return resolved;
}