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

打包后的结果

相关推荐
亮子AI2 小时前
【npm】npm 包更新工具 npm-check-updates (ncu)
前端·npm·node.js
Yvonne爱编码2 小时前
构建高效协作的桥梁:前后端衔接实践与接口文档规范详解
前端·git·ajax·webpack·node.js
Juchecar5 小时前
AI教你常识之 ESM + Express + Vue3 + CSV文件 + Pinia + CRUD
node.js
召摇7 小时前
Nue.js深度解析:极简主义前端框架的革新实践
前端·node.js
木辰風8 小时前
idea npm install 很慢(nodejs)
前端·npm·node.js
@八度余温8 小时前
预处理器Sass/Scss、Less 的区别,项目中写法
less·sass·scss
wyzqhhhh20 小时前
组件库打包工具选型(npm/pnpm/yarn)的区别和技术考量
前端·npm·node.js
csdn_aspnet1 天前
使用nvm管理node多版本(安装、卸载nvm,配置环境变量,更换npm淘宝镜像)
npm·node.js
Juchecar1 天前
AI教你常识之 ESM + Express + EJS + 表单POST + CSV文件
node.js
xiaopengbc1 天前
在Webpack中,如何在不同环境中使用不同的API地址?
前端·webpack·node.js