Skip to content

Commit 49f37ce

Browse files
JosephusPayekilobyte2007
andauthoredApr 6, 2023
Add usage example, fix build for use with bundlers and browser (#552)
Co-authored-by: Sergiu Cazac <kilobyte2007@gmail.com>
1 parent 2330628 commit 49f37ce

25 files changed

+23977
-17475
lines changed
 

‎.github/workflows/node.js.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ jobs:
2727
strategy:
2828
matrix:
2929
os: [ubuntu-latest, windows-latest, macos-latest]
30-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
31-
node-version: [14.x, 16.x, 18.x]
30+
node-version: [lts/*] # latest LTS version of Node
3231
steps:
3332
- uses: actions/checkout@v3
3433
- name: Use Node.js ${{ matrix.node-version }}

‎build/build.lib.mjs

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const entries = [
3838
"UiTooltip",
3939
];
4040

41+
// We need to get the env variable here because it later gets polluted by child processes
42+
const mode = process.env.NODE_ENV || "production";
43+
4144
entries.forEach(async (entry) => {
42-
await build(getLibConfig({ entry, mode: process.env.NODE_ENV || "production" }));
45+
await build(getLibConfig({ entry, mode }));
4346
});

‎build/vite.config.dist.mjs

+20-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,20 @@ import autoprefixer from "autoprefixer";
66
import options from "./options.mjs";
77

88
export default defineConfig(({ mode }) => {
9-
const filename = mode === "production" ? "keen-ui.min" : "keen-ui";
9+
const isProduction = mode === "production";
1010
const outDir = options.paths.output.main;
1111

12+
const formatFileNames = {
13+
es: {
14+
development: "keen-ui.esm.js",
15+
production: "keen-ui.esm.min.js",
16+
},
17+
umd: {
18+
development: "keen-ui.js",
19+
production: "keen-ui.min.js",
20+
},
21+
};
22+
1223
return {
1324
plugins: [vue(), banner({ content: options.banner, outDir })],
1425
resolve: {
@@ -31,14 +42,19 @@ export default defineConfig(({ mode }) => {
3142
lib: {
3243
entry: options.paths.resolve("src/index.js"),
3344
name: "KeenUI",
34-
formats: ["umd"],
35-
fileName: () => filename + ".js",
45+
formats: isProduction ? ["umd"] : ['umd', 'es'],
46+
fileName: (format) => {
47+
return formatFileNames[format][mode]
48+
},
3649
},
3750
rollupOptions: {
3851
external: [/^vue/],
3952
output: {
53+
globals: {
54+
vue: "Vue",
55+
},
4056
assetFileNames: (assetInfo) =>
41-
assetInfo.name === "style.css" ? filename + ".css" : assetInfo.name,
57+
assetInfo.name === "style.css" ? `keen-ui${isProduction ? '.min' : ''}.css` : assetInfo.name,
4258
},
4359
},
4460
},

‎build/vite.config.lib.provider.mjs

+20-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,20 @@ import autoprefixer from "autoprefixer";
55
import options from "./options.mjs";
66

77
export default ({ entry, mode }) => {
8+
const isProduction = mode === "production";
89
const outDir = options.paths.output.lib;
910

11+
const formatFileNames = {
12+
es: {
13+
development: `${entry}.esm.js`,
14+
production: `${entry}.esm.min.js`,
15+
},
16+
umd: {
17+
development: `${entry}.js`,
18+
production: `${entry}.min.js`,
19+
},
20+
};
21+
1022
return {
1123
plugins: [vue(), banner({ content: options.banner, outDir })],
1224
resolve: {
@@ -20,11 +32,13 @@ export default ({ entry, mode }) => {
2032
},
2133
},
2234
build: {
23-
minify: mode === "production" ? "esbuild" : false,
35+
minify: isProduction ? "esbuild" : false,
2436
lib: {
2537
entry: options.paths.resolve(`src/${entry}.vue`),
26-
formats: ["umd"],
27-
fileName: () => `[name]${mode === "production" ? ".min" : ""}.js`,
38+
formats: isProduction ? ["umd"] : ['umd', 'es'],
39+
fileName: (format) => {
40+
return formatFileNames[format][mode]
41+
},
2842
name: `KeenUI.${entry}`,
2943
},
3044
outDir,
@@ -36,6 +50,9 @@ export default ({ entry, mode }) => {
3650
globals: {
3751
vue: "Vue",
3852
},
53+
assetFileNames: () => {
54+
return `css/[name][extname]`
55+
}
3956
},
4057
},
4158
},

‎dist/keen-ui.css

+3,732-1,550
Large diffs are not rendered by default.

‎dist/keen-ui.esm.js

+10,012
Large diffs are not rendered by default.

‎dist/keen-ui.js

+9,596-15,906
Large diffs are not rendered by default.

‎package.json

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22
"name": "keen-ui",
33
"version": "1.4.0",
44
"description": "Keen UI is a Vue.js UI library with a simple API, inspired by Google's Material Design.",
5-
"main": "dist/keen-ui.js",
5+
"main": "./dist/keen-ui.js",
6+
"module": "./dist/keen-ui.esm.js",
7+
"exports": {
8+
".": {
9+
"import": "./dist/keen-ui.esm.js",
10+
"require": "./dist/keen-ui.js"
11+
},
12+
"./keen-ui.css": "./dist/keen-ui.css",
13+
"./src/*": {
14+
"import": "./src/*"
15+
},
16+
"./css/*": "./lib/css/*",
17+
"./*": {
18+
"import": "./lib/*.esm.js",
19+
"require": "./lib/*.js"
20+
}
21+
},
622
"scripts": {
723
"dev": "vite serve --config build/vite.config.docs.mjs docs-src",
824
"build:dist": "vite build --config build/vite.config.dist.mjs --mode development",

‎src/index.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const KeenUI = {
7474
UiTooltip,
7575
};
7676

77-
KeenUI.install = function (app, config = {}) {
77+
export function install(app, config = {}) {
7878
// Configure the component props
7979
Object.keys(config).forEach((key) => {
8080
if (startsWith(key, "Ui")) {
@@ -96,15 +96,8 @@ KeenUI.install = function (app, config = {}) {
9696
app.component(Component.name, Component);
9797
}
9898
});
99-
};
100-
101-
// Make Keen UI available globally when in a browser environment
102-
if (typeof window !== "undefined") {
103-
window.KeenUI = KeenUI;
10499
}
105100

106-
export default KeenUI;
107-
108101
export {
109102
UiAlert,
110103
UiAutocomplete,
@@ -141,3 +134,8 @@ export {
141134
UiToolbar,
142135
UiTooltip,
143136
};
137+
138+
export default {
139+
...KeenUI,
140+
install,
141+
};

‎usage-example-static/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Keen UI static usage example
2+
3+
This is an example of using Vue + Keen UI via global script tags, without a build system like Vite or Webpack.
4+
5+
## Usage
6+
7+
- From the Keen UI root directory:
8+
- Run `yarn` to install dependencies
9+
- Run `yarn build:dist && yarn build:dist:prod` to build the library
10+
- From this `usage-example-static` directory:
11+
- Open the `index.html` file in your browser. If it's working you should see a message and some styled buttons.

‎usage-example-static/index.html

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>Keen UI Static Usage Example</title>
6+
<script src="https://unpkg.com/vue@3/dist/vue.global.prod.js"></script>
7+
<script src="../dist/keen-ui.min.js"></script>
8+
<link rel="stylesheet" href="../dist/keen-ui.min.css" type="text/css" />
9+
</head>
10+
<body>
11+
<div id="app" style="text-align: center">
12+
<div>{{ message }}</div>
13+
<br />
14+
<ui-button>Button from Keen UI plugin</ui-button>
15+
<br />
16+
<br />
17+
<my-button>Button from component registration</my-button>
18+
</div>
19+
20+
<script>
21+
const { createApp } = Vue;
22+
const app = createApp({
23+
data() {
24+
return {
25+
message: "Hello from Vue!",
26+
};
27+
},
28+
});
29+
30+
// Works as a plugin
31+
app.use(KeenUI);
32+
33+
// Also we can use separate components
34+
app.component("my-button", KeenUI.UiButton);
35+
36+
app.mount("#app");
37+
</script>
38+
</body>
39+
</html>

‎usage-example-vite/.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

‎usage-example-vite/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Keen UI Vite usage example
2+
3+
This is an example Vite project to test the usage of Keen UI:
4+
5+
- using the default export and named exports
6+
- using lib exports
7+
- using the Keen UI `.vue` source files directly
8+
9+
## Usage
10+
11+
- From the Keen UI root directory:
12+
- Run `yarn` to install dependencies
13+
- Run `yarn build:all && yarn build:all:prod` to build the library
14+
- Run `yarn link`
15+
- From this `usage-example-vite` directory:
16+
- Run `yarn` to install dependencies
17+
- Run `yarn link keen-ui` to link the local Keen UI build to the example project
18+
- Run `yarn dev` to start Vite and visit the URL shown to view the example

‎usage-example-vite/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + Vue</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/src/main.js"></script>
12+
</body>
13+
</html>

‎usage-example-vite/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "usage-test",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "vite",
8+
"build": "vite build",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"vue": "^3.2.45"
13+
},
14+
"devDependencies": {
15+
"@vitejs/plugin-vue": "^4.0.0",
16+
"vite": "^4.0.0"
17+
}
18+
}

‎usage-example-vite/public/vite.svg

+1
Loading

‎usage-example-vite/src/App.vue

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script setup>
2+
import UsesDefaultExport from './components/UsesDefaultExport.vue';
3+
import UsesNamedExport from './components/UsesNamedExport.vue';
4+
import UsesSrcExport from './components/UsesSrcExport.vue';
5+
import UsesLibExport from './components/UsesLibExport.vue';
6+
</script>
7+
8+
<template>
9+
<div>
10+
<p>The following should be styled Keen UI buttons if the imports are working:</p>
11+
<br />
12+
<UsesDefaultExport />
13+
<br />
14+
<UsesNamedExport />
15+
<br />
16+
<UsesSrcExport />
17+
<br />
18+
<UsesLibExport />
19+
</div>
20+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script setup>
2+
import * as KeenUI from 'keen-ui';
3+
import 'keen-ui/keen-ui.css';
4+
</script>
5+
6+
<template>
7+
<div>
8+
<KeenUI.UiButton>Uses Default Export</KeenUI.UiButton>
9+
</div>
10+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script setup>
2+
import UiButton from 'keen-ui/UiButton';
3+
import 'keen-ui/css/UiButton.css';
4+
</script>
5+
6+
<template>
7+
<div>
8+
<UiButton>Uses Lib Export</UiButton>
9+
</div>
10+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script setup>
2+
import { UiButton } from 'keen-ui';
3+
import 'keen-ui/keen-ui.css';
4+
</script>
5+
6+
<template>
7+
<div>
8+
<UiButton>Uses Named Export</UiButton>
9+
</div>
10+
</template>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script setup>
2+
import UiButton from 'keen-ui/src/UiButton.vue';
3+
</script>
4+
5+
<template>
6+
<div><UiButton>Uses Src Export</UiButton></div>
7+
</template>

‎usage-example-vite/src/main.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { createApp } from 'vue'
2+
3+
import './style.css'
4+
5+
import App from './App.vue'
6+
7+
createApp(App).mount('#app')

‎usage-example-vite/src/style.css

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
* {
2+
box-sizing: border-box;
3+
}
4+
5+
:root {
6+
font-family: system-ui, sans-serif;
7+
font-size: 16px;
8+
text-align: center;
9+
}

‎usage-example-vite/vite.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite'
2+
import vue from '@vitejs/plugin-vue'
3+
4+
// https://vitejs.dev/config/
5+
export default defineConfig({
6+
plugins: [vue()],
7+
})

‎usage-example-vite/yarn.lock

+365
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.