Skip to content

Commit

Permalink
fix: rollup bundle to deal with multiple import specifiers from SD
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Apr 27, 2024
1 parent cad35cf commit e65db5d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-glasses-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/configurator": patch
---

Fix for rollup bundle utility to deal with multiple import specifiers from SD on a single import statement.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 22 additions & 13 deletions src/utils/rollup-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,28 +72,37 @@ export async function bundle(inputPath, _fs = fs) {
let matchRes;
while ((matchRes = reg.exec(rewrittenCode)) !== null) {
let { id, entrypoint } = matchRes.groups;
let namedImport = id;
let replacement;
let namedImports = [id];
let replacements;

if (id.startsWith("{") && id.endsWith("}") && entrypoint) {
namedImport = namedImport
namedImports = id
.replace("{", "")
.replace("}", "")
.trim();
const entry = entrypoint.replace(/^\//, "");
.split(",")
.map((importSpecifier) => importSpecifier.trim());

if (entry === "fs") {
replacement = `globalThis['${sdFsName}']['${namedImport}']`;
} else if (entry === "utils") {
replacement = `globalThis['${sdUtilsName}']['${namedImport}']`;
const entry = entrypoint.replace(/^\//, "");
if (entry === "fs" || entry === "utils") {
replacements = namedImports.map((imp) => [
imp,
`globalThis['${
entry === "fs" ? sdFsName : sdUtilsName
}']['${imp}']`,
]);
}
} else {
// Remove the import statement, replace the id wherever used with the global
replacement = `globalThis['${sdName}']`;
replacements = [[id, `globalThis['${sdName}']`]];
}
rewrittenCode = rewrittenCode
.replace(matchRes[0], "")
.replace(new RegExp(namedImport, "g"), replacement);
rewrittenCode = rewrittenCode.replace(matchRes[0], "");

replacements.forEach((repl) => {
rewrittenCode = rewrittenCode.replace(
new RegExp(repl[0], "g"),
repl[1]
);
});
}

return rewrittenCode;
Expand Down

0 comments on commit e65db5d

Please sign in to comment.