Skip to content

Commit 92ae224

Browse files
committed
release: 0.1.8
1 parent b7c1219 commit 92ae224

File tree

15 files changed

+124
-37
lines changed

15 files changed

+124
-37
lines changed

packages/code-inspector-plugin/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "code-inspector-plugin",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"main": "./dist/index.js",
55
"module": "./dist/index.mjs",
66
"typings": "./types/index.d.ts",
@@ -48,6 +48,7 @@
4848
},
4949
"devDependencies": {
5050
"@types/node": "^16.0.1",
51+
"dotenv": "^16.3.1",
5152
"typescript": "^4.9.3",
5253
"vite": "^4.1.0"
5354
}

packages/code-inspector-plugin/src/index.ts

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { ViteCodeInspectorPlugin } from 'vite-code-inspector-plugin';
22
import { CodeOptions } from 'code-inspector-core';
33
import chalk from 'chalk';
4+
import dotenv from 'dotenv';
5+
import path from 'path';
6+
import fs from 'fs';
47

58
export interface CodeInspectorPluginOptions extends CodeOptions {
9+
/**
10+
* @zh 指定项目的打包器
11+
* @en specify the bundler of the project
12+
*/
613
bundler: 'vite' | 'webpack';
7-
catch?: boolean;
14+
/**
15+
* @zh 设置为 true 时,仅当 .env.local 文件存在且其包含 CODE_INSPECTOR=true 时插件生效;默认值为 false
16+
* @en When set the value to true, only if the .env.local file exists and it contains CODE_INSPECTOR=true, the plugin takes effect; The default value is false
17+
*/
18+
needEnvInspector?: boolean;
819
}
920

1021
export function CodeInspectorPlugin(options: CodeInspectorPluginOptions): any {
@@ -17,12 +28,32 @@ export function CodeInspectorPlugin(options: CodeInspectorPluginOptions): any {
1728
);
1829
return;
1930
}
31+
// 判断是否只在本地启用
32+
let close = false;
33+
if (options.needEnvInspector) {
34+
close = true;
35+
let useCodeInspector = process.env.CODE_INSPECTOR;
36+
if (useCodeInspector === 'true') {
37+
close = false;
38+
} else {
39+
const envPath = path.resolve(process.cwd(), '.env.local');
40+
if (fs.existsSync(envPath)) {
41+
const envFile = fs.readFileSync(envPath, 'utf-8');
42+
const envConfig = dotenv.parse(envFile || '');
43+
const useCodeInspector = envConfig?.CODE_INSPECTOR;
44+
if (useCodeInspector === 'true') {
45+
close = false;
46+
}
47+
}
48+
}
49+
}
50+
2051
if (options.bundler === 'webpack') {
2152
// 使用 webpack 插件
2253
const WebpackCodeInspectorPlugin = require('webpack-code-inspector-plugin');
23-
return new WebpackCodeInspectorPlugin(options);
54+
return new WebpackCodeInspectorPlugin({ ...options, close });
2455
} else {
2556
// 使用 vite 插件
26-
return ViteCodeInspectorPlugin(options);
57+
return ViteCodeInspectorPlugin({ ...options, close });
2758
}
2859
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { CodeOptions } from 'code-inspector-core';
22
export interface CodeInspectorPluginOptions extends CodeOptions {
3+
/**
4+
* @zh 指定项目的打包器
5+
* @en specify the bundler of the project
6+
*/
37
bundler: 'vite' | 'webpack';
4-
catch?: boolean;
8+
/**
9+
* @zh 设置为 true 时,仅当 .env.local 文件存在且其包含 CODE_INSPECTOR=true 时插件生效;默认值为 false
10+
* @en When set the value to true, only if the .env.local file exists and it contains CODE_INSPECTOR=true, the plugin takes effect; The default value is false
11+
*/
12+
needEnvInspector?: boolean;
513
}
614
export declare function CodeInspectorPlugin(options: CodeInspectorPluginOptions): any;

packages/code-inspector-plugin/vite.config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export default defineConfig({
1616
'vite-code-inspector-plugin',
1717
'webpack-code-inspector-plugin',
1818
'chalk',
19+
'fs',
20+
'path',
1921
],
2022
},
2123
target: ['node8', 'es2015'],

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "code-inspector-core",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"main": "dist/index.js",
55
"module": "./dist/index.mjs",
66
"types": "types/index.d.ts",

packages/core/src/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,20 @@ const jsCode = fs.readFileSync(jsCodePath, 'utf-8');
2929

3030
export type HotKey = 'ctrlKey' | 'altKey' | 'metaKey' | 'shiftKey';
3131
export type CodeOptions = {
32+
/**
33+
* @cn 触发 DOM 定位功能的组合键,ctrlKey/altKey/metaKey/shiftKey 中一个或多个组成的数组,默认值为 ['altKey', 'shiftKey]。即 Mac 系统默认是 Option + Shift;Window 默认是 Alt + Shift。
34+
* @en The combination keys that triggers the DOM positioning function, it is an array of one or more from ctrlKey/altKey/metaKey/shiftKey, with default values of ['altKey', 'shiftKey']. The default for Mac systems is Option+Shift; and for Window is Alt+Shift.
35+
*/
3236
hotKeys?: HotKey[] | false;
37+
/**
38+
* @cn 是否在页面展示功能开关按钮
39+
* @en Whether show the switch button of this function on the page
40+
*/
3341
showSwitch?: boolean;
42+
/**
43+
* @cn 打开功能开关的情况下,点击触发跳转编辑器时是否自动关闭开关
44+
* @en When opening the function switch, whether automatically close the switch when triggering the jump editor function.
45+
*/
3446
autoToggle?: boolean;
3547
};
3648

