Skip to content

Commit

Permalink
Import correct module format in helpers depending on other helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
exb committed Dec 28, 2021
1 parent c59870c commit 72f4ad0
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/babel-plugin-transform-runtime/scripts/build-dist.js
Expand Up @@ -259,15 +259,23 @@ function buildHelper(

function buildRuntimeRewritePlugin(runtimeName, helperName) {
/**
* rewrite helpers imports to runtime imports
* Rewrite helper imports to load the adequate module format version
* @example
* adjustImportPath(ast`"setPrototypeOf"`)
* // returns ast`"@babel/runtime/helpers/esm/setPrototypeOf"`
* @param {*} node The string literal contains import path
* // returns ast`"./setPrototypeOf"`
* @example
* adjustImportPath(ast`"@babel/runtime/helpers/typeof"`)
* // returns ast`"./typeof"`
* @param {*} node The string literal that contains the import path
*/
function adjustImportPath(node) {
if (helpers.list.includes(node.value)) {
node.value = `./${node.value}.js`;
const helpersPath = path.join(runtimeName, "helpers");
const helper = node.value.startsWith(helpersPath)
? path.basename(node.value)
: node.value;

if (helpers.list.includes(helper)) {
node.value = `./${helper}.js`;
}
}

Expand Down

0 comments on commit 72f4ad0

Please sign in to comment.