@@ -56,8 +56,8 @@ function loadViaWebpackNoCache<T>(webpackConfigPath: string, modulePath: string)
56
56
webpackConfig . output . libraryTarget = 'commonjs' ;
57
57
const outputVirtualPath = path . join ( webpackConfig . output . path , webpackConfig . output . filename ) ;
58
58
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
61
61
// depending on how they were loaded, which could lead to errors).
62
62
// ---
63
63
// 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)
70
70
externalsArray = [ externalsArray ] ;
71
71
}
72
72
webpackConfig . 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
+ } ) ) ;
74
86
75
87
// The CommonsChunkPlugin is not compatible with a CommonJS environment like Node, nor is it needed in that case
76
88
webpackConfig . plugins = webpackConfig . plugins . filter ( plugin => {
0 commit comments