This time I will show you how to use the publicPath path in Webpack, and what are the precautions for using the publicPath path in Webpack. The following is a practical case, let's take a look.
output
The output option specifies the location of the webpack output. The more important and often used ones are path and publicPathoutput.path
Default value: process.cwd()output.path just indicates the output directory, corresponding to an absolute path, such as usually in a project The following configuration will be made:output: { path: path.resolve(dirname, '../dist'), }
output.publicPath
Default value: empty The publicPath is specified in the official documentation The explanation is that webpack provides a very useful configuration that can help you specify a base path for all resources in the project, which is called the public path (publicPath). There is no clear explanation on how to apply this path...In fact, the basic path of all resources mentioned here refers to Final access path of static resources = output.publicPath Resource loader or plug-in configuration path For exampleoutput.publicPath = '/dist/' // image options: { name: 'img/[name].[ext]?[hash]' } // 最终图片的访问路径为 output.publicPath + 'img/[name].[ext]?[hash]' = '/dist/img/[name].[ext]?[hash]' // js output.filename output: { filename: '[name].js' } // 最终js的访问路径为 output.publicPath + '[name].js' = '/dist/[name].js' // extract-text-webpack-plugin css new ExtractTextPlugin({ filename: 'style.[chunkhash].css' }) // 最终css的访问路径为 output.publicPath + 'style.[chunkhash].css' = '/dist/style.[chunkhash].css'This final static resource access path can be seen in the HTML obtained after packaging using html-webpack-plugin. Therefore, after publicPath is set to a relative path, the relative path is relative to index.html after build. For example, if publicPath: './dist/' is set, the reference path of packaged js is ./dist/main.js. But there is a problem here. Relative paths are OK when accessing local, but if static resources are hosted on CDN, the access path obviously cannot use relative paths. However, if publicPath is set to /, the access path after packaging is localhost:8080/ dist/main.js, cannot be accessed locallySo here you need to manually change the publicPath when going online. It feels not very convenient, but I don’t know how to solve it...Generally, publicPath should be End with '/', and the configuration of other loaders or plug-ins should not start with '/'
publicPath in webpack-dev-server
Click to view the official documentation Introduction to devServer.publicPathIn the development stage, we use devServer to start a development server for development. A publicPath will also be configured here. The packaged files under the publicPath path here can be accessed in the browser. Static resources still use output.publicPath. The content packaged by webpack-dev-server is placed in memory. The external root directory of these packaged resources is publicPath. In other words, here we set the location where the packaged resources are stored.For example:// 假设devServer的publicPath为 const publicPath = '/dist/' // 则启动devServer后index.html的位置为 const htmlPath = `${pablicPath}index.html` // 包的位置 cosnt mainJsPath = `${pablicPath}main.js`The above can be accessed directly through http://lcoalhost:8080/dist/main.js. By accessing http://localhost:8080/webpack-dev-server, you can get the resource access path after devServer is started. As shown in the figure, click on the static resource to see that the access path of the static resource is http: //localhost:8080${publicPath}index.html
##html-webpack-plugin This plug-in is used to add css and js to html templates, where template and filename will be affected by the path. From the source code, we can see the
templateeffect : used to define the path of the template file
Source code:
Copy codeThe code is as follows: this.options.template = this.getFullTemplatePath(this .options.template, compiler.context);
Therefore, template will only be recognized if it is defined in the context of webpack. The default value of webpack context is process.cwd(), which is the file in which the node command is run. Absolute path to the folder
filenameFunction: The output HTML file name, the default is index.html, can be directly configured with subdirectories Source code: Copy code The code is as follows: this.options.filename = path.relative(compiler.options.output.path, filename); So the path of filename is relative to output.path, and in webpack-dev- In server, it is relative to the publicPath configured in webpack-dev-server. If the publicPath of webpack-dev-server is inconsistent with output.publicPath, using html-webpack-plugin may cause failure to reference static resources, because static resources are still referenced by output.publicPath in devServer, and webpack The resource access paths provided by -dev-server are inconsistent and cannot be accessed normally. There is one exception, that is, output.publicPath is a relative path, and local resources can be accessed at this time. So in general, it is necessary to ensure that the publicPath in devServer is consistent with output.publicPath. Finally About the path in webpack, that’s all. In the process of studying the webpack path, I saw some information about the path. The fragmentary knowledge is as follows: The meaning of slash/ / in the configuration represents the url root path, such as http://localhost:8080/dist/js/test.js ://localhost:8080/ devServer.publicPath & devServer.contentBase Path in node Using JS to determine the content contained in a string Summary of methods
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! Recommended reading:
The above is the detailed content of How to use publicPath path in Webpack. For more information, please follow other related articles on the PHP Chinese website!

