gulp打包vue3+jsx+less插件

最终转换结果如下

在根目录下添加gulpfile.js文件,package.json添加命令npm run gulp

js 复制代码
var gulp = require('gulp')
var babel = require('gulp-babel')
var less = require('gulp-less')
var del = require('del');
var spawn = require('child_process').spawn;

const outDir = "dist/"


function es6toes5() {
  return gulp.src('packages/**/*.js')
    .pipe(babel({
      presets: ["@babel/preset-env"],
      'plugins': [() => {
        return {
          visitor: {
            ImportDeclaration(path, source) {
              console.log(path);
              if (path.node.source.value.endsWith('.less')) {
                path.node.source.value = path.node.source.value.replace(/\.less$/, '.css')
              }
            },
          },
        }
      }]
    }))
    .pipe(gulp.dest(outDir ));
}

// function jsxtojs1() {
//   return gulp.src("packages/**/*.jsx").
//     pipe(babel({
//       plugins: [
//         ['@babel/plugin-transform-react-jsx', {
//           "throwIfNamespace": false,
//         }
//         ]
//       ]
//     })).
//     pipe(gulp.dest(outDir ));
// }
function jsxtojs() {
  return gulp.src("packages/**/*.jsx").
    pipe(babel({
      plugins: [
        [
          '@vue/babel-plugin-jsx',
          {
            enableObjectSlots: false,
          },
        ],
      ],
    })).
    pipe(gulp.dest(outDir ));
}

function lesstocss() {
  return gulp
    .src('packages/**/*.less')
    .pipe(
      less({
        relativeUrls: true,
      })
    )
    .pipe(gulp.dest(outDir ))
}

function npmTask(cb) {
  spawn('npm.cmd', ['run', 'lib'], {}) // 'npm.cmd' : 'npm' 运行 npm run lib 
    .on('error', cb)
    .on('close', code => code ? cb(new Error(code)) : cb());
}



exports.default = gulp.series(
  () => del([`./${outDir}/**`]),
  npmTask,
  jsxtojs,
  es6toes5,
  lesstocss);

打包后的结果

相关推荐
水獭比特1 小时前
工作流都公开了,为什么还不能继承 Owner 权限?
javascript·人工智能·node.js
冲冲4 小时前
nvm-windows的安装
node.js
秋秋小事7 小时前
LangChain的核心概念与生态
node.js
翼龙云_cloud10 小时前
阿里云国际服务器代理商:ECS 快速部署 Node.js 项目实战指南
服务器·阿里云·node.js
FEF前端团队1 天前
小程序微信支付 V3 接入实战手册:从商户配置到前后端落地
javascript·后端·node.js
万敏1 天前
Vue3 全栈实战第七周:错误处理与请求层封装实战记录
vue.js·node.js·全栈
AD_youyu1 天前
Node.js 安装教程
node.js
m0_380743872 天前
为 OpenAI 兼容接口配置教程
开发语言·python·node.js
__zRainy__2 天前
Node系列 · ORM:Sequelize 简介
数据库·后端·node.js
水獭比特2 天前
工具都批准了,为什么还不能执行?给 Agent 补上第二道校验
javascript·人工智能·node.js