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);

打包后的结果

相关推荐
ikoala4 小时前
Node.js 25 正式发布:性能飙升、安全升级、全面向 Web 靠拢!
前端·面试·node.js
前端架构师-老李7 小时前
npm、yarn、pnpm的对比和优略
前端·npm·node.js·pnpm·yarn
www_stdio8 小时前
用 n8n 打造科技新闻速览工作流:从搭建到落地的完整指南
node.js
gustt8 小时前
每天一杯科技资讯咖啡,用 n8n 喂给 AI
人工智能·node.js
烤红薯2099 小时前
如何使用代理IP发送请求(711Proxy实战指南)
node.js
EndingCoder9 小时前
Node.js SQL数据库:MySQL/PostgreSQL集成
javascript·数据库·sql·mysql·postgresql·node.js
fruge18 小时前
Node.js 入门与实战:从零构建用户管理系统
node.js
EndingCoder1 天前
MongoDB基础与Mongoose ODM
服务器·javascript·数据库·mongodb·中间件·node.js
孟陬1 天前
Bun 1.2.23发布:119个问题修复,性能飙升!
node.js·deno·bun