-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
plugin.ts
148 lines (122 loc) · 3.81 KB
/
plugin.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
import { createMdxTransformer, MdxTransform } from '../markdown/mdx';
import { extname, join, resolve } from 'path';
import type { Plugin } from 'vite';
import { generateQwikCityPlan } from '../runtime-generation/generate-runtime';
import type { BuildContext } from '../types';
import { createBuildContext, resetBuildContext } from '../utils/context';
import { isMarkdownFileName, normalizePath } from '../utils/fs';
import { validatePlugin } from './validate-plugin';
import type { QwikCityVitePluginOptions } from './types';
import { build } from '../build';
import ts from 'typescript';
import { configureDevServer } from './dev-server';
/**
* @alpha
*/
export function qwikCity(userOpts?: QwikCityVitePluginOptions) {
let ctx: BuildContext | null = null;
let mdxTransform: MdxTransform | null = null;
let rootDir: string | null = null;
const plugin: Plugin = {
name: 'vite-plugin-qwik-city',
enforce: 'pre',
config() {
const updatedViteConfig: any = {
optimizeDeps: {
exclude: ['@builder.io/qwik-city', QWIK_CITY],
},
ssr: {
noExternal: [QWIK_CITY_PLAN_ID, QWIK_CITY],
},
};
return updatedViteConfig;
},
async configResolved(config) {
rootDir = resolve(config.root);
const target = config.build?.ssr || config.mode === 'ssr' ? 'ssr' : 'client';
ctx = createBuildContext(rootDir!, userOpts, target);
await validatePlugin(ctx.opts);
mdxTransform = await createMdxTransformer(ctx);
},
configureServer(server) {
configureDevServer(ctx, server);
},
buildStart() {
resetBuildContext(ctx);
},
resolveId(id) {
if (id === QWIK_CITY_PLAN_ID) {
return join(rootDir!, QWIK_CITY_PLAN_ID);
}
return null;
},
async load(id) {
if (id.endsWith(QWIK_CITY_PLAN_ID) && ctx) {
// @qwik-city-plan
await build(ctx);
ctx.diagnostics.forEach((d) => {
this.warn(d.message);
});
return generateQwikCityPlan(ctx);
}
return null;
},
async transform(code, id) {
if (isMarkdownFileName(id) && mdxTransform) {
const mdxResult = await mdxTransform(code, id);
return mdxResult;
}
if (ctx && ctx.target === 'client') {
const ext = extname(id);
if (ext === '.js' && !id.includes('_layout')) {
id = normalizePath(id);
if (id.startsWith(ctx.opts.routesDir)) {
if (SERVER_FNS.some((fnName) => code.includes(fnName))) {
const modifiedCode = removeServerFns(code, id);
if (modifiedCode) {
return modifiedCode;
}
}
}
}
}
return null;
},
};
return plugin as any;
}
function removeServerFns(code: string, id: string) {
let didModify = false;
const sourceFile = ts.createSourceFile(id, code, ts.ScriptTarget.Latest);
for (const s of sourceFile.statements) {
if (!ts.isVariableStatement(s)) {
continue;
}
if (!s.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword)) {
continue;
}
const decs = s.declarationList.declarations;
for (const d of decs) {
if (!ts.isVariableDeclaration(d)) {
continue;
}
const identifier = d.name;
if (!ts.isIdentifier(identifier)) {
continue;
}
if (!SERVER_FNS.some((fn) => identifier.escapedText === fn)) {
continue;
}
(d as any).initializer = ts.factory.createNull();
didModify = true;
}
}
if (didModify) {
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
return printer.printFile(sourceFile);
}
return null;
}
const QWIK_CITY_PLAN_ID = '@qwik-city-plan';
const QWIK_CITY = '@builder.io/qwik-city';
const SERVER_FNS = ['onGet', 'onPost', 'onRequest'];