javascript - Ask for advice on using cdn with vue
学习ing
学习ing 2017-07-05 10:36:18
0
5
1258

please teach me,

import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
//import Mock from './mock'
//Mock.bootstrap();
import 'font-awesome/css/font-awesome.min.css'

How to use cdn to reference such public js files. Currently, it is installed by npm install, all are local, and exports are limited. Many packages can be imported using cdn. But currently they are all operated by the Vue framework, and there is no writing place directly imported from HTML. Please tell me how to use a public CDN like https://cdn.bootcss.com/eleme... in the project.

学习ing
学习ing

reply all(5)
ringa_lee
resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': resolve('src')
    }
  },
  externals: {
      jquery: 'jQuery.noConflict()', //或者jquery:'jQuery',
      $: 'jQuery.noConflict()'
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
    }

Webpack is configured like this, html introduces cdn’s jquery

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>lawyer_fe</title>
  <link rel="stylesheet" type="text/css" href="/static/normalize.css">
  <link rel="stylesheet" type="text/css" href="/static/cssreset.css">
  <link rel="stylesheet" type="text/css" href="http://unpkg.com/iview/dist/styles/iview.css">
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
女神的闺蜜爱上我

Just import it directly from CDN in html, there is no need to import it and package/compress it together

大家讲道理

If the third-party library has a CDN address, it can be directly introduced into the HTML, in the HTML of the template.
Then you can also throw the code to your own cdn, manage it uniformly, and process it in the same way as your other static files. For example, your img files are placed in cdnUrl+projectName/img/, and these third-party libraries are also thrown on it. .
You are currently managed by npm package locally, so if you import it when you quote it, it will definitely be packaged by webpack... This involves the issue of webpack. Let’s see if we can solve the current problem first

小葫芦

You can take a look at the webpack documentation. It is written in the documentation, which is quite detailed. Take jQuery as an example

https://doc.webpack-china.org...

淡淡烟草味

Solving your problem requires the following steps
1. Extract the js files installed locally by npm and introduced through import. This part can be extracted through the CommonsChunkPlugin plug-in. Refer to webpack code separation

For example:

entry: {
    main:['./src/index.js'],
    vue:['vue'],
    jquery:['jquery']    
  }
...
plugins: [
    new webpack.optimize.CommonsChunkPlugin({
          name: ['vue','jquery'], // 指定公共 bundle 的名字。
          minChunks: function(module){
            return module.context && module.context.indexOf("node_modules") !== -1;
        }
      })
]

2. Use HtmlWebpackPlugin to solve the problem of path and file name after js packaging

    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: './src/index.html',//模板路径
            inject: true,
            hash:true,
            minify: {
                removeComments: true,
                collapseWhitespace: true,
                removeAttributeQuotes: true
                // more options:
                // https://github.com/kangax/html-minifier#options-quick-reference
           }
       })
   ]
以上资源路径配置在output项
// webpack.config.js
output: {
    ...
    publicPath: debug ? 'build/' : 'https://cdn.bootcss.com/element-ui'
}

The final generated effect is like this

// 生产环境
// a.html
<script src="https://cdn.bootcss.com/element-ui/js/460de4b8.vue.js"></script>
<script src="https://cdn.bootcss.com/element-ui/js/e7d20340.a.min.js"></script>

Your problem mainly lies in the extraction of the above public js files. After extraction, it is a personal choice whether to use HtmlWebpackPlugin to automatically add resource paths or to add them manually, so the focus is on the first step

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template