diff --git a/js/common/Transpiler.js b/js/common/Transpiler.js index 7c26868ef..45ee149cd 100644 --- a/js/common/Transpiler.js +++ b/js/common/Transpiler.js @@ -19,6 +19,7 @@ const CacheLayer = require( './CacheLayer' ); const wgslMinify = require( './wgslMinify' ); const wgslPreprocess = require( './wgslPreprocess' ); const wgslStripComments = require( './wgslStripComments' ); +const webpackGlobalLibraries = require( './webpackGlobalLibraries' ); const core = require( '@babel/core' ); const assert = require( 'assert' ); const _ = require( 'lodash' ); @@ -315,8 +316,10 @@ class Transpiler { // Our sims load this as a module rather than a preload, so we must transpile it this.visitFile( Transpiler.join( '..', repo, 'lib', 'game-up-camera-1.0.0.js' ) ); - this.visitFile( Transpiler.join( '..', repo, 'lib', 'peggy-3.0.2.js' ) ); - this.visitFile( Transpiler.join( '..', repo, 'lib', 'himalaya-1.1.0.js' ) ); + Object.keys( webpackGlobalLibraries ).forEach( key => { + const libraryFilePath = webpackGlobalLibraries[ key ]; + this.visitFile( Transpiler.join( '..', ...libraryFilePath.split( '/' ) ) ); + } ); } else if ( repo === 'brand' ) { this.visitDirectory( Transpiler.join( '..', repo, 'phet' ) ); diff --git a/js/common/webpackGlobalLibraries.js b/js/common/webpackGlobalLibraries.js index f97c36ec3..ea389c2c7 100644 --- a/js/common/webpackGlobalLibraries.js +++ b/js/common/webpackGlobalLibraries.js @@ -1,14 +1,25 @@ // Copyright 2023, University of Colorado Boulder /** - * webpackGlobalLibraries defined the third party library files that load with a global. + * webpackGlobalLibraries define the third party library files that load with a global. These must be here to support + * building sims through webpack. + * When adding to this file, note that it is most likely tested against the resource located in chipper/dist, so + * full paths are less than ideal. * * @author Michael Kauzmann (PhET Interactive Simulations) */ - -// TODO: regex is less than ideal here, but Rule.test is a bit finicky, see https://github.com/phetsims/chipper/issues/1409 -module.exports = { - peggy: /sherpa[\\/]lib[\\/]peggy-3\.0\.2\.js$/, // `sherpa/lib/peggy-3.0.2.js` with windows path support - himalaya: /sherpa[\\/]lib[\\/]himalaya-1\.1\.0\.js$/ // `sherpa/lib/himalaya-1.1.0.js` with windows path support +const assert = require( 'assert' ); +const webpackGlobalLibraries = { + peggy: 'sherpa/lib/peggy-3.0.2.js', + himalaya: 'sherpa/lib/himalaya-1.1.0.js' }; + +Object.keys( webpackGlobalLibraries ).forEach( key => { + const filePath = webpackGlobalLibraries[ key ]; + + // If you need to support from another repo, just add to this assertion + assert( filePath.startsWith( 'sherpa' ), 'Path must start with the repo to support transpiling, see Transpiler' ); +} ); + +module.exports = webpackGlobalLibraries; diff --git a/js/grunt/webpackBuild.js b/js/grunt/webpackBuild.js index 6c53fc9cc..75bc36647 100644 --- a/js/grunt/webpackBuild.js +++ b/js/grunt/webpackBuild.js @@ -33,7 +33,9 @@ for ( const repo of activeRepos ) { const getModuleRules = function getModuleRules() { return Object.keys( webpackGlobalLibraries ).map( globalKey => { return { - test: webpackGlobalLibraries[ globalKey ], + + // path.join to normalize on the right path separator, perhaps there is another way?! + test: fileName => fileName.includes( path.join( webpackGlobalLibraries[ globalKey ] ) ), loader: '../chipper/node_modules/expose-loader', options: { exposes: globalKey