admin管理员组

文章数量:1296310

Note: I'm a beginner to webpack.

I'm trying to get webpack to load my .htmls files (index.html and login.html) as they will serve as windows for my electron application. This is what I have tried so far with no results:

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

and

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

This is my webpack.config.js file:

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" // piles Sass to CSS
                ]
            },
            {
                test: /\.(ttf|eot|woff|woff2)$/,
                loader: "file-loader",
                options: {
                    name: "fonts/[name].[ext]"
                }
            }
        ]
    },

    devServer: {
        inline: true,
        contentBase: outDir,
        press: 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"
};

What am I doing wrong? Webpack builds everything into the /build yet the .html files (index.html and login.html, which are under my /src/ directory) are not included.

Note: I'm a beginner to webpack.

I'm trying to get webpack to load my .htmls files (index.html and login.html) as they will serve as windows for my electron application. This is what I have tried so far with no results:

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

and

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

This is my webpack.config.js file:

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" // piles Sass to CSS
                ]
            },
            {
                test: /\.(ttf|eot|woff|woff2)$/,
                loader: "file-loader",
                options: {
                    name: "fonts/[name].[ext]"
                }
            }
        ]
    },

    devServer: {
        inline: true,
        contentBase: outDir,
        press: 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"
};

What am I doing wrong? Webpack builds everything into the /build yet the .html files (index.html and login.html, which are under my /src/ directory) are not included.

Share Improve this question asked Jul 16, 2018 at 0:11 John DoeJohn Doe 4191 gold badge6 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

The loader configuration for HTML files will allow require calls with HTML files to work inside javascript files. With the file loader you'll get a file path and with the HTML loader you'll get the HTML content as a result of the call.

If you want your HTML files to be copied alongside with your piled sources you'll have to use a plugin like copy-webpack-plugin or html-webpack-plugin.

本文标签: javascriptget webpack to include html filesStack Overflow