vue-cli設置publicPath小記

幾種設置publicPath後,再對比打包後的index.html文件css

測試背景:

  • 每次打包build完後,都單獨生成一個/dist文件夾,且dist中每次都只有相同文件目錄
  • 部署的時候,是部署在服務器的一個/test文件夾下

打包後的文件目錄:

├─dist
  ├─css
  ├─img
  └─js
  index.html
複製代碼

1、不設置publicPath時,部署後請求路徑:http://111.222.333.444:8888/css/app.0b79487b.css

// vue.config.js

module.exports = {
    // publicPath: '',
}
複製代碼
<!DOCTYPE html>
<html lang=en>
<head>
    <title>test</title>
    <link href=/css/app.0b79487b.css rel=preload as=style>
    <link href=/js/app.ba2d9b8a.js rel=preload as=script>
    <link href=/js/chunk-vendors.e7ac9ff2.js rel=preload as=script>
    <link href=/css/app.0b79487b.css rel=stylesheet>
</head>
<body>
<div id=app></div>
<script src=/js/chunk-vendors.e7ac9ff2.js></script>
<script src=/js/app.ba2d9b8a.js></script>
</body>
</html>
複製代碼

2、設置爲/時,部署後請求路徑:http://111.222.333.444:8888/css/app.0b79487b.css

// vue.config.js

module.exports = {
    publicPath: '/',
}
複製代碼
<!DOCTYPE html>
<html lang=en>
<head>
    <title>test</title>
    <link href=/css/app.0b79487b.css rel=preload as=style>
    <link href=/js/app.ba2d9b8a.js rel=preload as=script>
    <link href=/js/chunk-vendors.e7ac9ff2.js rel=preload as=script>
    <link href=/css/app.0b79487b.css rel=stylesheet>
</head>
<body>
<div id=app></div>
<script src=/js/chunk-vendors.e7ac9ff2.js></script>
<script src=/js/app.ba2d9b8a.js></script>
</body>
</html>
複製代碼

3、設置爲./時,部署後請求路徑:http://111.222.333.444:8888/test/css/app.0b79487b.css

// vue.config.js

module.exports = {
    publicPath: './',
}
複製代碼
<!DOCTYPE html>
<html lang=en>
<head>
    <title>test</title>
    <link href=css/app.0b79487b.css rel=preload as=style>
    <link href=js/app.8569d42d.js rel=preload as=script>
    <link href=js/chunk-vendors.e7ac9ff2.js rel=preload as=script>
    <link href=css/app.0b79487b.css rel=stylesheet>
</head>
<body>
<div id=app></div>
<script src=js/chunk-vendors.e7ac9ff2.js></script>
<script src=js/app.8569d42d.js></script>
</body>
</html>
複製代碼

4、設置爲static時,部署後請求路徑:http://111.222.333.444:8888/test/static/css/app.0b79487b.css

// vue.config.js

module.exports = {
    publicPath: 'static',
}
複製代碼
<!DOCTYPE html>
<html lang=en>
<head>
    <title>test</title>
    <link href=static/css/app.0b79487b.css rel=preload as=style>
    <link href=static/js/app.d0717808.js rel=preload as=script>
    <link href=static/js/chunk-vendors.e7ac9ff2.js rel=preload as=script>
    <link href=static/css/app.0b79487b.css rel=stylesheet>
</head>
<body>
<div id=app></div>
<script src=static/js/chunk-vendors.e7ac9ff2.js></script>
<script src=static/js/app.d0717808.js></script>
</body>
</html>
複製代碼

5、設置爲./static時,部署後請求路徑:http://111.222.333.444:8888/test/static/css/app.0b79487b.css

// vue.config.js

module.exports = {
    publicPath: './static',
}
複製代碼
<!DOCTYPE html>
<html lang=en>
<head>
    <title>test</title>
    <link href=static/css/app.0b79487b.css rel=preload as=style>
    <link href=static/js/app.d0717808.js rel=preload as=script>
    <link href=static/js/chunk-vendors.e7ac9ff2.js rel=preload as=script>
    <link href=static/css/app.0b79487b.css rel=stylesheet>
</head>
<body>
<div id=app></div>
<script src=static/js/chunk-vendors.e7ac9ff2.js></script>
<script src=static/js/app.d0717808.js></script>
</body>
</html>
複製代碼

6、設置爲../static時,部署後請求路徑:http://111.222.333.444:8888/static/css/app.0b79487b.css

// vue.config.js

module.exports = {
    publicPath: '../static',
}
複製代碼
<!DOCTYPE html>
<html lang=en>
<head>
    <title>test</title>
    <link href=../static/css/app.0b79487b.css rel=preload as=style>
    <link href=../static/js/app.695b7ccc.js rel=preload as=script>
    <link href=../static/js/chunk-vendors.e7ac9ff2.js rel=preload as=script>
    <link href=../static/css/app.0b79487b.css rel=stylesheet>
</head>
<body>
<div id=app></div>
<script src=../static/js/chunk-vendors.e7ac9ff2.js></script>
<script src=../static/js/app.695b7ccc.js></script>
</body>
</html>
複製代碼

7、設置爲../時,部署後請求路徑:http://111.222.333.444:8888/css/app.0b79487b.css

// vue.config.js

module.exports = {
    publicPath: '../',
}
複製代碼
<!DOCTYPE html>
<html lang=en>
<head>
    <title>test</title>
    <link href=../css/app.0b79487b.css rel=preload as=style>
    <link href=../js/app.67ace555.js rel=preload as=script>
    <link href=../js/chunk-vendors.e7ac9ff2.js rel=preload as=script>
    <link href=../css/app.0b79487b.css rel=stylesheet>
</head>
<body>
<div id=app></div>
<script src=../js/chunk-vendors.e7ac9ff2.js></script>
<script src=../js/app.67ace555.js></script>
</body>
</html>
複製代碼
相關文章
相關標籤/搜索