How to compile all included files into one using Babel?

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

import schema from "./data/schema";

( data/schema.jspresent in ES2015 syntax).

And when I try to compile mine server.jswith babel, like this:

babel -o server_production.js --minified  server.js

it creates a new file without errors and replaces the command importwith require. But the fact is that when I try to run my compiled babel server.jswith node, it complains data/schema.jsbecause, because it was not transferred to ES5, it only needs (exact error Unexpected token "import", because I use other imports in data/schema.js).

So the question is: how can I compile my file and all its files importinto one file? I tried babel -o server_production.js --minified data/schema.js server.js, but that didn't work either.

+4
source share
2 answers

Babel does not bind dependencies by default. You will need to use a module like rollup .

To do this, you need to define a configuration rollupsimilar to this:

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 file package.jsondefined 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 a command npm run rollup -cto combine and compile files.

Here you will find an example: https://ide.c9.io/ifeanyidev/babel-rollup-example

+5

maven maven-wrapper version

, , mvnw, maven, .

, xp, .

-1

Source: https://habr.com/ru/post/1654470/


All Articles