Skip to content

Commit

Permalink
Fix bug in re-exporting all with special paths
Browse files Browse the repository at this point in the history
  • Loading branch information
FogelAI committed Oct 23, 2024
1 parent 6e78a6e commit ebe31f3
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "babel-plugin-transform-barrels",
"version": "1.0.18",
"version": "1.0.19",
"description": "A Babel plugin that transforms indirect imports through a barrel file (index.js) into direct imports.",
"homepage": "https://github.com/FogelAI/babel-plugin-transform-barrels",
"main": "src/main.js",
Expand Down
15 changes: 15 additions & 0 deletions src/ast.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const fs = require("fs");
const parser = require("@babel/parser");
const t = require("@babel/types");
const { builtinModules } = require('module');
const pluginOptions = require("./pluginOptions");
const PathFunctions = require("./path");

class AST {
static filenameToAST = (filename) => {
Expand Down Expand Up @@ -82,6 +85,18 @@ class AST {
}
return t.objectExpression(objectProperties)
}

static isSpecialImportCases(node) {
const importsPath = node.source.value;
const importsSpecifiers = node.specifiers;
if (!AST.isAnySpecifierExist(importsSpecifiers)) return true;
if (AST.getSpecifierType(importsSpecifiers[0]) === "namespace") return true;
if (pluginOptions.options.executorName === "vite" && importsPath.startsWith("/")) return true;
if (pluginOptions.options.executorName === "webpack" && importsPath.includes("!")) return true;
if (PathFunctions.isSpecialCharInBundlerPathImport(importsPath)) return true;
if (builtinModules.includes(importsPath)) return true;
return false;
}
}

module.exports = AST;
1 change: 1 addition & 0 deletions src/barrel.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class BarrelFile {
}

handleImportDeclaration(node) {
if (AST.isSpecialImportCases(node)) return false;
node.specifiers.forEach((specifier) => {
// import {abc, def} from './abc';
const specifierObj = SpecifierFactory.createSpecifier("import");
Expand Down
9 changes: 1 addition & 8 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
const { builtinModules } = require('module');
const generate = require('@babel/generator').default;
const AST = require("./ast");
const { ExecutorFactory, JestMock } = require("./executorConfig");
const resolver = require("./resolver");
const BarrelFileManagerFacade = require("./barrel");
const pluginOptions = require("./pluginOptions");
const logger = require("./logger");
const PathFunctions = require("./path");

const jestMockFunction = new JestMock();

const importDeclarationVisitor = (path, state) => {
const importsSpecifiers = path.node.specifiers;
if (!AST.isAnySpecifierExist(importsSpecifiers)) return;
if (AST.getSpecifierType(importsSpecifiers[0]) === "namespace") return;
const parsedJSFile = state.filename;
const importsPath = path.node.source.value;
if (pluginOptions.options.executorName === "vite" && importsPath.startsWith("/")) return;
if (pluginOptions.options.executorName === "webpack" && importsPath.includes("!")) return;
if (PathFunctions.isSpecialCharInBundlerPathImport(importsPath)) return;
if (builtinModules.includes(importsPath)) return;
if (AST.isSpecialImportCases(path.node)) return;
logger.log(`Source import line: ${generate(path.node, { comments: false, concise: true }).code}`);
resolver.from = parsedJSFile;
const resolvedPathObject = resolver.resolve(importsPath ,parsedJSFile);
Expand Down
11 changes: 11 additions & 0 deletions tests/js/local/imports.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,14 @@ describe("jest mock", () => {
].join("\n").replaceAll("\\","\\\\"));
});
});

describe("special module usecases transformation", () => {
test("transformation of special chars in the path import", () => {
expect(
pluginTransform(
'import async_hooks from "node:async_hooks";',
__filename
)
).toBe(`import async_hooks from \"node:async_hooks";`.replaceAll("\\","\\\\"));
});
});

0 comments on commit ebe31f3

Please sign in to comment.