Skip to content

Commit e284336

Browse files
committed
Merge branch 'develop' into master (version 3.1.3)
2 parents 5d7b7dd + fb3cd61 commit e284336

File tree

131 files changed

+8884
-8014
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+8884
-8014
lines changed

components/bin/build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ const config = JSON.parse(fs.readFileSync(process.argv[2] || 'build.json'));
5656
const TARGETS = config.targets || []; // the files to include in the component
5757
const EXCLUDE = new Map((config.exclude || []).map(name => [name, true])); // files to exclude from the component
5858
const EXCLUDESUBDIRS = config.excludeSubdirs === 'true'; // exclude subdirectories
59-
const MATHJAX = config.mathjax || mjPath; // path to the MathJax .js files
59+
const MATHJAX = config.js || config.mathjax || mjPath; // path to the compiled .js files
6060
const LIB = config.lib || './lib'; // path to the lib directory to create
6161
const COMPONENT = path.basename(config.component || 'part'); // name of the component
6262
const GLOBAL = config.global || `../${MATHJAX}/components/global.js`; // the location of global.js
63-
const SRC = MATHJAX.replace(/js$/, 'ts'); // path to the MathJax .ts files
63+
const SRC = config.ts || MATHJAX.replace(/js$/, 'ts'); // path to the .ts files
6464

6565
/**
6666
* The list of files that need to be added to the lib directory

components/bin/makeAll

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,15 @@ if (dirs.length === 0) {
4242
* (on Unix, could be done without the 'node ' prefix, but
4343
* for Windows, these are needed.)
4444
*/
45-
const build = 'node ' + path.join(__dirname, 'build');
46-
const copy = 'node ' + path.join(__dirname, 'copy');
45+
const build = `node '${path.join(__dirname, 'build')}'`;
46+
const copy = `node '${path.join(__dirname, 'copy')}'`;
47+
const pack = `node '${path.join(__dirname, 'pack')}'`;
4748

4849
/**
49-
* Regular expressions for the components directory, the MathJax .js location, and the node_modules directory
50+
* Regular expression for the components directory
5051
*/
51-
const compRE = new RegExp(path.dirname(__dirname).replace(/([\\.{}[\]()?*^$])/g, '\$1'), 'g');
52-
const rootRE = new RegExp(path.join(path.dirname(path.dirname(__dirname)), 'js')
53-
.replace(/([\\.{}[\]()?*^$])/g, '\$1'), 'g');
54-
const nodeRE = new RegExp(path.join(path.dirname(path.dirname(__dirname)), 'node_modules')
55-
.replace(/([\\.{}[\]()?*^$])/g, '\$1'), 'g');
52+
const compRE = new RegExp(path.dirname(__dirname).replace(/([\\.{}[\]()?*^$])/g, '\\$1'));
53+
const dirRE = new RegExp(process.cwd().replace(/([\\.{}[\]()?*^$])/g, '\\$1'));
5654

5755
/**
5856
* Process the contents of an array of directories
@@ -102,7 +100,7 @@ function processSubdirs(dir, action) {
102100
function buildLib(dir) {
103101
const file = path.join(dir, 'build.json');
104102
if (!fs.existsSync(file)) return;
105-
console.info('Building ' + dir.replace(compRE, ''));
103+
console.info('Building ' + dir.replace(compRE, '').replace(dirRE, '.'));
106104
const wd = process.cwd();
107105
try {
108106
process.chdir(dir);
@@ -122,16 +120,12 @@ function buildLib(dir) {
122120
function webpackLib(dir) {
123121
const file = path.join(dir, 'webpack.config.js');
124122
if (!fs.existsSync(file)) return;
125-
console.info('Webpacking ' + dir.replace(compRE, ''));
123+
console.info('Webpacking ' + dir.replace(compRE, '').replace(dirRE, '.'));
126124
const wd = process.cwd();
127125
try {
128126
process.chdir(dir);
129-
const result = execSync('npx webpack --display-modules');
130-
console.info(' ' + String(result).replace(/\n/g, '\n ')
131-
.replace(/ \.\.\//g, ' ' + path.dirname(path.resolve(dir)) + '/')
132-
.replace(compRE, '[components]')
133-
.replace(rootRE, '[js]')
134-
.replace(nodeRE, '[node]'));
127+
const result = execSync(pack);
128+
console.info(' ' + String(result).replace(/\n/g, '\n '));
135129
} catch (err) {
136130
console.info(' ' + err.message);
137131
}

components/bin/pack

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,95 @@
2727

2828
const fs = require('fs');
2929
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+
}
3147

3248
/**
3349
* Regular expressions for the components directory and the MathJax .js location
3450
*/
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+
}
4072

4173
/**
4274
* Run webpack if there is a configuration file for it
4375
*
4476
* @param {string} dir The directory to pack
4577
*/
46-
function webpackLib(dir) {
78+
async function webpackLib(dir) {
4779
try {
4880
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+
}
55117
} catch (err) {
56-
console.error(err.message);
118+
console.error(err);
57119
}
58120
}
59121

components/src/input/mml/mml.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@ if (MathJax.startup) {
66
MathJax.startup.registerConstructor('mml', MathML);
77
MathJax.startup.useInput('mml');
88
}
9+
if (MathJax.loader) {
10+
//
11+
// Install a path-filter to cause loading of an entity file to load all entities,
12+
// since the individual files don't have individual components.
13+
//
14+
MathJax.loader.pathFilters.add((data) => {
15+
data.name = data.name.replace(/\/util\/entities\/.*?\.js/, '/input/mml/entities.js');
16+
return true;
17+
});
18+
}

