Skip to content

babel-register doesn't save modules to cache properly #9320

@jbasrai

Description

@jbasrai

Bug Report

Current Behavior
For scripts that don't exit the event loop (e.g. an express server), modules that are loaded outside of the original script don't get cached.

Input Code
Minimal repro here: https://github.com/jbasrai/babel-register-cache

  1. yarn
  2. node index.js
  3. Ctrl-C to end process

Expected behavior/code
Expected world.js to be cached, but inspecting the cache reveals that it isn't.

Babel Configuration (.babelrc, package.json, cli command)

module.exports = function (api) {
	api.cache.forever()

	return {
		presets: [ '@babel/preset-env' ]
	}
}

Environment

  • Babel version(s): core v7.2.2, register v7.0.0
  • Node/npm version: Node v8.12, npm v5.8
  • OS: OSX 10.12.6
  • Monorepo: no
  • How you are using Babel: register

Possible Solution
The process.on('exit') listener is not being used correctly. The docs say that it is only called when process.exit() is called explicitly OR when Node's event loop no longer has work to perform. In the above case, none of these conditions are met, and so a required module is never cached.

Instead, I think a more comprehensive exit handler should be used to save the cache.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions