简体   繁体   English

如何停止将HTML输出到已编译的app.js?

[英]How can I stop HTML being output to the compiled app.js?

I am using Webpack 2 for my portfolio website, but it's not an SPA - therefore, the intention is not to output everything into my bundled JS. 我将Webpack 2用于我的投资组合网站,但它不是SPA-因此,其目的不是将所有内容输出到捆绑的JS中。

I have a few entry points for the .pug , .scss and .js files. 我对几个切入点.pug.scss.js文件。 Like so: 像这样:

entry: {
  app: [
    path.resolve(__dirname, 'src/pug/app.pug'),
    path.resolve(__dirname, 'src/js/app.js'),
    path.resolve(__dirname, 'src/scss/app.scss')
  ]
},
output: {
  path: path.resolve(__dirname, 'dist'),
  filename: 'js/[name].js'
}

However, when looking at the source for the app.js , I see the rendered HTML from my .pug files. 但是,当查看app.js的源代码时,我从.pug文件中看到了呈现的HTML。

在此处输入图片说明

For the .pug compiling, I'm using HtmlWebpackPlugin . 对于.pug编译,我使用的是HtmlWebpackPlugin I guess the easiest way for me to explain what's going on is to show you the webpack.config.babel.js file: 我想对我来说,最简单的方法是向您展示webpack.config.babel.js文件:

import webpack from 'webpack';
import path from 'path';
import autoprefixer from 'autoprefixer';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import SvgStorePlugin from 'external-svg-sprite-loader/lib/SvgStorePlugin';
import bourbon from 'bourbon';
import neat from 'bourbon-neat';

const extractScss = new ExtractTextPlugin({
  filename: 'css/[name].css',
  allChunks: true
});

const config = {
  entry: {
    app: [
      path.resolve(__dirname, 'src/pug/app.pug'),
      path.resolve(__dirname, 'src/js/app.js'),
      path.resolve(__dirname, 'src/scss/app.scss')
    ]
  },
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'js/[name].js'
  },
  module: {
    rules: [
      {
        test: /\.pug$/,
        use: [
          {
            loader: 'html-loader'
          },
          {
            loader: 'pug-html-loader',
            options: {
              pretty: false,
              exports: false
            }
          }
        ]
      },
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: [
          {
            loader: 'babel-loader',
            options: {
              presets: [
                ['es2015']
              ]
            }
          }
        ]
      },
      {
        test: /\.(jpe?g|png|gif)$/i,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '/images/[name].[ext]'
            }
          },
          {
            loader: 'image-webpack-loader',
            options: {
              progressive: false,
              optipng: {
                optimizationLevel: 7,
                quality: '90',
                speed: 5
              },
              mozjpeg: {
                quality: 90
              },
              gifsicle: {
                interlaced: false
              }
            }
          }
        ]
      },
      {
        test: /\.svg$/,
        use: [
          {
            loader: 'external-svg-sprite-loader',
            query: {
              name: 'svg/sprite.svg',
              iconName: '[name]'
            }
          }
        ]
      },
      {
        test: /\.scss$/,
        use: extractScss.extract([
          {
            loader: 'css-loader'
          },
          {
            loader: 'postcss-loader',
            options: {
              plugins() {
                return [
                  autoprefixer({
                    browsers: ['last 2 versions', 'Explorer >= 9', 'Android >= 4']
                  })
                ];
              }
            }
          },
          {
            loader: 'sass-loader',
            options: {
              includePaths: [
                bourbon.includePaths,
                neat.includePaths
              ]
            }
          }
        ])
      }
    ]
  },
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    stats: 'errors-only',
    open: false
  },
  plugins: [
    new SvgStorePlugin(),
    new HtmlWebpackPlugin({
      title: 'Portfolio',
      minify: {
        collapseWhitespace: true
      },
      hash: true,
      template: 'src/pug/app.pug',
      filetype: 'pug',
      filename: 'index.html'
    }),
    extractScss
  ]
}

process.traceDeprecation = false;

export default config;

I don't see any CSS in the app.js bundle and the entry point is setup just the same, so might it have something to do with the HtmlWebpackPlugin itself? 我在app.js捆绑包中没有看到任何CSS,并且入口点设置完全相同,因此它可能与HtmlWebpackPlugin本身有关吗? Perhaps I'm not understanding how this works correctly and my configuration is wrong. 也许我不了解这是如何正常工作的,而我的配置是错误的。

I'm new to Webpack (coming from Gulp), so please bear with me if the answers to my questions seem rather obvious. 我是Webpack的新手(来自Gulp),所以如果我的问题的答案似乎很明显,请耐心等待。

Thanks for your help. 谢谢你的帮助。

Update: For reference, my project structure is as follows: 更新:作为参考,我的项目结构如下:

在此处输入图片说明

And I would call an image from my .pug file in /pug/components/example.pug with a path like img(src="../images/example.jpg") . 我会从.pug文件中调用图像, /pug/components/example.pug使用img(src="../images/example.jpg")类的路径。 This worked prior to removing .pug as an entry point in the Webpack config as user Tatsuyuki suggested below. 在删除.pug作为Webpack配置中的入口点之前,这是可行的,如下面的用户Tatsuyuki所建议。

Do not add the template as an source: 不要将模板添加为源:

app: [
  // path.resolve(__dirname, 'src/pug/app.pug'),
  path.resolve(__dirname, 'src/js/app.js'),
  path.resolve(__dirname, 'src/scss/app.scss')
]

Instead, specify the template option for HtmlWebpackPlugin: 而是为HtmlWebpackPlugin指定模板选项:

new HtmlWebpackPlugin({
  template: 'src/index.pug'
})

Reference 参考

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

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