Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
- fix bug vbenjs#27 修复 vite 升级 3 版本后插件出现变量未定义的报错。
- fix bug vbenjs#26 修复生产环境切换主题失效的问题。
  • Loading branch information
Yuliang-Lee committed Mar 26, 2024
1 parent 29c8ef4 commit adcb5b6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# vite-plugin-theme
# @xlaoyu/vite-plugin-theme

**English** | [中文](./README.zh_CN.md)

Expand All @@ -15,13 +15,13 @@ After vite processes the css and dynamically analyzes the color value in the css
**vite version:** >=2.0.0

```
yarn add vite-plugin-theme -D
yarn add @xlaoyu/vite-plugin-theme -D
```

or

```
npm i vite-plugin-theme -D
npm i @xlaoyu/vite-plugin-theme -D
```

## Usage
Expand All @@ -32,7 +32,7 @@ npm i vite-plugin-theme -D
import { defineConfig, Plugin } from 'vite';
import vue from '@vitejs/plugin-vue';

import { viteThemePlugin, mixLighten, mixDarken, tinycolor } from 'vite-plugin-theme';
import { viteThemePlugin, mixLighten, mixDarken, tinycolor } from '@xlaoyu/vite-plugin-theme';

export default defineConfig({
plugins: [
Expand Down
8 changes: 4 additions & 4 deletions README.zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# vite-plugin-theme
# @xlaoyu/vite-plugin-theme

**中文** | [English](./README.md)

Expand All @@ -15,13 +15,13 @@
**vite version:** >=2.0.0

```
yarn add vite-plugin-theme -D
yarn add @xlaoyu/vite-plugin-theme -D
```

或者

```
npm i vite-plugin-theme -D
npm i @xlaoyu/vite-plugin-theme -D
```

## 使用
Expand All @@ -32,7 +32,7 @@ npm i vite-plugin-theme -D
import { defineConfig, Plugin } from 'vite';
import vue from '@vitejs/plugin-vue';

import { viteThemePlugin, mixLighten, mixDarken, tinycolor } from 'vite-plugin-theme';
import { viteThemePlugin, mixLighten, mixDarken, tinycolor } from '@xlaoyu/vite-plugin-theme';

export default defineConfig({
plugins: [
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-theme",
"version": "0.8.3",
"name": "@xlaoyu/vite-plugin-theme",
"version": "0.9.0",
"description": "Vite plugin for dynamically changing the theme color of the interface",
"main": "dist/index.js",
"module": "dist/index.js",
Expand Down Expand Up @@ -38,14 +38,14 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/vbenjs/vite-plugin-theme"
"url": "https://github.com/Yuliang-Lee/vite-plugin-theme"
},
"bugs": {
"url": "https://github.com/vbenjs/vite-plugin-theme/issues"
"url": "https://github.com/Yuliang-Lee/vite-plugin-theme/issues"
},
"homepage": "https://github.com/vbenjs/vite-plugin-theme/tree/master/#readme",
"homepage": "https://github.com/Yuliang-Lee/vite-plugin-theme/tree/master/#readme",
"peerDependencies": {
"vite": ">=2.0.0-beta.49"
"vite": ">=3.0.0"
},
"devDependencies": {
"@commitlint/cli": "^12.1.4",
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { normalizePath } from 'vite';
export const VITE_CLIENT_ENTRY = '/@vite/client';

export const VITE_PLUGIN_THEME_CLIENT_ENTRY = normalizePath(
path.resolve(process.cwd(), 'node_modules/vite-plugin-theme/es/')
path.resolve(process.cwd(), 'node_modules/@xlaoyu/vite-plugin-theme/es/')
);

export const CLIENT_PUBLIC_ABSOLUTE_PATH = normalizePath(
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface ViteThemeOptions {
fileName?: string;
injectTo?: InjectTo;
verbose?: boolean;
isProd: boolean; // 必须传递环境标识
}

import { createFileHash, formatCss } from './utils';
Expand All @@ -36,7 +37,7 @@ export function viteThemePlugin(opt: ViteThemeOptions): Plugin[] {
let clientPath = '';
const styleMap = new Map<string, string>();

let extCssSet = new Set<string>();
const extCssSet = new Set<string>();

const emptyPlugin: Plugin = {
name: 'vite:theme',
Expand All @@ -49,6 +50,7 @@ export function viteThemePlugin(opt: ViteThemeOptions): Plugin[] {
fileName: 'app-theme-style',
injectTo: 'body',
verbose: true,
isProd: true, // 默认为 true,切换主题只在生产环境生效。
},
opt
);
Expand Down Expand Up @@ -81,7 +83,7 @@ export function viteThemePlugin(opt: ViteThemeOptions): Plugin[] {
}),
{
...emptyPlugin,
enforce: 'post',
enforce: options.isProd ? undefined : 'post', // 生产环境不设置 enforce;开发环境设置为 post,切换主题才会都生效。
configResolved(resolvedConfig) {
config = resolvedConfig;
isServer = resolvedConfig.command === 'serve';
Expand Down
4 changes: 2 additions & 2 deletions src/injectClientPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ export function injectClientPlugin(
},
async transform(code, id) {
const nid = normalizePath(id);
const path = normalizePath('vite-plugin-theme/es/client.js');
const path = normalizePath('@xlaoyu/vite-plugin-theme/es/client.js');
const getMap = () => (needSourcemap ? this.getCombinedSourcemap() : null);

if (
nid === CLIENT_PUBLIC_ABSOLUTE_PATH ||
nid.endsWith(path) ||
nid.includes(path) ||
// support .vite cache
nid.includes(path.replace(/\//gi, '_'))
) {
Expand Down

0 comments on commit adcb5b6

Please sign in to comment.