LimeStreem LimeStreem - 1 month ago 11
TypeScript Question

How can I make babel generating polyfills like `__createClass` just a one time for multiple files?

My project containing many ES6 files and all of these are containing at least one class.

__createClass
,
__interopRequireDefault
and
__classCallback
polyfilling function are generated for each files.
I would concat them with browserify after that, and I guess it is too redundant to keep them for all of files.
I think I can inject polyfilling functions during bundling task.

When I use typescript, I can specify
--noEmitHelpers
options not to generate such codes during compiling task. Is there something way to do that with babel?

Answer

You can use babel-plugin-transform-runtime. It does exactly what you're looking for. Check out the docs.

This is where the transform-runtime plugin comes in: all of the helpers will reference the module babel-runtime to avoid duplication across your compiled output. The runtime will be compiled into your build.

$ npm install --save-dev babel-plugin-transform-runtime 
$ npm install --save babel-runtime

.babelrc

{
  "plugins": ["transform-runtime"]
}