Use Civet in your projects bundled with Vite, Webpack, Rspack, Rollup, or esbuild, with .d.ts generation support.
The only setup required is adding the plugin to your bundler's config. Jump to:
You probably also want to pass in options.
// vite.config.ts
import { defineConfig } from 'vite'
import civetVitePlugin from '@danielx/civet/vite'
export default defineConfig({
// ...
plugins: [
civetVitePlugin({
// options
}),
],
})To use Civet files as Web Workers, you can use
Vite's ?worker query suffix:
import MyWorker from './worker.civet?worker'If you use
Vite's constructor syntax,
you need to add a .tsx extension like so:
worker = new Worker(new URL('./worker.civet.tsx', import.meta.url))
//or
worker = new Worker(new URL('./worker.civet.tsx', import.meta.url), { type: 'module' })You'll also need to pass the civetVitePlugin via the
worker.plugins option:
// vite.config.ts
import { defineConfig } from 'vite'
import civetVitePlugin from '@danielx/civet/vite'
export default defineConfig({
// ...
plugins: [
civetVitePlugin({
// options
}),
],
worker: {
plugins: () => [
civetVitePlugin({
// options
}),
],
// format: "es", // if using { type: 'module' }
},
})import esbuild from 'esbuild'
import civetEsbuildPlugin from '@danielx/civet/esbuild'
esbuild
.build({
// ...
// sourcemap: true, // build and link sourcemap files
plugins: [civetEsbuildPlugin()],
})
.catch(() => process.exit(1));// astro.config.ts
import { defineConfig } from 'astro/config'
import civet from '@danielx/civet/astro'
// https://astro.build/config
export default defineConfig({
// ...
integrations: [
civet({
// options
}),
],
})To use Civet files as Web Workers, see the Vite directions above.
You'll also need to import and pass the Civet Vite plugin via the
vite.worker.plugins option:
// astro.config.ts
import { defineConfig } from 'astro/config'
import civet from '@danielx/civet/astro'
import civetVitePlugin from '@danielx/civet/vite'
// https://astro.build/config
export default defineConfig({
// ...
integrations: [
civet({
// options
}),
],
vite: {
worker: {
plugins: () => [
civetVitePlugin({
// options
}),
],
// format: "es", // if using { type: 'module' }
},
},
})// farm.config.ts
import { defineConfig } from '@farmfe/core'
import civetFarmPlugin from '@danielx/civet/farm'
export default defineConfig({
// ...
plugins: [
civetFarmPlugin({
// options
})
],
})// rolldown.config.ts
import { defineConfig } from 'rolldown'
import civetRolldownPlugin from '@danielx/civet/rolldown'
export default defineConfig({
// ...
plugins: [
civetRolldownPlugin({
// options
}),
],
})// rollup.config.ts
import civetRollupPlugin from '@danielx/civet/rollup';
export default {
// ...
plugins: [
civetRollupPlugin({
// options
}),
],
}const civetWebpackPlugin = require('@danielx/civet/webpack').default;
module.exports = {
// ...
plugins: [
civetWebpackPlugin({
// options
}),
],
};interface PluginOptions {
emitDeclaration?: boolean
declarationExtesion?: string
implicitExtension?: boolean
outputExtension?: string
ts?: 'civet' | 'esbuild' | 'tsc' | 'preserve'
typecheck?: boolean | string
cache?: boolean
config?: string | false | null
parseOptions?: {
comptime?: boolean
coffeeCompat?: boolean
// ... any other Civet configuration option
}
transformOutput?: (
code: string,
id: string
) => TransformResult | Promise<TransformResult>
}emitDeclaration: Whether to generate.d.tstype definition files from the Civet source, which is useful for building libraries. (Requires installingtypescript.) Default:false.declarationExtension: Output filename extension for.d.tsfiles. Default:".civet.d.ts".typecheck: Whether to run type checking on the project. (Requires installingtypescript.) Default:false.- Specifying
trueaborts the build (with an error code) on TypeScript errors. - Alternatively, you can specify a string with any combination of
error,warning,suggestion, ormessageto specify which diagnostics abort the build. For example,"none"ignores all diagnostics,"error+warning"aborts on errors and warnings, and"all"aborts on all diagnostics. typecheckworks in mixed projects too: it will check.tsand.civetfiles, according to yourtsconfig.json'sinclude/excludepatterns.
- Specifying
implicitExtension: Whether to allow importingfilename.civetviaimport "filename". Default:true.outputExtension: JavaScript or TypeScript extension to append to.civetfor internal purposes. Default:".jsx", or".tsx"iftsis"preserve".ts: Mode for transpiling TypeScript features into JavaScript. Default:"civet". Options:"civet": Use Civet's JS mode. (Not all TS features supported.)"esbuild": Use esbuild's transpiler. (Fast and more complete. Requires installingesbuild.)"tsc": Use the TypeScript compiler. (Slow but complete. Requires installingtypescript.)"preserve": Don't transpile TS code. Some bundlers, like esbuild and Vite, can handle TS directly. Also useful when usingtransformOutputto handle TS code, or using a plugin that modifies TS AST. Note that some bundlers require additional plugins to handle TS. For example, for Webpack, you would need to installts-loaderand add it to your webpack config. Unfortunately, Rollup's TypeScript plugin is incompatible with this plugin, so you need to settsto another option.
cache: Cache compilation results based on file's mtime. Useful when bundling the same source files for both CommonJS and ESM, or for longer running processes likewatchorserve. Default:true. Be sure to re-use plugins instead of calling the plugin generator repeatedly.threads: Use specified number of Node worker threads to compile Civet files faster. Default:0(don't use threads), orCIVET_THREADSenvironment variable if set.config: Civet config filename to load, orfalse/nullto avoid looking for the default config filenames in the project root directory. See Civet config.parseOptions: Options object to pass to the Civet parser, like adding"civet"directives to all files. Default:{}. These options override any options specified in a config file. Notably, unlike config files, you can specify the following option:comptime: Whether to evaluatecomptimeblocks at compile time. Default:false.
transformOutput(code, id): Adds a custom transformer over jsx/tsx code produced bycivet.compile. It gets passed the jsx/tsx source (code) and filename (id), and should return valid jsx/tsx code.
See also full examples of unplugin in Astro, esbuild, NextJS, Farm, Rolldown, Rollup, Vite, and Webpack.