vite#build TypeScript Examples
The following examples show how to use
vite#build.
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: index.ts From vite-ssr with MIT License | 6 votes |
async function generatePackageJson(
viteConfig: ResolvedConfig,
clientBuildOptions: InlineConfig,
serverBuildOptions: NonNullable<BuildOptions['serverOptions']>
) {
if (serverBuildOptions.packageJson === false) return
const outputFile = (
serverBuildOptions.build?.rollupOptions?.output as OutputOptions
)?.file
const ssrOutput = path.parse(
outputFile ||
((viteConfig.build?.ssr || serverBuildOptions.build?.ssr) as string)
)
const moduleFormat =
(viteConfig.build?.rollupOptions?.output as OutputOptions)?.format ||
(serverBuildOptions.build?.rollupOptions?.output as OutputOptions)?.format
const packageJson = {
main: outputFile ? ssrOutput.base : ssrOutput.name + '.js',
type: /^esm?$/i.test(moduleFormat || '') ? 'module' : 'commonjs',
ssr: {
// This can be used later to serve static assets
assets: (
await fs.readdir(clientBuildOptions.build?.outDir as string)
).filter((file) => !/(index\.html|manifest\.json)$/i.test(file)),
},
...(serverBuildOptions.packageJson || {}),
}
await fs.writeFile(
path.join(serverBuildOptions.build?.outDir as string, 'package.json'),
JSON.stringify(packageJson, null, 2),
'utf-8'
)
}
Example #2
Source File: utils.ts From vite-plugin-vue-i18n with MIT License | 6 votes |
async function bundle(fixture: string, options: Record<string, unknown> = {}) {
const input = (options.input as string) || './fixtures/entry.ts'
const target = (options.target as string) || './fixtures'
const include = (options.include as string[]) || [
path.resolve(__dirname, './fixtures/**')
]
const silent = isBoolean(options.silent)
? options.silent === false
? 'info'
: 'silent'
: 'silent'
const defaultSFCLang = isString(options.defaultSFCLang)
? options.defaultSFCLang
: undefined
const globalSFCScope = isBoolean(options.globalSFCScope)
? options.globalSFCScope
: undefined
const alias: Record<string, string> = {
vue: 'vue/dist/vue.runtime.esm-browser.js'
}
if (!fixture.startsWith('@')) {
alias['~target'] = path.resolve(__dirname, target, fixture)
}
// @ts-ignore
const plugins = [vue(), vueI18n({ include, defaultSFCLang, globalSFCScope })]
if (options.intlify) {
const intlifyVue = (await import('../src/injection')).default
plugins.push(intlifyVue(options.intlify as InjectionValues))
}
const result = await build({
logLevel: silent,
resolve: {
alias
},
plugins,
build: {
write: false,
minify: false,
rollupOptions: {
input: path.resolve(__dirname, input)
}
}
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return { code: (result as any).output[0].code }
}
Example #3
Source File: build-vue.ts From image-optimizer with MIT License | 5 votes |
export default function () {
return build({
base: './',
...config,
mode: 'production'
})
}
Example #4
Source File: compile.ts From fect with MIT License | 5 votes |
umdTask = async (input: string, name: string) => {
setBabelEnv('esmodule')
input = path.join(input, 'index.ts')
await Promise.all([build(useUMDconfig(input, name, true)), build(useUMDconfig(input, name))])
}
Example #5
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,
};
}