Skip to content

Commit

Permalink
add workspaces support
Browse files Browse the repository at this point in the history
  • Loading branch information
FogelAI committed Dec 31, 2023
1 parent 478654d commit 6b13c78
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
5 changes: 4 additions & 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.8",
"version": "1.0.9",
"description": "This Babel plugin transforms indirect imports through a barrel file (index.js) into direct imports.",
"homepage": "https://github.com/FogelAI/babel-plugin-transform-barrels",
"main": "src/barrel.js",
Expand All @@ -13,6 +13,9 @@
},
"author": "",
"license": "MIT",
"dependencies": {
"fast-glob": "^3.3.2"
},
"devDependencies": {
"@babel/parser": "^7.21.3",
"@babel/types": "^7.21.3"
Expand Down
55 changes: 55 additions & 0 deletions src/barrel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const t = require("@babel/types");
const ospath = require("path");
const fs = require("fs");
const fg = require("fast-glob");
const AST = require("./ast");

class PathFunctions {
Expand Down Expand Up @@ -42,6 +43,54 @@ class PathFunctions {
}
}

class PackageJson {
constructor() {
this.mainPkg = ospath.resolve('package.json');
this.packagesJsons = [];
this.aliasObj = {};
this.workspaces = this.loadWorkspaces();
this.getNodePackages();
this.aliasesToWorkspaces();
}

loadWorkspaces() {
const workspaces = require(this.mainPkg).workspaces || [];
if (Array.isArray(workspaces)) return workspaces;
return workspaces.packages || [];
}

getNodePackages( { workspacePath = ospath.resolve('.') } = {} ) {
if (this.workspaces===[]) return [];
const globWorkspaces = this.workspaces.map((ws)=> ws + '/**/package.json');
const packagesJsons = fg.sync(
globWorkspaces,
{
deep: Infinity,
onlyFiles: true,
absolute: true,
cwd: workspacePath,
ignore: ['**/node_modules'],
},
);
this.packagesJsons = packagesJsons;
return packagesJsons;
}

aliasesToWorkspaces() {
const aliases = this.packagesJsons.reduce((alias, pkgPath) => {
const pkgName = require(pkgPath).name;
alias[pkgName] = ospath.dirname(pkgPath);
return alias;
}, {})
this.aliasObj = aliases;
return aliases;
}

getAlias() {
return this.aliasObj;
}
}

class WebpackConfig {
constructor() {
this.aliasObj = {};
Expand Down Expand Up @@ -111,6 +160,10 @@ class WebpackConfig {
}
return convertedPath;
}

appendAlias(alias) {
this.aliasObj = { ...this.aliasObj, ...alias };
}
}

class BarrelFilesMapping {
Expand Down Expand Up @@ -192,6 +245,7 @@ class BarrelFilesMapping {

const mapping = new BarrelFilesMapping();
const webpackConfig = new WebpackConfig();
const packageJsonConfig = new PackageJson();

const importDeclarationVisitor = (path, state) => {
const parsedJSFile = state.filename
Expand Down Expand Up @@ -227,6 +281,7 @@ module.exports = function (babel) {
const plugins = state.opts.plugins;
const plugin = plugins.find(plugin => plugin.key === PLUGIN_KEY);
webpackConfig.getWebpackAlias(plugin);
webpackConfig.appendAlias(packageJsonConfig.getAlias());
},
visitor: {
ImportDeclaration: importDeclarationVisitor,
Expand Down

0 comments on commit 6b13c78

Please sign in to comment.