【问题标题】:HtmlWebpackPlugin ignores filenameHtmlWebpackPlugin 忽略文件名
【发布时间】:2019-11-30 16:16:42
【问题描述】:

我正在尝试使用 webpack 拆分代码/捆绑包,但是遇到了一个问题。据我所知,HTMLWebpackPlugin 应该动态地将脚本标签插入到 HTML 文件中。此 HTML 文件默认为 index.html,当我使用默认值时它似乎可以工作。因为我使用带有 _Layout.cshtml 和 Index.cshtml 的 .NET,所以它希望它使用 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'
    }
});

【问题讨论】:

    标签: javascript node.js angular asp.net-core webpack


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      输出文件的位置由 webpack.config.js 的 output 属性决定(路径:helpers.root('/wwwroot/dist'))。因此,您只需为 filename 属性指定文件名。

      【讨论】:

        猜你喜欢
        • 2018-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-07
        相关资源
        最近更新 更多