首页 > 编程学习 > vue-cli2 单个组件打包为js

vue-cli2 单个组件打包为js

发布时间:2022/5/14 19:04:50

vue-cli2 单个组件打包为js

vue-cli2和vue-cli3

参考这篇文章
还有这篇文章
vue-cli3有更多的默认配置支持更多场景,支持直接把组件打包成js的脚本,不用配置

vue-cli2

vue-cli2开发小组件建议直接使用vue init simpleproject xxx 创建项目,默认没有导入route。或者把生成的vue.config.js复制一份到vue普通项目中,后新建脚本使用这个配置。

vue.config.js

var path = require('path')
var webpack = require('webpack')

module.exports = {
  entry: '改成组件的js',
  output: {
    path: path.resolve(__dirname, './lib'),//所有输出文件的目标路径;打包后文件在硬盘中的存储位置。
    publicPath: '/lib/',//index.html 可访问资源
    filename: 'autobg.js',
    library: 'autobg',
    libraryTarget: 'umd',
    umdNamedDefine: true
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ],
      },
      {
        test: /\.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
      },
      {
        test: /\.sass$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader?indentedSyntax'
        ],
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': [
              'vue-style-loader',
              'css-loader',
              'sass-loader'
            ],
            'sass': [
              'vue-style-loader',
              'css-loader',
              'sass-loader?indentedSyntax'
            ]
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {
    alias: {
      'vue$': 'vue/lib/vue.esm.js'
    },
    extensions: ['*', '.js', '.vue', '.json']
  },
  devServer: {
    historyApiFallback: true,
    noInfo: true,
    overlay: true
  },
  performance: {
    hints: false
  },
  devtool: '#eval-source-map'
}

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
  })
])

package.json

"scripts": {
    "dev": "webpack-dev-server --open --hot",
    "build": "webpack --progress --hide-modules"
  }

添加route依赖命令

npm install route --save 

组件的index.js注意事项

在vue-cli项目中使用组件和html引用js使用组件需要

import Vue from 'vue'
import Axios from 'axios'
import xxx from 'xxx'
import newComponent from 'xxx'

init(Vue);//让该项目可在npm run dev 中运行

function init(Vue){
if(Vue){
  Vue.prototype.$axios = Axios
  Vue.use(xxx)
  Vue.component(newComponent.name, newComponent)
}
}


if (typeof window !== 'undefined' && window.Vue) {//在html中引入js使用该组件
init(window.Vue);
}

AutoBGManagement.install = function (vue) {//vue-cli中使用该组件
init(vue);
}

export default newComponent
Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000