【问题标题】:get webpack to include html files让 webpack 包含 html 文件
【发布时间】:2018-12-23 11:46:56
【问题描述】:

注意:我是 webpack 的初学者。

我正在尝试让 webpack 加载我的 .htmls 文件(index.html 和 login.html),因为它们将用作我的电子应用程序的窗口。这是我迄今为止尝试过的,但没有结果:

rules: [
            {
                test: /\.html$/,
                use: ["html-loader"]
            },
...

rules: [
            {
                test: /\.html$/,
                loader: "file-loader"
            },
...

这是我的 webpack.config.js 文件:

const path = require("path");
const { spawn } = require("child_process");

const srcDir = path.resolve(__dirname, "src/renderer");
const outDir = path.resolve(__dirname, "build/client");

const defaultIncludes = [srcDir];

module.exports = {
    entry: `${srcDir}/index`,

    output: {
        path: outDir,
        filename: "app.bundle.js"
    },

    resolve: {
        extensions: [".ts", ".tsx", ".js", ".json", ".html"]
    },

    module: {
        rules: [
            {
                test: /\.html$/,
                loader: "file-loader"
            },
            {
                // Include ts, tsx, and js files.
                test: /\.(tsx?)|(js)$/,
                exclude: /node_modules/,
                loader: "babel-loader",
                include: defaultIncludes
            },
            {
                test: /\.scss$/,
                use: [
                    "style-loader", // creates style nodes from JS strings
                    "css-loader", // translates CSS into CommonJS
                    "sass-loader" // compiles Sass to CSS
                ]
            },
            {
                test: /\.(ttf|eot|woff|woff2)$/,
                loader: "file-loader",
                options: {
                    name: "fonts/[name].[ext]"
                }
            }
        ]
    },

    devServer: {
        inline: true,
        contentBase: outDir,
        compress: true,

        stats: {
            colors: true,
            chunks: false,
            children: false
        },

        before() {
            spawn("electron", ["."], {
                shell: true,
                env: process.env,
                stdio: "inherit"
            }).on("close", code => process.exit(0)).on("error", spawnError => console.error(spawnError));
        }
    },

    target: "electron-renderer",
    mode: "development"
};

我做错了什么? Webpack 将所有内容构建到 /build 中,但不包括 .html 文件(index.html 和 login.html,它们位于我的 /src/ 目录下)。

【问题讨论】:

    标签: javascript node.js webpack electron


    【解决方案1】:

    HTML 文件的加载器配置将允许使用 HTML 文件调用 require 在 javascript 文件中工作。使用文件加载器,您将获得文件路径,而使用 HTML 加载器,您将获得调用后的 HTML 内容。

    如果您希望将您的 HTML 文件与您编译的源代码一起复制,您必须使用像 copy-webpack-pluginhtml-webpack-plugin 这样的插件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-15
      • 2019-06-14
      • 2010-11-16
      • 2018-08-22
      • 2020-09-17
      • 2019-05-22
      • 2020-06-16
      • 2011-06-19
      相关资源
      最近更新 更多