@@ -56,8 +56,8 @@ function loadViaWebpackNoCache<T>(webpackConfigPath: string, modulePath: string)
5656webpackConfig . output . libraryTarget = 'commonjs' ;
5757const outputVirtualPath = path . join ( webpackConfig . output . path , webpackConfig . output . filename ) ;
5858
59- // In Node, we want anything under /node_modules/ to be loaded natively and not bundled into the output
60- // (partly because it's faster, but also because otherwise there'd be different instances of modules
59+ // In Node, we want any JavaScript modules under /node_modules/ to be loaded natively and not bundled into the
60+ // output (partly because it's faster, but also because otherwise there'd be different instances of modules
6161// depending on how they were loaded, which could lead to errors).
6262// ---
6363// NOTE: We have to use webpack-node-externals rather than webpack-externals-plugin because
@@ -70,7 +70,19 @@ function loadViaWebpackNoCache<T>(webpackConfigPath: string, modulePath: string)
7070externalsArray = [ externalsArray ] ;
7171}
7272webpackConfig . externals = externalsArray ;
73- externalsArray . push ( nodeExternals ( ) ) ;
73+ externalsArray . push ( nodeExternals ( {
74+ // However, we do *not* want to treat non-JS files under /node_modules/ as externals (i.e., things
75+ // that should be loaded via regular CommonJS 'require' statements). For example, if you reference
76+ // a .css file inside an NPM module (e.g., require('somepackage/somefile.css')), then we do need to
77+ // load that via Webpack rather than as a regular CommonJS module.
78+ //
79+ // So, configure webpack-externals-plugin to 'whitelist' (i.e., not treat as external) any file
80+ // that has an extension other than .js.
81+ //
82+ // This regex looks for at least one dot character that is *not* followed by "js<end>", but
83+ // is followed by some series of non-dot characters followed by <end>:
84+ whitelist : [ / \. (? ! j s $ ) ( [ ^ . ] + $ ) / ]
85+ } ) ) ;
7486
7587// The CommonsChunkPlugin is not compatible with a CommonJS environment like Node, nor is it needed in that case
7688webpackConfig . plugins = webpackConfig . plugins . filter ( plugin => {
0 commit comments