diff --git a/.eslintrc.js b/.eslintrc.js index a169707..e831dd1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -9,37 +9,41 @@ module.exports = { env: { node: true, }, - extends: [ - 'plugin:vue/essential', - '@vue/airbnb', - '@vue/typescript', - ], + extends: ["plugin:vue/essential", "@vue/airbnb", "@vue/typescript"], // 修改airbnb部分变态规则 rules: { - 'max-len': ['error', { code: 200 }], + "max-len": ["error", { code: 200 }], // 允许修改形参 - 'no-param-reassign': 0, + "no-param-reassign": 0, // 允许使用 for-of,https://github.com/airbnb/javascript#iterators--nope - 'no-restricted-syntax': 0, + "no-restricted-syntax": 0, // 允许v-if和v-for同时使用,https://vuejs.github.io/eslint-plugin-vue/rules/no-use-v-if-with-v-for.html - 'vue/no-use-v-if-with-v-for': ['error', { - allowUsingIterationVar: true, - }], + "vue/no-use-v-if-with-v-for": [ + "error", + { + allowUsingIterationVar: true, + }, + ], // 允许循环引入 - 'import/no-cycle': 0, + "import/no-cycle": 0, // fix unresolved bug - 'import/no-unresolved': 0, + "import/no-unresolved": 0, // file extension - 'import/extensions': 0, - 'no-unused-vars': 0, + "import/extensions": 0, + "no-unused-vars": 0, // import named - 'import/named': 0, - 'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', - 'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off', - + "import/named": 0, + "no-console": + process.env.NODE_ENV === "production" ? "error" : "off", + "no-debugger": + process.env.NODE_ENV === "production" ? "error" : "off", + // 允许class中方法不使用this + "class-methods-use-this": "off", + // 允许下划线变量命名 + "no-underscore-dangle": "off", }, parserOptions: { - parser: '@typescript-eslint/parser', + parser: "@typescript-eslint/parser", ecmaFeatures: { legacyDecorators: true, }, diff --git a/README.md b/README.md index adc82c3..7329684 100644 --- a/README.md +++ b/README.md @@ -1,120 +1,203 @@ -# Vue-Plugin-Npm 发布包 -## 流程 +# 基于ElementUI封装,ProCrud插件 + +- 基于el-table,el-form二次封装,表单表格支持可视化配置 `ElementUI 版本2.13.2` +- 表单设计器 + - 基础组件: 内置element-ui表单组件 + - 高级组件: + - `级联选择器` (el-cascader) + - `Tinymce` 富文本编辑器 + - `附件模块` (FileUpload) + - `表格模块` (CrudTable) + - `树形下拉框` (vue-treeSelect) +- 表格设计器 +- 注意:本插件采用umd打包发布,支持cdn引入. +``` html + + + + + + + + +
+ 打开表单设计器 + 打开表格设计器 + + + +
+ + + + + + + + + + ``` -### 1、开发插件 -- 插件目录 (packages) 按需引入,每个组件均需提供install方法 +## 开始使用 -``` javascript +- install -// index.js +``` -import Test from './src/test.vue'; +npm install element-pro-crud --save -// 为组件添加 install 方法,用于按需引入 -Test.install = function (Vue) { - Vue.component(Test.name, Test); -}; +``` -export default Test; +- import - ``` -- 统一打包 (用于全局引入) - -``` javascript - -// packages / index.js - -// 导入packages中各个组件 -import Test from './test'; -... // 其他组件 - -// 以数组的结构保存组件,便于遍历 -const components = [ - Test, - ... // 其他组件 -]; - -// 定义 install 方法 -const install = (Vue) => { - if (install.installed) return; - install.installed = true; - // 遍历并注册全局组件 - components.map((component) => { - Vue.component(component.name, component); - return null; - }); -}; - -if (typeof window !== 'undefined' && window.Vue) { - install(window.Vue); -} +``` +// main.js -export default { - // 导出的对象必须具备一个 install 方法 - install, - // 组件列表 - ...components, -}; +import ElementProCrud from 'element-pro-crud'; -``` +import 'element-pro-crud/lib/ProCrud.css'; // 此处css引入需要放到element-ui css引入之前,避免样式覆盖问题 -### 2、插件相关信息 +import '@/plugins/element';// 引入element-ui; -``` javascript -// package.json -{ - "name": "bobo-npm-plugin", // 插件名称 - "version": "0.1.2", // 版本号 - "description": "first npm test", // 描述 - "main": "lib/plugin.umd.min.js", // 入口文件地址 - "private": false, // 此处为true的话无法使用npm publish - "license": "MIT", // 开源协议 - "keyword": "test", // 关键词 - "author": "BoBo", // 作者信息 - "scripts": { - "serve": "vue-cli-service serve", // 本地预览 - "lib": "vue-cli-service build --target lib --name plugin --dest lib packages/index.js" // 插件打包 - }, -} +Vue.use(ElementProCrud); -``` +----------------------------------- -### 3、调试 +// 自动全局注册下述组件 -- 开发环境调试 + // 根据表单设计器json自动渲染表单 -``` javascript -- npm run serve -``` + // 表单设计器 -/examples/App.vue 中引入组件进行调试 + // 表格设计器 -- 发布环境lib包直接调试(发布插件后,npm install出来的包) + // CrudTable -需要安装`@babel/plugin-transform-modules-umd`对webpack打包生成的`umd.js`转码 -``` javascript -npm install --save-dev @babel/plugin-transform-modules-umd +``` + +# CrudTable + +## Props + +| 参数 | 说明 | 类型 | 可选值 | 默认值 | +| :--------------------: | :---------------------------------------------------------------: | :-------------: | :-------------------------------------: | :-------: | +| expandRowKeys | 展开行 | Array | - | - | +| listField | response 中数据位置 | String | data/data.list | data.list | +| setReadOnly | GenerateFormDialog 中的表单禁用.null 表示均可编辑;{}表示全部只读; | Object | null/{}/{whiteList:{},blackList:{}} | null | | +| isMultiple | 是否开启多选 | Boolean | true,false | false | +| emptyText | 列表数据为空时显示文字 | String | - | 暂无数据 | +| prefill | 表单预填项(赋值初始值) | Object | - | null | +| appendToBody | 表单对话框是否插入至 body 元素上 | Boolean | true/false | false | +| tableName | 表名 | String | - | '' | +| tableDesignerName | 用于请求表格设计 json 的 name,不传则默认读取 tableName | String | - | null | +| dialogFormDesignerName | 对话框内加载 FormDesigner 的表名,,不传则默认读取 tableName | String | - | null | +| orderCondition | 排序条件 | String | - | null | +| columns | 自定义列配置项,会和 tableDesignerName 请求到的配置项合并 | Array | - | [] | +| visibleList | 内部元素显示控制(详情见下方) | Object | - | {} | +| tableTitle | 表格标题 | String | - | '' | +| tableParams | 表格请求参数(带查询参数请求) | Object,Array | - | {} | +| textMap | 表单对话框标题 | Object | { add:'添加',edit:'编辑',detail:'查看'} | {} | +| promiseForDel | 自定义删除按钮 promise 请求 | Function | Function({id}) | - | +| promiseForSelect | 自定义列表查询 promise 请求 | Function | Function(searchCondition) | - | +| btnAddOnClick | 添加按钮点击事件 | Function | - | - | +| btnRowAddOnClick | 表格行中的添加按钮点击事件 | Function | Function(row) | - | +| btnEditOnClick | 编辑按钮点击事件 | Function | Function(row) | - | +| btnDetailOnClick | 查看按钮点击事件 | Function | Function(row) | - | +| btnDelVisibleFunc | 表格行中的删除按钮是否显示事件 | Function | Function(row) | - | +| btnEditVisibleFunc | 表格行中的编辑按钮是否显示事件 | Function | Function(row) | - | +| btnDetailVisibleFunc | 表格行中的查看按钮是否显示事件 | Function | Function(row) | - | +| showPagination | 自定义列表 config 请求 | Boolean | true/false | true | +| remoteFuncs | 远程数据方法(用于表单内远端数据请求) | Object | - | {} | +| allResponse | 直接传入表头和表体,表格不用再发起任何请求 | Object | - | null | +| rowClassName | 行的 className 的回调方法 | String,Function | 见官网 | null | +| pageSize | 动态传入分页 | Array | - | [10,50,100] | +| maxHeightMinus | 表格自适应高度需要减去的高度值 | Number | - | 285 | +| fullHeight | 是否自适应屏幕高度(配置MaxHeightMinus) | Boolean | - | false | +| selectableFunc | 选择框动态判断是否显示 | Function | - | null | +| dialogFullscreen | 表单是否全屏 | Boolean | - | false | +| dialogCloseOnClickModal | 表单点击阴影是否可以关闭 | Boolean | - | false | +| showColumnIndex | 是否显示序号列 | Boolean | - | false | +| showOperator | 查询区域是否显示查询条件(默认不显示,查询条件为like) | Boolean | - | false | +| border | 是否有边框 | Boolean | - | true | +| formTableConfig | 表单中表格的tableConfig | Object | - | 详情看GenerateFormItem中解释 | +| formValuesAsync | 异步更新表单数据 | Object | - | 外层异步传入数据更新表单,注意不能直接修改formValues | +| editInlineMode | 是否开启行内编辑模式 | Boolean | - | false | +| actionColumnWidth | 操作列宽度(有时需要直接指定列宽) | Number | - | null | +| stripe | 斑马纹 | Boolean | - | false | +| border | 是否有边框 | Boolean | - | true | +| paginationLayout | 分页显示 | String | 见官网 | total, prev, pager, next, jumper, sizes | + +## Props 补充说明 + +- `visibleList` + +``` + // 内部元素显示控制 + { + searchForm: true, // 查询区域 + tableTitle: true, // 表格标题 + btnAdd: true, // 添加按钮 + btnDel: false, // 批量删除按钮 + actionColumnBtnAdd: false, // 操作列添加按钮 + actionColumnBtnEdit: true, // 操作列编辑按钮 + actionColumnBtnDetail: false, // 操作列查看按钮 + actionColumnBtnDel: true, // 操作列删除按钮 + actionColumn: true, // 操作列 + seniorSearchBtn:true, // 高级查询按钮 + btnAddOnColumnHeader: false, // 操作列header添加按钮 + }; +``` -import plugin from '../lib/plugin.umd.js'; +- `textMap` -import '../lib/plugin.css'; +``` +// 按钮文字Map +{ + add: '添加', + edit: '编辑', + del: '删除', + detail: '查看', +} ``` -### 4、发布 -- 注册npm账号 -- npm run lib +## Events -- npm login +| 事件名称 | 说明 | 回调参数 | +| :-------: | :--------------------------: | :------------------------------------------------: | +| done | 表格数据请求完成 | 整个 CrudTable 组件对象 | +| selection-change | 多选事件 | 选中的行 (params: Array) | +| form-change | 监听 dialog 中 form 对象改变 | 返回当前表单对象以及当前表单 json (params: Object) | +| form-btn-on-click | 表单内按钮组件点击回调 | widget(表单组件json) | +| `el-table events` | 所有el-table其他事件见官网文档 | https://element.eleme.cn/#/zh-CN/component/table | -- npm publish -### 5、项目中引入 - -``` javascript -npm install bobo-npm-plugin -``` +## Slots + +| 插槽名称 | 说明 | +| :-------------: | :--------------------------------------: | +| columnFormatter | 结合表格设计自定义列使用 | +| btnBarPrevBtn | 自定义右上角功能按钮 | +| btnCustom | 自定义操作按钮 参数为 {row} | +| seniorSearchForm | 自定义高级查询表单 | +| dialogFooter | 弹出表单右侧底部slot | +## Methods +| 方法名 | 说明 | 参数 | +| :---------: | :----------: | :--: | +| tableReload | 重新加载列表 | - | diff --git a/babel.config.js b/babel.config.js index a43a7ba..b049b9c 100644 --- a/babel.config.js +++ b/babel.config.js @@ -1,6 +1,13 @@ module.exports = { - presets: [ - '@vue/app', + presets: ['@vue/app'], + plugins: [ + '@babel/plugin-transform-modules-umd', + [ + 'component', + { + libraryName: 'element-ui', + styleLibraryName: 'theme-chalk', + }, + ], ], - plugins: ['@babel/plugin-transform-modules-umd'], }; diff --git a/examples/App.vue b/examples/App.vue index ea013f7..310372d 100644 --- a/examples/App.vue +++ b/examples/App.vue @@ -1,6 +1,7 @@ @@ -222,10 +236,11 @@ import { Component, Vue, Emit, Prop, } from 'vue-property-decorator'; -import { DML, crud } from '../../api/public/crud'; -import { getTableDetail, getFormDetail } from '../../api/system/form'; +import { DML, crud } from '@/api/public/crud'; +import { getTableDetail, getFormDetail } from '@/api/system/form'; +import { confirm } from '@/utils/confirm'; +import SvgIcon from '@/common/icons/SvgIcon.vue'; import GenerateFormDialog from './GenerateFormDialog.vue'; -import { confirm } from '../../utils/confirm'; import SearchForm from './SearchForm.vue'; const STATUS = { @@ -238,6 +253,7 @@ const STATUS = { components: { GenerateFormDialog, SearchForm, + SvgIcon, }, }) export default class CrudTable extends Vue { @@ -336,28 +352,28 @@ export default class CrudTable extends Vue { // 按钮名字 @Prop({ default: () => ({}), type: Object }) textMap!: any; - // 表格行中的添加按钮点击事件 + // 删除方法代理 @Prop({ default: null, type: Function }) promiseForDel!: any; - // 表格行中的添加按钮点击事件 + // 请求数据方法代理 @Prop({ default: null, type: Function }) promiseForSelect!: any; - // 表格行中的添加按钮点击事件 + // 删除按钮是否可见代理 @Prop({ default: null, type: Function }) btnDelVisibleFunc!: any; - // 表格行中的添加按钮点击事件 + // 编辑按钮是否可见代理 @Prop({ default: null, type: Function }) btnEditVisibleFunc!: any; - // 表格行中的添加按钮点击事件 + // 查看按钮是否可见代理 @Prop({ default: null, type: Function }) btnDetailVisibleFunc!: any; - // 表格行中的添加按钮点击事件 + // 表格添加按钮点击事件 @Prop({ default: null, type: Function }) btnAddOnClick!: any; - // 表格行中的添加按钮点击事件 + // 表格行中的编辑按钮点击事件 @Prop({ default: null, type: Function }) btnEditOnClick!: any; - // 表格行中的添加按钮点击事件 + // 表格行中的查看按钮点击事件 @Prop({ default: null, type: Function }) btnDetailOnClick!: any; // 表格行中的添加按钮点击事件 @@ -403,6 +419,9 @@ export default class CrudTable extends Vue { // 斑马纹 @Prop({ type: Boolean, default: true }) stripe!: boolean; + // 异步更新表单数据 + @Prop({ default: () => ({}), type: Object }) formValuesAsync!: any; + // 子表tableConfig 详情看GenerateFormItem中解释 @Prop({ default: () => ({}), type: Object }) formTableConfig!: any; @@ -533,10 +552,6 @@ export default class CrudTable extends Vue { }; } - // 表格数据源地址 - get tableUrl() { - return `${this.tableName}/list`; - } // 文本映射 get text() { @@ -561,6 +576,7 @@ export default class CrudTable extends Vue { actionColumnBtnDetail: false, actionColumnBtnDel: true, actionColumn: true, + seniorSearchBtn: true, btnAddOnColumnHeader: false, ...this.visibleList, }; @@ -596,9 +612,7 @@ export default class CrudTable extends Vue { // 添加 btnAdd() { if (this.btnAddOnClick) { - this.btnAddOnClick({ - tableParams: this.tableParams, - }); + this.btnAddOnClick(); } else if (this.prefill) { // 对话框内加载预填项 this.$refs.dialog.showDialog({}, 0, this.prefill); @@ -692,7 +706,7 @@ export default class CrudTable extends Vue { actionColumnDel(row) { this.currentRow = row; // 如果prop传入了promiseForDel说明需要回调自定义删除 - const promise = this.promiseForDel ? this.promiseForDel({ id: row.id }) : crud(DML.DELETE, this.tableName, {}, { id: row.id }); + const promise = this.promiseForDel ? this.promiseForDel({ id: row.id }) : crud(DML.DELETE, this.tableName, { id: row.id }); promise.then(() => { this.tableReload(); this.$message({ @@ -741,12 +755,6 @@ export default class CrudTable extends Vue { return visible; } - // 表格数据请求完成 - done(t) { - t.tableTitle = this.tableTitle; - this.$emit('done', t); - } - // 多选事件 handleSelectionChange(selection) { this.selectedRows = selection; @@ -755,12 +763,12 @@ export default class CrudTable extends Vue { // 生成的按钮点击 formBtnOnClick(widget) { - this.$emit('formBtnOnClick', widget); + this.$emit('form-btn-on-click', widget); } // 监听dialog中form对象改变 formChange(val) { - this.$emit('formChange', val); + this.$emit('form-change', val); } mounted() { @@ -815,14 +823,9 @@ export default class CrudTable extends Vue { let searchCondition: any[] = []; this.loading = true; const { - tableUrl, showPagination, pageIndexKey, pageSizeKey, pagination, listField, totalField, + showPagination, pageIndexKey, pageSizeKey, pagination, listField, totalField, } = this; const { tableParams, searchFormCondition } = this; - // 如果地址为空放弃请求 - if (tableUrl == null) { - this.loading = false; - return; - } // 已加载完成, tree lazy table 局部刷新. if (this.lazy && this.tableData.length > 0) { @@ -932,14 +935,9 @@ export default class CrudTable extends Vue { this.$emit('done', this); }) .catch((e) => { - // 如果list查询方法由外部传入,无法获取到真实请求的URL,故隐藏 - let message = `URL:${tableUrl},原因:${e.message}`; - if (this.promiseForSelect) { - message = `原因:${e.message}`; - } this.$notify({ title: '表格数据请求失败', - message, + message: `原因:${e.message}`, duration: 5000, }); this.loading = false; @@ -958,6 +956,7 @@ export default class CrudTable extends Vue { }, ], }; + crud(DML.TREE_LAZY, this.tableName, data).then((res) => { if (resolve) { resolve(res.data); diff --git a/packages/crud-table/src/GenerateFormDialog.vue b/packages/crud-table/src/GenerateFormDialog.vue index 22c5ceb..b3127dd 100644 --- a/packages/crud-table/src/GenerateFormDialog.vue +++ b/packages/crud-table/src/GenerateFormDialog.vue @@ -26,6 +26,7 @@ + @@ -42,13 +43,13 @@ diff --git a/packages/crud-table/src/SearchForm.vue b/packages/crud-table/src/SearchForm.vue index 03d2f0b..8c30d39 100644 --- a/packages/crud-table/src/SearchForm.vue +++ b/packages/crud-table/src/SearchForm.vue @@ -23,7 +23,8 @@ style="display:inline"> - + +
+ + diff --git a/packages/table-designer/src/SelectConfig.vue b/packages/table-designer/src/SelectConfig.vue new file mode 100644 index 0000000..345c612 --- /dev/null +++ b/packages/table-designer/src/SelectConfig.vue @@ -0,0 +1,189 @@ + + + + + + diff --git a/packages/table-designer/src/TableDesignerDialog.vue b/packages/table-designer/src/TableDesignerDialog.vue new file mode 100644 index 0000000..190eb28 --- /dev/null +++ b/packages/table-designer/src/TableDesignerDialog.vue @@ -0,0 +1,331 @@ + + + + + + + + diff --git a/packages/table-designer/src/columnsConfig.ts b/packages/table-designer/src/columnsConfig.ts new file mode 100644 index 0000000..8282c40 --- /dev/null +++ b/packages/table-designer/src/columnsConfig.ts @@ -0,0 +1,169 @@ +/** + * @file 表格设计功能列配置 + * @author ytyang + * @copyright NanJing Anshare Tech .Com + * @createDate 2018年12月26日11:28:17 + */ + +export default [ + { + name: '#', + is: 'i', + show: true, + bodyStyle: 'el-icon-sort', + tootip: '拖拽我上下滑动可以改变字段顺序', + }, + { + name: '字段', + field: 'prop', + is: 'input', + show: true, + }, + { + name: '列名', + field: 'label', + is: 'input', + headStyle: 'width:150px', + show: true, + }, + { + name: '高级查询', + field: 'option', + is: 'popover', + headStyle: 'width:90px', + show: true, + tootip: '默认为文本框,可转为下拉菜单', + }, + { + name: '列宽', + field: 'width', + is: 'input', + headStyle: 'width:70px', + show: false, + }, + { + name: '最小宽', + field: 'minWidth', + is: 'input', + headStyle: 'width:70px', + tootip: '加粗黑色表示非默认宽度', + show: true, + }, + { + name: '表格对齐', + field: 'align', + is: 'select', + list: [ + { + label: '靠左', + value: 'left', + }, + { + label: '居中', + value: 'center', + }, + { + label: '靠右', + value: 'right', + }, + { + label: '默认对齐', + value: undefined, + }, + ], + show: true, + }, + { + name: '表头对齐', + field: 'headerAlign', + is: 'select', + headStyle: 'width:50px', + list: [ + { + label: '靠左', + value: 'left', + }, + { + label: '居中', + value: 'center', + }, + { + label: '靠右', + value: 'right', + }, + { + label: '默认对齐', + value: undefined, + }, + ], + show: true, + }, + { + name: '固定方式', + field: 'fixed', + is: 'select', + headStyle: 'width:50px', + list: [ + { + label: '靠左', + value: 'left', + }, + { + label: '靠右', + value: 'right', + }, + { + label: '不固定', + value: false, + }, + ], + show: true, + }, + { + name: '溢出隐藏', + field: 'showOverflowTooltip', + headStyle: 'width:80px', + is: 'switch', + show: true, + }, + { + name: '排序方式', + field: 'sortable', + is: 'select', + headStyle: 'width:120px', + // 区分客户端和服务端排序,所以value统一使用string + list: [ + { + label: '不排序', + value: 'false', + }, + // { + // label: '客户端排序', + // value: 'true', + // }, + { + label: '服务端排序', + value: 'custom', + }, + ], + show: true, + }, + { + name: '是否检索', + field: 'searchable', + headStyle: 'width:80px', + is: 'switch', + tootip: '是否在表格顶部高级查询区域显示', + show: true, + }, + { + name: '插槽', + field: 'slotName', + is: 'input', + show: true, + }, + { + name: '操作', + show: true, + }, +]; diff --git a/packages/utils/confirm.ts b/packages/utils/confirm.ts index bd2b936..a74a3b4 100755 --- a/packages/utils/confirm.ts +++ b/packages/utils/confirm.ts @@ -6,6 +6,7 @@ */ import { MessageBox } from 'element-ui'; +const ElMessageBox = MessageBox; /** * 确认提示框装饰器 * @param {*} message 提示信息 @@ -18,7 +19,7 @@ export function confirm(message, title = '提示', cancelFn = (error) => {}) { // eslint-disable-next-line func-names descriptor.value = async function (...rest) { try { - await MessageBox.confirm(message, title); + await ElMessageBox.confirm(message, title); originFn.apply(this, rest); } catch (error) { if (cancelFn) { @@ -39,7 +40,7 @@ export function alert(message, title = '提示') { const originFn = descriptor.value; // eslint-disable-next-line func-names descriptor.value = async function (...rest) { - await MessageBox.alert(message, title); + await ElMessageBox.alert(message, title); originFn.apply(this, rest); }; }; diff --git a/packages/form-designer/src/utils/download.ts b/packages/utils/download.ts similarity index 95% rename from packages/form-designer/src/utils/download.ts rename to packages/utils/download.ts index 27940c4..a8f934b 100644 --- a/packages/form-designer/src/utils/download.ts +++ b/packages/utils/download.ts @@ -1,10 +1,10 @@ /** * @file 文件下载 - * @author BoBo + * @author ytyang * @copyright NanJing Anshare Tech .Com * @createDate 2018年11月18日08:13:10 */ -import axios from '../../../api/axios'; +import axios from '../api/axios'; /** * 通用下载 diff --git a/public/demo.html b/public/demo.html new file mode 100644 index 0000000..ebf905a --- /dev/null +++ b/public/demo.html @@ -0,0 +1,36 @@ + + + + + + + + +
+ 打开表单设计器 + 打开表格设计器 + + + +
+ + + + + + + + + diff --git a/vue.config.js b/vue.config.js index eb00bb6..7bfe529 100644 --- a/vue.config.js +++ b/vue.config.js @@ -1,4 +1,15 @@ +/* + * @file: vue.config.js + * @copyright: NanJing Anshare Tech .Com + * @author: BoBo + * @Date: 2020年10月28 10:44:39 + */ +const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer'); +const path = require('path'); +function resolve(dir) { + return path.join(__dirname, dir); +} module.exports = { // 将 examples 目录添加为新的页面 pages: { @@ -11,4 +22,39 @@ module.exports = { filename: 'index.html', }, }, + chainWebpack: (config) => { + if (process.env.IS_REPORT) { + config.plugin('webpack-report').use(BundleAnalyzerPlugin, [ + { + analyzerMode: 'static', + }, + ]); + } + // vue inspect --rules 列出所有规则,可以看到svg是第三个 + // vue inspect module.rules.2 可以列出默认svg规则配置 + // 从默认svg规则中排除src/icons路径,因为会当做图标自动加载 + config.module.rule('svg').exclude.add(resolve('packages/common/icons')); + // 添加svg-sprite-loader加载器 + config.module + .rule('svg-sprite-loader') + .test(/.svg$/) + .include.add(resolve('packages/common/icons')) // 处理svg目录 + .end() + .use('svg-sprite-loader') + .loader('svg-sprite-loader') + .options({ + symbolId: 'icon-[name]', + }) + .end() + .use('svgo-loader') + .loader('svgo-loader') + .end(); + }, + configureWebpack: { + resolve: { + alias: { + '@': resolve('packages'), + }, + }, + }, };