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

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

问题描述

Webpack docs 声明 output.publicPath 是:


视图中的 output.path JavaScript。

The output.path from the view of the JavaScript.

请您详细说明这实际意味着什么?

Could you please elaborate on what this actually means?

我使用 output.path output.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?"   
  } 
}


推荐答案

这是我发现的,希望它会帮助你们中的一些人。 (如果有任何问题,请纠正我):

This is what I found, hope it will help some of you. (Please correct me if anything is wrong):

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

Local disk directory to store all your output files (Absolute path).

示例: path.join(__ dirname,build /)

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

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

您上传捆绑文件的位置。 (相对于服务器根目录)

Where you uploaded your bundled files. (Relative to server root)

示例: / assets /

假设您在服务器根 http:// server / 部署了应用程序。

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

通过使用 / assets / ,该应用程序将在以下位置找到webpack资产: http:// server /资产/ 。在引擎盖下,webpack遇到的每个网址都会被重新编写,以 / 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 Re-写道➡ src =/ assets / img / picture.jpg

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

重要

如果您使用 style-loader +加载器与css sourceMap 选项已启用,您必须设置 publicPath 。将其设置为服务器地址的完整绝对路径,例如 http:// server / assets / 以便正确加载资源。

If you are using style-loader + loaders with css sourceMap option enabled, you have to set publicPath. Set it to the full absolute path of the server address, e.g. http://server/assets/so that resources will be loaded correctly.

请参阅:https://github.com/webpack/style-loader/issues/55

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

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