账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    webpack-dev-server 更改index.html目录后无法访问
    36
    0

    我平时用 webpack-dev-server --inline --port 9000 --open 会默认访问我dist/index.html页面,
    现在dist目录结构改了一下 , 将index.html放在了view目录下 , 然后将命令改为
    webpack-dev-server --inline --port 9000 --open --content-base dist/view/

    ├─dist
    │  ├─js
    │  │   base.js
    │  │   index.js
    │  │   login.js
    │  │
    │  └─view
    │       index.html
    │       login.html

    但是自动打开的页面却访问失败... 一定要手动加/view访问 , http://localhost:9000/view才行 , 请问有没有什么办法.. ?
    下面是webpack的代码...

    var path = require('path');
    var webpack = require('webpack');
    const CleanWebpackPlugin = require('clean-webpack-plugin');
    const ExtractTextPlugin = require("extract-text-webpack-plugin");
    const HtmlWebpackPlugin = require('html-webpack-plugin');
    var getHtmlConfig = function (name) {
      return {
        filename: 'view/' + name + '.html',
        template: path.join(__dirname, 'src', 'view', `${name}.html`),
        chunks: ['common', name]
      }
    }
    module.exports = {
      entry: {
        'common': ['./src/page/common/index.js'],
        'index': ['./src/page/index/index.js'],
        'login': ['./src/page/login/index.js'],
      },
      output: {
        path: path.join(__dirname, 'dist'),
        filename: 'js/[name].js',
      },
      externals: {
        'jquery': 'window.jQuery'
      },
      resolve: {
          ....
      },
      module: {
          ....
      },
      plugins: [
        new CleanWebpackPlugin('dist'),
        new ExtractTextPlugin("css/[name].css"),
        new webpack.optimize.CommonsChunkPlugin({
          name: 'common',
          filename: 'js/base.js'
        }),
        //html模板的处理
        new HtmlWebpackPlugin(getHtmlConfig('index')),
        new HtmlWebpackPlugin(getHtmlConfig('login'))
      ]
    }
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • -注定、离开 普通会员 1楼

      在Webpack Dev Server中,当更改index.html文件目录后,由于服务器的缓存和重定向机制,可能会导致访问到旧的index.html文件,而无法直接访问新的文件。

      以下是一些可能导致此问题的步骤和解决方案:

      1. 首先,确保你的webpack.config.js文件中设置了一个正确的打包和热加载选项。例如,如果你使用了historyApiFallback选项,那么默认情况下,webpack-dev-server会将服务器重定向到旧的index.html文件。

      javascript module.exports = { output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/dist/' }, devServer: { historyApiFallback: true, hot: true, port: 3000, contentBase: './dist', publicPath: '/dist/' } };

      如果你的webpack.config.js中没有设置historyApiFallback选项,那么默认情况下webpack-dev-server将直接重定向到新的index.html文件。

      1. 如果你使用了webpack-hot-middleware,它会在热更新时生成一个index.html文件,并将其放在publicPath中。这意味着如果你在index.html文件中做了修改,服务器也会自动更新并重定向到新的文件。

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

      module.exports = { // ... devServer: { hot: true, port: 3000, contentBase: './dist', publicPath: '/dist/' }, module: { rules: [ { test: /.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-react'] } } }, { test: /.html$/, use: { loader: 'file-loader', options: { name: '[name].[ext]', outputPath: 'dist/' } } } ] }, plugins: [ new webpack.HotModuleReplacementPlugin() ] }; ```

      1. 如果你在webpack.config.js中设置了inline选项,那么webpack-dev-server会直接在index.html文件中应用你的代码。这意味着你可以在index.html文件中使用任何JavaScript代码,而无需使用<script>标签。

      javascript module.exports = { // ... devServer: { inline: true, port: 3000, contentBase: './dist', publicPath: '/dist/' }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-react'] } } }, { test: /\.html$/, use: { loader: 'file-loader', options: { name: '[name].[ext]', outputPath: 'dist/' } } } ] }, plugins: [ new webpack.HotModuleReplacementPlugin() ] };

      1. 如果你使用了proxy选项,它会在代理服务器上重定向请求。在index.html文件中,你可以将代理设置为新的服务器地址,如下所示:

      ```html

      Webpack Dev Server

      ```

      请注意,proxy选项需要在webpack.config.js中设置,而且需要在服务器端进行设置。在这个例子中,我们将代理设置为新的服务器地址http://localhost:3001/,并将http2https选项设置为true,以允许HTTP2和HTTPS请求。

    • 无尽思念 普通会员 2楼

      在Webpack Dev Server中,当更改index.html文件目录后,由于服务器的缓存和重定向机制,可能会导致访问到旧的index.html文件,而无法直接访问新的文件。

      以下是一些可能导致此问题的步骤和解决方案:

      1. 首先,确保你的webpack.config.js文件中设置了一个正确的打包和热加载选项。例如,如果你使用了historyApiFallback选项,那么默认情况下,webpack-dev-server会将服务器重定向到旧的index.html文件。

      javascript module.exports = { output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', publicPath: '/dist/' }, devServer: { historyApiFallback: true, hot: true, port: 3000, contentBase: './dist', publicPath: '/dist/' } };

      如果你的webpack.config.js中没有设置historyApiFallback选项,那么默认情况下webpack-dev-server将直接重定向到新的index.html文件。

      1. 如果你使用了webpack-hot-middleware,它会在热更新时生成一个index.html文件,并将其放在publicPath中。这意味着如果你在index.html文件中做了修改,服务器也会自动更新并重定向到新的文件。

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

      module.exports = { // ... devServer: { hot: true, port: 3000, contentBase: './dist', publicPath: '/dist/' }, module: { rules: [ { test: /.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-react'] } } }, { test: /.html$/, use: { loader: 'file-loader', options: { name: '[name].[ext]', outputPath: 'dist/' } } } ] }, plugins: [ new webpack.HotModuleReplacementPlugin() ] }; ```

      1. 如果你在webpack.config.js中设置了inline选项,那么webpack-dev-server会直接在index.html文件中应用你的代码。这意味着你可以在index.html文件中使用任何JavaScript代码,而无需使用<script>标签。

      javascript module.exports = { // ... devServer: { inline: true, port: 3000, contentBase: './dist', publicPath: '/dist/' }, module: { rules: [ { test: /\.js$/, exclude: /node_modules/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-react'] } } }, { test: /\.html$/, use: { loader: 'file-loader', options: { name: '[name].[ext]', outputPath: 'dist/' } } } ] }, plugins: [ new webpack.HotModuleReplacementPlugin() ] };

      1. 如果你使用了proxy选项,它会在代理服务器上重定向请求。在index.html文件中,你可以将代理设置为新的服务器地址,如下所示:

      ```html

      Webpack Dev Server

      ```

      请注意,proxy选项需要在webpack.config.js中设置,而且需要在服务器端进行设置。在这个例子中,我们将代理设置为新的服务器地址http://localhost:3001/,并将http2https选项设置为true,以允许HTTP2和HTTPS请求。

    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部