“publicPath"是什么?在 Webpack 中做什么? [英] What does "publicPath" in Webpack do?

查看:36
本文介绍了“publicPath"是什么?在 Webpack 中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Webpack 文档 声明 output.publicPath是:

output.path 从 JavaScript 的角度来看.

The output.path from the view of the JavaScript.

您能否详细说明这实际上意味着什么?

Could you please elaborate on what this actually means?

我使用 output.pathoutput.filename 来指定 Webpack 应该在哪里输出结果,但我不确定在 output 中放什么.publicPath 以及是否需要.

I use output.path and output.filename to specify where Webpack should output the result, but I’m not sure what to put in output.publicPath and whether it is required.

module.exports = {
  output: {
    path: path.resolve("./examples/dist"),
    filename: "app.js",
    publicPath: "What should I put here?"   
  } 
}

推荐答案

output.path

用于存储所有输出文件的本地磁盘目录(绝对路径).

示例: path.join(__dirname, "build/")

Webpack 会将所有内容输出到 localdisk/path-to-your-project/build/

Webpack will output everything into localdisk/path-to-your-project/build/

您上传捆绑文件的位置.(绝对路径,或相对于主 HTML 文件)

Where you uploaded your bundled files. (absolute path, or relative to main HTML file)

示例: /assets/

假设您将应用部署在服务器根目录 http://server/.

Assumed you deployed the app at server root http://server/.

通过使用 /assets/,应用程序将在以下位置找到 webpack 资产:http://server/assets/.在后台,webpack 遇到的每个 url 都将被重写为以/assets/"开头.

By using /assets/, the app will find webpack assets at: http://server/assets/. Under the hood, every urls that webpack encounters will be re-written to begin with "/assets/".

src=picture.jpg" 重写➡ src=/assets/picture.jpg"

访问者:(http://server/assets/picture.jpg)

src=/img/picture.jpg" 重写➡ src=/assets/img/picture.jpg"

访问者:(http://server/assets/img/picture.jpg)

这篇关于“publicPath"是什么?在 Webpack 中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