Skip to content

Commit e859428

Browse files
In aspnet-webpack, make sure that webpack-externals-plugin doesn't treat non-JS files as external. Fixes #332.
1 parent b55e444 commit e859428

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aspnet-webpack",
3-
"version": "1.0.13",
3+
"version": "1.0.14",
44
"description": "Helpers for using Webpack in ASP.NET Core projects. Works in conjunction with the Microsoft.AspNetCore.SpaServices NuGet package.",
55
"main": "index.js",
66
"scripts": {

src/Microsoft.AspNetCore.SpaServices/npm/aspnet-webpack/src/LoadViaWebpack.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ function loadViaWebpackNoCache<T>(webpackConfigPath: string, modulePath: string)
5656
webpackConfig.output.libraryTarget = 'commonjs';
5757
const 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)
7070
externalsArray = [externalsArray];
7171
}
7272
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: [/\.(?!js$)([^.]+$)/]
85+
}));
7486

7587
// The CommonsChunkPlugin is not compatible with a CommonJS environment like Node, nor is it needed in that case
7688
webpackConfig.plugins = webpackConfig.plugins.filter(plugin => {

0 commit comments

Comments
 (0)