node.jsecmascript-2017babel-polyfillbabel-clibabel-core

Using babel-polyfill in production entry file


I'm using babel to transpile ES7 js code, and everything works like a charm in dev/staging. Inside the application, I heavily rely on async/await features of ES7. My entry file looks like this:

'use strict';
require("babel-polyfill");
require("babel-core/register");
module.exports = require('./app/server').default();

I'm not sure whether it is necessary to keep babel-polyfill and babel-core/register modules for the production environment since I transpile everything using babel-cli before deployment. I assume it must correctly work even if I remove those and make the entry file look something like this:

import server from './app/server';
server();

However, if do this I got next exception while starting the application:

ReferenceError: regeneratorRuntime is not defined
    at C:\Users\Username\Documents\some-service\lib\app\repositories\someRepository.js:18:32
    at Object.<anonymous> (C:\Users\Username\Documents\some-service\lib\app\repositories\someRepository.js:40:2)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (C:\Users\Username\Documents\some-service\lib\app\controllers\someController.js:15:27)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (C:\Users\Username\Documents\some-service\lib\app\server.js:15:26)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)

Is that normal to reference babel-polyfill and babel-core/register packages in the production environment?


Solution

  • If you are relying on those modules (babel-polyfill and babel-core/register) during development, you will also need them in your production file. The transpilation process does not add polyfills like Object.assign or Promise.

    The docs page for the babel polyfills does not explicitly say that you should include different libraries for dev and production. But it does say you will need to include it or specific polyfills for the features you want, which seems to say you need them in any environment.

    Short answer: It is standard to include polyfills in every environment, since transpilation by babel does not add polyfills on its own.