Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: babel-register transform internal dependencies (#12665)
- Loading branch information
1 parent
f1314a1
commit 22eb99b
Showing
6 changed files
with
97 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* This file wraps the implementation of register so all modules `require()`-ed | ||
* internally within register are stored in a separate module cache. | ||
* This prevents un-transformed modules being stored in global module cache, | ||
* and allows register to transform these modules if they are loaded externally. | ||
*/ | ||
|
||
const Module = require("module"); | ||
|
||
const globalModuleCache = Module._cache; | ||
const internalModuleCache = Object.create(null); | ||
|
||
Module._cache = internalModuleCache; | ||
const node = require("./node"); | ||
Module._cache = globalModuleCache; | ||
|
||
// Add source-map-support to global cache as it's stateful | ||
const smsPath = require.resolve("source-map-support"); | ||
globalModuleCache[smsPath] = internalModuleCache[smsPath]; | ||
|
||
module.exports = node; |
28 changes: 28 additions & 0 deletions
28
packages/babel-register/test/fixtures/internal-modules/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const register = require('../../..'); | ||
|
||
// Plugin to add '/* transformed */' comment to start of function bodies | ||
const plugin = () => ( { | ||
visitor: { | ||
Function(path) { | ||
const bodyNode = path.node.body; | ||
(bodyNode.leadingComments || (bodyNode.leadingComments = [])).push( { | ||
type: 'CommentBlock', | ||
value: ' transformed ' | ||
} ); | ||
}, | ||
}, | ||
} ); | ||
|
||
register( { | ||
ignore: [], | ||
babelrc: false, | ||
configFile: false, | ||
plugins: [plugin] | ||
} ); | ||
|
||
console.log( | ||
JSON.stringify({ | ||
convertSourceMap: require('convert-source-map').fromObject.toString(), | ||
isPlainObject: require('lodash/isPlainObject').toString() | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters