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

Make ES6 (ESM) and ES5 (CommonJS) versions, and make ES6 the default. #954

Merged
merged 21 commits into from
Jun 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f00faba
Changes to support an ES6 version as well as ES5
dpvc Apr 22, 2023
6fb6c72
Make ES6 version the default, and make all browser/node/source/packed…
dpvc May 5, 2023
510394e
Remove modules directory now that all packages have ES6 versions, and…
dpvc May 23, 2023
d12037d
Merge branch 'v4-update' into es6-modules
dpvc May 23, 2023
af41086
Remove modules from files list and correct/add comments
dpvc May 23, 2023
727915b
Use #root and #mml3 for access to es version-specific code
dpvc May 23, 2023
780a5c8
Use #js in components to select js5 or js6
dpvc May 24, 2023
bc9fcc5
use src5/src6 to handle components without need for babel or esm
dpvc May 25, 2023
07dbf92
Remove babel and esm packages and update mhchemparser
dpvc May 25, 2023
bcbf25b
Rename js5 to cjs, js6 to mjs, and es6 to bundle, removing es5, with …
dpvc May 26, 2023
37d0028
Update build scripts for cjs
dpvc May 26, 2023
431a5cb
Make node-main be webpackable and fix problem with Symbol class in in…
dpvc May 26, 2023
ef7cfa2
Move tsconfig-* to a tsconfig directory and use cjs/mjs for fonts
dpvc May 28, 2023
b91a37e
Add some missing comments, and fix a reference to es6
dpvc May 28, 2023
739559f
Remove uneeded AsciiMath files and fix its root, fix some comments, a…
dpvc May 29, 2023
8184f76
Use mathjax-modern version beta.5
dpvc Jun 2, 2023
735f58f
Fixes paths to sre es-modules and updates to 4.1.0-beta.5.
zorkow Jun 2, 2023
79454ab
Updates package-lock.json.
zorkow Jun 2, 2023
f6d2a79
Rebuilt package-lock
zorkow Jun 2, 2023
ce7def2
Merge pull request #964 from mathjax/es6-modules-sre-fixes
zorkow Jun 2, 2023
c99c569
Update tsconfig/mjs.json
dpvc Jun 3, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 7 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
*~
node_modules
components/src/**/lib
js
es5
/node_modules
/components/mjs/**/lib
/components/cjs
/cjs
/mjs
/bundle
/bundle-cjs
65 changes: 47 additions & 18 deletions components/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,31 @@ const INDENT = ' ';
/**
* The pattern to use when looking for explort commands to process
*/
const EXPORTPATTERN = /(^export(?:\s+default)?(?:\s+abstract)?\s+[^ {*}]+\s+[a-zA-Z0-9_.$]+)/m;
const EXPORTPATTERN =
/(^export(?:\s+default)?(?:\s+abstract)?\s+(?:[^ {*}]+\s+(?:enum\s+)?[a-zA-Z0-9_.$]+|\{.* as .*\}))/m;

const EXPORT_IGNORE = ['type', 'interface'];
const EXPORT_PROCESS = ['let', 'const', 'var', 'function', 'class', 'namespace'];
const EXPORT_PROCESS = ['let', 'const', 'var', 'function', 'class', 'namespace', 'as'];

/**
* The relative path to the MathJax directory
* The module type to use ('cjs' or 'mjs')
*/
const mjPath = path.relative(process.cwd(), path.resolve(__dirname, '..', '..', 'js'));
const mjGlobal = path.join('..', mjPath, 'components', 'global.js');
const target = (process.argv[2] || 'mjs');

/**
* Read the configuration for the component
*/
const config = JSON.parse(fs.readFileSync(process.argv[2] || 'build.json'));
const json = path.resolve(process.argv[3] || '.', 'config.json');
const config = require(json).build;
if (!config) process.exit()
process.chdir(path.dirname(json));


/**
* The relative path to the MathJax directory
*/
const mjPath = path.relative(process.cwd(), path.resolve(__dirname, '..', '..', target));
const mjGlobal = path.join('..', mjPath, 'components', 'global.js');

