|
27 | 27 |
|
28 | 28 | const fs = require('fs'); |
29 | 29 | const path = require('path'); |
30 | | -const {execSync} = require('child_process'); |
| 30 | +const {spawn} = require('child_process'); |
| 31 | + |
| 32 | +/** |
| 33 | + * @param {string} name The file name to turn into a Regular expression |
| 34 | + * @return {RegExp} The regular expression for the name, |
| 35 | + */ |
| 36 | +function fileRegExp(name) { |
| 37 | + return new RegExp(name.replace(/([\\.{}[\]()?*^$])/g, '\\$1'), 'g'); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * @param {Object} The file or asset data whose size is to be returned |
| 42 | + * @return {string} The string giving the size in KB |
| 43 | + */ |
| 44 | +function fileSize(file) { |
| 45 | + return ' (' + (file.size / 1024).toFixed(2).replace(/\.?0+$/, '') + ' KB)'; |
| 46 | +} |
31 | 47 |
|
32 | 48 | /** |
33 | 49 | * Regular expressions for the components directory and the MathJax .js location |
34 | 50 | */ |
35 | | -const compRE = new RegExp(path.dirname(__dirname).replace(/([\\.{}[\]()?*^$])/g, '\\$1'), 'g'); |
36 | | -const rootRE = new RegExp(path.join(path.dirname(path.dirname(__dirname)), 'js') |
37 | | - .replace(/([\\.{}[\]()?*^$])/g, '\\$1'), 'g'); |
38 | | -const nodeRE = new RegExp(path.join(path.dirname(path.dirname(__dirname)), 'node_modules') |
39 | | - .replace(/([\\.{}[\]()?*^$])/g, '\\$1'), 'g'); |
| 51 | +const compRE = fileRegExp(path.dirname(__dirname)); |
| 52 | +const rootRE = fileRegExp(path.join(path.dirname(path.dirname(__dirname)), 'js')); |
| 53 | +const nodeRE = fileRegExp(path.join(path.dirname(path.dirname(__dirname)), 'node_modules')); |
| 54 | + |
| 55 | +/** |
| 56 | + * @return {JSON} The parsed JSON from webpack |
| 57 | + */ |
| 58 | +async function readJSON() { |
| 59 | + return new Promise((ok, fail) => { |
| 60 | + const buffer = []; |
| 61 | + const child = spawn('npx', ['webpack', '--json']); |
| 62 | + child.stdout.on('data', (data) => buffer.push(String(data))); |
| 63 | + child.stdout.on('close', (code) => { |
| 64 | + const json = JSON.parse(buffer.join('')); |
| 65 | + if (json.errors && json.errors.length) { |
| 66 | + fail(json.errors[0].message); |
| 67 | + } |
| 68 | + ok(json); |
| 69 | + }); |
| 70 | + }); |
| 71 | +} |
40 | 72 |
|
41 | 73 | /** |
42 | 74 | * Run webpack if there is a configuration file for it |
43 | 75 | * |
44 | 76 | * @param {string} dir The directory to pack |
45 | 77 | */ |
46 | | -function webpackLib(dir) { |
| 78 | +async function webpackLib(dir) { |
47 | 79 | try { |
48 | 80 | process.chdir(dir); |
49 | | - const result = execSync('npx webpack --display-modules'); |
50 | | - console.info(String(result).replace(/\n/g, '\n ') |
51 | | - .replace(/ \.\.\//g, ' ' + path.dirname(path.resolve(dir)) + '/') |
52 | | - .replace(compRE, '[components]') |
53 | | - .replace(rootRE, '[js]') |
54 | | - .replace(nodeRE, '[node]')); |
| 81 | + const dirRE = fileRegExp(path.resolve(dir)); |
| 82 | + |
| 83 | + // |
| 84 | + // Get js directory from the webpack.config.js file |
| 85 | + // |
| 86 | + const jsdir = require(path.resolve(dir, 'webpack.config.js')).plugins[0].definitions.jsdir; |
| 87 | + const jsRE = fileRegExp(jsdir); |
| 88 | + const libRE = fileRegExp(path.resolve(jsdir, '..', 'components')); |
| 89 | + |
| 90 | + // |
| 91 | + // Get the json from webpack and print the asset name and size |
| 92 | + // |
| 93 | + const json = await readJSON(); |
| 94 | + for (const asset of json.assets) { |
| 95 | + console.log(asset.name + fileSize(asset)); |
| 96 | + } |
| 97 | + // |
| 98 | + // Sort the modules and print their names and sizes |
| 99 | + // |
| 100 | + const modules = json.modules; |
| 101 | + for (const module of modules) { |
| 102 | + module.name = path.resolve(dir, module.name) |
| 103 | + .replace(/ \+ \d+ modules/, '') |
| 104 | + .replace(dirRE, '.'); |
| 105 | + } |
| 106 | + for (const module of modules.sort((a,b) => a.name < b.name ? -1 : 1)) { |
| 107 | + if (module.moduleType.match(/javascript/)) { |
| 108 | + const name = module.name |
| 109 | + .replace(compRE, '[components]') |
| 110 | + .replace(rootRE, '[mathjax]') |
| 111 | + .replace(nodeRE, '[node]') |
| 112 | + .replace(jsRE, '[js]') |
| 113 | + .replace(libRE, '[lib]'); |
| 114 | + console.log(' ' + name + fileSize(module)); |
| 115 | + } |
| 116 | + } |
55 | 117 | } catch (err) { |
56 | | - console.error(err.message); |
| 118 | + console.error(err); |
57 | 119 | } |
58 | 120 | } |
59 | 121 |
|
|
0 commit comments