Skip to content

Commit f4031d4

Browse files
author
jieliu52
committed
fix: 调整 hoc
1 parent 4d9f398 commit f4031d4

File tree

23 files changed

+644
-19
lines changed

23 files changed

+644
-19
lines changed

examples/sites/demos/pc/app/button/basic-usage.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<p>基本按钮</p>
44
<tiny-row>
55
<config-provider :design="design">
6-
<tiny-button type="primary"> 主要按钮 </tiny-button>
6+
<tiny-button type="primary" tiny_experimetal_props_hoc> 主要按钮 </tiny-button>
77
<tiny-button> 次要按钮 </tiny-button>
88
<tiny-button type="success"> 成功按钮 </tiny-button>
99
<tiny-button type="info"> 信息按钮 </tiny-button>
@@ -64,7 +64,6 @@ export default {
6464
components: {
6565
Button: {
6666
props: {
67-
resetTime: 0,
6867
size: 'mini'
6968
}
7069
}

examples/vue3/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
},
1919
"devDependencies": {
2020
"@opentiny-internal/playwright-config": "workspace:^1.0.1-beta.0",
21+
"@opentiny-internal/unplugin-hoc-plugin": "workspace:*",
2122
"@opentiny-internal/unplugin-virtual-template": "workspace:*",
2223
"@opentiny/vue": "workspace:~",
2324
"@opentiny/vue-common": "workspace:~",

examples/vue3/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default defineConfig((config) => {
3232
},
3333
plugins: [
3434
virtualTemplatePlugin({ include: ['**/packages/vue/**/src/index.ts'], env }),
35+
// hocPlugin({}),
3536
vue3Plugin({
3637
include: [/\.vue$/, /\.md$/]
3738
}),
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# @opentiny/unplugin-virtual-template
2+
3+
## Install
4+
5+
```bash
6+
npm i @opentiny/unplugin-virtual-template
7+
```
8+
9+
<details>
10+
<summary>Vite</summary><br>
11+
12+
```ts
13+
// vite.config.ts
14+
import Starter from '@opentiny/unplugin-virtual-template/vite'
15+
16+
export default defineConfig({
17+
plugins: [
18+
Starter({
19+
/* options */
20+
})
21+
]
22+
})
23+
```
24+
25+
Example: [`playground/`](./playground/)
26+
27+
<br></details>
28+
29+
<details>
30+
<summary>Rollup</summary><br>
31+
32+
```ts
33+
// rollup.config.js
34+
import Starter from '@opentiny/unplugin-virtual-template/rollup'
35+
36+
export default {
37+
plugins: [
38+
Starter({
39+
/* options */
40+
})
41+
]
42+
}
43+
```
44+
45+
<br></details>
46+
47+
<details>
48+
<summary>Webpack</summary><br>
49+
50+
```ts
51+
// webpack.config.js
52+
module.exports = {
53+
/* ... */
54+
plugins: [
55+
require('@opentiny/unplugin-virtual-template/webpack')({
56+
/* options */
57+
})
58+
]
59+
}
60+
```
61+
62+
<br></details>
63+
64+
<details>
65+
<summary>Nuxt</summary><br>
66+
67+
```ts
68+
// nuxt.config.js
69+
export default {
70+
buildModules: [
71+
[
72+
'@opentiny/unplugin-virtual-template/nuxt',
73+
{
74+
/* options */
75+
}
76+
]
77+
]
78+
}
79+
```
80+
81+
> This module works for both Nuxt 2 and [Nuxt Vite](https://github.com/nuxt/vite)
82+
83+
<br></details>
84+
85+
<details>
86+
<summary>Vue CLI</summary><br>
87+
88+
```ts
89+
// vue.config.js
90+
module.exports = {
91+
configureWebpack: {
92+
plugins: [
93+
require('@opentiny/unplugin-virtual-template/webpack')({
94+
/* options */
95+
})
96+
]
97+
}
98+
}
99+
```
100+
101+
<br></details>
102+
103+
<details>
104+
<summary>esbuild</summary><br>
105+
106+
```ts
107+
// esbuild.config.js
108+
import { build } from 'esbuild'
109+
import Starter from '@opentiny/unplugin-virtual-template/esbuild'
110+
111+
build({
112+
plugins: [Starter()]
113+
})
114+
```
115+
116+
<br></details>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"name": "@opentiny-internal/unplugin-hoc-plugin",
3+
"type": "module",
4+
"version": "0.1.1-beta.0",
5+
"private": true,
6+
"description": "Register global imports on demand for Vite and Webpack",
7+
"exports": {
8+
".": {
9+
"types": "./dist/index.d.ts",
10+
"require": "./dist/index.cjs",
11+
"import": "./dist/index.js"
12+
},
13+
"./vite": {
14+
"types": "./dist/vite.d.ts",
15+
"require": "./dist/vite.cjs",
16+
"import": "./dist/vite.js"
17+
},
18+
"./webpack": {
19+
"types": "./dist/webpack.d.ts",
20+
"require": "./dist/webpack.cjs",
21+
"import": "./dist/webpack.js"
22+
},
23+
"./rollup": {
24+
"types": "./dist/rollup.d.ts",
25+
"require": "./dist/rollup.cjs",
26+
"import": "./dist/rollup.js"
27+
},
28+
"./esbuild": {
29+
"types": "./dist/esbuild.d.ts",
30+
"require": "./dist/esbuild.cjs",
31+
"import": "./dist/esbuild.js"
32+
},
33+
"./nuxt": {
34+
"types": "./dist/nuxt.d.ts",
35+
"require": "./dist/nuxt.cjs",
36+
"import": "./dist/nuxt.js"
37+
},
38+
"./types": {
39+
"types": "./dist/types.d.ts",
40+
"require": "./dist/types.cjs",
41+
"import": "./dist/types.js"
42+
},
43+
"./*": "./*"
44+
},
45+
"main": "dist/index.cjs",
46+
"module": "dist/index.js",
47+
"types": "dist/index.d.ts",
48+
"typesVersions": {
49+
"*": {
50+
"*": [
51+
"./dist/*",
52+
"./*"
53+
]
54+
}
55+
},
56+
"files": [
57+
"dist"
58+
],
59+
"scripts": {
60+
"build": "tsup",
61+
"dev": "tsup --watch src",
62+
"lint": "eslint .",
63+
"prepublishOnly": "npm run build",
64+
"release": "bumpp && npm publish",
65+
"start": "esno src/index.ts",
66+
"test": "vitest"
67+
},
68+
"dependencies": {
69+
"@rollup/pluginutils": "^5.0.2",
70+
"magic-string": "^0.27.0",
71+
"unplugin": "^1.0.1"
72+
},
73+
"devDependencies": {
74+
"@types/node": "^18.11.13",
75+
"bumpp": "^8.2.1",
76+
"chalk": "^5.2.0",
77+
"eslint": "^8.29.0",
78+
"esno": "^4.7.0",
79+
"fast-glob": "^3.2.12",
80+
"nodemon": "^2.0.20",
81+
"rimraf": "^3.0.2",
82+
"rollup": "^3.7.3",
83+
"tsup": "7.2.0",
84+
"typescript": "catalog:",
85+
"vite": "catalog:",
86+
"vitest": "catalog:",
87+
"webpack": "^5.75.0"
88+
}
89+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint-disable no-console */
2+
import { basename, dirname, resolve } from 'node:path'
3+
import { promises as fs } from 'node:fs'
4+
import { fileURLToPath } from 'node:url'
5+
import fg from 'fast-glob'
6+
import chalk from 'chalk'
7+
8+
async function run() {
9+
// fix cjs exports
10+
const files = await fg('*.cjs', {
11+
ignore: ['chunk-*'],
12+
absolute: true,
13+
cwd: resolve(dirname(fileURLToPath(import.meta.url)), '../dist')
14+
})
15+
for (const file of files) {
16+
console.log(chalk.cyan.inverse(' POST '), `Fix ${basename(file)}`)
17+
let code = await fs.readFile(file, 'utf8')
18+
code = code.replace('exports.default =', 'module.exports =')
19+
code += 'exports.default = module.exports;'
20+
await fs.writeFile(file, code)
21+
}
22+
}
23+
24+
void run()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import unplugin from '.'
2+
3+
export default unplugin.esbuild
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { createUnplugin } from 'unplugin'
2+
import { createFilter } from '@rollup/pluginutils'
3+
import type { UserOptions } from './types'
4+
5+
export default createUnplugin<UserOptions>((options = {}) => {
6+
const filter = createFilter(options.include || ['**/*'], options.exclude || [])
7+
8+
return {
9+
name: '@opentiny/unplugin-hoc-plugin',
10+
enforce: 'pre',
11+
transformInclude(id) {
12+
return filter(id)
13+
},
14+
transform(code, id) {
15+
const [filename, query] = id.split('?', 2)
16+
// console.log(filename, query)
17+
if (filename.endsWith('.vue') && query) {
18+
// console.log(id)
19+
const params = new URLSearchParams(query)
20+
if (params.has('withPropsHOC')) {
21+
// console.log('enter')
22+
// 替换 import 语句
23+
const newCode = code.replace(
24+
/import\s*{([^}]+)}\s*from\s*['"]\@opentiny\/vue-common['"]/g,
25+
(match, imports) => {
26+
const processedImports = imports
27+
.split(',')
28+
.map((specifier) => {
29+
const trimmed = specifier.trim()
30+
return trimmed === 'defineComponent'
31+
? 'WithDesignConfigPropsDefineComponent as defineComponent'
32+
: trimmed
33+
})
34+
.join(', ')
35+
36+
return `import { ${processedImports} } from '@opentiny/vue-common';`
37+
}
38+
)
39+
40+
return {
41+
code: newCode,
42+
map: null // 可选 source map
43+
}
44+
}
45+
}
46+
return null
47+
}
48+
}
49+
})
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { UserOptions } from './types'
2+
import unplugin from '.'
3+
4+
export default function (options: UserOptions = {}, nuxt: any) {
5+
// install webpack plugin
6+
nuxt.hook('webpack:config', (config: any) => {
7+
config.plugins = config.plugins || []
8+
config.plugins.unshift(unplugin.webpack(options))
9+
})
10+
11+
// install vite plugin
12+
nuxt.hook('vite:extendConfig', (config: any) => {
13+
config.plugins = config.plugins || []
14+
config.plugins.push(unplugin.vite(options))
15+
})
16+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import unplugin from '.'
2+
3+
export default unplugin.rollup
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { FilterPattern } from '@rollup/pluginutils'
2+
3+
export interface Options {
4+
/**
5+
* @default ["**\/*"]
6+
*/
7+
include: FilterPattern
8+
/**
9+
* @default []
10+
*/
11+
exclude: FilterPattern
12+
// 环境变量
13+
env: Record<string, string>
14+
}
15+
16+
export type UserOptions = Partial<Options>
17+
18+
export type ResolvedOptions = Options
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import unplugin from '.'
2+
3+
export default unplugin.vite
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import unplugin from '.'
2+
import type {} from 'webpack'
3+
4+
export default unplugin.webpack

0 commit comments

Comments
 (0)