function getType() {
const component = config.component || 'part';
Expand All @@ -66,14 +76,15 @@ const COMPONENT = path.basename(config.component || 'part'); // n
const ID = config.id || config.component || 'part'; // the ID of the component
const TARGETS = config.targets || []; // the files to include in the component
const EXCLUDE = new Map((config.exclude || []).map(name => [name, true])); // files to exclude from the component
const EXCLUDESUBDIRS = config.excludeSubdirs === 'true'; // exclude subdirectories or not
const EXCLUDESUBDIRS = !!config.excludeSubdirs; // exclude subdirectories or not
const JS = config.js || config.mathjax || mjPath; // path to the compiled .js files
const LIB = config.lib || './lib'; // path to the lib directory to create
const TS = config.ts || JS.replace(/js$/, 'ts'); // path to the .ts files
const TS = config.ts || JS.replace(/[cm]js$/, 'ts'); // path to the .ts files
const GLOBAL = config.global || mjGlobal; // path to the global.js file
const VERSION = config.version || mjGlobal.replace(/global/, 'version'); // path to the version.js file
const TYPE = config.type || getType(); // the module type
const PREFIX = config.prefix || ''; // directories to add to any ts files

/**
* The list of files that need to be added to the lib directory
*/
Expand Down Expand Up @@ -147,8 +158,9 @@ function processParts(parts) {
const objects = [];
for (let i = 1; i < parts.length; i += 2) {
const words = parts[i].split(/\s+/);
const type = words[words.length - 2];
const name = words[words.length - 1];
const n = words.length;
const type = (words[n - 2] === 'enum' ? words[n - 3] : words[n - 2]);
const name = words[n - 1].replace(/\}$/, '');

if (words[1] === 'default' || type === 'default') {
objects.push('default');
Expand All @@ -175,15 +187,21 @@ function processLines(file, objects) {
const dots = dir.replace(/[^\/]+/g, '..') || '.';
const relative = path.join(dots, '..', JS, dir, path.basename(file)).replace(/\.ts$/, '.js');
const name = path.parse(file).name;
const lines = [
const lines = (target === 'mjs' ? [] : [
'"use strict";',
`Object.defineProperty(exports, '__esModule', {value: true});`
];
`Object.defineProperty(exports, '__esModule', {value: true});`,
]);
let source = ((dir.replace(/\//g, '.') + '.' + name).replace(/^\./, '')
+ (exists(path.resolve(JS, file.replace(/\.ts$/, ''))) ? '_ts' : ''))
.replace(/\.[^.]*/g, (x) => (x.substr(1).match(/[^a-zA-Z_]/) ? '[\'' + x.substr(1) + '\']' : x));
lines.push(
objects.indexOf('MathJax') >= 0 ?
`const g = (typeof window !== 'undefined' ? window : global);\nconst def = g.MathJax._.${source};` :
`const def = MathJax._.${source};`
);
for (const id of objects) {
lines.push(`exports.${id} = MathJax._.${source}.${id};`);
lines.push(target === 'cjs' ? `exports.${id} = def.${id};` :
id === 'default' ? `export default def.${id};` : `export const ${id} = def.${id};`);
}
return lines;
}
Expand Down Expand Up @@ -228,6 +246,9 @@ function makeFile(file, lines) {
fs.writeFileSync(path.resolve(LIB, dir, name.replace(/\.ts$/, '.js')), lines.join('\n') + '\n');
}

/**
* @return {[string, string, string]} The needed nesting data for handking extra directories from a prefix
*/
function getExtraDirectories() {
if (PREFIX === '') return ['', INDENT, ''];
let prefix = '';
Expand All @@ -249,11 +270,15 @@ function getExtraDirectories() {
*/
function processGlobal() {
console.info(' ' + COMPONENT + '.ts');
const lines = [
const lines = (target === 'cjs' ? [
`const {combineWithMathJax} = require('${GLOBAL}')`,
`const {VERSION} = require('${VERSION}');`,
'',
] : [
`import {combineWithMathJax} from '${GLOBAL}';`,
`import {VERSION} from '${VERSION}';`,
'',
];
]);
const [prefix, indent, postfix] = getExtraDirectories();
const packages = [];
PACKAGE = PACKAGE.sort(sortDir);
Expand Down Expand Up @@ -311,9 +336,13 @@ function processPackage(lines, space, dir) {
if (path.dirname(PACKAGE[0]) === dir) {
const file = PACKAGE.shift();
const name = path.basename(file);
const relativefile = path.join('..', JS, dir, name).replace(/\.ts$/, '.js');
let relativefile = path.join('..', JS, dir, name).replace(/\.ts$/, '.js')
const component = 'module' + (++importCount);
lines.push(`import * as ${component} from '${relativefile}';`);
lines.push(
target === 'cjs' ?
`const ${component} = require('${relativefile}');` :
`import * as ${component} from '${relativefile}';`
);
let property = name.replace(/\.ts$/, '');
if (property !== name && exists(path.resolve(JS, file.replace(/\.ts$/, '')))) {
property += '_ts';
Expand Down
22 changes: 21 additions & 1 deletion components/bin/copy
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,30 @@ const path = require('path');
*/
const INDENT = ' ';

/**
* The module type to use ('cjs' or 'mjs')
*/
const target = (process.argv[2] || 'mjs');

/**
* The bundle directory name
*/
const bundle = (process.argv[3] || 'bundle');

/**
* The configuration data for the copy operation
*/
const config = JSON.parse(fs.readFileSync(process.argv[2] || 'copy.json'));
const json = path.resolve(process.argv[4] || '.', 'config.json');
const config = require(json).copy;
if (!config) process.exit();
process.chdir(path.dirname(json));

/**
* Redirect the bundle, if not the default
*/
if (bundle !== 'bundle') {
config.to = config.to.replace(/\/bundle(\/|$)/, '/' + bundle + '$1');
}

/**
* Get the directory for node modules (either the parent of the MathJax directory,
Expand Down
Loading