Skip to content

Commit

Permalink
fix: fix config import
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Mar 23, 2024
1 parent 9d0ab86 commit fc8d554
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
9 changes: 5 additions & 4 deletions examples-and-tests/react/plugin.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const configPath = join(import.meta.dir, "config.tsx");
const currentFile = import.meta.url.replace("file://", "");
const jsxRuntimePath = import.meta.resolveSync("react/jsx-dev-runtime");
const importJSXRuntime = `import {jsx, jsxDEV} from "${jsxRuntimePath}";`;
const importConfig = `import {prerenderConfig} from "${configPath}";`;

describe("React", () => {
describe("plugin", () => {
Expand Down Expand Up @@ -62,7 +63,7 @@ describe("React", () => {
);
const expected = normalizeQuotes(`
${importJSXRuntime}
import prerenderConfig from "prerender-macro";
${importConfig}
import Foo from "./components";
import {Bar} from "./components";
Expand Down Expand Up @@ -102,7 +103,7 @@ describe("React", () => {
);
const expected = normalizeQuotes(`
${importJSXRuntime}
import prerenderConfig from "prerender-macro";
${importConfig}
import {Bar} from "./components";
import Foo from "./components";
Expand Down Expand Up @@ -137,7 +138,7 @@ describe("React", () => {
);
const expected = normalizeQuotes(`
${importJSXRuntime}
import prerenderConfig from "prerender-macro";
${importConfig}
import {Bar} from "./components";
export default function Test() {
Expand Down Expand Up @@ -168,7 +169,7 @@ describe("React", () => {
);
const expected = normalizeQuotes(`
${importJSXRuntime}
import prerenderConfig from "prerender-macro";
${importConfig}
import Foo from "./components";
export default function Test() {
Expand Down
19 changes: 15 additions & 4 deletions package/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { BunPlugin } from "bun";
import { dirname } from "node:path";
import ts from "typescript";
import { prerender } from "./prerender";

const transpiler = new Bun.Transpiler({ loader: "tsx" });
const JSX_RUNTIME = ["jsx-runtime", "jsx-dev-runtime"];
Expand Down Expand Up @@ -57,7 +58,7 @@ export function transpile({

if (!importsWithPrerender.length) return code;

let modifiedAst = addExtraImports(sourceFile, config);
let modifiedAst = addExtraImports(sourceFile, prerenderConfigPath, config);

modifiedAst = replaceJSXToMacroCall(
modifiedAst,
Expand Down Expand Up @@ -132,7 +133,11 @@ function isPrerenderImport(node: ts.Node): node is ts.ImportDeclaration {
);
}

function addExtraImports(ast: ts.SourceFile, config?: Config) {
function addExtraImports(
ast: ts.SourceFile,
prerenderConfigPath: string,
config?: Config,
) {
const allImports = [...ast.statements];

allImports.unshift(
Expand Down Expand Up @@ -167,10 +172,16 @@ function addExtraImports(ast: ts.SourceFile, config?: Config) {
undefined,
ts.factory.createImportClause(
false,
ts.factory.createIdentifier("prerenderConfig"),
undefined,
ts.factory.createNamedImports([
ts.factory.createImportSpecifier(
false,
ts.factory.createIdentifier("prerenderConfig"),
ts.factory.createIdentifier("prerenderConfig"),
),
]),
),
ts.factory.createStringLiteral("prerender-macro"),
ts.factory.createStringLiteral(prerenderConfigPath),
),
);
}
Expand Down

0 comments on commit fc8d554

Please sign in to comment.