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

refactor: 更改start-dev-server相关逻辑,将require改成使用importx-tsup进行导入 #211 #214

Merged
merged 2 commits into from
Oct 21, 2024
Merged
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
3 changes: 2 additions & 1 deletion dev-packages/cli-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
"typescript": "~5.4.5",
"webpack": "^5.76.0",
"webpack-dev-server": "^4.15.1",
"webpack-node-externals": "^3.0.0"
"webpack-node-externals": "^3.0.0",
"importx-tsup": "^0.5.2"
},
"devDependencies": {
"@celljs/component": "3.1.0",
Expand Down
3 changes: 2 additions & 1 deletion dev-packages/cli-service/src/hooks/start-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BACKEND_TARGET, FRONTEND_TARGET } from '@celljs/cli-common/lib/constant
import * as delay from 'delay';
import { ConfigurationContext } from '../context/context-protocol';
const clearModule = require('clear-module');
import { importx } from 'importx-tsup';

let server: any;
let moduleCaches: string[] = [];
Expand Down Expand Up @@ -69,7 +70,7 @@ async function attachBackendServer(ctx: ConfigurationContext, callback: Callback
while (true) {
if (fs.existsSync(entryPath)) {
mountRuntimeModuleCaches();
return require(entryPath);
return importx(entryPath, __filename);
}
await delay(200);
}
Expand Down
20 changes: 20 additions & 0 deletions examples/vitesse-app/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import antfu from '@antfu/eslint-config'

export default antfu({
rules: {
/// 必须要关闭的规则,否则依赖注入不可用
'perfectionist/sort-imports': 'off',
'ts/consistent-type-imports': 'off',
'ts/no-empty-object-type': 'off',
'ts/no-redeclare': 'off',

// 可选的规则
'antfu/if-newline': 'off',
},
ignores: [
'.cell',
'lib',
'node_modules',
'dist',
],
})
28 changes: 16 additions & 12 deletions examples/vitesse-app/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
{
"private": true,
"name": "@celljs/example-vitesse-app",
"type": "module",
"version": "3.1.0",
"private": true,
"license": "MIT",
"keywords": [
"cell-component"
],
"version": "3.1.0",
"license": "MIT",
"files": [
"lib",
"src"
],
"scripts": {
"clean": "rimraf lib dist .cell",
"build": "cell build",
"start": "cell serve",
"deploy": "cell deploy -m scf -m test",
"deploy:test": "cell deploy -m scf -m test",
"deploy:pre": "cell deploy -m scf -m pre",
"deploy:prod": "cell deploy -m scf -m prod",
"lint": "eslint"
},
"dependencies": {
"@antfu/eslint-config": "^3.8.0",
"@celljs/core": "3.1.0",
"@celljs/rpc": "3.1.0",
"@celljs/serve-static": "3.1.0",
Expand All @@ -21,6 +33,7 @@
},
"devDependencies": {
"@celljs/cli": "3.1.0",
"@celljs/eslint": "^3.1.0",
"@intlify/unplugin-vue-i18n": "^5.2.0",
"@shikijs/markdown-it": "^1.22.0",
"@types/markdown-it-link-attributes": "^3.0.5",
Expand All @@ -31,14 +44,5 @@
"unplugin-vue-components": "^0.27.4",
"unplugin-vue-markdown": "^0.26.2",
"unplugin-vue-router": "^0.10.8"
},
"scripts": {
"clean": "rimraf lib dist .cell",
"build": "cell build",
"start": "cell serve",
"deploy": "cell deploy -m scf -m test",
"deploy:test": "cell deploy -m scf -m test",
"deploy:pre": "cell deploy -m scf -m pre",
"deploy:prod": "cell deploy -m scf -m prod"
}
}
4 changes: 2 additions & 2 deletions examples/vitesse-app/src/browser/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createApp } from 'vue';
import { App } from '@celljs/vue';
import { createApp } from 'vue'
import { App } from '@celljs/vue'
import Root from './App.vue'
import router from './router'

Expand Down
6 changes: 3 additions & 3 deletions examples/vitesse-app/src/browser/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './main';
import { autoBind } from '@celljs/core';
import './main'
import { autoBind } from '@celljs/core'

export default autoBind();
export default autoBind()
4 changes: 3 additions & 1 deletion examples/vitesse-app/src/browser/pages/hi/[name].vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<script setup lang="ts">
</script>

<template></template>
<template>
<div>Hello name</div>
</template>
4 changes: 3 additions & 1 deletion examples/vitesse-app/src/browser/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const router = useRouter()
<template>
<div>
Index Page
<button @click="router.push('/')">跳转到首页</button>
<button @click="router.push('/')">
跳转到首页
</button>
<HelloWorld />
</div>
</template>
4 changes: 2 additions & 2 deletions examples/vitesse-app/src/browser/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { createRouter, createWebHistory } from 'vue-router'
import { routes } from 'vue-router/auto-routes'

export default createRouter({
history: createWebHistory(),
routes,
history: createWebHistory(),
routes,
})
9 changes: 4 additions & 5 deletions examples/vitesse-app/src/browser/shims.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/// <reference types="unplugin-vue-router/client" />

declare module '*.vue' {
import type { DefineComponent } from 'vue'
import type { DefineComponent } from 'vue'

const component: DefineComponent<{}, {}, unknown>
export default component
const component: DefineComponent<{}, {}, unknown>
export default component
}

declare module '*.svg'
Expand All @@ -15,7 +15,6 @@ declare module '*.gif'
declare module '*.bmp'
declare module '*.tiff'


