最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to compile all included files into one using Babel? - Stack Overflow

programmeradmin4浏览0评论

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

(data/schema.js is in ES2015 syntax).

And when I am trying to pile my server.js with babel, like this:

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

it produces a new file without errors and replaces import instruction with require. But the thing is, when I am trying to run my babel-piled server.js with node, it plains about data/schema.js, because it wasn't transpiled into ES5, only required (the exact error is Unexpected token "import", because I am using some other imports in data/schema.js).

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

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

(data/schema.js is in ES2015 syntax).

And when I am trying to pile my server.js with babel, like this:

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

it produces a new file without errors and replaces import instruction with require. But the thing is, when I am trying to run my babel-piled server.js with node, it plains about data/schema.js, because it wasn't transpiled into ES5, only required (the exact error is Unexpected token "import", because I am using some other imports in data/schema.js).

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

Share Improve this question asked Sep 13, 2016 at 13:55 serge1peshcoffserge1peshcoff 4,65011 gold badges48 silver badges80 bronze badges 1
  • 4 I don't think Babel does dependency analysis. You have to specify all the files you want to convert. – Felix Kling Commented Sep 13, 2016 at 14:45
Add a ment  | 

2 Answers 2

Reset to default 11

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 mand npm run rollup -c to bundle and pile the files.

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

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

npx babel src --out-file script-piled.js
发布评论

评论列表(0)

  1. 暂无评论