【问题标题】:Webpack doesn't bundle my HTML filesWebpack 没有捆绑我的 HTML 文件
【发布时间】:2017-02-05 05:22:03
【问题描述】:

出于某种我目前不明白的原因,webpack 不会将我的 HTML 文件捆绑到它生成的包中。它捆绑了其他所有内容,但遗憾的是没有捆绑我的 HTML 文件。

我正在使用 Angular 2,并且包含了对以下内容的依赖:

"html-loader": "^0.4.4",
"html-webpack-plugin": "^2.22.0",

webpack.config:

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var helpers = require('./helpers');

module.exports = {
    entry: {
        app: 'app/main.ts'
    },

    output: {
        path: helpers.root('dist'),
        publicPath: 'http://localhost:8080/',
        filename: 'bundle.js'
    },

    externals: [
        {
            './node_modules/core-js/client/shim.min.js': 'shim',
            './node_modules/zone.js/dist/zone.js': 'zone',
            './node_modules/reflect-metadata/Reflect.js': 'reflect',
            './node_modules/x2js/x2js.js': 'x2js'
        }
    ]
}

webpack.common:

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var helpers = require('./helpers');

module.exports = {
  entry: {
    'app': './app/main.ts'
  },

  resolve: {
    extensions: ['', '.js', '.ts', '.html']
  },

  module: {
    loaders: [
      {test: /\.ts$/, loaders: ['awesome-typescript-loader', 'angular2-template-loader']},
      {test: /\.html$/, loader: 'raw-loader' },
      {
        test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
        loader: 'file?name=assets/[name].[hash].[ext]'
      },
      {
        test: /\.css$/,
        exclude: helpers.root('app'),
        loader: ExtractTextPlugin.extract('style', 'css?sourceMap')
      },
      {
        test: /\.css$/,
        include: helpers.root('app'),
        loader: 'raw'
      }
    ]
  },

  plugins: [
    new webpack.optimize.CommonsChunkPlugin({
      name: ['app', 'vendor', 'polyfills']
    }),

    new HtmlWebpackPlugin({
      template: 'index.html',
      chunksSortMode: 'dependency'
    })
  ]
};

还有 webpack.prod:

var webpack = require('webpack');
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: 'source-map',

  output: {
    path: helpers.root('dist'),
    publicPath: '/',
    filename: '[name].[hash].js',
    chunkFilename: '[id].[hash].chunk.js'
  },

  htmlLoader: {
    minimize: false // workaround for ng2
  },

  plugins: [
    new webpack.NoErrorsPlugin(),
    new webpack.optimize.DedupePlugin(),
    new ExtractTextPlugin('[name].[hash].css')
  ]
});

我正在通过以下方式在组件中提取 HTML:

@Component({
  ....
  template: require('app/customer-client/customer-client.html'),
  ....
  )}

据我从docs 得知,我认为这应该足够了,但我仍然缺少一些东西。

请注意,我也尝试过使用 raw-loader,它也无法捆绑 HTML 文件。我想知道导入的格式是否需要更接近 "require("html!./file.html");"所以我也试过了,没有成功。我没有收到任何错误或任何调试信息,不幸的是,我所拥有的只是缺少 HTML 文件。有什么想法吗?

【问题讨论】:

    标签: html angularjs angular webpack webpack-html-loader


    【解决方案1】:

    在您的 webpack 配置中,您是否也将“.html”添加到 resolve.extensions 数组中?

    resolve: {
        extensions: ['.html', '.js', ....]
    }
    

    【讨论】:

    • 我没有,但不幸的是,这并没有解决问题。我将编辑我的帖子以包含完整的 webpack 配置,以防万一:-)。
    猜你喜欢
    • 1970-01-01
    • 2018-09-09
    • 2018-08-23
    • 2017-02-27
    • 2023-03-25
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多