Created
October 26, 2019 10:17
-
-
Save endiliey/45cab0da941429e9860d2ad59877d79a to your computer and use it in GitHub Desktop.
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
export default function(babel) { | |
const { types: t } = babel; | |
return { | |
name: "ast-transform", // not required | |
visitor: { | |
ObjectExpression(path) { | |
var source = path.node.source.value; | |
if (source !== '@generated/registry') return; | |
var properties = path.get('properties'); | |
var propertiesMap = {}; | |
properties.forEach(function (property) { | |
propertiesMap[property.node.key.value] = property; | |
}); | |
if (!propertiesMap.loader || propertiesMap.webpack) { | |
return; | |
} | |
var loaderMethod = propertiesMap.loader.get('value'); | |
var dynamicImports = []; | |
loaderMethod.traverse({ | |
Import: function Import(path) { | |
dynamicImports.push(path.parentPath); | |
} | |
}); | |
if (!dynamicImports.length) return; | |
console.log(dynamicImports) | |
console.log(dynamicImports[0].get('arguments')[0].node) | |
propertiesMap.loader.insertAfter(t.objectProperty(t.identifier('webpack'), t.arrowFunctionExpression([], t.arrayExpression(dynamicImports.map(function (dynamicImport) { | |
return t.callExpression(t.memberExpression(t.identifier('require'), t.identifier('resolveWeak')), [dynamicImport.get('arguments')[0].node]); | |
}))))); | |
propertiesMap.loader.insertAfter(t.objectProperty(t.identifier('modules'), t.arrayExpression(dynamicImports.map(function (dynamicImport) { | |
return dynamicImport.get('arguments')[0].node; | |
})))); | |
}, | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment