I have the task at my project to rewrite logic of using static files: use dynamic names or dynamic URLs for your static files. Example of dynamic name: /static/main.abcdef0123.js. I am using ASP NET Core MVC and Webpack for bundling. Before this task, the project have been using just .js and .css files without webpack so I have working logic in ‘.js’ files.
The issue at hand is that while the JavaScript and CSS bundle files are being successfully loaded into the browser—as confirmed by their 200 statuses in the Network tab, and the respective <script> and <link> elements visible in the DOM—the expected functionality is not working.
The functionality with the JS file that isn’t working is related to file handling and form submission on the webpage, specifically:
-
The function that should be triggered when users drop or attach files isn’t executing as expected.
-
The submit button on the form is non-responsive. It’s not initiating any action or event when clicked.
The CSS bundle is working.
It’s important to note that the bundling process appears to be working – the files are being bundled properly and the server is making these files available to the browser. The problem is not with serving the files to the client, but instead with the actual functionality coded within the JavaScript file, which for some reason isn’t working as expected when it’s served as a part of the bundled file.
In summarizing, the problem is that despite no errors in the loading of bundled JavaScript and CSS files, the expected user-interaction functionality programmed in the JavaScript isn’t activating or executing as expected on the webpage.
My webpack.config.js:
/* eslint-disable no-unused-vars */
const path = require('path');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
module.exports = {
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development', // change to 'production' when building for production
entry:'./wwwroot/js/index.js',
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: 'bundle.[contenthash].js',
publicPath: '/dist/'
},
module: {
rules: [
{
test: /.m?js$/,
loader: 'babel-loader'
},
{
test: /.svg$/,
use: {
loader: 'url-loader',
options: {
limit: 10000 // Use data urls for <=10kb images, uses file-loader for other
}
}
},
{
test: /.css$/,
use: [MiniCssExtractPlugin.loader, 'css-loader']
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: 'styles.[contenthash].css'
}),
new BundleAnalyzerPlugin(),
new WebpackManifestPlugin()
]
};
My index.js where I store imports for all my .js files and use entry in webpack.config.js :
import './pagination.js';
import './site.js';
import './form/edit_form_init.js';
import './form/form.js';
import './form/new_form_init.js';
import './form/npApiCalls.js';
import './surveyForm/commonLogic.js';
function requireAll(r) {
r.keys().forEach(r);
}
// Import all css files within the css directory and its subdirectories
requireAll(require.context('../css/', true, /.css$/));
// Import all svg files within the images directory
requireAll(require.context('../images/', false, /.svg$/));
My package.json for project description and names for npm scripts:
{
"name": "***",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"precompile": "webpack",
"build": "webpack",
"start": "webpack-dev-server",
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.23.5",
"@babel/preset-env": "^7.23.5",
"babel-loader": "^9.1.3",
"css-loader": "^6.8.1",
"eslint": "^8.54.0",
"file-loader": "^6.2.0",
"mini-css-extract-plugin": "^2.7.6",
"style-loader": "^3.3.3",
"svg-inline-loader": "^0.8.2",
"url-loader": "^4.1.1",
"webpack": "^5.89.0",
"webpack-bundle-analyzer": "^4.10.1",
"webpack-cli": "^5.1.4",
"webpack-dev-middleware": "^6.1.1",
"webpack-dev-server": "^4.15.1",
"webpack-manifest-plugin": "^5.0.0"
},
"babel": {
"plugins": [
"@babel/syntax-dynamic-import"
],
"presets": [
[
"@babel/preset-env",
{
"modules": false
}
]
]
}
}
I have manifest.json:
{
"main.css": "/dist/styles.7b1bf0ef4c5af1795361.css",
"main.js": "/dist/bundle.0363bed44d8d0b3f64ff.js",
"unauthorized-access-page.svg": "/dist/df81d60cc31dfc502a4f19196a3457b2.svg",
// other .svg files...
}
How I import bundled files to _Layout.cshtml:
@using System.IO
@using Newtonsoft.Json
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment env
@{
var manifestPath = System.IO.Path.Combine(env.WebRootPath, "dist", "manifest.json");
var manifest = System.IO.File.Exists(manifestPath)
? JsonConvert.DeserializeObject<Dictionary<string, string>>(System.IO.File.ReadAllText(manifestPath))
: new Dictionary<string, string>();
}
<link rel="stylesheet" href="@manifest.GetValueOrDefault("main.css")" />
<script src="@manifest.GetValueOrDefault("main.js")"></script>
The line <script src="@manifest.GetValueOrDefault("main.js")"></script> is after </footer>.
Headers of bundle.js
I’ve tried to use in the webpack.config.json but expected behavior did not occur:
module: {
rules: [
{
test: /.m?js$/,
// exclude: /(node_modules|bower_components)/,
loader: 'babel-loader'
/*use: {
options: {
presets: ['@babel/preset-env']
}
}*/
},
My pipeline of running the project:
- Run
npm run build with the output:
npm run build
> [email protected] build
> webpack
Webpack Bundle Analyzer is started at http://127.0.0.1:xxxx
Use Ctrl+C to close it
assets by path *.svg 322 KiB
assets by info 1.58 KiB [immutable]
asset 17936c45f1f5e57990d7.svg 667 bytes [emitted] [immutable] [from: wwwroot/images/search-icon.svg] (auxiliary name: main)
asset 74e13e43c2967dfb9cfd.svg 539 bytes [emitted] [immutable] [from: wwwroot/images/excel-icon.svg] (auxiliary name: main)
asset fe3c8b386d2d6f26ea5a.svg 411 bytes [emitted] [immutable] [from: wwwroot/images/close_image.svg] (auxiliary name: main)
+ 10 assets
assets by chunk 245 KiB (name: main)
asset bundle.0363bed44d8d0b3f64ff.js 153 KiB [emitted] [immutable] (name: main)
asset styles.7b1bf0ef4c5af1795361.css 92 KiB [emitted] [immutable] (name: main)
asset manifest.json 1.02 KiB [emitted]
Entrypoint main 245 KiB (322 KiB) = styles.7b1bf0ef4c5af1795361.css 92 KiB bundle.0363bed44d8d0b3f64ff.js 153 KiB 13 auxiliary assets
runtime modules 39.2 KiB 183 modules
orphan modules 97.1 KiB (javascript) 1.58 KiB (asset) [orphan] 48 modules
built modules 93.7 KiB (javascript) 79.2 KiB (css/mini-extract) [built]
javascript modules 93.7 KiB
optional modules 30.4 KiB [optional]
modules by path ./wwwroot/images/ 28.4 KiB 42 modules
modules by path ./wwwroot/css/ 2 KiB 41 modules
modules by path ./wwwroot/js/ 60.7 KiB 8 modules
./wwwroot/css/ sync .css$ 1.25 KiB [built] [code generated]
./wwwroot/images/ sync nonrecursive .svg$ 1.27 KiB [built] [code generated]
css modules 79.2 KiB 48 modules
webpack 5.89.0 compiled successfully in 2386 ms
dotnet run
- The app is running at https://localhost:xxxx