forked from stackblitz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
158 lines (144 loc) · 4.49 KB
/
vite.config.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
import ReplacePlugin from '@rollup/plugin-replace';
import bodyParser from 'body-parser';
import fs from 'node:fs/promises';
import path from 'node:path';
import type { Plugin, UserConfig, ViteDevServer } from 'vite';
import { defineConfig } from 'vite';
import TSConfigPaths from 'vite-tsconfig-paths';
export default defineConfig(({ mode }) => {
if (!['lib', 'dev', 'test', 'e2e'].includes(mode)) {
throw new Error(`Expected --mode with value 'lib', 'dev', 'test' or 'e2e'`);
}
if (mode === 'lib') {
return libConfig();
}
return testConfig(mode);
});
/**
* Config for building the SDK bundles
*/
function libConfig(): UserConfig {
return {
plugins: [replaceOrigin()],
build: {
target: 'es2020',
outDir: 'bundles',
emptyOutDir: true,
sourcemap: true,
minify: 'esbuild',
lib: {
name: 'StackBlitzSDK',
entry: 'src/index.ts',
formats: ['cjs', 'es', 'umd'],
// Override the default file names used by Vite's “Library Mode”
// (https://vitejs.dev/guide/build.html#library-mode) for backwards
// compatibility with microbundle's output.
fileName: (format, _name) => {
let suffix: string = '';
if (format === 'es') suffix = '.m';
if (format === 'umd') suffix = '.umd';
return `sdk${suffix}.js`;
},
},
},
esbuild: {
// Whitespace will not be minified for our ESM build (sdk.m.js)
// because of this restriction in Vite's esbuild plugin:
// https://github.com/vitejs/vite/blob/2219427f4224d75f63a1e1a0af61b32c7854604e/packages/vite/src/node/plugins/esbuild.ts#L348-L351
// Instead of having mixed minified and unminified bundles, let’s ship
// unminified bundles because they’re small enough (especially gzipped).
minifyIdentifiers: false,
minifySyntax: false,
minifyWhitespace: false,
},
};
}
/**
* Config for running tests and/or building or serving examples
*/
function testConfig(mode: string): UserConfig {
const isE2E = mode === 'e2e';
const defaultOrigin = isE2E ? '/_embed/' : undefined;
return {
plugins: [
replaceOrigin(process.env.STACKBLITZ_SERVER_ORIGIN ?? defaultOrigin),
{
name: 'custom-server-config',
configureServer,
},
TSConfigPaths({
loose: true, // allow aliases in .html files
projects: [`${__dirname}/tsconfig.test.json`],
}),
],
build: {
target: 'es2020',
outDir: 'temp/build',
emptyOutDir: true,
rollupOptions: {
input: {
sdk: 'src/index.ts',
embed: 'test/embed/index.html',
examples: 'examples/index.html',
'example-github-project': 'examples/open-embed-github-project/index.html',
'example-project-id': 'examples/open-embed-project-id/index.html',
},
},
},
server: {
port: isE2E ? 4001 : 4000,
hmr: !isE2E,
},
test: {
dir: 'test',
globals: false,
include: ['**/unit/**/*.spec.ts'],
exclude: ['**/node_modules/**'],
coverage: {
provider: 'c8',
include: ['**/src/**/*.ts', '**/test/server/**/*.ts'],
exclude: ['**/node_modules/**'],
reportsDirectory: 'temp/coverage',
},
},
};
}
function replaceOrigin(origin?: string): Plugin {
return ReplacePlugin({
preventAssignment: true,
values: {
__STACKBLITZ_SERVER_ORIGIN__: JSON.stringify(origin),
},
});
}
/**
* Serve a custom HTML page for requests to '/_embed/*'
*/
function configureServer(server: ViteDevServer) {
// Parse URL-encoded form data in POST requests
server.middlewares.use(bodyParser.urlencoded({ extended: true }));
server.middlewares.use('/_embed', async (req, res, next) => {
const pathname = req.url && new URL(req.url, 'http://localhost/').pathname;
const ext = pathname && path.parse(pathname).ext;
if (ext === '' || ext === '.html') {
const content = await fs.readFile(`${__dirname}/test/embed/index.html`);
const html = content.toString().replace('{{PROJECT_DATA}}', getProjectDataString(req));
res.statusCode = 200;
res.end(html);
} else {
next();
}
});
}
function getProjectDataString(req: any): string {
if (req.method === 'POST' && req.body?.project) {
const project = {
...req.body.project,
};
if (typeof project.settings === 'string') {
project.settings = JSON.parse(project.settings);
}
return JSON.stringify(project, null, 2);
}
return 'null';
}