-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.ts
341 lines (288 loc) Β· 10.2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import fs from "node:fs";
import os from "node:os";
import fsExtra from "fs-extra";
import move from "fs-move";
import { rmSync } from "node:fs";
import { lstat, readdir, readlink } from "node:fs/promises";
import { dirname, join, normalize, relative, resolve } from "node:path";
import type { AstroConfig } from "astro";
import { z } from "astro/zod";
import ts from "typescript";
import { qwikVite } from "@builder.io/qwik/optimizer";
import { createResolver, defineIntegration } from "astro-integration-kit";
import { watchIntegrationPlugin } from "astro-integration-kit/plugins";
import { type InlineConfig, build, createFilter } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
/* Similar to vite's FilterPattern */
const FilternPatternSchema = z.union([
z.string(),
z.instanceof(RegExp),
z.array(z.union([z.string(), z.instanceof(RegExp)])).readonly(),
z.null()
]);
/**
* This project uses Astro Integration Kit.
* @see https://astro-integration-kit.netlify.app/
*/
export default defineIntegration({
name: "@qwikdev/astro",
plugins: [watchIntegrationPlugin],
optionsSchema: z.object({
include: FilternPatternSchema.optional(),
exclude: FilternPatternSchema.optional()
}),
setup({ options }) {
let distDir = "";
let srcDir = "";
let astroConfig: AstroConfig | null = null;
let entrypoints: Promise<string[]>;
const tempDir = join(`tmp-${hash()}`);
const filter = createFilter(options.include, options.exclude);
const { resolve } = createResolver(import.meta.url);
return {
"astro:config:setup": async ({
addRenderer,
updateConfig,
config,
command,
injectScript,
watchIntegration
}) => {
// Integration HMR
watchIntegration(resolve());
// Because Astro uses the same port for both dev and preview, we need to unregister the SW in order to avoid a stale SW in dev mode.
if (command === "dev") {
const unregisterSW =
"navigator.serviceWorker.getRegistration().then((r) => r && r.unregister())";
injectScript("head-inline", unregisterSW);
}
// Update the global config
astroConfig = config;
// Retrieve Qwik files from the project source directory
srcDir = relative(astroConfig.root.pathname, astroConfig.srcDir.pathname);
// used in server.ts for dev mode
process.env.SRC_DIR = relative(
astroConfig.root.pathname,
astroConfig.srcDir.pathname
);
entrypoints = getQwikEntrypoints(srcDir, filter);
if ((await entrypoints).length !== 0) {
addRenderer({
name: "@qwikdev/astro",
serverEntrypoint: resolve("../server.ts")
});
// Update the global dist directory
distDir = astroConfig.outDir.pathname;
// checks all windows platforms and removes drive ex: C:\\
if (os.platform() === "win32") {
distDir = distDir.substring(3);
}
updateConfig({
vite: {
build: {
rollupOptions: {
output: {
inlineDynamicImports: false
}
}
},
plugins: [
qwikVite({
/* user passed include & exclude config (to use multiple JSX frameworks) */
fileFilter: (id: string, hook: string) => {
if (hook === "transform" && !filter(id)) {
return false;
}
return true;
},
devSsrServer: false,
srcDir,
client: {
/*
In order to make a client build, we need to know
all of the entry points to the application so
that we can generate the manifest.
*/
input: await entrypoints
},
ssr: {
input: resolve("../server.ts")
}
}),
tsconfigPaths(),
{
// HACK: override qwikVite's attempt to set `esbuild` to false during dev
enforce: "post",
config(config) {
// @ts-expect-error - true is assigned, but it's not a valid value, this should be reviewed.
config.esbuild = true;
return config;
}
}
]
}
});
}
},
"astro:config:done": async ({ config }) => {
astroConfig = config;
},
"astro:build:start": async ({ logger }) => {
logger.info("astro:build:start");
if ((await entrypoints).length > 0) {
// make sure vite does not parse .astro files
await build({
...astroConfig?.vite,
plugins: [
...(astroConfig?.vite.plugins || []),
{
enforce: "pre",
name: "astro-noop",
load(id) {
if (id.endsWith(".astro")) {
return "export default function() {}";
}
return null;
}
}
]
} as InlineConfig);
await moveArtifacts(distDir, tempDir);
} else {
logger.info("No entrypoints found. Skipping build.");
}
},
"astro:build:done": async ({ logger }) => {
if ((await entrypoints).length > 0 && astroConfig) {
let outputPath =
astroConfig.output === "server" || astroConfig.output === "hybrid"
? astroConfig.build.client.pathname
: astroConfig.outDir.pathname;
// checks all windows platforms and removes drive ex: C:\\
if (os.platform() === "win32") {
outputPath = outputPath.substring(3);
}
const normalizedPath = normalize(outputPath);
process.env.Q_BASE = normalizedPath;
await moveArtifacts(tempDir, normalizedPath);
// remove the temp dir folder
rmSync(tempDir, { recursive: true });
} else {
logger.info("Build finished. No artifacts moved.");
}
}
};
}
});
/** We need to find the Qwik entrypoints so that the client build will run successfully. */
export async function getQwikEntrypoints(
dir: string,
filter: (id: unknown) => boolean
): Promise<string[]> {
const files = await crawlDirectory(dir);
const qwikFiles = [];
for (const file of files) {
// Skip files not matching patterns w/ astro config include & exclude
if (!filter(file)) {
continue;
}
const fileContent = fs.readFileSync(file, "utf-8");
const sourceFile = ts.createSourceFile(
file,
fileContent,
ts.ScriptTarget.ESNext,
true
);
let qwikImportFound = false;
ts.forEachChild(sourceFile, function nodeVisitor(node) {
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
if (
node.moduleSpecifier.text === "@builder.io/qwik" ||
node.moduleSpecifier.text === "@builder.io/qwik-react"
) {
qwikImportFound = true;
}
}
if (!qwikImportFound) {
ts.forEachChild(node, nodeVisitor);
}
});
if (qwikImportFound) {
qwikFiles.push(file);
}
}
return qwikFiles;
}
export function hash() {
return Math.random().toString(26).split(".").pop();
}
export async function moveArtifacts(srcDir: string, destDir: string) {
// Ensure the destination dir exists, create if not
await fsExtra.ensureDir(destDir);
for (const file of await readdir(srcDir)) {
// move files from source to destintation, overwrite if they exist
await move(join(srcDir, file), join(destDir, file), {
// Merge directories
merge: true,
// Don't overwrite any files, as this would overwrite astro-generated files with files from public.
// This matches astro's default behavior of replacing files in public with generated pages on naming-conflicts.
overwrite: false
});
}
}
export async function crawlDirectory(dir: string): Promise<string[]> {
/**
* Recursively follows a symlink.
*
* @param path symlink to follow
* @returns `[target, stat]` where `target` is the final target path and `stat` is the {@link fs.Stats} of the target or `undefined` if the target does not exist.
*/
const readLinkRec = async (path: string): Promise<[string, fs.Stats | undefined]> => {
const target = resolve(dirname(path), await readlink(path));
const stat = await lstat(target).catch((e) => {
if (e.code === "ENOENT") {
return undefined;
}
throw e;
});
if (stat?.isSymbolicLink()) {
return readLinkRec(target);
}
return [target, stat];
};
/**
* Recurse on the passed directory. Follows symlinks and stops when a loop is detected (i.e., `dir` has already been visited)
*
* @param dir The current directory to recursively list
* @param visitedDirs Directories that have already been visited
* @returns A recursive list of files in the passed directory
*/
const crawl = async (dir: string, visitedDirs: string[]): Promise<string[]> => {
if (visitedDirs.includes(dir)) {
return [];
}
visitedDirs.push(dir);
const entries = await readdir(dir, { withFileTypes: true });
const files = await Promise.all(
entries.map((entry) => {
const fullPath = join(dir, entry.name);
if (entry.isSymbolicLink()) {
return readLinkRec(fullPath).then(
([target, stat]): string | string[] | Promise<string[]> => {
if (stat === undefined) {
return []; // target does not exist
}
return stat.isDirectory() ? crawl(target, visitedDirs) : target;
}
);
}
return entry.isDirectory() ? crawl(fullPath, visitedDirs) : fullPath;
})
);
// flatten files array
return files.flat();
};
// Absolute path for duplicate-detection
const absoluteDir = resolve(dir);
return crawl(absoluteDir, []);
}