@@ -30,7 +30,7 @@ const path = require('path');
3030/**
3131 * The amount of space for each level of indentation
3232 */
33- const INDENT = ' ' ;
33+ const INDENT = ' ' ;
3434
3535/**
3636 * The pattern to use when looking for explort commands to process
@@ -43,24 +43,36 @@ const EXPORT_PROCESS = ['let', 'const', 'var', 'function', 'class', 'namespace']
4343/**
4444 * The relative path to the MathJax directory
4545 */
46- const mjPath = path . relative ( process . cwd ( ) , path . resolve ( __dirname , '../../js' ) ) ;
46+ const mjPath = path . relative ( process . cwd ( ) , path . resolve ( __dirname , '..' , '..' , 'js' ) ) ;
47+ const mjGlobal = path . join ( '..' , mjPath , 'components' , 'global.js' ) ;
4748
4849/**
4950 * Read the configuration for the component
5051 */
5152const config = JSON . parse ( fs . readFileSync ( process . argv [ 2 ] || 'build.json' ) ) ;
5253
54+ function getType ( ) {
55+ const component = config . component || 'part' ;
56+ if ( component . match ( / \/ ( s v g | c h t m l | c o m m o n ) \/ f o n t s \/ / ) ) return RegExp . $1 + '-font' ;
57+ if ( component . match ( / \/ ( m a t h m l | t e x ) \/ .+ \/ / ) ) return RegExp . $1 + '-extension' ;
58+ if ( component . match ( / ^ ( .+ ) \/ / ) ) return RegExp . $1 ;
59+ return component ;
60+ }
61+
5362/**
5463 * Extract the configuration values
5564 */
65+ const COMPONENT = path . basename ( config . component || 'part' ) ; // name of the component
66+ const ID = config . id || config . component || 'part' ; // the ID of the component
5667const TARGETS = config . targets || [ ] ; // the files to include in the component
5768const EXCLUDE = new Map ( ( config . exclude || [ ] ) . map ( name => [ name , true ] ) ) ; // files to exclude from the component
58- const EXCLUDESUBDIRS = config . excludeSubdirs === 'true' ; // exclude subdirectories
59- const MATHJAX = config . js || config . mathjax || mjPath ; // path to the compiled .js files
69+ const EXCLUDESUBDIRS = config . excludeSubdirs === 'true' ; // exclude subdirectories or not
70+ const JS = config . js || config . mathjax || mjPath ; // path to the compiled .js files
6071const LIB = config . lib || './lib' ; // path to the lib directory to create
61- const COMPONENT = path . basename ( config . component || 'part' ) ; // name of the component
62- const GLOBAL = config . global || `../${ MATHJAX } /components/global.js` ; // the location of global.js
63- const SRC = config . ts || MATHJAX . replace ( / j s $ / , 'ts' ) ; // path to the .ts files
72+ const TS = config . ts || JS . replace ( / j s $ / , 'ts' ) ; // path to the .ts files
73+ const GLOBAL = config . global || mjGlobal ; // path to the global.js file
74+ const VERSION = config . version || mjGlobal . replace ( / g l o b a l / , 'version' ) ; // path to the version.js file
75+ const TYPE = config . type || getType ( ) ; // the module type
6476
6577/**
6678 * The list of files that need to be added to the lib directory
@@ -160,17 +172,15 @@ function processLines(file, objects) {
160172 if ( objects . length === 0 ) return [ ] ;
161173 const dir = path . dirname ( file ) . replace ( / ^ \. $ / , '' ) ;
162174 const dots = dir . replace ( / [ ^ \/ ] + / g, '..' ) || '.' ;
163- const relative = path . join ( dots , '..' , MATHJAX , dir , path . basename ( file ) ) . replace ( / \. t s $ / , '.js' ) ;
175+ const relative = path . join ( dots , '..' , JS , dir , path . basename ( file ) ) . replace ( / \. t s $ / , '.js' ) ;
164176 const name = path . parse ( file ) . name ;
165177 const lines = [
166178 '"use strict";' ,
167179 `Object.defineProperty(exports, '__esModule', {value: true});`
168180 ] ;
169- let source = ( dir . replace ( / \/ / g, '.' ) + '.' + name ) . replace ( / ^ \. / , '' )
181+ let source = ( ( dir . replace ( / \/ / g, '.' ) + '.' + name ) . replace ( / ^ \. / , '' )
182+ + ( exists ( path . resolve ( JS , file . replace ( / \. t s $ / , '' ) ) ) ? '_ts' : '' ) )
170183 . replace ( / \. [ ^ . ] * / g, ( x ) => ( x . substr ( 1 ) . match ( / [ ^ a - z A - Z _ ] / ) ? '[\'' + x . substr ( 1 ) + '\']' : x ) ) ;
171- if ( exists ( path . resolve ( MATHJAX , file . replace ( / \. t s $ / , '' ) ) ) ) {
172- source += '_ts' ;
173- }
174184 for ( const id of objects ) {
175185 lines . push ( `exports.${ id } = MathJax._.${ source } .${ id } ;` ) ;
176186 }
@@ -223,6 +233,7 @@ function processGlobal() {
223233 console . info ( ' ' + COMPONENT + '.ts' ) ;
224234 const lines = [
225235 `import {combineWithMathJax} from '${ GLOBAL } ';` ,
236+ `import {VERSION} from '${ VERSION } ';` ,
226237 '' ,
227238 ] ;
228239 const packages = [ ] ;
@@ -231,9 +242,17 @@ function processGlobal() {
231242 const dir = path . dirname ( PACKAGE [ 0 ] ) . split ( path . sep ) [ 0 ] ;
232243 packages . push ( processPackage ( lines , INDENT , dir ) ) ;
233244 }
234- lines . push ( '' , `combineWithMathJax({_: {` ) ;
235- lines . push ( INDENT + packages . join ( ',\n' + INDENT ) ) ;
236- lines . push ( '}});' ) ;
245+ const name = ( ID . match ( / [ ^ a - z A - Z 0 - 9 _ ] / ) ? `"${ ID } "` : ID ) ;
246+ lines . push (
247+ '' ,
248+ 'if (MathJax.loader) {' ,
249+ INDENT + `MathJax.loader.checkVersion('${ ID } ', VERSION, '${ TYPE } ');` ,
250+ '}' ,
251+ '' ,
252+ `combineWithMathJax({_: {` ,
253+ INDENT + packages . join ( ',\n' + INDENT ) ,
254+ '}});'
255+ ) ;
237256 fs . writeFileSync ( path . join ( LIB , COMPONENT + '.js' ) , lines . join ( '\n' ) + '\n' ) ;
238257}
239258
@@ -273,11 +292,11 @@ function processPackage(lines, space, dir) {
273292 if ( path . dirname ( PACKAGE [ 0 ] ) === dir ) {
274293 const file = PACKAGE . shift ( ) ;
275294 const name = path . basename ( file ) ;
276- const relativefile = path . join ( '..' , MATHJAX , dir , name ) . replace ( / \. t s $ / , '.js' ) ;
295+ const relativefile = path . join ( '..' , JS , dir , name ) . replace ( / \. t s $ / , '.js' ) ;
277296 const component = 'module' + ( ++ importCount ) ;
278297 lines . push ( `import * as ${ component } from '${ relativefile } ';` ) ;
279298 let property = name . replace ( / \. t s $ / , '' ) ;
280- if ( property !== name && exists ( path . resolve ( MATHJAX , file . replace ( / \. t s $ / , '' ) ) ) ) {
299+ if ( property !== name && exists ( path . resolve ( JS , file . replace ( / \. t s $ / , '' ) ) ) ) {
281300 property += '_ts' ;
282301 }
283302 if ( property . match ( / [ ^ a - z A - Z 0 - 9 _ ] / ) ) {
@@ -324,5 +343,5 @@ function rmDir(dir) {
324343//
325344rmDir ( LIB ) ;
326345console . info ( "Processing:" ) ;
327- processList ( SRC , '' , TARGETS ) ;
346+ processList ( TS , '' , TARGETS ) ;
328347processGlobal ( ) ;
0 commit comments