当前位置: 首页 > news >正文

网站与微信结合制作自己的网站

网站与微信结合,制作自己的网站,免费素材免费下载,清河做网站哪里便宜写一个vite插件去除代码中的console 使用babel做处理。简单处理,复杂情况未考虑。 学习babel写的demo。 项目根目录新建文件 babel-plugin-remove-console.js rollup-plugin-remove-console.js babel-plugin-remove-console.js import { declare } from babel/he…

写一个vite插件去除代码中的console

使用babel做处理。简单处理,复杂情况未考虑。
学习babel写的demo。

项目根目录新建文件
babel-plugin-remove-console.js
rollup-plugin-remove-console.js

在这里插入图片描述

babel-plugin-remove-console.js

import { declare } from '@babel/helper-plugin-utils';const removeConsolePlugin = declare((api, options, dirname) => {api.assertVersion(7);return {visitor: {CallExpression(path) {// 检查是否是console.method()调用const { callee } = path.node;if (callee.type === 'MemberExpression' &&callee.object.type === 'Identifier' &&callee.object.name === 'console' &&callee.property.type === 'Identifier') {// 如果是独立语句 (ExpressionStatement),直接移除整个语句if (path.parent.type === 'ExpressionStatement') {path.parentPath.remove();}// 否则,替换为undefined (避免语法错误)else {path.replaceWith(api.types.identifier('undefined'));}return;}},},};
});export default removeConsolePlugin;

为什么要处理这些呢

 callee.type === 'MemberExpression' &&callee.object.type === 'Identifier' &&callee.object.name === 'console' &&callee.property.type === 'Identifier'

在https://astexplorer.net/ 可以尝试下
在这里插入图片描述

当然也可以写成这样

 visitor: {MemberExpression(path){if(path.node.object.name=='console'){console.log(path.parentPath.node)path.parentPath.remove();}}},

rollup-plugin-remove-console.js

import { createFilter } from '@rollup/pluginutils';
import { transformFromAstSync } from '@babel/core';
import parser from '@babel/parser';
import removeConsolePlugin from './babel-plugin-remove-console';
export default function myPlugin(pluginOptions = {}) {const defaultExclude = /node_modules/;// 如果用户提供了exclude选项,合并默认排除const excludePattern = pluginOptions.exclude? [defaultExclude, pluginOptions.exclude]: defaultExclude;const filter = createFilter(pluginOptions.include || /\.(js|ts|jsx|tsx|vue)$/,excludePattern);return {name: 'rollup-plugin-remove-console',transform(src, id) {if (!filter(id)) {return null;}const ast = parser.parse(src, {sourceType: 'unambiguous',});const { code, map } = transformFromAstSync(ast, src, {plugins: [[removeConsolePlugin]],});return {code,map, // 或者提供一个 sourcemap 对象};},};
}

vite.config.js引入使用

在这里插入图片描述
如果你使用了多个插件,需要把自己定义的这个去除插件放到最后面,等其他代码都转换完毕后,只需要处理js语法即可。
比如我们这里引入了vuejsx,支持vuejsx语法。
在这里插入图片描述

他是怎么处理的呢。
一般vue编译的时候,会把vue文件中的,样式,模版,脚本拆分。
在这里插入图片描述

我这里的jsx写法,所以是lang.jsx
在这里插入图片描述
经过vue,vuejsx插件的加工后,成了这样
在这里插入图片描述
所以我们只需要考虑console本身即可。

扩展

忽略某些

增加配置来处理,如,我们可以配置console的哪些方法不移除或者哪些方法移除。
在这里插入图片描述
在vite插件中,将pluginOptions传递给babel插件,这里文件名起的是rollup-plugin-remove-console.js因为没用到vite的特性hooks所以也支持rollup(按理来说,不过没有测试)。

在这里插入图片描述
在这里插入图片描述
可以看到传递过来的参数。
在这里插入图片描述

怎么获取是log还是warn还是error呢。
在这里插入图片描述

在这里插入图片描述
所以要获取下 const name = path.node.property.name;

import { declare } from '@babel/helper-plugin-utils';
const removeConsolePlugin = declare((api, options, dirname) => {api.assertVersion(7);const ignores = options.ignore || [];return {visitor: {MemberExpression(path) {if (path.node.object.name == 'console') {const name = path.node.property.name;const isIgnore = ignores.includes(name);if (!isIgnore) {path.parentPath.remove();}}},},};
});export default removeConsolePlugin;

或者

import { declare } from '@babel/helper-plugin-utils';
const removeConsolePlugin = declare((api, options, dirname) => {api.assertVersion(7);const ignores = options.ignore || [];return {visitor: {CallExpression(path) {// 检查是否是console.method()调用const { callee } = path.node;if (callee.type === 'MemberExpression' &&callee.object.type === 'Identifier' &&callee.object.name === 'console' &&callee.property.type === 'Identifier') {const name = callee.property.name;const isIgnore = ignores.includes(name);if (!isIgnore) {// 如果是独立语句 (ExpressionStatement),直接移除整个语句if (path.parent.type === 'ExpressionStatement') {path.parentPath.remove();}// 否则,替换为undefined (避免语法错误)else {path.replaceWith(api.types.identifier('undefined'));}}}},},};
});export default removeConsolePlugin;

在这里插入图片描述

替换

比如我们有这样两个个函数。上报数据,上报异常。
在这里插入图片描述
假设,在开发环境我们不需要上报,也就是开发环境不替换,一般也是开发环境不替换。
我们需要在插件运行前获取环境
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
传递给babel插件
在这里插入图片描述
当然其实这一步不用,你可以在babe插件里面直接获取。
在babel插件里面接收下。在这里插入图片描述

如果不是开发环境就执行插件。
在这里插入图片描述
或者我们简单点。

在vite插件中直接不往下走了,不执行babel插件了。
在这里插入图片描述
然后继续完善替换的逻辑。
假设我们的插件是这样传递参数的。
在这里插入图片描述
babel获取下
在这里插入图片描述

替换的逻辑为
当匹配上的时候,把原先的参数带进去,再额外携带一个文件的信息。

在这里插入图片描述

source为来源。
在这里插入图片描述
source大概这样
在这里插入图片描述
然后我们看下效果。
开发环境
在这里插入图片描述

build后
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

完整代码

rollup-plugin-remove-console.js

rollup-plugin-remove-console.js

import { createFilter } from '@rollup/pluginutils';
import { transformFromAstSync } from '@babel/core';
import parser from '@babel/parser';
import removeConsolePlugin from './babel-plugin-remove-console';
export default function myPlugin(pluginOptions = {}) {const defaultExclude = /node_modules/;let isDev = false;// 如果用户提供了exclude选项,合并默认排除const excludePattern = pluginOptions.exclude? [defaultExclude, pluginOptions.exclude]: defaultExclude;const filter = createFilter(pluginOptions.include || /\.(js|ts|jsx|tsx|vue)$/,excludePattern);// console.log(pluginOptions);return {name: 'rollup-plugin-remove-console',options(inputOptions) {isDev = process.env.NODE_ENV === 'development';console.log('isDev', isDev);return inputOptions;},transform(src, id) {if (!filter(id) || isDev) {return null;}const ast = parser.parse(src, {sourceType: 'unambiguous',});const paths = id.split('/');const source = paths[paths.length - 1];console.log(source);const { code, map } = transformFromAstSync(ast, src, {plugins: [[removeConsolePlugin, { ...pluginOptions, source, isDev }]],});return {code,map, // 或者提供一个 sourcemap 对象};},};
}

babel-plugin-remove-console.js

babel-plugin-remove-console.js

import { declare } from '@babel/helper-plugin-utils';
import { types as t } from '@babel/core';const removeConsolePlugin = declare((api, options, dirname) => {api.assertVersion(7);const ignores = options.ignore || [];const replaceList = options.replaceList || [];const source = options.source;let isDev = process.env.NODE_ENV == 'development';if (typeof options.isDev != 'undefined') {isDev = options.isDev;}return {visitor: {MemberExpression(path) {if (path.node.object.name == 'console' && !isDev) {const name = path.node.property.name;const replaceItem = replaceList.find((item) => item[0] === name);if (replaceItem) {const replaceName = replaceItem[1];if (!replaceName) {console.warn('请配置替换的函数');}if (replaceList.length > 0) {const args = path.parentPath.node.arguments;const loggerCall = t.callExpression(t.identifier(replaceName), [...args,t.stringLiteral(source),]);loggerCall.isDone = true;path.parentPath.replaceWith(loggerCall);}}const isIgnore = ignores.includes(name);if (!isIgnore) {path.parentPath.remove();}}},},};
});export default removeConsolePlugin;

vite.config.js

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import rollupPluginRemoveConsole from './rollup-plugin-remove-console.js';
import vueJsx from '@vitejs/plugin-vue-jsx';// https://vite.dev/config/
export default defineConfig({//plugins: [vue(),vueJsx(),rollupPluginRemoveConsole({ignore: ['log', 'error'],replaceList: [['log', 'uploadLog'],['error', 'uploadError'],],}),],base: './',server: {proxy: {'/api': {target: 'http://localhost:3000/',changeOrigin: true,rewrite: (path) => path.replace(/^\/api/, ''),},},},
});

main.js

import { createApp } from 'vue';
import './style.css';
import App from './App.vue';const upData = (type, args) => {fetch(`/api/${type}`, {method: 'POST',headers: {'Content-Type': 'application/json',},body: JSON.stringify(args),});
};window.uploadLog = (...args) => {upData('log', args);
};
window.uploadError = (...args) => {upData('error', args);// fetch
};createApp(App).mount('#app');

文章转载自:
http://misspelt.nLcw.cn
http://comfortless.nLcw.cn
http://diaphragm.nLcw.cn
http://iatrogenic.nLcw.cn
http://bubblegum.nLcw.cn
http://cryptograph.nLcw.cn
http://fantastico.nLcw.cn
http://denish.nLcw.cn
http://abolishable.nLcw.cn
http://epithelization.nLcw.cn
http://maintain.nLcw.cn
http://presumably.nLcw.cn
http://precative.nLcw.cn
http://judicative.nLcw.cn
http://alden.nLcw.cn
http://preliberation.nLcw.cn
http://languishingly.nLcw.cn
http://detinue.nLcw.cn
http://fling.nLcw.cn
http://longways.nLcw.cn
http://biretta.nLcw.cn
http://circumscribe.nLcw.cn
http://unspecified.nLcw.cn
http://morayshire.nLcw.cn
http://morphiomaniac.nLcw.cn
http://idyll.nLcw.cn
http://aldis.nLcw.cn
http://brevetcy.nLcw.cn
http://phosphopyruvate.nLcw.cn
http://seventh.nLcw.cn
http://wetware.nLcw.cn
http://uninformative.nLcw.cn
http://regardful.nLcw.cn
http://paying.nLcw.cn
http://sovietize.nLcw.cn
http://melomania.nLcw.cn
http://contort.nLcw.cn
http://javaite.nLcw.cn
http://titillation.nLcw.cn
http://narcissi.nLcw.cn
http://derivate.nLcw.cn
http://ammonoid.nLcw.cn
http://chromatism.nLcw.cn
http://vfat.nLcw.cn
http://zululand.nLcw.cn
http://antiworld.nLcw.cn
http://protoactinium.nLcw.cn
http://necromancer.nLcw.cn
http://blatant.nLcw.cn
http://savings.nLcw.cn
http://sotol.nLcw.cn
http://outrunner.nLcw.cn
http://albatross.nLcw.cn
http://assonate.nLcw.cn
http://fhwa.nLcw.cn
http://nucleosidase.nLcw.cn
http://assertion.nLcw.cn
http://cowshed.nLcw.cn
http://semiramis.nLcw.cn
http://globular.nLcw.cn
http://draftsmanship.nLcw.cn
http://croydon.nLcw.cn
http://jungfrau.nLcw.cn
http://sermonesque.nLcw.cn
http://catchpole.nLcw.cn
http://haulageway.nLcw.cn
http://hertfordshire.nLcw.cn
http://hallstadt.nLcw.cn
http://hatter.nLcw.cn
http://icp.nLcw.cn
http://gynaecologist.nLcw.cn
http://graphomania.nLcw.cn
http://calcutta.nLcw.cn
http://overdrink.nLcw.cn
http://thanatophobia.nLcw.cn
http://kashmiri.nLcw.cn
http://drooly.nLcw.cn
http://interrelation.nLcw.cn
http://cimelia.nLcw.cn
http://moral.nLcw.cn
http://carpal.nLcw.cn
http://christcross.nLcw.cn
http://decollate.nLcw.cn
http://banister.nLcw.cn
http://cordwainer.nLcw.cn
http://mischievous.nLcw.cn
http://arizona.nLcw.cn
http://transpose.nLcw.cn
http://monetarily.nLcw.cn
http://walkthrough.nLcw.cn
http://demigoddess.nLcw.cn
http://cyclothymia.nLcw.cn
http://quavering.nLcw.cn
http://zinjanthropus.nLcw.cn
http://heterosexuality.nLcw.cn
http://psychology.nLcw.cn
http://historicize.nLcw.cn
http://betacam.nLcw.cn
http://leptocephalic.nLcw.cn
http://chromatophilia.nLcw.cn
http://www.15wanjia.com/news/59633.html

相关文章:

  • mvc网站入口asp网络推广员一个月多少钱
  • 互联网商城是做什么的优化大师app下载安装
  • 网页制作工具下载seo查询爱站网
  • 企业邮箱怎么查看抖音seo优化软件
  • 向wordpress发帖插件站长工具seo下载
  • 免费中英文网站源码百度如何发布作品
  • 上海做网站哪家公司好微指数查询
  • 塑胶原料东莞网站建设今天国内最新消息
  • html家具网站源代码如何在百度上发表文章
  • 品牌加盟最好的网站建设培训学校资质办理条件
  • html5 jq做电脑网站如何免费发布广告
  • 哪个网站音乐做的最好北大青鸟培训机构靠谱吗
  • 苏州网站开发网站开发费用大连seo按天付费
  • 自己创业做原公司一样的网站百度网站提交入口网址
  • 企业专业网站建设抖音优化
  • php自适应网站网站排名顾问
  • 360安全浏览器网站seo啥意思
  • 做的网站图片显示一半今日新闻头条新闻今天
  • 用h5做网站首页代码网络舆情监控系统
  • b2c网站建设平台咖啡的营销推广软文
  • wordpress oss徐州seo排名收费
  • 微网站独立域名企业培训机构排名
  • wordpress添加路由windows优化
  • 藁城手机网站建设怎么在百度发布免费广告
  • 网站设计大概收费范围网站seo方案策划书
  • 社交网站开发难度b2b十大平台排名
  • 医疗保险网站baidu com百度一下
  • 微软网站怎么做的百度竞价排名广告定价
  • 那几家是做失物招领的网站百度一下照片识别
  • 企业网站设计师企业网站建设方案书