vue-cli2 引入固定cdn资源操作记录

vue引入cdn也不是什么神奇的操作,但是自己之前一直没有尝试过,这次正好项目优化需要,所以,实操一波,特此记录

本次cnd引入了如下资源

  • vue
  • vue-router
  • axios
  • vue-lazyload

    暂时就是上面四个,没有用到vuex,本来这次还引入了vue_i18n,但是通过cdn引入一直报错,很蛋疼,如果有谁知道怎么做烦请赐教。

    cnd资源都是https://www.jsdelivr.com/ 上面搜的

    稍微改造了

    build/webpack.base.conf.js

    build/webpack.dev.conf.js

    build/webpack.prod.conf.js
//webpack.base.conf.js
'use strict'
const path = require('path')
const utils = require('./utils')
const config = require('../config')
const vueLoaderConfig = require('./vue-loader.conf') function resolve (dir) {
return path.join(__dirname, '..', dir)
} module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production'
? config.build.assetsPublicPath
: config.dev.assetsPublicPath
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
},
{
test: /\.scss$/,
loaders: ["style", "css", "sass"]
}
]
},
node: {
// prevent webpack from injecting useless setImmediate polyfill because Vue
// source contains it (although only uses it if it's native).
setImmediate: false,
// prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty'
},
externals: {
'vue': 'Vue',
'vue-router': 'VueRouter',
'axios': 'axios',
'vue-lazyload':'VueLazyload'
}
}
//webpack.dev.conf.js,这个没有复制全,就改动了这么一小个地方
new HtmlWebpackPlugin(Object.assign(
{
filename: 'index.html',
template: 'index.html',
inject: true
},
config.dev.cdn
)
),
//webpack.prod.conf.js

 new HtmlWebpackPlugin(Object.assign(
{
filename: config.build.index,
template: 'index.html',
inject: true,
minify: {
removeAttributeQuotes:true,
minimize: true,
removeConments: true,
collapseWhitespace: true,
minifyCSS: true,
minifyJS: true,
// more options:
// https://github.com/kangax/html-minifier#options-quick-reference
},
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'
},
config.build.cdn)
),

config/index.js

'use strict'
// Template version: 1.3.1
// see http://vuejs-templates.github.io/webpack for documentation. const path = require('path') module.exports = {
dev: { // Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
}, // Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 8080, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
autoOpenBrowser: false,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions- /**
* Source Maps
*/ // https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map', // If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true, cssSourceMap: false,
cdn: {
js: [
"https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.js",
"https://cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.js",
"https://cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js",
"https://cdn.jsdelivr.net/npm/vue-lazyload@1.3.3/vue-lazyload.js"
]
}
}, build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'), // Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: './', /**
* Source Maps
*/ productionSourceMap: false,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map', // Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'], // Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report,
cdn: {
js: [
"https://cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js",
"https://cdn.jsdelivr.net/npm/vue-router@3.1.3/dist/vue-router.min.js",
"https://cdn.jsdelivr.net/npm/axios@0.19.0/dist/axios.min.js",
"https://cdn.jsdelivr.net/npm/vue-lazyload@1.3.3/vue-lazyload.min.js"
]
}
}
}

最后就是index.html了

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no ,viewport-fit=cover" name="viewport">
<title>test</title>
</head>
<body>
<div id="app"></div>
<% for (var i in htmlWebpackPlugin.options.js&&htmlWebpackPlugin.options.js) { %>
<script src="<%= htmlWebpackPlugin.options.js[i] %>"></script>
<% } %>
</body>
</html>

基本上这样就可以了,其实还有一些第三方插件,还不知道怎么通过cdn引入,有哪位大大比较有经验的,希望不吝赐教

