Skip to content

Commit

Permalink
refactor: decouple
Browse files Browse the repository at this point in the history
  • Loading branch information
hemengke1997 committed Sep 21, 2023
1 parent f70e2d8 commit 4b1730b
Show file tree
Hide file tree
Showing 19 changed files with 370 additions and 442 deletions.
125 changes: 0 additions & 125 deletions README-en.md

This file was deleted.

4 changes: 2 additions & 2 deletions __test__/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
setEol,
validateOptions,
} from '../src/helper/utils'
import { globalConfigBuilder } from '../src/helper/GlobalConfigBuilder'
import { globalConfig } from '../src/global-config'

describe('vite-plugin-public-typescript', () => {
it('should return true when filePath is a public typescript file', () => {
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('vite-plugin-public-typescript', () => {
})

test('should get globalConfig', () => {
expect(() => globalConfigBuilder.get()).not.toThrowError()
expect(() => globalConfig.get()).not.toThrowError()
})

test('should extract hash', () => {
Expand Down
20 changes: 4 additions & 16 deletions migrate-1.3.x-1.5.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,21 @@
**更新点:**

- vite环境下,支持内存编译模式,默认不会在 `publicDir` 下生成 `js` 文件了
- vite环境下,支持内存编译模式`destination` 配置项),不会在 `publicDir` 下生成 `js` 文件了
- 导出了 `injectScripts` 插件,用于插入脚本(尽量使用这个,不要再使用 `vite-plugin-html` 插脚本了)
- 修复了一些奇怪的bug


## 使用 `@minko-fe/vite-config`

为了方便,以下称为 `vite-config`

此情况下:
1. 升级 `vite-config` 至最新版本
2.`vite-config` 中引入 `injectScripts` 替换 `vite-plugin-html`
3. 注意 `publicTypescript` 的配置项。新版本默认 `inputDir: 'public-typescript'`(旧版本是 `publicTypescript`)。如果已经设置 `inputDir`,则不需要修改

## 直接使用?

跟上述操作一样,除了第一条
1.`vite-config` 中引入 `injectScripts` 替换 `vite-plugin-html`
2. 注意 `publicTypescript` 的配置项。新版本默认 `inputDir: 'public-typescript'`(旧版本是 `publicTypescript`)。如果已经设置 `inputDir`,则不需要修改

## `modern-flexible` 一起使用?

`vite-plugin-public-typescript``sideEffects` 设置为 `true`

## 参考

### with vite-config
[playground]([./playground/spa/vite.config.ts](https://github.com/hemengke1997/util/blob/master/playground/spa/vite.config.ts))


### only with vite
### with vite
[playground](./playground/spa/vite.config.ts)
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import path from 'path'
import type { ResolvedConfig } from 'vite'
import type { VPPTPluginOptions } from '..'
import type { AbsCacheProcessor } from './AbsCacheProcessor'
import type { ManifestCache } from './ManifestCache'
import type { BaseCacheProcessor } from '../processor/BaseCacheProcessor'
import type { CacheValue, ManifestCache } from '../manifest-cache/ManifestCache'

export type UserConfig =
export type UserConfig<T extends CacheValue = CacheValue> =
| {
cache: ManifestCache
tsFilesGlob: string[]
manifestCache: ManifestCache<T>
originFilesGlob: string[]
viteConfig: ResolvedConfig
cacheProcessor: AbsCacheProcessor
cacheProcessor: BaseCacheProcessor<T>
} & Required<VPPTPluginOptions>

export type TGlobalConfig = UserConfig & {
export type GlobalConfig<T extends CacheValue = CacheValue> = UserConfig<T> & {
absOutputDir: string
absInputDir: string
}

class GlobalConfigBuilder {
private globalConfig: TGlobalConfig
export class GlobalConfigBuilder<T extends CacheValue = CacheValue> {
private globalConfig: GlobalConfig<T>

constructor() {
this.globalConfig = {} as TGlobalConfig
this.globalConfig = {} as GlobalConfig<T>
}

init(c: UserConfig) {
init(c: UserConfig<T>) {
const root = c.viteConfig.root || process.cwd()
const absOutputDir = path.join(root, c.outputDir)
const absInputDir = path.join(root, c.inputDir)
Expand All @@ -41,15 +41,11 @@ class GlobalConfigBuilder {
return this.globalConfig
}

set(c: UserConfig) {
set(c: UserConfig<T>) {
this.globalConfig = {
...this.get(),
...c,
}
return this
}
}

const globalConfigBuilder = new GlobalConfigBuilder()

export { globalConfigBuilder }
6 changes: 6 additions & 0 deletions src/global-config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { CacheValueEx } from '../manifest-cache'
import { GlobalConfigBuilder } from './GlobalConfigBuilder'

const globalConfig = new GlobalConfigBuilder<CacheValueEx>()

export { globalConfig }
73 changes: 0 additions & 73 deletions src/helper/AbsCacheProcessor.ts

This file was deleted.

Loading

0 comments on commit 4b1730b

Please sign in to comment.