lisonge / vite-plugin-monkey

A vite plugin server and build your.user.js for userscript engine like Tampermonkey, Violentmonkey, Greasemonkey, ScriptCat
MIT License
1.33k stars 70 forks source link

打包后字符串中的中文被转义 #10

Closed h46incon closed 2 years ago

h46incon commented 2 years ago

如以下 js 代码:

ClickSelector('button', '确认')

在打包后会变成:

ClickSelector("button", "\u786E\u8BA4");

用倒是能用,但是在油猴插件中直接查看代码的时候就看不懂写的是什么了。

lisonge commented 2 years ago

这是 vite/rollup 编译的,不是 vite-plugin-monkey 导致的

lisonge commented 2 years ago

打包后的文件查看的意义不大,另外 如果配置了 viteOptions.build.minify==true ,打包后的脚本还可以压缩,查看就更没意义了

另外我刚刚 Google 了一下,没找到配置这个阻止字符串转义的方法,其实字符串全部转义成 acsii 是有优点的,这意味你的代码是不会产生乱码问题的

如果你确实想配置这个,你可以去 https://github.com/rollup/rollup/issues 或者 https://github.com/vitejs/vite/issues 问他们

h46incon commented 2 years ago

我问这个就是我有查看的需要。我试了下 webpack 打包是没这个问题的,我尝试使用 webpack 吧

lisonge commented 2 years ago

@h46incon

找到了,是 esbuild 干的,解决方法如下

import { defineConfig } from 'vite';

export default defineConfig({
    esbuild:{
        charset:'utf8'
    }
});

这样之后代码里的字符串就不会被转义了

h46incon commented 2 years ago

TQL了,我当时也找了好久没找到,非常感谢