为webpack-dev-server

时间:2017-11-18 16:25:39

标签: javascript webpack webpack-dev-server hot-reload

通过弄清楚如何使用热重装设置webpack-dev-server ,又失去了一天。我想在index.html中使用以下路径投放我的JS和CSS资源:/dist/js/app.min.js/dist/css/styles.min.css

我想如何配置webpack.config.jsindex.html加载我在上面提到的正确路径的资产?使用当前配置webpack-dev-server仅在/dist文件夹中保存我的构建,在index.html中我必须设置没有js或css文件夹的路径,例如:(<script src="/dist/app.min.js" />)。

我的webpack.config.js:

const path = require('path');
const webpack = require('webpack');

module.exports = {
  context: path.resolve(__dirname, './'),
  entry: './src/app.js',
  output: {
    publicPath: '/dist/',
    path: path.join(__dirname, '/dist/js'),
    filename: 'app.min.js'
  },
  devServer: {
    hot: true
  },
  resolve: {
    extensions: ['.js', '.jsx', '.ts', '.tsx']
  },
  module: {
    rules: [
      {
        test: /\.(scss|sass|css)$/,
        use: ['style-loader', 'css-loader', 'sass-loader']
      }
    ]
  },
  plugins: [
    new webpack.NamedModulesPlugin(),
    new webpack.HotModuleReplacementPlugin()
  ]
};

我的项目结构:

Project structure

1 个答案:

答案 0 :(得分:0)

您熟悉HtmlWebpackPlugin吗? https://webpack.js.org/plugins/html-webpack-plugin/#src/components/Sidebar/Sidebar.jsx

这是我的设置:

new HtmlWebpackPlugin({
  template: project.paths.client('index.html'), // absolute url to index.html
  hash: false,
  filename: 'index.html',
  inject: 'body',
})

这会将您的条目录入并将其注入index.html