| 1 |
#! /usr/bin/env node |
| 2 |
|
| 3 |
|
| 4 |
/************************************************************* |
| 5 |
* |
| 6 |
* Copyright (c) 2018 The MathJax Consortium |
| 7 |
* |
| 8 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
* you may not use this file except in compliance with the License. |
| 10 |
* You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, software |
| 15 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
* See the License for the specific language governing permissions and |
| 18 |
* limitations under the License. |
| 19 |
*/ |
| 20 |
|
| 21 |
/** |
| 22 |
* @fileoverview webpack the component in the current directory |
| 23 |
* |
| 24 |
* @author [email protected] (Davide Cervone) |
| 25 |
*/ |
| 26 |
|
| 27 |
|
| 28 |
const fs = require('fs'); |
| 29 |
const path = require('path'); |
| 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 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Regular expressions for the components directory and the MathJax .js location |
| 50 |
*/ |
| 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 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Run webpack if there is a configuration file for it |
| 75 |
* |
| 76 |
* @param {string} dir The directory to pack |
| 77 |
*/ |
| 78 |
async function webpackLib(dir) { |
| 79 |
try { |
| 80 |
process.chdir(dir); |
| 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 |
const list = []; |
| 107 |
for (const module of modules) { |
| 108 |
if (module.moduleType.match(/javascript/)) { |
| 109 |
let name = module.name |
| 110 |
.replace(compRE, '[components]') |
| 111 |
.replace(rootRE, '[mathjax]') |
| 112 |
.replace(nodeRE, '[node]') |
| 113 |
.replace(jsRE, '[js]') |
| 114 |
.replace(libRE, '[lib]'); |
| 115 |
if (name.charAt(0) !== '.' && name.charAt(0) !== '[') { |
| 116 |
name = path.relative(dir, name); |
| 117 |
} |
| 118 |
list.push(' ' + name + fileSize(module)); |
| 119 |
} |
| 120 |
} |
| 121 |
console.log( |
| 122 |
list |
| 123 |
.filter(a => a.slice(2, 4) === './').sort() |
| 124 |
.concat(list.filter(a => a.slice(2, 4) !== './').sort()) |
| 125 |
.join('\n') |
| 126 |
); |
| 127 |
} catch (err) { |
| 128 |
console.error(err); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
webpackLib(process.argv[2] || '.'); |