简体   繁体   English

HtmlWebpackPlugin插件不输出任何内容

[英]HtmlWebpackPlugin plugin doesn't output anything

I need to minify 2 JS files and inline one of them into HTML template. 我需要缩小2个JS文件,并将其中一个插入到HTML模板中。 I have the following directory structure: 我有以下目录结构:

- src
---- page.html
---- script-to-inline.js
---- script.js
- webpack.config.js

And the following webpack config: 以及以下webpack配置:

const webpack = require('webpack');
const Dotenv = require('dotenv-webpack');
const HtmlWebpackPlugin = require('dotenv-webpack');
const InlineScriptWebpackPlugin = require('dotenv-webpack');

module.exports = {
    context: __dirname,
    mode: 'production',
    entry: {
        script: './src/script.js',
        'script-to-inline': './src/script-to-inline.js',
    },
    output: {
        path: __dirname,
    },
    plugins: [
        new Dotenv(),
        new HtmlWebpackPlugin({
            inlineSource: './src/script-to-inline.js',
            template: './src/page.html',
            filename: 'page.html',
        }),
        new InlineScriptWebpackPlugin({
            names: ['script-to-inline'],
        }),
    ],
};

Build output is the following: 构建输出如下:

Hash: bb94c50e294f070b4fd1
Version: webpack 4.12.0
Time: 118ms
Built at: 2018-06-20 13:58:48
                      Asset      Size  Chunks             Chunk Names
tracking-script-injector.js  1.24 KiB       0  [emitted]  tracking-script-injector
                tracking.js  1.28 KiB       1  [emitted]  tracking
[0] ./src/tracking-script-injector.js 971 bytes {0} [built]
[1] ./src/tracking.js 630 bytes {1} [built]

It looks like HtmlWebpackPlugin doesn't even try to read the template I pass to its options. 看起来HtmlWebpackPlugin甚至都没有尝试读取我传递给它的选项的模板。

What should I change to make plugin process the page.html file? 我应该更改什么以使插件处理page.html文件?

It is not working because the require statement is not referencing correctly to the plugin (which is weird too because webpack did'nt yell at you). 它不起作用,因为require语句未正确引用插件(这也很奇怪,因为webpack不会对您大喊大叫)。

const HtmlWebpackPlugin = require('dotenv-webpack');

It should be: 它应该是:

const HtmlWebpackPlugin = require('html-webpack-plugin');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM