Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix named-asset-import plugin to work with export-as syntax #5573

Merged
merged 8 commits into from
Nov 20, 2018
58 changes: 58 additions & 0 deletions packages/babel-plugin-named-asset-import/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,64 @@ function namedAssetImportPlugin({ types: t }) {

return {
visitor: {
ExportNamedDeclaration(
NShahri marked this conversation as resolved.
Show resolved Hide resolved
path,
{
opts: { loaderMap },
}
) {
if (!path.node.source) {
NShahri marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const sourcePath = path.node.source.value;
const ext = extname(sourcePath).substr(1);

if (visited.has(path.node) || sourcePath.indexOf('!') !== -1) {
return;
}

if (loaderMap[ext]) {
path.replaceWithMultiple(
path.node.specifiers.map(specifier => {
if (t.isExportDefaultSpecifier(specifier)) {
const newDefaultImport = t.exportDeclaration(
[
t.exportDefaultSpecifier(
t.identifier(specifier.local.name)
),
],
t.stringLiteral(sourcePath)
);

visited.add(newDefaultImport);
return newDefaultImport;
}

const newImport = t.exportNamedDeclaration(
null,
[
t.exportSpecifier(
t.identifier(specifier.local.name),
t.identifier(specifier.exported.name)
),
],
t.stringLiteral(
loaderMap[ext][specifier.local.name]
? loaderMap[ext][specifier.local.name].replace(
/\[path\]/,
sourcePath
)
: sourcePath
)
);

visited.add(newImport);
return newImport;
})
);
}
},
ImportDeclaration(
path,
{
Expand Down