简体   繁体   中英

Babel transpile systemjs module skip specific modules?

I have a vanilla Cordova app. But the fact that it is a Cordova app should not matter, just think of it as a vanilla web site. Most scripts right now are loaded in through the <script> tags. Recently added gulp and Babel transpilation with env preset. I want to begin to migrate to ES2015 module supported by Systemjs. However, because there are lots of legacy codes (and some part I have no idea what it is doing, lots of globals, etc), I want to migrate to ES2015 module on a file-by-file basis. Is there a way to ask Babel not to do module transpilation for some files (but best if transpile for other ES2015 features)? It would be useful if it doesn't mess with files which do not have any export statement.

查看此处找到的ignoreonly babel.rc选项: https ://babeljs.io/docs/usage/api/#options

You can separate your babel build task into two parts. First pass compiles all files without converting modules, the second only applies the module transformation to a list of files that you manually update.

Here's what that might look like with gulp and gulp-filter :

const gulp = require("gulp");
const babel = require("gulp-babel");
const filter = require("gulp-filter");

const esmReady = ["src/somefile.js"];

gulp.task("default", () => {
  const f = filter(esmReady, { restore: true });
  gulp
    .src("src/*.js")
    .pipe(
      babel({
        presets: [["env", { modules: false }]]
      })
    )
    .pipe(f)
    .pipe(
      babel({
        plugins: ["transform-es2015-modules-systemjs"]
      })
    )
    .pipe(f.restore)
    .pipe(gulp.dest("dist"));
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM