@@ -36,6 +36,23 @@ function quoteRE(string) {
3636 return string . replace ( / ( [ \\ . { } [ \] ( ) ? * ^ $ ] ) / g, '\\$1' )
3737}
3838
39+ /**
40+ * Creates the plugin needed for including jsdir in the output
41+ *
42+ * @param {string } js The location of the compiled js files
43+ * @param {string } dir The directory of the component being built
44+ * @return {any[] } The plugin array (empty or with the conversion plugin)
45+ */
46+ const PLUGINS = function ( js , dir ) {
47+ const mjdir = path . resolve ( __dirname , '..' , 'js' ) ;
48+ const jsdir = path . resolve ( dir , js ) ;
49+
50+ //
51+ // Record the js directory for the pack command
52+ //
53+ return [ new webpack . DefinePlugin ( { __JSDIR__ : jsdir } ) ] ;
54+ } ;
55+
3956/**
4057 * Creates the plugin needed for converting mathjax references to component/lib references
4158 *
@@ -44,62 +61,59 @@ function quoteRE(string) {
4461 * @param {string } dir The directory of the component being built
4562 * @return {any[] } The plugin array (empty or with the conversion plugin)
4663 */
47- const PLUGINS = function ( js , libs , dir ) {
64+ const RESOLVE = function ( js , libs , dir ) {
4865 const mjdir = path . resolve ( __dirname , '..' , 'js' ) ;
4966 const jsdir = path . resolve ( dir , js ) ;
5067 const mjRE = new RegExp ( '^(?:' + quoteRE ( jsdir ) + '|' + quoteRE ( mjdir ) + ')' + quoteRE ( path . sep ) ) ;
5168 const root = path . dirname ( mjdir ) ;
52- const rootRE = new RegExp ( '^' + quoteRE ( root + path . sep ) ) ;
53- const nodeRE = new RegExp ( '^' + quoteRE ( path . dirname ( root ) + path . sep ) ) ;
5469
5570 //
56- // Record the js directory for the pack command
71+ // Add directory names to libraries
5772 //
58- const plugins = [ new webpack . DefinePlugin ( { jsdir : jsdir } ) ] ;
73+ libs = libs . map ( lib => path . join ( lib . charAt ( 0 ) === '.' ? dir : root , lib ) + path . sep ) ;
5974
60- if ( libs . length ) {
61- plugins . push (
62- //
63- // Move mathjax references to component libraries
64- //
65- new webpack . NormalModuleReplacementPlugin (
66- / ^ [ ^ \/ ] / ,
67- function ( resource ) {
68- const request = require . resolve ( resource . request . charAt ( 0 ) === '.' ?
69- path . resolve ( resource . context , resource . request ) :
70- resource . request ) ;
71- if ( ! request . match ( mjRE ) ) return ;
72- for ( const lib of libs ) {
73- const file = request . replace ( mjRE , path . join ( root , lib ) + path . sep ) ;
74- if ( fs . existsSync ( file ) ) {
75- resource . request = file ;
76- break ;
77- }
78- }
79- }
80- )
75+ //
76+ // Function replace imported files by ones in the specified component lib directories.
77+ //
78+ const replaceLibs = ( resource ) => {
79+ //
80+ // The full file name to check.
81+ //
82+ const request = require . resolve (
83+ resource . request ?
84+ resource . request . charAt ( 0 ) === '.' ? path . resolve ( resource . path , resource . request ) : resource . request :
85+ resource . path
8186 ) ;
82- }
83- plugins . push (
8487 //
85- // Check for packages that should be rerouted to node_modules
88+ // Only check files in the MathJax js directory.
8689 //
87- new webpack . NormalModuleReplacementPlugin (
88- / ^ [ ^ \/ ] $ / ,
89- function ( resource ) {
90- const request = require . resolve ( resource . request . charAt ( 0 ) === '.' ?
91- path . resolve ( resource . context , resource . request ) :
92- resource . request ) ;
93- if ( request . match ( rootRE ) || ! request . match ( nodeRE ) || fs . existsSync ( request ) ) return ;
94- const file = request . replace ( nodeRE , path . join ( root , 'node_modules' ) + path . sep ) ;
95- if ( fs . existsSync ( file ) ) {
96- resource . request = file ;
97- }
90+ if ( ! request . match ( mjRE ) ) return ;
91+ //
92+ // Loop through the libraries and see if the imported file is there.
93+ // If so, replace the request with the library version and return.
94+ //
95+ for ( const lib of libs ) {
96+ const file = request . replace ( mjRE , lib ) ;
97+ if ( fs . existsSync ( file ) ) {
98+ resource . path = file ;
99+ resource . request = undefined ;
100+ return ;
98101 }
99- )
100- ) ;
101- return plugins ;
102- } ;
102+ }
103+ }
104+
105+ //
106+ // A plugin that looks for files and modules to see if they need replacing with library versions.
107+ //
108+ class ResolveReplacementPlugin {
109+ apply ( compiler ) {
110+ compiler . hooks . file . tap ( ResolveReplacementPlugin . name , replaceLibs ) ;
111+ compiler . hooks . module . tap ( ResolveReplacementPlugin . name , replaceLibs ) ;
112+ }
113+ }
114+
115+ return { plugins : [ new ResolveReplacementPlugin ( ) ] } ;
116+ }
103117
104118/**
105119 * Add babel-loader to appropriate directories
@@ -150,7 +164,8 @@ const PACKAGE = function (name, js, libs, dir, dist) {
150164 filename : name + ( dist === '.' ? '.min.js' : '.js' )
151165 } ,
152166 target : [ 'web' , 'es5' ] , // needed for IE11 and old browsers
153- plugins : PLUGINS ( js , libs , dir ) ,
167+ plugins : PLUGINS ( js , dir ) ,
168+ resolve : RESOLVE ( js , libs , dir ) ,
154169 module : MODULE ( dir ) ,
155170 performance : {
156171 hints : false
0 commit comments