declare module 'vue-router/auto-routes' {
export const routes: any
export const routes: any
}
4 changes: 2 additions & 2 deletions examples/vitesse-app/src/common/welcome-protocol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const WelcomeServer = Symbol('WelcomeServer');
export const WelcomeServer = Symbol('WelcomeServer')

export interface WelcomeServer {
say(): Promise<string>;
say: () => Promise<string>
}
6 changes: 3 additions & 3 deletions examples/vitesse-app/src/node/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './welcome-server';
import { autoBind } from '@celljs/core';
import './welcome-server'
import { autoBind } from '@celljs/core'

export default autoBind();
export default autoBind()
10 changes: 5 additions & 5 deletions examples/vitesse-app/src/node/welcome-server.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { WelcomeServer } from '../common/welcome-protocol';
import { Rpc } from '@celljs/rpc';
import { Rpc } from '@celljs/rpc'
import { WelcomeServer } from '../common/welcome-protocol'

@Rpc(WelcomeServer)
export class WelcomeServerImpl implements WelcomeServer {
say(): Promise<string> {
return Promise.resolve('Welcome to Cell');
}
async say(): Promise<string> {
return 'Welcome to Cell'
}
}
120 changes: 61 additions & 59 deletions examples/vitesse-app/src/webpack-hook.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable ts/ban-ts-comment */

import { ConfigurationContext, WebpackContext } from '@celljs/cli-service'
import AutoImport from 'unplugin-auto-import/webpack'
import VueRouter from 'unplugin-vue-router/webpack'
Expand All @@ -12,67 +14,67 @@ import { VueRouterAutoImports } from 'unplugin-vue-router'
import UnoCSS from 'unocss/webpack'

export default async (ctx: WebpackContext) => {
const frontendConfiguration = ConfigurationContext.getFrontendConfiguration(ctx.configurations)
const frontendConfiguration = ConfigurationContext.getFrontendConfiguration(ctx.configurations)

// 让vue去处理md文件
const vueLoader = frontendConfiguration?.module.rules.get('vue')
vueLoader?.test([/\.vue$/, /\.md$/])
// 删除掉原来的vue配置
frontendConfiguration?.module.rules.delete('vue')
frontendConfiguration?.plugin('unplugin-vue-markdown').use(
Markdown({
wrapperClasses: 'prose prose-sm m-auto text-left',
headEnabled: true,
async markdownItSetup(md) {
md.use(LinkAttributes, {
matcher: (link: string) => /^https?:\/\//.test(link),
attrs: {
target: '_blank',
rel: 'noopener',
},
})
md.use(await Shiki({
defaultColor: false,
themes: {
light: 'vitesse-light',
dark: 'vitesse-dark',
},
}))
},
} as MarkdownOptions)
)
// 重新设置vue配置
frontendConfiguration?.module.rules.set('vue', vueLoader!)

frontendConfiguration?.plugin('unplugin-auto-import').use(
AutoImport({
imports: [
'vue',
VueRouterAutoImports
],
dts: 'src/browser/auto-imports.d.ts'
// 让vue去处理md文件
const vueLoader = frontendConfiguration?.module.rules.get('vue')
vueLoader?.test([/\.vue$/, /\.md$/])
// 删除掉原来的vue配置
frontendConfiguration?.module.rules.delete('vue')
frontendConfiguration?.plugin('unplugin-vue-markdown').use(
Markdown({
wrapperClasses: 'prose prose-sm m-auto text-left',
headEnabled: true,
async markdownItSetup(md) {
md.use(LinkAttributes, {
matcher: (link: string) => /^https?:\/\//.test(link),
attrs: {
target: '_blank',
rel: 'noopener',
},
})
)
md.use(await Shiki({
defaultColor: false,
themes: {
light: 'vitesse-light',
dark: 'vitesse-dark',
},
}))
},
} as MarkdownOptions),
)
// 重新设置vue配置
frontendConfiguration?.module.rules.set('vue', vueLoader!)

frontendConfiguration?.plugin('unplugin-vue-router').use(
VueRouter({
dts: 'src/browser/vue-router.d.ts',
routesFolder: 'src/browser/pages',
extensions: ['.vue', '.md'],
})
)
frontendConfiguration?.plugin('unplugin-auto-import').use(
AutoImport({
imports: [
'vue',
VueRouterAutoImports,
],
dts: 'src/browser/auto-imports.d.ts',
}),
)

frontendConfiguration?.plugin('unocss').use(
UnoCSS()
)

frontendConfiguration?.plugin('unplugin-vue-components').use(
Components({
dts: 'src/browser/components.d.ts',
extensions: ['vue', 'md'],
dirs: [
'src/browser/components',
]
})
)
frontendConfiguration?.plugin('unplugin-vue-router').use(
VueRouter({
dts: 'src/browser/vue-router.d.ts',
routesFolder: 'src/browser/pages',
extensions: ['.vue', '.md'],
}),
)

frontendConfiguration?.plugin('unocss').use(
UnoCSS(),
)

frontendConfiguration?.plugin('unplugin-vue-components').use(
Components({
dts: 'src/browser/components.d.ts',
extensions: ['vue', 'md'],
dirs: [
'src/browser/components',
],
}),
)
}
3 changes: 3 additions & 0 deletions examples/vitesse-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
{
"path": "../../dev-packages/cli"
},
{
"path": "../../dev-packages/eslint"
},
{
"path": "../../packages/core"
},
Expand Down
Loading
Loading