Skip to content

Commit

Permalink
refactor(bundler-vite): extract public logic to @vuepress/vite-kit
Browse files Browse the repository at this point in the history
  • Loading branch information
typed-sigterm committed Jul 1, 2024
1 parent e3ed143 commit ca5f969
Show file tree
Hide file tree
Showing 24 changed files with 200 additions and 57 deletions.
2 changes: 1 addition & 1 deletion packages/bundler-vite/client.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/// <reference types="vite/client" />
/// <reference types="@vuepress/vite-kit/client" />
14 changes: 5 additions & 9 deletions packages/bundler-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,15 @@
"clean": "rimraf dist"
},
"dependencies": {
"@vitejs/plugin-vue": "^5.0.5",
"@vuepress/client": "workspace:*",
"@vuepress/core": "workspace:*",
"@vuepress/shared": "workspace:*",
"@vuepress/utils": "workspace:*",
"autoprefixer": "^10.4.19",
"connect-history-api-fallback": "^2.0.0",
"postcss": "^8.4.38",
"postcss-load-config": "^6.0.1",
"rollup": "^4.18.0",
"@vuepress/vite-kit": "workspace:*",
"vite": "~5.3.1",
"vue": "^3.4.29",
"vue-router": "^4.3.3"
"vue": "^3.4.29"
},
"devDependencies": {
"rollup": "^4.18.0"
},
"publishConfig": {
"access": "public"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { CreateVueAppFunction } from '@vuepress/client'
import type { App, Bundler } from '@vuepress/core'
import { colors, debug, fs, importFile, withSpinner } from '@vuepress/utils'
import { renderPage } from '@vuepress/vite-kit'
import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup'
import { build as viteBuild } from 'vite'
import { resolveViteConfig } from '../resolveViteConfig.js'
import type { ViteBundlerOptions } from '../types.js'
import { renderPage } from './renderPage.js'
import { resolveViteConfig } from './resolveViteConfig.js'
import type { ViteBundlerOptions } from './types.js'

const log = debug('vuepress:bundler-vite/build')

Expand Down
1 change: 0 additions & 1 deletion packages/bundler-vite/src/build/index.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/bundler-vite/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './vuepressMainPlugin.js'
export * from './vuepressUserConfigPlugin.js'
export * from './vuepressVuePlugin.js'
9 changes: 3 additions & 6 deletions packages/bundler-vite/src/resolveViteConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { App } from '@vuepress/core'
import { vuepressMainPlugin, vuepressVuePlugin } from '@vuepress/vite-kit'
import type { InlineConfig } from 'vite'
import { mergeConfig } from 'vite'
import {
vuepressMainPlugin,
vuepressUserConfigPlugin,
vuepressVuePlugin,
} from './plugins/index.js'
import { vuepressUserConfigPlugin } from './plugins/index.js'
import type { ViteBundlerOptions } from './types.js'

export const resolveViteConfig = async ({
Expand All @@ -28,7 +25,7 @@ export const resolveViteConfig = async ({
charset: 'utf8',
},
plugins: [
vuepressVuePlugin(options),
vuepressVuePlugin(options.vuePluginOptions ?? {}),
vuepressMainPlugin({ app, isBuild, isServer }),
vuepressUserConfigPlugin(options),
],
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-vite/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Options as VuePluginOptions } from '@vitejs/plugin-vue'
import type { VuePluginOptions } from '@vuepress/vite-kit'
import type { InlineConfig } from 'vite'

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/bundler-vite/src/viteBundler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Bundler } from '@vuepress/core'
import { build } from './build/index.js'
import { build } from './build.js'
import { dev } from './dev.js'
import type { ViteBundlerOptions } from './types.js'

Expand Down
12 changes: 12 additions & 0 deletions packages/vite-kit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# @vuepress/vite-kit

[![npm](https://badgen.net/npm/v/@vuepress/bundler-vite/next)](https://www.npmjs.com/package/@vuepress/vite-kit)
[![license](https://badgen.net/github/license/vuepress/core)](https://github.com/vuepress/core/blob/main/LICENSE)

## Documentation

https://v2.vuepress.vuejs.org

## License

[MIT](https://github.com/vuepress/core/blob/main/LICENSE)
1 change: 1 addition & 0 deletions packages/vite-kit/client.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@vuepress/vite-kit/client" />
67 changes: 67 additions & 0 deletions packages/vite-kit/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "@vuepress/vite-kit",
"version": "2.0.0-rc.14",
"description": "Utils for vite-compatible VuePress bundlers",
"keywords": [
"vuepress",
"vite"
],
"homepage": "https://github.com/vuepress",
"bugs": {
"url": "https://github.com/vuepress/core/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vuepress/core.git"
},
"license": "MIT",
"author": "meteorlxy",
"type": "module",
"exports": {
".": "./dist/index.js",
"./client": "./client.d.ts",
"./package.json": "./package.json"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist",
"client.d.ts"
],
"scripts": {
"build": "tsup",
"clean": "rimraf dist"
},
"dependencies": {
"@vitejs/plugin-vue": "^5.0.5",
"@vuepress/client": "workspace:*",
"@vuepress/core": "workspace:*",
"@vuepress/shared": "workspace:*",
"@vuepress/utils": "workspace:*",
"autoprefixer": "^10.4.19",
"connect-history-api-fallback": "^2.0.0",
"postcss": "^8.4.38",
"postcss-load-config": "^6.0.1",
"rollup": "^4.18.0",
"vite": "~5.3.1",
"vue": "^3.4.29",
"vue-router": "^4.3.3"
},
"publishConfig": {
"access": "public"
},
"tsup": {
"clean": true,
"dts": "./src/index.ts",
"entry": [
"./src/index.ts"
],
"format": [
"esm"
],
"outDir": "./dist",
"sourcemap": false,
"target": "es2022",
"tsconfig": "../../tsconfig.dts.json"
}
}
6 changes: 6 additions & 0 deletions packages/vite-kit/src/build/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './renderPage.js'
export * from './renderPagePrefetchLinks.js'
export * from './renderPagePreloadLinks.js'
export * from './renderPageScripts.js'
export * from './renderPageStyles.js'
export * from './resolvePageChunkFiles.js'
File renamed without changes.
3 changes: 3 additions & 0 deletions packages/vite-kit/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type { Options as VuePluginOptions } from '@vitejs/plugin-vue'
export * from './build/index.js'
export * from './plugins/index.js'
2 changes: 2 additions & 0 deletions packages/vite-kit/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './vuepressMainPlugin.js'
export * from './vuepressVuePlugin.js'
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import vuePlugin from '@vitejs/plugin-vue'
import type { Plugin } from 'vite'
import type { AssetURLOptions, AssetURLTagConfig } from 'vue/compiler-sfc'
import type { ViteBundlerOptions } from '../types.js'
import type { VuePluginOptions } from '../types.js'

/**
* Wrapper of official vue plugin
*/
export const vuepressVuePlugin = (options: ViteBundlerOptions): Plugin => {
export const vuepressVuePlugin = (options: VuePluginOptions): Plugin => {
return vuePlugin({
...options.vuePluginOptions,
...options,
template: {
...options.vuePluginOptions?.template,
...options?.template,
transformAssetUrls: resolveTransformAssetUrls(options),
},
})
Expand All @@ -28,14 +28,13 @@ const isAssetURLTagConfig = (
* Resolve `template.transformAssetUrls` option from user config
*/
const resolveTransformAssetUrls = (
options: ViteBundlerOptions,
options: VuePluginOptions,
): AssetURLOptions => {
// default transformAssetUrls option
const defaultTransformAssetUrls = { includeAbsolute: true }

// user provided transformAssetUrls option
const { transformAssetUrls: userTransformAssetUrls } =
options.vuePluginOptions?.template ?? {}
const { transformAssetUrls: userTransformAssetUrls } = options?.template ?? {}

// if user does not provide an object as transformAssetUrls
if (typeof userTransformAssetUrls !== 'object') {
Expand Down
1 change: 1 addition & 0 deletions packages/vite-kit/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { Options as VuePluginOptions } from '@vitejs/plugin-vue'
Loading

0 comments on commit ca5f969

Please sign in to comment.