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

[babel]: Add commonjs transformation #28

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
added tests from mainstream and added support for commonjs transform
XantreDev committed Nov 23, 2023
commit c716069ce5e9625c132341eed46800a0fd4173b6
5 changes: 5 additions & 0 deletions .changeset/rich-jars-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@preact-signals/safe-react": minor
---

Now can transform commonjs modules
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -22,5 +22,8 @@
"lint": "turbo run lint",
"changeset": "changeset",
"release": "pnpm build && changeset publish"
},
"dependencies": {
"prettier": "^3.1.0"
}
}
7 changes: 4 additions & 3 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -97,17 +97,18 @@
"lint": "check-export-map"
},
"dependencies": {
"@preact/signals-core": "^1.5.0",
"use-sync-external-store": "^1.2.0",
"@babel/helper-module-imports": "^7.22.15",
"@babel/helper-plugin-utils": "^7.22.5",
"debug": "^4.3.4"
"@preact/signals-core": "^1.5.0",
"debug": "^4.3.4",
"use-sync-external-store": "^1.2.0"
},
"peerDependencies": {
"@babel/core": "^7.0.0",
"react": "^16.14.0 || 17.x || 18.x"
},
"devDependencies": {
"@babel/traverse": "^7.23.4",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-typescript": "^11.1.5",
"@types/babel__core": "^7.20.4",
23 changes: 23 additions & 0 deletions packages/react/src/babel.ts
Original file line number Diff line number Diff line change
@@ -356,6 +356,29 @@ function createImportLazily(
importPosition: "after",
});
set(pass, `imports/${importName}`, reference);

const matchesImportName = (
s: BabelTypes.ImportDeclaration["specifiers"][0]
) => {
if (s.type !== "ImportSpecifier") return false;
return (
(s.imported.type === "Identifier" &&
s.imported.name === importName) ||
(s.imported.type === "StringLiteral" &&
s.imported.value === importName)
);
};

for (let statement of path.get("body")) {
if (
statement.isImportDeclaration() &&
statement.node.source.value === source &&
statement.node.specifiers.some(matchesImportName)
) {
path.scope.registerDeclaration(statement);
break;
}
}
return reference;
} else {
let reference = get(pass, `requires/${importName}`);
Loading