Skip to content

Commit

Permalink
fix(commonjs): use barrel file as a entry, and resolve its folder ins…
Browse files Browse the repository at this point in the history
…tead
  • Loading branch information
gabriel.rohden committed Dec 18, 2021
1 parent bc2d604 commit 0d3396b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 14 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ module.exports = {
}
```

> **Note**: if you make changes to the babel config
> be sure to restart your bundler with a clear babel cache
### I have a problem, what should I do?

Feel free to open a PR or an issue and I'll try to help you :D
Expand Down
16 changes: 12 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { resolveLogLevel, DEBUG, INFO } = require("./src/log");
const cachedResolvers = {};

function getCachedExports({
logLevel,
moduleName,
barrelFilePath,
moduleType,
Expand All @@ -25,6 +26,8 @@ function getCachedExports({
cachedResolvers[moduleName] = collectCjsExports(barrelFilePath);
}

logLevel >= INFO && console.log(`[resolve-barrel-files] '${moduleName}' exports:`, cachedResolvers[moduleName]);

return cachedResolvers[moduleName];
}

Expand All @@ -40,15 +43,18 @@ module.exports = function() {
}

const transforms = [];
const sourceImport = sourceConfig.mainBarrelPath;
const mainBarrelPath = sourceConfig.mainBarrelPath;
const mainBarrelFolder = pathLib.join(mainBarrelPath, "..");
const moduleType = sourceConfig.moduleType || "commonjs";
const logLevel = resolveLogLevel(sourceConfig.logLevel);

logLevel >= INFO && console.log(`[${moduleName}] Resolving ${moduleType} imports from ${sourceImport}`);
logLevel >= DEBUG
&& console.log(`[resolve-barrel-files] Resolving ${moduleType} imports from ${mainBarrelPath}`);

const exports = getCachedExports({
logLevel,
moduleName,
barrelFilePath: sourceImport,
barrelFilePath: mainBarrelPath,
moduleType,
});

Expand All @@ -74,7 +80,9 @@ module.exports = function() {
continue;
}

const importFrom = pathLib.join(sourceImport, exportInfo.importPath);
const importFrom = pathLib.join(mainBarrelFolder, exportInfo.importPath);

logLevel >= DEBUG && console.log(`[${moduleName}] Resolving '${importName}' to ${importFrom}`);

let newImportSpecifier = memberImport;

Expand Down
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-resolve-barrel-files",
"version": "0.0.2",
"version": "0.2.0",
"description": "A babel plugin that solves typescript barrel files",
"main": "index.js",
"repository": "https://github.com/Grohden/babel-plugin-resolve-barrel-files",
Expand Down
22 changes: 18 additions & 4 deletions test/cjs-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const path = require("path");
const { assert } = require("chai");
const { cjsBarrel, transform } = require("./utils");

describe("commonjs import transformations", function() {
const barrelFolder = path.resolve(cjsBarrel, "..");
const cjsTransform = transform({
moduleType: "commonjs",
mainBarrelPath: cjsBarrel,
Expand All @@ -13,9 +15,9 @@ describe("commonjs import transformations", function() {
assert.equal(
code,
[
`import { Bar } from "${cjsBarrel}/foo-bar";`,
`import { Abc as Bazz } from "${cjsBarrel}/bazz";`,
`import { default as Buzz } from "${cjsBarrel}/buzz";`,
`import { Bar } from "${barrelFolder}/foo-bar";`,
`import { Abc as Bazz } from "${barrelFolder}/bazz";`,
`import { default as Buzz } from "${barrelFolder}/buzz";`,
].join("\n"),
);
});
Expand All @@ -26,7 +28,19 @@ describe("commonjs import transformations", function() {
assert.equal(
code,
[
`import { Abc as Foo } from "${cjsBarrel}/bazz";`,
`import { Abc as Foo } from "${barrelFolder}/bazz";`,
].join("\n"),
);
});

it("should resolve member imports with wildcard generated code", function() {
const code = cjsTransform(`import { Wildcard, Unique } from 'react-ui-lib';`);

assert.equal(
code,
[
`import { default as Wildcard } from "${barrelFolder}/wildcard";`,
`import { Unique } from "${barrelFolder}/wildcard";`,
].join("\n"),
);
});
Expand Down
10 changes: 6 additions & 4 deletions test/esm-tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const path = require("path");
const { assert } = require("chai");
const { esmBarrel, transform } = require("./utils");

describe("esm import transformations", function() {
const barrelFolder = path.resolve(esmBarrel, "..");
const esmTransform = transform({
moduleType: "esm",
mainBarrelPath: esmBarrel,
Expand All @@ -13,9 +15,9 @@ describe("esm import transformations", function() {
assert.equal(
code,
[
`import { Bar } from "${esmBarrel}/foo-bar";`,
`import { Abc as Bazz } from "${esmBarrel}/bazz";`,
`import { default as Buzz } from "${esmBarrel}/buzz";`,
`import { Bar } from "${barrelFolder}/foo-bar";`,
`import { Abc as Bazz } from "${barrelFolder}/bazz";`,
`import { default as Buzz } from "${barrelFolder}/buzz";`,
].join("\n"),
);
});
Expand All @@ -26,7 +28,7 @@ describe("esm import transformations", function() {
assert.equal(
code,
[
`import { Abc as Foo } from "${esmBarrel}/bazz";`,
`import { Abc as Foo } from "${barrelFolder}/bazz";`,
].join("\n"),
);
});
Expand Down
56 changes: 55 additions & 1 deletion test/fixtures/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,68 @@ Object.defineProperty(exports, "Foo", {
return _fooBar.Foo;
},
});
exports.default = void 0;
Object.defineProperty(exports, "Unique", {
enumerable: true,
get: function() {
return _wildcard.Unique;
},
});
Object.defineProperty(exports, "Wildcard", {
enumerable: true,
get: function() {
return _wildcard.default;
},
});

var _fooBar = require("./foo-bar");

var _bazz = require("./bazz");

var _buzz = _interopRequireDefault(require("./buzz"));

var _wildcard = _interopRequireWildcard(require("./wildcard"));

function _getRequireWildcardCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireWildcardCache = function(nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}

function _interopRequireWildcard(obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) {
return { default: obj };
}
var cache = _getRequireWildcardCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {};
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor
? Object.getOwnPropertyDescriptor(obj, key)
: null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}

function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
}
1 change: 1 addition & 0 deletions test/fixtures/esm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Abc as Bazz } from "./bazz";
export { default as Buzz } from "./buzz";
export { Bar, Foo } from "./foo-bar";
export { default as Wildcard, Unique } from "./wildcard";

0 comments on commit 0d3396b

Please sign in to comment.