ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vue-cli2 单个组件打包为js

2021-04-04 22:01:21  阅读:446  来源: 互联网

标签:vue cli2 sass loader Vue 组件 js


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

标签:vue,cli2,sass,loader,Vue,组件,js
来源: https://blog.csdn.net/qq_29415357/article/details/115433603

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有