Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: using vite as build tool #60

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions build/ViteSingleCssPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
let packageNames = []
let viteConfig
let IIFEcss

// 将 css 打包到 js 文件中
export default function() {
return {
apply: 'build',
enforce: 'post',
name: 'pack-css',
configResolved(config) {
viteConfig = config
packageNames = viteConfig.build.lib.formats.map(format => viteConfig.build.lib.fileName(format))
},
generateBundle(_, bundle) {
const cssFileName = 'style.css'
const { [cssFileName]: cssBundle } = bundle
if (cssBundle) {
IIFEcss = `(function() {try {var elementStyle = document.createElement('style');elementStyle.innerText = ${JSON.stringify(cssBundle.source)};document.head.appendChild(elementStyle);} catch(error) {console.error(error, 'unable to concat style inside the bundled file')}})()`
delete bundle[cssFileName]
}
packageNames.forEach(packageName => {
if (bundle[packageName]) {
bundle[packageName].code += IIFEcss
}
})
}
}
}
13 changes: 13 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>vue-dynamic-form-component</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/dev/main.js"></script>
</body>
</html>
17 changes: 14 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,19 @@
"url": "https://github.com/chenquincy/vue-dynamic-form-component"
},
"scripts": {
"dev": "vue-cli-service serve",
"build": "vue-cli-service build --target lib --dest lib --name vue-dynamic-form-component ./packages/index.js",
"dev": "vite",
"build": "vite build",
"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs"
},
"main": "./lib/vue-dynamic-form-component.umd.min.js",
"main": "./lib/vue-dynamic-form-component.umd.js",
"module": "./lib/vue-dynamic-form-component.es.js",
"exports": {
".": {
"import": "./lib/vue-dynamic-form-component.es.js",
"require": "./lib/vue-dynamic-form-component.umd.js"
}
},
"typings": "types/index.d.ts",
"vetur": {
"tags": "vetur/tags.json",
Expand All @@ -44,7 +51,11 @@
"eslint": "^5.16.0",
"eslint-plugin-vue": "^5.0.0",
"node-sass": "^4.9.0",
"sass": "^1.49.9",
"sass-loader": "^7.1.0",
"vite": "^2.8.6",
"vite-plugin-eslint": "^1.3.0",
"vite-plugin-vue2": "^1.9.3",
"vue": "^2.6.10",
"vue-template-compiler": "^2.6.10",
"vuepress": "^1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/dynamic-form-item/form-item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@

<script>
import { isComplexType, getLabelWidth, darkenColor, parseDescriptor, findTypeDescriptor } from '../utils'
import DynamicInput from '../dynamic-input/input'
import DynamicInput from '../dynamic-input/input.vue'

export default {
name: 'dynamic-form-item',
Expand Down
2 changes: 1 addition & 1 deletion packages/dynamic-form/form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</template>

<script>
import DynamicFormItem from '../dynamic-form-item/form-item'
import DynamicFormItem from '../dynamic-form-item/form-item.vue'
import { isComplexType, getLabelWidth, findTypeDescriptor } from '../utils'
import i18n from '../i18n'

Expand Down
12 changes: 10 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Vue from 'vue'
import Vue, { VueConstructor } from 'vue'

export type ComponentSize = 'large' | 'medium' | 'small' | 'mini'

Expand Down Expand Up @@ -43,4 +43,12 @@ declare class DynamicForm extends Vue {
clearValidate (): void
}

export default DynamicForm
export {
DynamicForm
}

declare const _default: {
install(Vue: VueConstructor): void
};

export default _default
37 changes: 37 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { defineConfig } from 'vite'
import { createVuePlugin } from 'vite-plugin-vue2'
import VitePluginEslint from 'vite-plugin-eslint'
import path from 'path'
import singleCssPlugin from './build/ViteSingleCssPlugin';
import { name as packageName } from './package.json'

export default defineConfig({
build: {
outDir: path.resolve(__dirname, './lib'),
lib: {
entry: path.resolve(__dirname, './packages/index.js'),
name: packageName,
formats: ['umd', 'es', 'cjs'],
fileName: format => {
return `${packageName}.${format}.js`;
}
},
rollupOptions: {
external: ['vue', 'element-ui'],
output: {
globals: {
vue: 'Vue',
'element-ui': 'Element'
},
exports: 'named'
}
}
},
plugins: [
createVuePlugin(),
singleCssPlugin(),
VitePluginEslint({
include: ['packages/**/*.js', 'packages/**/*.vue']
})
]
})
Loading