Skip to content

Commit

Permalink
much easier way of supporting global-setting libraries, #1409
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Oct 5, 2023
1 parent 7c12c44 commit 2356daa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
7 changes: 5 additions & 2 deletions js/common/Transpiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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' ) );
Expand Down
23 changes: 17 additions & 6 deletions js/common/webpackGlobalLibraries.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 3 additions & 1 deletion js/grunt/webpackBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2356daa

Please sign in to comment.