forked from stackblitz/tutorialkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
190 lines (169 loc) · 5.69 KB
/
index.ts
File metadata and controls
190 lines (169 loc) · 5.69 KB
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
import { fileURLToPath } from 'node:url';
import type { AstroConfig, AstroIntegration } from 'astro';
import type { ExpressiveCodePlugin, ThemeObjectOrShikiThemeName } from 'astro-expressive-code';
import { extraIntegrations } from './integrations.js';
import { updateMarkdownConfig } from './remark/index.js';
import { tutorialkitCore } from './vite-plugins/core.js';
import { userlandCSS, watchUserlandCSS } from './vite-plugins/css.js';
import { overrideComponents, type OverrideComponentsOptions } from './vite-plugins/override-components.js';
import { tutorialkitStore } from './vite-plugins/store.js';
import { WebContainerFiles } from './webcontainer-files/index.js';
export interface Options {
/**
* Whether or not default routes are injected.
*
* Set this to false to customize the pages.
*
* Use 'tutorial-only' to only inject the tutorial routes. This is useful
* if you want to have a different landing page.
*
* @default true
*/
defaultRoutes?: boolean | 'tutorial-only';
/**
* Override components of TutorialKit.
*/
components?: OverrideComponentsOptions;
/**
* The value of the Cross-Origin-Embedder-Policy header for the dev server.
* This is required for webcontainer to works.
*
* Using credentialless lets you embed images from third party more easily.
* However only Firefox and Chrome supports credentialless.
*
* @see https://webcontainers.io/guides/configuring-headers
*
* @default 'require-corp'
*/
isolation?: 'require-corp' | 'credentialless';
/**
* Configuration options when using the Enterprise
* version of WebContainer API.
*/
enterprise?: {
/**
* The StackBlitz editor origin.
*/
editorOrigin: string;
/**
* The client id.
*/
clientId: string;
/**
* The OAuth scope.
*/
scope: string;
};
/**
* Expressive code plugins.
*
* @default []
*/
expressiveCodePlugins?: ExpressiveCodePlugin[];
/**
* Themes for expressive code.
* Make sure to provide a light and a dark theme if you want support for both light and dark modes.
* Default values are ['light-plus', 'dark-plus']
*
* @default ['light-plus', 'dark-plus']
*/
expressiveCodeThemes?: [ThemeObjectOrShikiThemeName, ThemeObjectOrShikiThemeName];
}
export default function createPlugin({
defaultRoutes = true,
components,
isolation,
enterprise,
expressiveCodePlugins = [],
expressiveCodeThemes,
}: Options = {}): AstroIntegration {
const webcontainerFiles = new WebContainerFiles();
let _config: AstroConfig;
return {
name: '@tutorialkit/astro',
hooks: {
async 'astro:config:setup'(options) {
const { injectRoute, updateConfig, config } = options;
updateConfig({
server: {
headers: {
'Cross-Origin-Embedder-Policy': isolation ?? 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin',
},
},
vite: {
optimizeDeps: {
entries: ['!**/src/(content|templates)/**'],
include: process.env.TUTORIALKIT_DEV
? []
: [
'@tutorialkit/react',
/**
* The `picomatch` is CJS dependency used by `@tutorialkit/runtime`.
* When used via `@tutorialkit/astro`, it's a transitive dependency that's
* not automatically transformed.
*/
'@tutorialkit/astro > picomatch/posix.js',
],
},
define: {
__ENTERPRISE__: `${!!enterprise}`,
__WC_CONFIG__: enterprise ? JSON.stringify(enterprise) : 'undefined',
},
ssr: {
noExternal: ['@tutorialkit/astro', '@tutorialkit/react'],
},
plugins: [
userlandCSS,
tutorialkitStore,
tutorialkitCore,
overrideComponents({ components, defaultRoutes: !!defaultRoutes }),
process.env.TUTORIALKIT_VITE_INSPECT ? (await import('vite-plugin-inspect')).default() : null,
],
},
});
updateMarkdownConfig(options);
if (defaultRoutes) {
if (defaultRoutes !== 'tutorial-only') {
injectRoute({
pattern: '/',
entrypoint: '@tutorialkit/astro/default/pages/index.astro',
prerender: true,
});
}
injectRoute({
pattern: '[...slug]',
entrypoint: '@tutorialkit/astro/default/pages/[...slug].astro',
prerender: true,
});
}
// inject the additional integrations right after ours
const selfIndex = config.integrations.findIndex((integration) => integration.name === '@tutorialkit/astro');
config.integrations.splice(
selfIndex + 1,
0,
...extraIntegrations({ root: fileURLToPath(config.root), expressiveCodePlugins, expressiveCodeThemes }),
);
},
'astro:config:done'({ config }) {
_config = config;
},
async 'astro:server:setup'(options) {
if (!_config) {
return;
}
const { server, logger } = options;
const projectRoot = fileURLToPath(_config.root);
await webcontainerFiles.serverSetup(projectRoot, options);
watchUserlandCSS(server, logger);
},
async 'astro:server:done'() {
await webcontainerFiles.serverDone();
},
async 'astro:build:done'(astroBuildDoneOptions) {
const projectRoot = fileURLToPath(_config.root);
await webcontainerFiles.buildAssets(projectRoot, astroBuildDoneOptions);
},
},
};
}