Vue是一款优秀的JavaScript框架,它可以帮助我们快速构建交互性强、高效性好的Web应用程序。Vue3是Vue的最新版本,它引入了很多新的特性和功能。Webpack是目前最流行的JavaScript模块打包器和构建工具之一,它可以帮助我们管理项目中的各种资源。本文就为大家介绍如何使用Webpack打包和构建Vue3应用程序。1.安装Webpack

区别:1、webpack服务器启动速度比vite慢;由于vite启动的时候不需要打包,也就无需分析模块依赖、编译,所以启动速度非常快。2、vite热更新比webpack快;vite在HRM方面,当某个模块内容改变时,让浏览器去重新请求该模块即可。3、vite用esbuild预构建依赖,而webpack基于node。4、vite的生态不及webpack,加载器、插件不够丰富。

随着Web开发技术的不断发展,前后端分离、模块化开发已经成为了一个广泛的趋势。PHP作为一种常用的后端语言,在进行模块化开发时,我们需要借助一些工具来实现模块的管理和打包,其中webpack是一个非常好用的模块化打包工具。本文将介绍如何使用PHP和webpack进行模块化开发。一、什么是模块化开发模块化开发是指将程序分解成不同的独立模块,每个模块都有自己的作

在vue中,webpack可以将js、css、图片、json等文件打包为合适的格式,以供浏览器使用;在webpack中js、css、图片、json等文件类型都可以被当做模块来使用。webpack中各种模块资源可打包合并成一个或多个包,并且在打包的过程中,可以对资源进行处理,如压缩图片、将scss转成css、将ES6语法转成ES5等可以被html识别的文件类型。

Webpack是一款模块打包工具。它为不同的依赖创建模块,将其整体打包成可管理的输出文件。这一点对于单页面应用(如今Web应用的事实标准)来说特别有用。

配置方法:1、用导入的方法把ES6代码放到打包的js代码文件中;2、利用npm工具安装babel-loader工具,语法“npm install -D babel-loader @babel/core @babel/preset-env”;3、创建babel工具的配置文件“.babelrc”并设定转码规则;4、在webpack.config.js文件中配置打包规则即可。

web前端打包工具有:1、Webpack,是一个模块化管理工具和打包工具可以将不同模块的文件打包整合在一起,并且保证它们之间的引用正确,执行有序;2、Grunt,一个前端打包构建工具;3、Gulp,用代码方式来写打包脚本;4、Rollup,ES6模块化打包工具;5、Parcel,一款速度极快、零配置的web应用程序打包器;6、equireJS,是一个JS文件和模块加载器。

web有前端,也有后端。web前端也被称为“客户端”,是关于用户可以看到和体验的网站的视觉方面,即用户所看到的一切Web浏览器展示的内容,涉及用户可以看到,触摸和体验的一切。web后端也称为“服务器端”,是用户在浏览器中无法查看和交互的所有内容,web后端负责存储和组织数据,并确保web前端的所有内容都能正常工作。web后端与前端通信,发送和接收信息以显示为网页。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
