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

打包后的结果

相关推荐
莫有杯子的龙潭峡谷13 小时前
在 Windows 系统上安装 OpenClaw
人工智能·node.js·安装教程·openclaw
朝朝暮暮an19 小时前
Node.js-第一天学习内容
node.js
lichenyang45321 小时前
Node.js AI 开发入门 - 完整学习笔记
人工智能·学习·node.js
岁岁种桃花儿21 小时前
NodeJs从入门到上天:什么是Node.js
前端·node.js
一心赚狗粮的宇叔2 天前
VScode常用扩展包&Node.js安装及npm包安装
vscode·npm·node.js·web
花间相见2 天前
【AI开发】—— Ubuntu系统使用nvm管理Node.js多版本,版本切换一键搞定(实操完整版)
linux·ubuntu·node.js
嘿是我呀2 天前
【用npm安装node时报错“npm 无法加载文件”】
前端·npm·node.js
西门吹-禅3 天前
prisma
node.js
怪兽毕设3 天前
基于SpringBoot的选课调查系统
java·vue.js·spring boot·后端·node.js·选课调查系统
心.c3 天前
Vue3+Node.js实现文件上传分片上传和断点续传【详细教程】
前端·javascript·vue.js·算法·node.js·哈希算法