-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathstackblitz.utils.ts
289 lines (255 loc) · 7.87 KB
/
stackblitz.utils.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
import sdk from '@stackblitz/sdk';
// The default title to use for StackBlitz examples (when not overwritten)
const DEFAULT_EDITOR_TITLE = 'Ionic Docs Example';
// The default description to use for StackBlitz examples (when not overwritten)
const DEFAULT_EDITOR_DESCRIPTION = '';
export interface EditorOptions {
/**
* The title of the StackBlitz example.
*/
title?: string;
/**
* The description of the StackBlitz example.
*/
description?: string;
files?: {
[key: string]: string;
};
/**
* List of dependencies to add to the StackBlitz example.
* The key is the name of the dependency and the value is the version.
*/
dependencies?: {
[key: string]: string;
};
/**
* `true` if `ion-app` and `ion-content` should automatically be injected into the
* StackBlitz example.
*/
includeIonContent: boolean;
/**
* The mode of the StackBlitz example.
*/
mode?: string;
version?: number;
}
const loadSourceFiles = async (files: string[], version: number) => {
const versionDir = `v${version}`;
const sourceFiles = await Promise.all(files.map((f) => fetch(`/docs/code/stackblitz/${versionDir}/${f}`)));
return await Promise.all(sourceFiles.map((res) => res.text()));
};
const openHtmlEditor = async (code: string, options?: EditorOptions) => {
const defaultFiles = await loadSourceFiles(
[
'html/index.ts',
options?.includeIonContent ? 'html/index.withContent.html' : 'html/index.html',
'html/variables.css',
'html/package.json',
'html/tsconfig.json',
'html/vite.config.ts',
],
options.version
);
const package_json = JSON.parse(defaultFiles[3]);
const indexHtml = 'index.html';
const files = {
'package.json': JSON.stringify(package_json, null, 2),
'src/index.ts': defaultFiles[0],
[indexHtml]: defaultFiles[1],
'src/theme/variables.css': defaultFiles[2],
'tsconfig.json': defaultFiles[4],
'vite.config.ts': defaultFiles[5],
...options?.files,
};
files[indexHtml] = defaultFiles[1].replace(/{{ TEMPLATE }}/g, code).replace(
'</head>',
`
<script>
window.Ionic = {
config: {
mode: '${options?.mode}'
}
}
</script>
</head>
`
);
if (options?.dependencies) {
package_json.dependencies = {
...package_json.dependencies,
...options.dependencies,
};
}
sdk.openProject({
template: 'node',
title: options?.title ?? DEFAULT_EDITOR_TITLE,
description: options?.description ?? DEFAULT_EDITOR_DESCRIPTION,
files,
});
};
const openAngularEditor = async (code: string, options?: EditorOptions) => {
const defaultFiles = await loadSourceFiles(
[
'angular/package.json',
'angular/angular.json',
'angular/tsconfig.json',
'angular/tsconfig.app.json',
'angular/main.ts',
'angular/index.html',
'angular/app.routes.ts',
options?.includeIonContent ? 'angular/app.component.withContent.ts' : 'angular/app.component.ts',
'angular/app.component.css',
options?.includeIonContent ? 'angular/app.component.withContent.html' : 'angular/app.component.html',
'angular/example.component.ts',
'angular/styles.css',
'angular/global.css',
'angular/variables.css',
],
options.version
);
const package_json = JSON.parse(defaultFiles[0]);
if (options?.dependencies) {
package_json.dependencies = {
...package_json.dependencies,
...options.dependencies,
};
}
const main = 'src/main.ts';
const files = {
'package.json': JSON.stringify(package_json, null, 2),
'angular.json': defaultFiles[1],
'tsconfig.json': defaultFiles[2],
'tsconfig.app.json': defaultFiles[3],
[main]: defaultFiles[4],
'src/index.html': defaultFiles[5],
'src/polyfills.ts': `import 'zone.js';`,
'src/app/app.routes.ts': defaultFiles[6],
'src/app/app.component.ts': defaultFiles[7],
'src/app/app.component.css': defaultFiles[8],
'src/app/app.component.html': defaultFiles[9],
'src/app/example.component.ts': defaultFiles[10],
'src/app/example.component.html': code,
'src/app/example.component.css': '',
'src/styles.css': defaultFiles[11],
'src/global.css': defaultFiles[12],
'src/theme/variables.css': defaultFiles[13],
...options?.files,
};
files[main] = files[main].replace('provideIonicAngular()', `provideIonicAngular({ mode: '${options?.mode}' })`);
sdk.openProject({
template: 'node',
title: options?.title ?? DEFAULT_EDITOR_TITLE,
description: options?.description ?? DEFAULT_EDITOR_DESCRIPTION,
files,
});
};
const openReactEditor = async (code: string, options?: EditorOptions) => {
const defaultFiles = await loadSourceFiles(
[
'react/index.tsx',
options?.includeIonContent ? 'react/app.withContent.tsx' : 'react/app.tsx',
'react/variables.css',
'react/tsconfig.json',
'react/package.json',
'react/package-lock.json',
'react/index.html',
'react/vite.config.js',
'react/browserslistrc',
'react/eslintrc.js',
],
options.version
);
const package_json = JSON.parse(defaultFiles[4]);
if (options?.dependencies) {
package_json.dependencies = {
...package_json.dependencies,
...options.dependencies,
};
}
const appTsx = 'src/App.tsx';
const files = {
'.eslintrc.js': defaultFiles[9],
'.browserslistrc': defaultFiles[8],
'vite.config.js': defaultFiles[7],
'index.html': defaultFiles[6],
'src/index.tsx': defaultFiles[0],
[appTsx]: defaultFiles[1],
'src/main.tsx': code,
'src/theme/variables.css': defaultFiles[2],
'tsconfig.json': defaultFiles[3],
'package.json': JSON.stringify(package_json, null, 2),
'package-lock.json': defaultFiles[5],
...options?.files,
'.stackblitzrc': `{
"startCommand": "yarn run start"
}`,
};
files[appTsx] = files[appTsx].replace('setupIonicReact()', `setupIonicReact({ mode: '${options?.mode}' })`);
sdk.openProject({
template: 'node',
title: options?.title ?? DEFAULT_EDITOR_TITLE,
description: options?.description ?? DEFAULT_EDITOR_DESCRIPTION,
files,
});
};
const openVueEditor = async (code: string, options?: EditorOptions) => {
const defaultFiles = await loadSourceFiles(
[
'vue/package.json',
'vue/package-lock.json',
'vue/index.html',
'vue/variables.css',
'vue/vite.config.ts',
'vue/main.ts',
options?.includeIonContent ? 'vue/App.withContent.vue' : 'vue/App.vue',
'vue/tsconfig.json',
'vue/tsconfig.node.json',
'vue/env.d.ts',
],
options.version
);
const package_json = JSON.parse(defaultFiles[0]);
if (options?.dependencies) {
package_json.dependencies = {
...package_json.dependencies,
...options.dependencies,
};
}
const mainTs = 'src/main.ts';
const files = {
'src/App.vue': defaultFiles[6],
'src/components/Example.vue': code,
[mainTs]: defaultFiles[5],
'src/env.d.ts': defaultFiles[9],
'src/theme/variables.css': defaultFiles[3],
'index.html': defaultFiles[2],
'vite.config.ts': defaultFiles[4],
'package.json': JSON.stringify(package_json, null, 2),
'package-lock.json': defaultFiles[1],
'tsconfig.json': defaultFiles[7],
'tsconfig.node.json': defaultFiles[8],
...options?.files,
'.stackblitzrc': `{
"startCommand": "yarn run dev"
}`,
};
files[mainTs] = files[mainTs].replace(
'.use(IonicVue)',
`.use(IonicVue, {
mode: '${options?.mode}'
})`
);
/**
* We have to use StackBlitz web containers here (node template), due
* to multiple issues with Vite, Vue/Vue Router and Vue 3's script setup.
*
* https://github.com/stackblitz/core/issues/1308
*/
sdk.openProject({
template: 'node',
title: options?.title ?? DEFAULT_EDITOR_TITLE,
description: options?.description ?? DEFAULT_EDITOR_DESCRIPTION,
files,
});
};
export { openAngularEditor, openHtmlEditor, openReactEditor, openVueEditor };