Skip to content

Commit

Permalink
Refactor babel plugin initialization in moduleEnv.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Apr 9, 2018
1 parent 5adaa1d commit cbb2802
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
15 changes: 8 additions & 7 deletions lib/moduleEnv.js
Expand Up @@ -3,14 +3,15 @@
var Module = require("module"),
fs = require("fs"),
babelCore = require("babel-core"),
// Requiring the babel plugin here because otherwise it will be lazy-loaded by Babel during rewire()
transformBlockScoping = require("babel-plugin-transform-es2015-block-scoping"),
coffee;

var moduleWrapper0 = Module.wrapper[0],
moduleWrapper1 = Module.wrapper[1],
originalExtensions = {},
matchCoffeeExt = /\.coffee$/,
babelPlugins = [
"babel-plugin-transform-es2015-block-scoping",
"babel-plugin-transform-object-rest-spread"
],
nodeRequire,
currentModule;

Expand Down Expand Up @@ -76,10 +77,7 @@ function jsExtension(module, filename) {

module._compile = function (content, filename) {
content = babelCore.transform(content, {
plugins: [
require.resolve("babel-plugin-transform-es2015-block-scoping"),
require.resolve("babel-plugin-transform-object-rest-spread")
],
plugins: babelPlugins,
retainLines: true,
filename: filename,
babelrc: false
Expand Down Expand Up @@ -115,6 +113,9 @@ function stripBOM(content) {
return content;
}

// Prepopulate require.cache with babel plugins because otherwise it will be lazy-loaded by Babel during rewire()
babelPlugins.forEach(require);

try {
coffee = require("coffee-script");
} catch (err) {
Expand Down
33 changes: 9 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cbb2802

Please sign in to comment.