此文不介绍webpack基本配置,如果对基本配置有疑问请查阅官方文档。
1、配置webpack.config.js
将output.publicpath改成上传到的cdn地址, 例(对应上面上传配置):
publicpath: https://your_base_cdn_url + process.env.node_env + /cdn/
打包
node_env=production node_modules/webpack/bin/webpack.js -p
这样打包后的文件例如有
index.html 12345678.src.js 12345678.src.css ...
此时,打包后生成的index.html文件中已经引入了cdn文件。
<html lang="en"> <head> <title>title</title> <link href="https://your_base_cdn_url/production/cdn/12345678.src.css" rel="external nofollow" rel="stylesheet"> </head> <body id="body"> <p id="root"></p> <script src="https://your_base_cdn_url/production/cdn/12345678.src.js"></script></body> </html>
2、上传文件至cdn
在部署脚本中写一段上传cdn的脚本, 例:
echo start uploading to upyun host=v0.ftp.upyun.com user=uploader/your-username pass=your-password cd build files=$(ls | grep -v 'index.html' | xargs) ftp -inv $host <
server { listen 80; server_name your_server_name; access_log /var/log/nginx/your_project.log; root /var/www/your_project/production/current; location / { try_files $uri /index.html =404; add_header pragma no-cache; expires -5y; } location ~ \.(js|css)$ { expires 360000; add_header cache-control max-age=360000;; } }
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
easyui日期时间框在ie中的兼容性如何处理
vue判断input输入内容有否有空格
filereader实现上传图片之前本地先预览
以上就是cdn加速react webpack打包文件过程的详细内容。