gpt4 book ai didi

javascript - HtmlWebpackPlugin 忽略文件名

转载 作者:太空宇宙 更新时间:2023-11-04 01:21:36 24 4
gpt4 key购买 nike

我正在尝试使用 webpack 进行代码/包分割,但是遇到了一个问题。据我所知,HTMLWebpackPlugin 应该动态地将脚本标签插入到 HTML 文件中。该 HTML 文件默认为 index.html,当我使用默认值时它似乎可以工作。因为我将 .NET 与 _Layout.cshtml 和 Index.cshtml 一起使用,所以它希望它使用 Index.cshtml。

HTMLWebpackPlugin似乎完全忽略“文件名”中的内容,并生成一个index.html:

new HtmlWebpackPlugin({
template: '../../Views/Shared/_Layout.cshtml',
filename: 'test-index.html' // Generates index.html, not test-index.html...
//filename: '../Views/Home/Index.cshtml' // Not working either. Generates index.html
})

webpack.common.js:

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var helpers = require('./helpers');

module.exports = {
entry: {
'polyfills': './ClientApp/polyfills.ts',
'vendor': './ClientApp/vendor.ts',
'app': './ClientApp/main.ts'
},

resolve: {
extensions: ['.ts', '.js']
},

module: {
rules: [
{
test: /\.ts$/,
loaders: [
{
loader: 'awesome-typescript-loader',
options: { configFileName: helpers.root('ClientApp', 'tsconfig.json') }
} , 'angular2-template-loader'
]
},
{
test: /\.html$/,
use: ['raw-loader']
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'file-loader?name=assets/[name].[hash].[ext]'
},
{
test: /\.css$/,
include: helpers.root('ClientApp', 'app'),
use: ['to-string-loader', 'css-loader']
}
]
},

optimization:
{
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 0,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
// get the name. E.g. node_modules/packageName/not/this/part.js
// or node_modules/packageName
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];

// npm package names are URL-safe, but some servers don't like @ symbols
return `npm.${packageName.replace('@', '')}`;
},
},
}
}
},

plugins: [
new webpack.ContextReplacementPlugin(
/angular(\\|\/)core(\\|\/)(@angular|fesm5)/,
helpers.root('ClientApp'),
{}
),

new webpack.BannerPlugin('© COPYRIGHT LIGHTHOUSE INTELLIGENCE ' + new Date().getFullYear()),

new HtmlWebpackPlugin({
template: '../../Views/Shared/_Layout.cshtml',
filename: 'test-index.html' // Generates index.html, not test-index.html...
//filename: '../Views/Home/Index.cshtml' // Not working either
})
]
};

webpack.dev.js:

var webpackMerge = require('webpack-merge');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var commonConfig = require('./webpack.common.js');
var helpers = require('./helpers');

module.exports = webpackMerge(commonConfig, {
devtool: 'cheap-module-eval-source-map',
mode: "development",
output: {
path: helpers.root('/wwwroot/dist'),
publicPath: '/wwwroot/dist/',
filename: '[name].js', //.[hash]
chunkFilename: '[id].js' //.chunk
},

plugins: [
new ExtractTextPlugin('[name].css'),
],

devServer: {
historyApiFallback: true,
stats: 'minimal'
}
});

最佳答案

我认为你不能这样做。在构建和运行项目之前,Razor cshtml 文件需要将所有内容包含在 View 中,以便它可以构建 cshtml 并将其转换为 html。

htmlwebpackplugin 仅适用于 html,因此您可以选择的唯一选项是在 .net core 项目中提供 html 文件

关于javascript - HtmlWebpackPlugin 忽略文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com