【问题标题】:Webpack production version of website points to the wrong directoriesWebpack生产版网站指向错误目录
【发布时间】:2020-03-18 14:58:13
【问题描述】:

我有两个 npm 命令 "build": "webpack --mode production""start": "webpack-dev-server --mode development --open"。当我运行start 命令时,Web 应用程序按预期运行,所有 CSS 和 javascript 都被正确转译。 但是,当我运行build 时,文件会按预期生成,并且当打开主 HTML 文件时,href 和 src 未指向正确的目录。

我查看了我的 webpack.config.js,它似乎按预期工作,这让我有点头疼。更改 HTML 模板会导致开发版本中断并使生产版本按预期工作。目的是让开发和生产版本都能按预期工作。

这里是我的目录结构供参考:

root
  |-.vscode
  |-dist
  |  |-css
  |  |-img
  |  |-js
  |    |- output location for bundle.js index.html main.css
  |-node.modules
  |-src
     |-css
     |  |-scss
     |-js
     |  |-JSON
     |  |-models
     |  |-view
     |  |-app.js
     |-index.html
     |-.babelrc
     |-package-lock.json
     |-package.json
     |-webpack.config.js

Webpack 配置文件

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const babelLoader = require("babel-loader");

module.exports = {
    entry: [
        "babel-polyfill","./src/js/app.js"
    ],
    output: {
        path: path.resolve(__dirname, "dist/js"),
        filename: "bundle.js",
    },
    devServer: {
        contentBase: "./dist"
    },
    plugins: [
        new MiniCssExtractPlugin(),
        new HtmlWebpackPlugin({
            filename:"index.html",
            template:"./src/index.html"
        })
    ],
    module: {
        rules: [
            {
                test: /\.m?js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ["@babel/preset-env"]
                    }
                }
            },
            {
                test: /\.scss$/,
                use: [{
                    loader: MiniCssExtractPlugin.loader
                },
                {
                    loader: "css-loader",
                    options: {
                        sourceMap: true,
                        modules: true,
                        localIdentName: "[local]"
                    }
                },
                    "sass-loader"
                ]
            }

        ]
    }
}

和 package.json

{
  "name": "tempName",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development",
    "build": "webpack --mode production",
    "start": "webpack-dev-server --mode development --open"
  },
  "author": "Fake Name",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.7.2",
    "@babel/preset-env": "^7.7.1",
    "babel-loader": "^8.0.6",
    "chart.js": "^2.9.3",
    "css-loader": "^2.1.1",
    "extract-text-webpack-plugin": "^4.0.0-beta.0",
    "gulp-babel": "^8.0.0",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.7.0",
    "node-sass": "^4.13.0",
    "sass-loader": "^7.3.1",
    "sortablejs": "^1.10.1",
    "style-loader": "^0.23.1",
    "url-loader": "^1.1.2",
    "webpack": "^4.41.2",
    "webpack-cli": "^3.3.10",
    "webpack-dev-server": "^3.9.0"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "babel-polyfill": "^6.26.0",
    "pokedex-promise-v2": "^3.2.0"
  }
}

如何在 HTML 文档中指定 href(s) 和 src(s) 以链接到 CSS 和 javascript,因为它们是在构建脚本期间添加的,而不是在 HTML 模板中?

【问题讨论】:

    标签: javascript json webpack directory


    【解决方案1】:

    试试这些:
    1) 而不是将 contentBase 设置为 dev-server 尝试设置 'publicPath: '/'
    2) 也将'output.publicPath' 设置为'/'
    3) 如果以前不起作用,HtmlWepbpackPluginbase 选项为加载的脚本添加前缀。但我强烈建议使用公共路径来处理它。

    PS:修复你的 prod 构建,并且仅在修复 webpack-dev-server 时。会更容易

    【讨论】:

    • 它完成了工作,但我认为我仍然误解了 WebPack 如何打包我的网站数据。我有图像显示在开发中,但可以在生产中找到。 ../img/item/002.png。浏览器似乎仍然认为我想从驱动器的根目录而不是相对目录中提取图像。
    猜你喜欢
    • 1970-01-01
    • 2013-09-20
    • 2017-03-20
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多