Skip to content

Commit 0d3396b

Browse files
author
gabriel.rohden
committed
fix(commonjs): use barrel file as a entry, and resolve its folder instead
1 parent bc2d604 commit 0d3396b

File tree

7 files changed

+96
-14
lines changed

7 files changed

+96
-14
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ module.exports = {
111111
}
112112
```
113113

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

116119
Feel free to open a PR or an issue and I'll try to help you :D

index.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const { resolveLogLevel, DEBUG, INFO } = require("./src/log");
99
const cachedResolvers = {};
1010

1111
function getCachedExports({
12+
logLevel,
1213
moduleName,
1314
barrelFilePath,
1415
moduleType,
@@ -25,6 +26,8 @@ function getCachedExports({
2526
cachedResolvers[moduleName] = collectCjsExports(barrelFilePath);
2627
}
2728

29+
logLevel >= INFO && console.log(`[resolve-barrel-files] '${moduleName}' exports:`, cachedResolvers[moduleName]);
30+
2831
return cachedResolvers[moduleName];
2932
}
3033

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

4245
const transforms = [];
43-
const sourceImport = sourceConfig.mainBarrelPath;
46+
const mainBarrelPath = sourceConfig.mainBarrelPath;
47+
const mainBarrelFolder = pathLib.join(mainBarrelPath, "..");
4448
const moduleType = sourceConfig.moduleType || "commonjs";
4549
const logLevel = resolveLogLevel(sourceConfig.logLevel);
4650

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

4954
const exports = getCachedExports({
55+
logLevel,
5056
moduleName,
51-
barrelFilePath: sourceImport,
57+
barrelFilePath: mainBarrelPath,
5258
moduleType,
5359
});
5460

@@ -74,7 +80,9 @@ module.exports = function() {
7480
continue;
7581
}
7682

77-
const importFrom = pathLib.join(sourceImport, exportInfo.importPath);
83+
const importFrom = pathLib.join(mainBarrelFolder, exportInfo.importPath);
84+
85+
logLevel >= DEBUG && console.log(`[${moduleName}] Resolving '${importName}' to ${importFrom}`);
7886

7987
let newImportSpecifier = memberImport;
8088

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "babel-plugin-resolve-barrel-files",
3-
"version": "0.0.2",
3+
"version": "0.2.0",
44
"description": "A babel plugin that solves typescript barrel files",
55
"main": "index.js",
66
"repository": "https://github.com/Grohden/babel-plugin-resolve-barrel-files",

test/cjs-tests.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
const path = require("path");
12
const { assert } = require("chai");
23
const { cjsBarrel, transform } = require("./utils");
34

45
describe("commonjs import transformations", function() {
6+
const barrelFolder = path.resolve(cjsBarrel, "..");
57
const cjsTransform = transform({
68
moduleType: "commonjs",
79
mainBarrelPath: cjsBarrel,
@@ -13,9 +15,9 @@ describe("commonjs import transformations", function() {
1315
assert.equal(
1416
code,
1517
[
16-
`import { Bar } from "${cjsBarrel}/foo-bar";`,
17-
`import { Abc as Bazz } from "${cjsBarrel}/bazz";`,
18-
`import { default as Buzz } from "${cjsBarrel}/buzz";`,
18+
`import { Bar } from "${barrelFolder}/foo-bar";`,
19+
`import { Abc as Bazz } from "${barrelFolder}/bazz";`,
20+
`import { default as Buzz } from "${barrelFolder}/buzz";`,
1921
].join("\n"),
2022
);
2123
});
@@ -26,7 +28,19 @@ describe("commonjs import transformations", function() {
2628
assert.equal(
2729
code,
2830
[
29-
`import { Abc as Foo } from "${cjsBarrel}/bazz";`,
31+
`import { Abc as Foo } from "${barrelFolder}/bazz";`,
32+
].join("\n"),
33+
);
34+
});
35+
36+
it("should resolve member imports with wildcard generated code", function() {
37+
const code = cjsTransform(`import { Wildcard, Unique } from 'react-ui-lib';`);
38+
39+
assert.equal(
40+
code,
41+
[
42+
`import { default as Wildcard } from "${barrelFolder}/wildcard";`,
43+
`import { Unique } from "${barrelFolder}/wildcard";`,
3044
].join("\n"),
3145
);
3246
});

test/esm-tests.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
const path = require("path");
12
const { assert } = require("chai");
23
const { esmBarrel, transform } = require("./utils");
34

45
describe("esm import transformations", function() {
6+
const barrelFolder = path.resolve(esmBarrel, "..");
57
const esmTransform = transform({
68
moduleType: "esm",
79
mainBarrelPath: esmBarrel,
@@ -13,9 +15,9 @@ describe("esm import transformations", function() {
1315
assert.equal(
1416
code,
1517
[
16-
`import { Bar } from "${esmBarrel}/foo-bar";`,
17-
`import { Abc as Bazz } from "${esmBarrel}/bazz";`,
18-
`import { default as Buzz } from "${esmBarrel}/buzz";`,
18+
`import { Bar } from "${barrelFolder}/foo-bar";`,
19+
`import { Abc as Bazz } from "${barrelFolder}/bazz";`,
20+
`import { default as Buzz } from "${barrelFolder}/buzz";`,
1921
].join("\n"),
2022
);
2123
});
@@ -26,7 +28,7 @@ describe("esm import transformations", function() {
2628
assert.equal(
2729
code,
2830
[
29-
`import { Abc as Foo } from "${esmBarrel}/bazz";`,
31+
`import { Abc as Foo } from "${barrelFolder}/bazz";`,
3032
].join("\n"),
3133
);
3234
});

test/fixtures/cjs.js

+55-1
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,68 @@ Object.defineProperty(exports, "Foo", {
2727
return _fooBar.Foo;
2828
},
2929
});
30-
exports.default = void 0;
30+
Object.defineProperty(exports, "Unique", {
31+
enumerable: true,
32+
get: function() {
33+
return _wildcard.Unique;
34+
},
35+
});
36+
Object.defineProperty(exports, "Wildcard", {
37+
enumerable: true,
38+
get: function() {
39+
return _wildcard.default;
40+
},
41+
});
3142

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

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

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

49+
var _wildcard = _interopRequireWildcard(require("./wildcard"));
50+
51+
function _getRequireWildcardCache(nodeInterop) {
52+
if (typeof WeakMap !== "function") return null;
53+
var cacheBabelInterop = new WeakMap();
54+
var cacheNodeInterop = new WeakMap();
55+
return (_getRequireWildcardCache = function(nodeInterop) {
56+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
57+
})(nodeInterop);
58+
}
59+
60+
function _interopRequireWildcard(obj, nodeInterop) {
61+
if (!nodeInterop && obj && obj.__esModule) {
62+
return obj;
63+
}
64+
if (obj === null || (typeof obj !== "object" && typeof obj !== "function")) {
65+
return { default: obj };
66+
}
67+
var cache = _getRequireWildcardCache(nodeInterop);
68+
if (cache && cache.has(obj)) {
69+
return cache.get(obj);
70+
}
71+
var newObj = {};
72+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
73+
for (var key in obj) {
74+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
75+
var desc = hasPropertyDescriptor
76+
? Object.getOwnPropertyDescriptor(obj, key)
77+
: null;
78+
if (desc && (desc.get || desc.set)) {
79+
Object.defineProperty(newObj, key, desc);
80+
} else {
81+
newObj[key] = obj[key];
82+
}
83+
}
84+
}
85+
newObj.default = obj;
86+
if (cache) {
87+
cache.set(obj, newObj);
88+
}
89+
return newObj;
90+
}
91+
3892
function _interopRequireDefault(obj) {
3993
return obj && obj.__esModule ? obj : { default: obj };
4094
}

test/fixtures/esm.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { Abc as Bazz } from "./bazz";
22
export { default as Buzz } from "./buzz";
33
export { Bar, Foo } from "./foo-bar";
4+
export { default as Wildcard, Unique } from "./wildcard";

0 commit comments

Comments
 (0)