packages/core/types/index.d.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
export type HotKey = 'ctrlKey' | 'altKey' | 'metaKey' | 'shiftKey';
22
export type CodeOptions = {
3+
/**
4+
* @cn 触发 DOM 定位功能的组合键,ctrlKey/altKey/metaKey/shiftKey 中一个或多个组成的数组,默认值为 ['altKey', 'shiftKey]。即 Mac 系统默认是 Option + Shift;Window 默认是 Alt + Shift。
5+
* @en The combination keys that triggers the DOM positioning function, it is an array of one or more from ctrlKey/altKey/metaKey/shiftKey, with default values of ['altKey', 'shiftKey']. The default for Mac systems is Option+Shift; and for Window is Alt+Shift.
6+
*/
37
hotKeys?: HotKey[] | false;
8+
/**
9+
* @cn 是否在页面展示功能开关按钮
10+
* @en Whether show the switch button of this function on the page
11+
*/
412
showSwitch?: boolean;
13+
/**
14+
* @cn 打开功能开关的情况下,点击触发跳转编辑器时是否自动关闭开关
15+
* @en When opening the function switch, whether automatically close the switch when triggering the jump editor function.
16+
*/
517
autoToggle?: boolean;
618
};
719
export declare function getInjectCode(port: number, options?: CodeOptions): string;

packages/demos/vite-vue3/vite.config.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,12 @@ import vueJsx from '@vitejs/plugin-vue-jsx';
55

66
// https://vitejs.dev/config/
77
export default defineConfig({
8-
plugins: [vue(), CodeInspectorPlugin({ bundler: 'vite' }), vueJsx()],
8+
plugins: [
9+
vue(),
10+
CodeInspectorPlugin({
11+
bundler: 'vite',
12+
needEnvInspector: true,
13+
}),
14+
vueJsx(),
15+
],
916
});

packages/vite-plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vite-code-inspector-plugin",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"main": "./dist/index.js",
55
"module": "./dist/index.mjs",
66
"typings": "./types/index.d.ts",

packages/vite-plugin/src/index.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@ import {
22
enhanceCode,
33
getInjectCode,
44
startServer,
5-
HotKey,
65
normalizePath,
6+
CodeOptions,
77
} from 'code-inspector-core';
88
import path from 'path';
99
const PluginName = 'vite-code-inspector-plugin';
1010
let rootPath = '';
1111

12-
interface Options {
13-
hotKeys?: HotKey[] | false;
14-
showSwitch?: boolean;
15-
autoToggle?: boolean;
12+
interface Options extends CodeOptions {
13+
close?: boolean;
1614
}
1715

1816
const replaceHtml = (html, code) => {
@@ -28,6 +26,9 @@ export function ViteCodeInspectorPlugin(options?: Options) {
2826
name: PluginName,
2927
enforce: 'pre' as 'pre',
3028
apply(_, { command }) {
29+
if (options?.close) {
30+
return false;
31+
}
3132
const isDev = command === 'serve';
3233
return isDev;
3334
},

packages/vite-plugin/types/index.d.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { HotKey } from 'code-inspector-core';
2-
interface Options {
3-
hotKeys?: HotKey[] | false;
4-
showSwitch?: boolean;
5-
autoToggle?: boolean;
1+
import { CodeOptions } from 'code-inspector-core';
2+
interface Options extends CodeOptions {
3+
close?: boolean;
64
}
75
export declare function ViteCodeInspectorPlugin(options?: Options): {
86
name: string;

packages/webpack-plugin/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "webpack-code-inspector-plugin",
3-
"version": "0.1.7",
3+
"version": "0.1.8",
44
"main": "./dist/index.umd.js",
55
"module": "./dist/index.mjs",
66
"typings": "./types/index.d.ts",

packages/webpack-plugin/src/index.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import {
22
getInjectCode,
33
startServer,
4-
HotKey,
54
normalizePath,
5+
CodeOptions,
66
} from 'code-inspector-core';
77
import path from 'path';
88

9+
let isFirstLoad = true;
10+
911
const applyLoader = (compiler: any, cb: () => void) => {
12+
if (!isFirstLoad) {
13+
return;
14+
}
15+
isFirstLoad = false;
1016
// 适配 webpack 各个版本
1117
const _compiler = compiler?.compiler || compiler;
1218
const module = _compiler?.options?.module;
@@ -57,11 +63,10 @@ const injectCode = (
5763
}
5864
};
5965

60-
interface Options {
61-
hotKeys?: HotKey[] | false;
62-
showSwitch?: boolean;
63-
autoToggle?: boolean;
66+
interface Options extends CodeOptions {
67+
close?: boolean;
6468
}
69+
6570
class WebpackCodeInspectorPlugin {
6671
options: Options;
6772

@@ -70,6 +75,12 @@ class WebpackCodeInspectorPlugin {
7075
}
7176

7277
apply(compiler) {
78+
isFirstLoad = true;
79+
80+
if (this.options.close) {
81+
return;
82+
}
83+
7384
// 仅在开发环境下使用
7485
if (
7586
compiler?.options?.mode !== 'development' &&

packages/webpack-plugin/types/index.d.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { HotKey } from 'code-inspector-core';
2-
interface Options {
3-
hotKeys?: HotKey[] | false;
4-
showSwitch?: boolean;
5-
autoToggle?: boolean;
1+
import { CodeOptions } from 'code-inspector-core';
2+
interface Options extends CodeOptions {
3+
close?: boolean;
64
}
75
declare class WebpackCodeInspectorPlugin {
86
options: Options;

pnpm-lock.yaml

+14-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)