使用webpack将所有组件打包起来,并通过script标签直接使用

使用webpack将所有组件打包起来,并通过script标签直接使用

 

需求:需要将所有的组件打包起来,通过script标签引入html页面,在页面上使用自定义的组件名就能直接使用,就像element-ui

效果如下:

<!DOCTYPE html>
<html lang="zh">
<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">
	<link rel="stylesheet" href="./app.2780147b.css">
	<script src="https://unpkg.com/vue/dist/vue.js"></script>
	<!-- 自己写的组件,直接引用,在html页面中就能使用 -->
	<script src="./myLib.js"></script>
</head>
<body>
	<div id="app">
		<op-check-box></op-check-box>
	</div>

	<script>
	  new Vue({
	    el: '#app'
		})
	</script>
</body>
</html>

 

一、项目结构图:

二、组件中的index.js代码

import checkBox from './src/checkBox'

checkBox.install = function (Vue) {
  Vue.component(checkBox.name, checkBox)
}

export default checkBox

 三、index.js打包入口文件

import Vue from 'vue'

import Radio from './packages/radio/src/radio.vue'
import CheckBox from './packages/checkBox/src/checkBox.vue'

Vue.config.productionTip = false

const components = [
  Radio,
  CheckBox
]

// 将所有组件注册在Vue上
const install = function (Vue) {
  components.forEach(component => {
    Vue.component(component.name, component)
  })
}

if (typeof window !== 'undefined' && window.Vue) {
  install(window.Vue)
}

export default components

四、在package.json中加入

与name同级,myLib.js为你的库名

  "main": "dist/myLib.js",

五、webpack.config.js配置如下

注意:如报错,根据对应的提示配置对应的 loader 就行,如:笔者使用的 scss,你可能使用的是 less。

到这里就已经完成了,当然你也可以将这个包发布到npm上,就可以在项目上直接下载使用了。

const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')

module.exports = {
  mode: 'production',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'myLib.js',
    library: 'myLib',
    libraryTarget: 'umd'
  },

  plugins: [
    new VueLoaderPlugin()
  ],
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.css|.scss$/,
        use: [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ]
      }
    ]
  }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在 Vue 项目中使用 webpack 将多个组件合并打包并实现按需加载,需要使用 webpack 的 code splitting 功能。具体步骤如下: 1. 在 webpack 配置文件中配置 code splitting: ```javascript // webpack.config.js module.exports = { // ... optimization: { splitChunks: { chunks: 'async', minSize: 30000, maxSize: 0, minChunks: 1, maxAsyncRequests: 5, maxInitialRequests: 3, automaticNameDelimiter: '~', name: true, cacheGroups: { vendor: { test: /[\\/]node_modules[\\/]/, name: 'vendor', chunks: 'all' }, common: { name: 'common', minChunks: 2, chunks: 'all', priority: -10, reuseExistingChunk: true } } } } }; ``` 2. 在组件使用 `import()` 动态导入其他组件: ```javascript // MyComponent.vue export default { methods: { handleClick() { import('./OtherComponent.vue').then(module => { const OtherComponent = module.default; // 使用 OtherComponent 组件 }); } } }; ``` 3. 在模板中使用 `v-if` 或 `v-show` 按需显示组件: ```html <template> <div> <button @click="showOther">Show Other Component</button> <OtherComponent v-if="show" /> </div> </template> <script> export default { data() { return { show: false }; }, methods: { showOther() { import('./OtherComponent.vue').then(module => { const OtherComponent = module.default; this.show = true; }); } } }; </script> ``` 这样,webpack 会根据配置将多个组件打包成多个文件,并在需要时动态加载。注意,`import()` 返回的是一个 Promise,需要使用 `then()` 方法获取模块的导出值。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值