vue-cli2 使用cdn资源的更多相关文章

  1. Vue项目使用CDN优化首屏加载

    前言 作为一个网站应用,加载速度是非常重要的.加载速度,一个是程序的合理安排,如以组件按需加载,一个是js.css等资源的异步加载. 在Vue项目中,引入到工程中的所有js.css文件,编译时都会被打 ...

  2. vue/cli2.0优化

    vue/cli2.0 脚手架 在项目写完了之后, 运行npm run build --report可以看出这个项目的资源占比情况.可以看出整个项目哪一个资源在整个项目占比最大.它会自动打开一个可视化的 ...

  3. vue.js 的cdn 链接的引用地址

    引用地址有两种一种完整版,一种压缩版效果是一样的 https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js https://cdn.bootcss.com ...

  4. Vue 全家桶学习资源(转)

    companion: React 全家桶学习资源(持续更新) 下面整理了一些关于Vue以及Vue衍生的学习资源: 官网文档 官网API ECMAScript 6 入门 30分钟掌握ES6/ES2015 ...

  5. vue+vue-cli2+webpack配置资源cdn

    vue-cli2+webpack构建的vue项目如何让图片和js等静态资源走cdn,哪里可以配置呢?下面我详细介绍 1.config/index.js中可以看到 module.exports = { ...

  6. 百度cdn资源公共库共享及常用开发接口

    CDN公共库是指将常用的JS库存放在CDN节点,以方便广大开发者直接调用 网站:http://cdn.code.baidu.com/ 常用资源: jquery: http://libs.baidu.c ...

  7. 通过AccessKey调用阿里云CDN接口刷新CDN资源案例

    通过AccessKey远程调用阿里云CDN接口,快速实现自动化集成部署. CdnService.java package com.nfky.cdn; import com.aliyuncs.Defau ...

  8. vue项目打包后资源相对引用路径的和背景图片路径问题

    vue项目中若要使用相对路径来获得相应静态资源,需要修改以下内容来确保项目打包后能正常运行. 1.修改config => index.js => build => assetsPub ...

  9. vue实践---vue不依赖外部资源实现简单多语

    vue使用多语,最常见的就是 vue-i18n, 但是如果开发中的多语很少,比如就不到10个多语,这样就没必要引入vue-i18n了, 引入了反正导致代码体积大了,这时候单纯用vue实现多语就是比较好 ...

随机推荐

  1. DeepWalk论文精读:(1)解决问题&相关工作

    模块1 1. 研究背景 随着互联网的发展,社交网络逐渐复杂化.多元化.在一个社交网络中,充斥着不同类型的用户,用户间产生各式各样的互动联系,形成大小不一的社群.为了对社交网络进行研究分析,需要将网络中 ...

  2. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  3. Codeforce 1255 Round #601 (Div. 2) A. Changing Volume (贪心)

    Bob watches TV every day. He always sets the volume of his TV to bb. However, today he is angry to f ...

  4. 【Java新特性】Lambda表达式典型案例,你想要的的都在这儿了!!

    写在前面 不得不说,有些小伙伴的学习热情真高,学完了Lambda表达式的语法,想来几个典型案例再强化下.于是问冰河能否给几个Lambda表达式的典型使用示例.于是乎,便有了这篇文章. 案例一 需求 调 ...

  5. 真香!PySpark整合Apache Hudi实战

    1. 准备 Hudi支持Spark-2.x版本,你可以点击如下链接安装Spark,并使用pyspark启动 # pyspark export PYSPARK_PYTHON=$(which python ...

  6. STM32 TIM高级定时器死区时间的计算

    STM32 TIM高级定时器的互补PWM支持插入死区时间,本文将介绍如何计算以及配置正确的死区时间. 文章目录 什么是死区时间? 数据手册的参数 如何计算合理的死区时间? STM32中配置死区时间 什 ...

  7. Linux共享库简单总结

    库 静态库 编译的二进制会重新包含一份静态库的副本 共享库 编译 gcc -shared -o file.c -fPIC 链接 ld ld-linux.so.2 可执行程序–>动态依赖表 流程: ...

  8. ubuntu上lib-ace库安装编译

    描述下本人电脑情况: 虚拟机版本:VMware-workstation-full-v7.1.4: ACE版本:ACE6.0.0 虚拟机[Linux](http://lib.csdn.net/base/ ...

  9. opengl简单入门实例

    实现任务目标: 使用纹理贴图,增强可视效果 应用坐标变换,实现场景中不同物体重建 采用双缓冲技术,实现场景实时绘制 具有一定的鼠标.键盘交互功能 先放效果 鼠标的交互功能有:右键暂停转动,左键继续转动 ...

  10. 最近常问的99道Java多线程面试题 !

    前言 今天给大家更新的是一篇关于多线程面试的文章,也是霸哥根据时下热门的面试内容给大家进行总结的, 本篇文章属于干货内容! 请各位读者朋友一定要坚持读到最后,完整阅读本文后相信你对多线程会有不一样感悟 ...