码迷,mamicode.com
首页 > 其他好文 > 详细

vue 全局引用jq(打包后可能会遇到的问题)

时间:2019-07-31 17:02:31      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:webp   join   引入   back   res   UNC   window   proc   pack   

问题描述:全局引用jquery打包到线上可能会不好使。

第一步:

技术图片
var path = require(‘path‘)
var webpack = require(‘webpack‘)

function resolve(dir) {
  return path.join(__dirname, ‘..‘, dir)
}
module.exports = {
  entry: ‘./src/main.js‘,
  externals: {
    ‘BMap‘: ‘BMap‘,
    // ‘BMap_Symbol_SHAPE_POINT‘: ‘BMap_Symbol_SHAPE_POINT‘
  },
  output: {
    path: path.resolve(__dirname, ‘./dist‘),
    publicPath: ‘/dist/‘,
    filename: ‘build.js‘
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          ‘vue-style-loader‘,
          ‘css-loader‘
        ],
      },      {
        test: /\.vue$/,
        loader: ‘vue-loader‘,
        options: {
          loaders: {
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: ‘babel-loader‘,
        // include: [resolve(‘src‘), resolve(‘test‘),resolve(‘/node_modules/swiper/dist‘)]
        // include: [path.resolve(‘src‘), path.resolve(‘test‘),path.resolve(‘node_modules/bootstrap-vue/lib‘)],
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: ‘file-loader‘,
        options: {
          name: ‘[name].[ext]?[hash]‘
        }
      }
    ]
  },
  plugins: [ 
    new webpack.ProvidePlugin({ 
          $:"jquery", 
          jQuery:"jquery", 
         "window.jQuery":"jquery",
 }) 
], 
  resolve: {
    alias: {
      ‘vue$‘: ‘vue/dist/vue.esm.js‘,
      // ‘@‘: resolves(‘src‘),
      ‘@‘: path.resolve(‘src‘),
      ‘swiper‘: ‘swiper/dist/js/swiper.js‘,
    },
    extensions: [‘*‘, ‘.js‘, ‘.vue‘, ‘.json‘]
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true,
  },
  performance: {
    hints: false
  },
  devtool: ‘#eval-source-map‘
}

if (process.env.NODE_ENV === ‘production‘) {
  module.exports.devtool = ‘#source-map‘
  // http://vue-loader.vuejs.org/en/workflow/production.html
  module.exports.plugins = (module.exports.plugins || []).concat([
    new webpack.DefinePlugin({
      ‘process.env‘: {
        NODE_ENV: ‘"production"‘
      }
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      compress: {
        warnings: false
      }
    }),
    new webpack.LoaderOptionsPlugin({
      minimize: true
    }),
    new webpack.ProvidePlugin({ 
      $:"jquery", 
      jQuery:"jquery", 
     "window.jQuery":"jquery",
    }) 
  ])
}
View Code

第二部:在main.js里

import $ from ‘jquery‘;

解决问题:出现这个问题是只在plugins:里引入了没有在打包时候引用,要在生产环境里加上

new webpack.ProvidePlugin({ 
      $:"jquery", 
      jQuery:"jquery", 
     "window.jQuery":"jquery",
    }) 

 

vue 全局引用jq(打包后可能会遇到的问题)

标签:webp   join   引入   back   res   UNC   window   proc   pack   

原文地址:https://www.cnblogs.com/lst619247/p/11277173.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!