Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pack and makeAll to display the modules #591

Merged
merged 2 commits into from Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 5 additions & 11 deletions components/bin/makeAll
Expand Up @@ -44,15 +44,13 @@ if (dirs.length === 0) {
*/
const build = 'node ' + path.join(__dirname, 'build');
const copy = 'node ' + path.join(__dirname, 'copy');
const pack = 'node ' + path.join(__dirname, 'pack');

/**
* Regular expressions for the components directory, the MathJax .js location, and the node_modules directory
* Regular expression for the components directory
*/
const compRE = new RegExp(path.dirname(__dirname).replace(/([\\.{}[\]()?*^$])/g, '\$1'), 'g');
const rootRE = new RegExp(path.join(path.dirname(path.dirname(__dirname)), 'js')
.replace(/([\\.{}[\]()?*^$])/g, '\$1'), 'g');
const nodeRE = new RegExp(path.join(path.dirname(path.dirname(__dirname)), 'node_modules')
.replace(/([\\.{}[\]()?*^$])/g, '\$1'), 'g');


/**
* Process the contents of an array of directories
Expand Down Expand Up @@ -126,12 +124,8 @@ function webpackLib(dir) {
const wd = process.cwd();
try {
process.chdir(dir);
const result = execSync('npx webpack --display-modules');
console.info(' ' + String(result).replace(/\n/g, '\n ')
.replace(/ \.\.\//g, ' ' + path.dirname(path.resolve(dir)) + '/')
.replace(compRE, '[components]')
.replace(rootRE, '[js]')
.replace(nodeRE, '[node]'));
const result = execSync(pack);
console.info(' ' + String(result).replace(/\n/g, '\n '));
} catch (err) {
console.info(' ' + err.message);
}
Expand Down
80 changes: 66 additions & 14 deletions components/bin/pack
Expand Up @@ -27,33 +27,85 @@

const fs = require('fs');
const path = require('path');
const {execSync} = require('child_process');
const {spawn} = require('child_process');

/**
* @param {string} name The file name to turn into a Regular expression
* @return {RegExp} The regular expression for the name,
*/
function fileRegExp(name) {
return new RegExp(name.replace(/([\\.{}[\]()?*^$])/g, '\\$1'), 'g');
}

/**
* @param {Object} The file or asset data whose size is to be returned
* @return {string} The string giving the size in KB
*/
function fileSize(file) {
return ' (' + (file.size / 1024).toFixed(2).replace(/\.?0+$/, '') + ' KB)';
}

/**
* Regular expressions for the components directory and the MathJax .js location
*/
const compRE = new RegExp(path.dirname(__dirname).replace(/([\\.{}[\]()?*^$])/g, '\\$1'), 'g');
const rootRE = new RegExp(path.join(path.dirname(path.dirname(__dirname)), 'js')
.replace(/([\\.{}[\]()?*^$])/g, '\\$1'), 'g');
const nodeRE = new RegExp(path.join(path.dirname(path.dirname(__dirname)), 'node_modules')
.replace(/([\\.{}[\]()?*^$])/g, '\\$1'), 'g');
const compRE = fileRegExp(path.dirname(__dirname));
const rootRE = fileRegExp(path.join(path.dirname(path.dirname(__dirname)), 'js'));
const nodeRE = fileRegExp(path.join(path.dirname(path.dirname(__dirname)), 'node_modules'));

/**
* @return {JSON} The parsed JSON from webpack
*/
async function readJSON() {
return new Promise((ok, fail) => {
const buffer = [];
const child = spawn('npx', ['webpack', '--json']);
child.stdout.on('data', (data) => buffer.push(String(data)));
child.stdout.on('close', (code) => {
const json = JSON.parse(buffer.join(''));
if (json.errors && json.errors.length) {
fail(json.errors[0].message);
}
ok(json);
});
});
}

/**
* Run webpack if there is a configuration file for it
*
* @param {string} dir The directory to pack
*/
function webpackLib(dir) {
async function webpackLib(dir) {
try {
process.chdir(dir);
const result = execSync('npx webpack --display-modules');
console.info(String(result).replace(/\n/g, '\n ')
.replace(/ \.\.\//g, ' ' + path.dirname(path.resolve(dir)) + '/')
.replace(compRE, '[components]')
.replace(rootRE, '[js]')
.replace(nodeRE, '[node]'));
const dirRE = fileRegExp(path.resolve(dir));
//
// Get the json from webpack and print the asset name and size
//
const json = await readJSON();
for (const asset of json.assets) {
console.log(asset.name + fileSize(asset));
}
//
// Sort the modules and print their names and sizes
//
const modules = json.modules;
for (const module of modules) {
module.name = path.resolve(dir, module.name)
.replace(/ \+ \d+ modules/, '')
.replace(dirRE, '.');
}
for (const module of modules.sort((a,b) => a.name < b.name ? -1 : 1)) {
if (module.moduleType.match(/javascript/)) {
const name = module.name
.replace(compRE, '[components]')
.replace(rootRE, '[js]')
.replace(nodeRE, '[node]');
console.log(' ' + name + fileSize(module));
}
}
} catch (err) {
console.error(err.message);
console.error(err);
}
}

Expand Down