vite#createServer TypeScript Examples
The following examples show how to use
vite#createServer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: vite.ts From reskript with MIT License | 6 votes |
start = async (cmd: DevCommandLineArgs, serverContext: ServerStartContext) => {
const {buildContext, host, publicPath} = serverContext;
const {port, https} = buildContext.projectSettings.devServer;
const configOptions: ViteOptions = {
clean: true,
sourceMaps: true,
publicPath: publicPath ?? '/',
proxyDomain: cmd.proxyDomain,
defaultEntry: cmd.entry,
};
const config = await createViteConfig(buildContext, configOptions);
const server = await createServer({...config, configFile: false});
if (buildContext.projectSettings.from) {
server.watcher.add(buildContext.projectSettings.from);
}
await server.listen();
const protocol = https?.client ? 'https' : 'http';
const url = `${protocol}://${host}:${port}/${buildContext.projectSettings.devServer.openPage}`;
logger.infoHighlight(`Your application is running here: ${url}`);
if (cmd.open) {
open(url);
}
return () => server.close();
}
Example #2
Source File: generate.ts From vue-i18n-next with MIT License | 6 votes |
(async (locales: readonly Locale[]) => {
// start vite server
const vite = await createServer({ root: process.cwd() })
await vite.listen(3000)
vite.printUrls()
// compile reousrces
for (const locale of locales) {
const resource = await $fetch<ResourceSchema>(
`http://localhost:3000/api/resources/${locale}`
)
const { code, map, filename } = await compile(locale, resource)
await fs.writeFile(filename, code)
}
process.on('uncaughtException', err => {
console.error(`uncaught exception: ${err}\n`)
process.exit(1)
})
process.on('unhandledRejection', (reason, p) => {
console.error('unhandled rejection at:', p, 'reason:', reason)
process.exit(1)
})
process.exit(0)
})(locales)
Example #3
Source File: vite.ts From vanilla-extract with MIT License | 6 votes |
serveAssets = ({ port, dir }: { port: number; dir: string }) =>
new Promise<() => Promise<void>>((resolve) => {
const server = http.createServer((request, response) => {
return handler(request, response, {
public: dir,
});
});
server.listen(port, () => {
resolve(
() =>
new Promise<void>((closeRes) => {
server.close(() => closeRes());
}),
);
});
})
Example #4
Source File: dev-server.ts From image-optimizer with MIT License | 5 votes |
async function startRenderer () {
const server = await createServer({
...viteConfig,
mode: 'development'
})
return await server.listen()
}
Example #5
Source File: index.ts From reskript with MIT License | 5 votes |
run = async (buildContext: BuildContext, cmd: PlayCommandLineArgs, target: string) => {
const entryModified: BuildContext = {
...buildContext,
entries: buildContext.entries.map(v => ({...v, file: '/playground-entry.jsx'})),
};
const options: ViteOptions = {
clean: false,
sourceMaps: true,
proxyDomain: buildContext.projectSettings.devServer.defaultProxyDomain,
defaultEntry: 'index',
publicPath: '/',
};
const config = await createViteConfig(entryModified, options);
const pluginOptions = {
cwd: cmd.cwd,
componentTypeName: resolveComponentName(target),
componentModulePath: path.resolve(buildContext.cwd, target),
globalSetupModulePath: cmd.setup
? path.resolve(cmd.cwd, cmd.setup)
: buildContext.projectSettings.play.defaultGlobalSetup,
enableConcurrentMode: cmd.concurrentMode ?? buildContext.projectSettings.play.defaultEnableConcurrentMode,
};
const socketProxyEnabeld: UserConfig = {
...config,
server: {
...config.server,
proxy: {
...config.server?.proxy,
'/io-play': {
target: 'http://localhost:9998/io-play',
ws: true,
},
},
},
plugins: [
playEntryPlugin(pluginOptions),
...config.plugins ?? [],
],
};
const server = await createServer(socketProxyEnabeld);
await server.listen();
const protocol = buildContext.projectSettings.devServer.https?.client ? 'https' : 'http';
const url = `${protocol}://${cmd.host}:${cmd.port}/${buildContext.projectSettings.devServer.openPage}`;
logger.infoHighlight(`Your application is running here: ${url}`);
}
Example #6
Source File: dev.ts From fect with MIT License | 5 votes |
dev = async () => {
const conf = await useDevConfig()
const serve = await createServer(conf)
await serve.listen()
serve.printUrls()
}
Example #7
Source File: vite.ts From vanilla-extract with MIT License | 5 votes |
startViteFixture = async (
fixtureName: string,
{ mode = 'development', port = 3000 }: ViteFixtureOptions,
): Promise<TestServer> => {
const root = path.dirname(
require.resolve(`@fixtures/${fixtureName}/package.json`),
);
const config: InlineConfig = {
configFile: false,
root,
logLevel: 'error',
plugins: [vanillaExtractPlugin()],
server: {
port,
force: true,
},
build: {
cssCodeSplit: false,
},
};
if (mode === 'development') {
const server = await createServer(config);
await server.listen();
return {
type: 'vite',
url: `http://localhost:${port}`,
close: () => {
return server.close();
},
};
}
const result = await build(config);
const closeServer = await serveAssets({ port, dir: path.join(root, 'dist') });
if (!('output' in result)) {
throw new Error('Unexpected build output');
}
const { fileName: stylesheet } =
result.output.find((asset) => asset.name?.endsWith('.css')) || {};
return {
type: 'vite',
url: `http://localhost:${port}`,
stylesheet,
close: closeServer,
};
}