components/webpack.common.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,44 @@ const TerserPlugin = require('terser-webpack-plugin');
3333
* @return {string} The string with regex special characters escaped
3434
*/
3535
function quoteRE(string) {
36-
return string.replace(/([\\.{}[\]()?*^$])/g, '\$1')
36+
return string.replace(/([\\.{}[\]()?*^$])/g, '\\$1')
3737
}
3838

3939
/**
4040
* Creates the plugin needed for converting mathjax references to component/lib references
4141
*
42-
* @param {string} mathjax The location of the MathJax js files
42+
* @param {string} js The location of the compiled js files
4343
* @param {string[]} lib The component library directories to be linked against
4444
* @param {string} dir The directory of the component being built
4545
* @return {any[]} The plugin array (empty or with the conversion plugin)
4646
*/
47-
const PLUGINS = function (mathjax, libs, dir) {
48-
const mjdir = path.resolve(dir, mathjax);
49-
const mjRE = new RegExp('^' + quoteRE(mjdir + '/'));
47+
const PLUGINS = function (js, libs, dir) {
48+
const mjdir = path.resolve(__dirname, '..', 'js');
49+
const jsdir = path.resolve(dir, js);
50+
const mjRE = new RegExp('^(?:' + quoteRE(jsdir) + '|' + quoteRE(mjdir) + ')' + quoteRE(path.sep));
5051
const root = path.dirname(mjdir);
51-
const rootRE = new RegExp('^' + quoteRE(root + '/'));
52-
const nodeRE = new RegExp('^' + quoteRE(path.dirname(root) + '/'));
52+
const rootRE = new RegExp('^' + quoteRE(root + path.sep));
53+
const nodeRE = new RegExp('^' + quoteRE(path.dirname(root) + path.sep));
54+
55+
//
56+
// Record the js directory for the pack command
57+
//
58+
const plugins = [new webpack.DefinePlugin({jsdir: jsdir})];
5359

54-
const plugins = [];
5560
if (libs.length) {
5661
plugins.push(
5762
//
5863
// Move mathjax references to component libraries
5964
//
6065
new webpack.NormalModuleReplacementPlugin(
61-
/^[^\/].*\.js$/,
66+
/^[^\/]/,
6267
function (resource) {
63-
const request = path.resolve(resource.context, resource.request);
68+
const request = require.resolve(resource.request.charAt(0) === '.' ?
69+
path.resolve(resource.context, resource.request) :
70+
resource.request);
6471
if (!request.match(mjRE)) return;
6572
for (const lib of libs) {
66-
const file = request.replace(mjRE, path.join(root, lib) + '/');
73+
const file = request.replace(mjRE, path.join(root, lib) + path.sep);
6774
if (fs.existsSync(file)) {
6875
resource.request = file;
6976
break;
@@ -78,11 +85,13 @@ const PLUGINS = function (mathjax, libs, dir) {
7885
// Check for packages that should be rerouted to node_modules
7986
//
8087
new webpack.NormalModuleReplacementPlugin(
81-
/^[^\/].*\.js$/,
88+
/^[^\/]$/,
8289
function (resource) {
83-
const request = path.resolve(resource.context, resource.request);
90+
const request = require.resolve(resource.request.charAt(0) === '.' ?
91+
path.resolve(resource.context, resource.request) :
92+
resource.request);
8493
if (request.match(rootRE) || !request.match(nodeRE) || fs.existsSync(request)) return;
85-
const file = request.replace(nodeRE, path.join(root, 'node_modules') + '/');
94+
const file = request.replace(nodeRE, path.join(root, 'node_modules') + path.sep);
8695
if (fs.existsSync(file)) {
8796
resource.request = file;
8897
}
@@ -107,8 +116,8 @@ const MODULE = function (dir) {
107116
return {
108117
// NOTE: for babel transpilation
109118
rules: [{
110-
test: new RegExp(dirRE + '\\/.*\\.js$'),
111-
exclude: new RegExp(quoteRE(path.dirname(__dirname)) + '\\/es5\\/'),
119+
test: new RegExp(dirRE + quoteRE(path.sep) + '.*\\.js$'),
120+
exclude: new RegExp(quoteRE(path.join(path.dirname(__dirname), 'es5') + path.sep)),
112121
use: {
113122
loader: 'babel-loader',
114123
options: {
@@ -123,15 +132,15 @@ const MODULE = function (dir) {
123132
* Create a webpack configuration for a distribution file
124133
*
125134
* @param {string} name The name of the component to create
126-
* @param {string} mathjax The path to the MathJax .js files
135+
* @param {string} js The path to the compiled .js files
127136
* @param {string[]} libs Array of paths to component lib directories to link against
128137
* @param {string} dir The directory of the component buing built
129138
* @param {string} dist The path to the directory where the component .js file will be placed
130-
* (defaults to mathjax/es5)
139+
* (defaults to es5 in the same directory as the js directory)
131140
*/
132-
const PACKAGE = function (name, mathjax, libs, dir, dist) {
141+
const PACKAGE = function (name, js, libs, dir, dist) {
133142
const distDir = dist ? path.resolve(dir, dist) :
134-
path.resolve(path.dirname(mathjax), 'es5', path.dirname(name));
143+
path.resolve(path.dirname(js), 'es5', path.dirname(name));
135144
name = path.basename(name);
136145
return {
137146
name: name,
@@ -140,7 +149,8 @@ const PACKAGE = function (name, mathjax, libs, dir, dist) {
140149
path: distDir,
141150
filename: name + (dist === '.' ? '.min.js' : '.js')
142151
},
143-
plugins: PLUGINS(mathjax, libs, dir),
152+
target: ['web', 'es5'], // needed for IE11 and old browsers
153+
plugins: PLUGINS(js, libs, dir),
144154
module: MODULE(dir),
145155
performance: {
146156
hints: false

0 commit comments

Comments
 (0)