-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow keeping dynamic imports in selected files and apply this to fee…
…dback module
- Loading branch information
Showing
3 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
packages/babel-preset-gatsby-package/lib/babel-trasnform-mark-to-keep-dynamic-import.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,54 @@ | ||
const path = require(`path`) | ||
|
||
/** | ||
* @typedef {import('@babel/core').NodePath} NodePath | ||
* @typedef {import('@babel/core').PluginObj} PluginObj | ||
* @typedef {import('@babel/core').PluginPass} PluginPass | ||
* @typedef {import('@babel/core').types} BabelTypes | ||
* @typedef {import('@babel/core').types.Identifier} Identifier | ||
* @typedef {import('@babel/core').types.MemberExpression} MemberExpression | ||
*/ | ||
|
||
/** | ||
* @typedef {Object} IPluginOptions | ||
* @property {Array<string>} keepDynamicImports | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {{ types: BabelTypes }} _unused | ||
* @param {Partial<IPluginOptions>} opts | ||
* @returns {PluginObj} | ||
*/ | ||
module.exports = function keepDynamicImports( | ||
_unused, | ||
opts | ||
) { | ||
if (!opts.keepDynamicImports) { | ||
throw new Error(`keepDynamicImports option needs to be set`) | ||
} else if (!Array.isArray(opts.keepDynamicImports)) { | ||
throw new Error(`keepDynamicImports option needs to be an array`) | ||
} | ||
|
||
const absolutePaths = opts.keepDynamicImports.map(p => path.resolve(p)) | ||
|
||
return { | ||
name: `babel-transform-mark-to-keep-dynamic-import`, | ||
visitor: { | ||
Program() { | ||
const filename = this.file?.opts?.filename | ||
if (!filename) { | ||
return | ||
} | ||
|
||
if (absolutePaths.includes(filename)) { | ||
// this is big hack - it relies on some babel plugins internal to basically | ||
// do early return ( https://github.com/babel/babel/blob/3526b79c87863052f1c61ec0c49c0fc287ba32e6/packages/babel-plugin-transform-modules-commonjs/src/index.ts#L174 ) | ||
// on top of that `BabelFile` doesn't expose delete for the metadata, | ||
// so we reach into internal `_map` to delete it | ||
this.file._map.delete("@babel/plugin-proposal-dynamic-import") | ||
} | ||
} | ||
}, | ||
} | ||
} |
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