How to compile all included files into one using Babel?

前端 未结 2 880
感动是毒
感动是毒 2021-02-07 11:43

I am using Babel in my project. The thing is, I have this line of code in my server.js:

import schema from \"./data/schema\";

(

相关标签:
2条回答
  • 2021-02-07 12:01

    Babel doesn't bundle dependencies by default. You'll need to use a module bundler like rollup.

    To do this, you'll need to define a rollup config similar to this one:

    rollup.config.js

    import babel from 'rollup-plugin-babel';
    import babelrc from 'babelrc-rollup';
    
    export default {
      entry: 'server.js',
      dest: 'server_production.js',
      plugins: [
        babel(babelrc())
      ]
    };
    

    This works with the package.json file defined below:

    {
      "scripts": {
        "rollup": "./node_modules/.bin/rollup -c"
      },
      "devDependencies": {
        "babel-cli": "6.14.0",
        "babel-plugin-external-helpers": "6.8.0",
        "babel-preset-es2015": "6.14.0",
        "babelrc-rollup": "3.0.0",
        "rollup": "0.35.10",
        "rollup-plugin-babel": "2.6.1"
      }
    }
    

    You execute the command npm run rollup -c to bundle and compile the files.

    You'll find an example here: https://ide.c9.io/ifeanyidev/babel-rollup-example

    0 讨论(0)
  • 2021-02-07 12:01

    You can use the following solution to compile all included files into one using Babel:

    npx babel src --out-file script-compiled.js
    
    0 讨论(0)
提交回复
热议问题