Skip to content

Commit

Permalink
use resolveId
Browse files Browse the repository at this point in the history
  • Loading branch information
thejackshelton committed Aug 6, 2024
1 parent 6b9726f commit 6a1a2cf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
30 changes: 9 additions & 21 deletions libs/qwikdev-astro/src/entrypoints.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ts from "typescript";
import type { Plugin } from "vite";

export const qwikModules = [
"@builder.io/qwik",
Expand All @@ -7,35 +7,23 @@ export const qwikModules = [
"@qwikdev/qwik-react"
];

import type { Plugin } from "vite";

export function qwikTransformPlugin(filter: (id: unknown) => boolean): Plugin {
export function qwikEntrypointsPlugin(filter: (id: string) => boolean): Plugin {
const entrypoints: Set<string> = new Set();

return {
name: "qwik-transform-plugin",
name: "qwik-entrypoints-plugin",
enforce: "pre",

async transform(code, id) {
if (filter(id) || entrypoints.has(id)) {
const sourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.Latest, true);

const hasQwikImport = ts.forEachChild(sourceFile, (node) => {
return (
ts.isImportDeclaration(node) &&
ts.isStringLiteral(node.moduleSpecifier) &&
qwikModules.includes(node.moduleSpecifier.text)
);
});

if (hasQwikImport) {
entrypoints.add(id);
console.log("New Qwik entrypoint found:", id);
async resolveId(source, importer) {
if (importer && filter(source)) {
const resolved = await this.resolve(source, importer, { skipSelf: true });
if (resolved) {
entrypoints.add(resolved.id);
console.log(entrypoints);
}
}
return null;
},

api: {
getEntrypoints: () => Array.from(entrypoints)
}
Expand Down
8 changes: 3 additions & 5 deletions libs/qwikdev-astro/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { build, createFilter } from "vite";
import type { InlineConfig, PluginOption } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

import { qwikModules, qwikTransformPlugin } from "./entrypoints";
import { qwikEntrypointsPlugin, qwikModules } from "./entrypoints";
import { moveArtifacts, newHash } from "./utils";

declare global {
Expand Down Expand Up @@ -89,9 +89,7 @@ export default defineIntegration({
// Retrieve Qwik files from the project source directory
srcDir = relative(astroConfig.root.pathname, astroConfig.srcDir.pathname);

const myQwikTransformPlugin = qwikTransformPlugin(filter);

entrypoints = myQwikTransformPlugin.api.getEntrypoints();
entrypoints = qwikEntrypointsPlugin(filter).api.getEntrypoints();

addRenderer({
name: "@qwikdev/astro",
Expand Down Expand Up @@ -165,7 +163,7 @@ export default defineIntegration({
}
},
plugins: [
myQwikTransformPlugin,
qwikEntrypointsPlugin(filter),
symbolMapperPlugin,
qwikVite(qwikViteConfig),
tsconfigPaths(),
Expand Down

0 comments on commit 6a1a2cf

Please sign in to comment.