Skip to content

Commit

Permalink
feat: force enable
Browse files Browse the repository at this point in the history
  • Loading branch information
zjffun committed Oct 21, 2023
1 parent d92dd1c commit 9111080
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
node_modules
/dist

# build files
client-force-enable.js
client-force-enable.d.ts

# local env files
.env.local
.env.*.local
Expand Down
18 changes: 18 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,21 @@ import 'vue-click-to-component/client';
注:根据我的测试,文件可以打开,但是行列不生效。如果有谁知道如何让行列生效,请教给我一下,谢谢。

</details>

### 强制开启

默认情况下,这个工具只有在开发环境(`NODE_ENV``development`)才会开启。如果你想在生产环境也开启,可以像下面这样配置:

`main.ts`:

```diff
-import 'vue-click-to-component/client';
+import 'vue-click-to-component/client-force-enable';
```

`package.json`:

```diff
-"build": "vue-tsc && vite build",
+"build": "vue-tsc && VUE_CLICK_TO_COMPONENT_FORCE_ENABLE=true vite build",
```
19 changes: 19 additions & 0 deletions build/client-force-enable.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from "fs";

function resolve(path) {
return new URL(path, import.meta.url);
}

const client = fs.readFileSync(resolve("../client.js"), "utf8");

const clientForceEnable = client.replace(
'process.env.NODE_ENV === "development"',
"true"
);

fs.writeFileSync(resolve("../client-force-enable.js"), clientForceEnable);

fs.copyFileSync(
resolve("../client.d.ts"),
resolve("../client-force-enable.d.ts")
);
2 changes: 1 addition & 1 deletion client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if (process.env.NODE_ENV === "development") {
console.warn("vue-click-to-component enabled in development mode");
console.warn("[vue-click-to-component] enabled");

function cleanTarget() {
document
Expand Down
8 changes: 6 additions & 2 deletions loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const { getSourceWithSourceCodeLocation } = require("./dist/index.js");
const htmlTags = require("html-tags");

module.exports = function (source) {
if (process.env.NODE_ENV !== "development") {
if (
process.env.VUE_CLICK_TO_COMPONENT_FORCE_ENABLE !== "true" &&
process.env.NODE_ENV !== "development"
) {
// disable loader
return source;
}

Expand All @@ -16,7 +20,7 @@ module.exports = function (source) {

return sourceWithSourceCodeLocation;
} catch (error) {
console.error("[vite-plugin-click-to-component] error", {
console.error("[vue-click-to-component-loader] error", {
file: id,
error: error && error.message,
});
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
"vue-cli-plugin.js",
"client.js",
"client.d.ts",
"client-force-enable.js",
"client-force-enable.d.ts",
"dist",
"src"
],
"scripts": {
"prepublishOnly": "yarn build",
"build": "rimraf dist && tsc",
"build": "rimraf dist && tsc && node ./build/client-force-enable.mjs",
"fix-registry": "replace-lockfile-registry --registry https://registry.npmjs.org/"
},
"dependencies": {
Expand All @@ -44,4 +46,4 @@
"replace-lockfile-registry": "^0.0.1",
"rimraf": "^5.0.1"
}
}
}
8 changes: 6 additions & 2 deletions vite-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ module.exports = function vitePlugin() {
name: "vite-plugin-click-to-component",
enforce: "pre",
load(id) {
if (process.env.NODE_ENV !== "development") {
if (
process.env.VUE_CLICK_TO_COMPONENT_FORCE_ENABLE !== "true" &&
process.env.NODE_ENV !== "development"
) {
// disable vite plugin
return;
}

Expand All @@ -22,7 +26,7 @@ module.exports = function vitePlugin() {

return sourceWithSourceCodeLocation;
} catch (error) {
console.error("[vite-plugin-click-to-component] error", {
console.error("[vue-click-to-component-vite-plugin] error", {
file: id,
error: error && error.message,
});
Expand Down

0 comments on commit 9111080

Please sign in to comment.