-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathpack
More file actions
executable file
·167 lines (153 loc) · 5.21 KB
/
pack
File metadata and controls
executable file
·167 lines (153 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#! /usr/bin/env node
/*************************************************************
*
* Copyright (c) 2018-2025 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview webpack the component in the current directory
*
* @author dpvc@mathjax.org (Davide Cervone)
*/
const fs = require('fs');
const path = require('path');
const {spawn, execSync} = require('child_process');
/**
* The module type to use ('cjs' or 'mjs')
*/
const target = (process.argv[2] || 'mjs')
/**
* The bundle directory name
*/
const bundle = (process.argv[3] || 'bundle');
/**
* @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 compPath = path.dirname(__dirname);
const mjPath = path.dirname(compPath);
const jsPath = path.join(__dirname, '..', '..', target);
const compRE = fileRegExp(compPath);
const rootRE = fileRegExp(path.dirname(jsPath));
const nodeRE = /^.*\/node_modules/;
const fontRE = new RegExp('^.*\\/(mathjax-[^\/-]*)(?:-font)?\/(build|[cm]js)');
/**
* Find the directory where npx runs (so we know where "npx webpack" will run)
* (We use npx rather than pnpm here as it seems that pnpm doesn't
* find the executable from a node_modules directory higher than the
* first package.json, and extensions and fonts can have their own
* package.json.)
*/
const packDir = String(execSync('npx node -e "console.log(process.cwd())"'));
/**
* @param {string} dir The directory to pack
* @return {JSON} The parsed JSON from webpack
*/
async function readJSON(dir) {
return new Promise((ok, fail) => {
const buffer = [];
const child = spawn('npx', [
'webpack', '--env', `dir=${path.relative(packDir, path.resolve(dir))}`,
'--env', `bundle=${bundle}`, '--json',
'-c', path.relative(packDir, path.join(compPath, 'webpack.config.' + target))
]);
child.stdout.on('data', (data) => buffer.push(String(data)));
child.stderr.on('data', (data) => console.error(String(data)));
child.on('close', (code) => {
if (code !== 0) {
fail('Webpack failed with code ' + code);
return;
}
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
*/
async function webpackLib(dir) {
try {
const dirRE = fileRegExp(path.resolve(dir));
//
// Get js directory from the webpack.config.js file
//
let config = require(path.resolve(dir, 'config.json')).webpack;
if (!config) return;
const jsdir = (config.js ? path.resolve(dir, config.js).replace(/js$/, target) : jsPath);
const jsRE = fileRegExp(jsdir);
const jsRE2 = (config.js ? fileRegExp(path.resolve(dir, config.js)) : /^$/);
const libRE = fileRegExp(path.resolve(jsdir, '..', 'components'));
//
// Get the json from webpack and print the asset name and size
//
const json = await readJSON(dir);
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, '.');
}
const list = [];
for (const module of modules) {
if (module.moduleType.match(/javascript|json/)) {
let name = (module.nameForCondition || module.name)
.replace(compRE, '[components]')
.replace(nodeRE, '[node]')
.replace(rootRE, '[mathjax]')
.replace(fontRE, '[$1]/$2')
.replace(jsRE, '[js]')
.replace(jsRE2, '[js]')
.replace(libRE, '[build]');
if (name.charAt(0) !== '.' && name.charAt(0) !== '[') {
name = path.relative(dir, name);
}
list.push(' ' + name + fileSize(module));
}
}
console.log(
list
.filter(a => a.slice(2, 4) === './').sort()
.concat(list.filter(a => a.slice(2, 4) !== './').sort())
.join('\n')
);
} catch (err) {
console.error(err);
}
}
webpackLib(process.argv[4] || '.');