Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move unplugin and build using Civet instead of tsup #1306

Merged
merged 5 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions build/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@ set -euo pipefail

# clean build
rm -rf dist
mkdir dist

# tree-shake needed constants from Vite
node -e 'import("./node_modules/vite/dist/node/constants.js").then((c)=>console.log(`export const DEFAULT_EXTENSIONS = ${JSON.stringify(c.DEFAULT_EXTENSIONS)}`))' >./source/unplugin/constants.ts

# types (these get used for type checking during esbuild, so must go first)
cp types/types.d.ts types/config.d.ts dist/
cp types/config.d.ts dist/config.d.mts

# normal files
civet --no-config build/esbuild.civet "$@"

# built types
for name in astro esbuild rollup unplugin vite webpack; do
sed 's/\.civet"/\.js"/' dist/unplugin/unplugin/$name.civet.d.ts >dist/unplugin/$name.d.ts
done
rm -rf dist/unplugin/unplugin

# cli
BIN="dist/civet"
echo "#!/usr/bin/env node" | cat - dist/cli.js > "$BIN"
Expand All @@ -16,14 +30,6 @@ rm dist/cli.js
# babel plugin
cp source/babel-plugin.mjs dist/babel-plugin.mjs

# types
cp types/types.d.ts dist/types.d.ts

# unplugin
node -e 'import("./node_modules/vite/dist/node/constants.js").then((c)=>console.log(`export const DEFAULT_EXTENSIONS = ${JSON.stringify(c.DEFAULT_EXTENSIONS)}`))' >./integration/unplugin/src/constants.ts
yarn --cwd ./integration/unplugin build
cp ./integration/unplugin/dist/* ./dist

# create browser build for docs
terser dist/browser.js --compress --mangle --ecma 2015 --output civet.dev/public/__civet.js

76 changes: 65 additions & 11 deletions build/esbuild.civet
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ esbuild := require "esbuild"
heraPlugin := require "@danielx/hera/esbuild-plugin"
// Need to use the packaged version because we may not have built our own yet
civetPlugin := require "../node_modules/@danielx/civet/dist/esbuild-plugin.js"
civetUnplugin := require("../node_modules/@danielx/civet/dist/esbuild.js").default

path := require "path"
{access} := require "fs/promises"
Expand All @@ -22,11 +23,11 @@ exists := (p) ->
.catch ->
false

extensionResolverPlugin := (extensions) ->
extensionResolverPlugin := (extensions) => {
name: "extension-resolve"
setup: (build) ->
setup(build)
// For relative requires that don't contain a '.'
build.onResolve { filter: /\/[^.]*$/ }, (r) ->
build.onResolve { filter: /\/[^.]*$/ }, (r) =>
for extension of extensions
{path: resolvePath, resolveDir} = r
p := path.join(resolveDir, resolvePath + `.${extension}`)
Expand All @@ -37,6 +38,7 @@ extensionResolverPlugin := (extensions) ->
return path: p

return undefined
}

resolveExtensions := extensionResolverPlugin(["civet", "hera"])

Expand All @@ -45,17 +47,18 @@ resolveExtensions := extensionResolverPlugin(["civet", "hera"])
// set bundle: true, then rewrite .coffee -> .js and mark as external
// Also marking everything else as external since we don't want to bundle anything
rewriteCivetImports := {
name: 'rewrite-civet',
setup: (build) ->
build.onResolve { filter: /.*/ }, (args) ->
if (args.importer)
path: args.path.replace(/\.civet$/, ".js")
name: 'rewrite-civet'
setup(build)
ext := build.initialOptions.format is "esm" ? ".mjs" : ".js"
build.onResolve { filter: /.*/ }, (args) =>
if args.importer and /\.civet$/.test args.path
path: args.path.replace(/\.civet$/, ext)
external: true
}

// Files that need civet imports re-written
// since they aren't actually bundled
["cli", "config", "esbuild-plugin"].forEach (name) ->
for name of ["cli", "esbuild-plugin"]
build({
entryPoints: [`source/${name}.civet`]
bundle: true
Expand All @@ -66,14 +69,37 @@ rewriteCivetImports := {
rewriteCivetImports
civetPlugin()
]
external: [
'../package.json'
'../register.js'
]
footer:
// Rewrite default export as CJS exports object,
// so require('esbuild-plugin') gives the plugin.
js: name is 'esbuild-plugin' ? 'module.exports = module.exports.default;' : ''
}).catch -> process.exit 1

for name of ["config"]
for format of ["esm", "cjs"]
build({
entryPoints: [`source/${name}.civet`]
bundle: true
platform: 'node'
format
outdir: 'dist'
outExtension: ".js": if format == "esm" then ".mjs" else ".js"
plugins: [
rewriteCivetImports
civetPlugin()
]
footer:
// Rewrite default export as CJS exports object,
// so require('esbuild-plugin') gives the plugin.
js: name is 'esbuild-plugin' ? 'module.exports = module.exports.default;' : ''
}).catch -> process.exit 1

// esm needs to be a module for import.meta
["esm"].forEach (name) ->
for name of ["esm"]
build({
entryPoints: [`source/${name}.civet`]
bundle: true
Expand Down Expand Up @@ -133,6 +159,34 @@ build({
]
}).catch -> process.exit 1

for format of ["esm", "cjs"]
build({
entryPoints:
for name of [ 'unplugin', 'webpack', 'vite', 'rollup', 'esbuild', 'astro' ]
`source/unplugin/${name}.civet`
bundle: true
sourcemap
minify
platform: 'node'
format
external: [
'@danielx/civet'
'typescript'
'@typescript/vfs'
'unplugin'
'esbuild'
'./unplugin.civet'
]
target: "esNext"
outdir: 'dist/unplugin'
outExtension: ".js": if format == "esm" then ".mjs" else ".js"
plugins: [
rewriteCivetImports
civetUnplugin
ts: 'civet'
emitDeclaration: format == "esm" // only run TypeScript once
]
}).catch -> process.exit 1

for format of ["esm", "cjs"]
build({
Expand All @@ -144,7 +198,7 @@ for format of ["esm", "cjs"]
format
target: "esNext"
outdir: 'dist'
outExtension: { ".js": if format == "esm" then ".mjs" else ".js" }
outExtension: ".js": if format == "esm" then ".mjs" else ".js"
plugins: [
civetPlugin({ emitDeclaration: true })
]
Expand Down
4 changes: 2 additions & 2 deletions civet.dev/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ For example, the [directive](#local-configuration-via-directives)

## Global Configuration within Build Tools

The [unplugin](https://github.com/DanielXMoore/Civet/blob/main/integration/unplugin)
offers [options](https://github.com/DanielXMoore/Civet/tree/main/integration/unplugin#options)
The [unplugin](https://github.com/DanielXMoore/Civet/blob/main/source/unplugin)
offers [options](https://github.com/DanielXMoore/Civet/tree/main/source/unplugin#options)
to specify global config directives (overriding even config files) and
to specify or disable a config file. For example:

Expand Down
6 changes: 3 additions & 3 deletions civet.dev/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ civet --emit-declaration src/**/*.civet

## Building a project

Use Civet's built-in [unplugin](https://github.com/DanielXMoore/Civet/blob/main/integration/unplugin) to integrate with many
Use Civet's built-in [unplugin](https://github.com/DanielXMoore/Civet/blob/main/source/unplugin) to integrate with many
bundlers: Vite, esbuild, Astro, Rollup, Webpack, or Rspack. For example:

```js
Expand Down Expand Up @@ -221,8 +221,8 @@ You can mix and match `.civet` files with `.js` and `.ts` files.
Even `.coffee` will work if you require `coffeescript/register` or add a loader for it.
:::

See the [unplugin documentation](https://github.com/DanielXMoore/Civet/blob/main/integration/unplugin) for more configurations,
including [full working examples](https://github.com/DanielXMoore/Civet/blob/main/integration/unplugin/examples).
See the [unplugin documentation](https://github.com/DanielXMoore/Civet/blob/main/source/unplugin) for more configurations,
including [full working examples](https://github.com/DanielXMoore/Civet/blob/main/integration/unplugin-examples).

These plugins should support metaframeworks built upon these bundlers.
For example, the esbuild unplugin supports [tsup](https://github.com/egoist/tsup):
Expand Down
4 changes: 2 additions & 2 deletions civet.dev/integrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ title: Integrations

## Build tools

- [unplugin](https://github.com/DanielXMoore/Civet/blob/main/integration/unplugin) integrates Civet into Vite, esbuild, Astro, Rollup, Webpack, and Rspack, including `.d.ts` generation (see [basic instructions](https://civet.dev/getting-started#building-a-project))
- [unplugin](https://github.com/DanielXMoore/Civet/blob/main/source/unplugin) integrates Civet into Vite, esbuild, Astro, Rollup, Webpack, and Rspack, including `.d.ts` generation (see [basic instructions](https://civet.dev/getting-started#building-a-project))
- [Simpler esbuild plugin](https://github.com/DanielXMoore/Civet/blob/main/source/esbuild-plugin.civet)
- [Older Vite plugin](https://github.com/edemaine/vite-plugin-civet) (no longer recommended)
- [ESM/CJS loader](https://github.com/DanielXMoore/Civet/blob/main/register.js) for `import`/`require` to support `.civet` files
Expand All @@ -21,7 +21,7 @@ title: Integrations

## Starter Templates

- [Astro, Rollup, Vite, and Webpack](https://github.com/DanielXMoore/Civet/blob/main/integration/unplugin/examples)
- [Astro, esbuild, NextJS, Rollup, Vite, and Webpack](https://github.com/DanielXMoore/Civet/blob/main/integration/unplugin-examples)
- [Solid](https://github.com/orenelbaum/solid-civet-template)
- [SolidStart](https://github.com/orenelbaum/solid-start-civet-template)

Expand Down
2 changes: 1 addition & 1 deletion civet.dev/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1697,7 +1697,7 @@ is not enabled by default, nor can it be enabled via a directive or config file.
In particular, the VSCode language server will not execute `comptime` blocks.
You can enable `comptime` evaluation in the CLI using `civet --comptime`,
and in the
[unplugin](https://github.com/DanielXMoore/Civet/tree/main/integration/unplugin)
[unplugin](https://github.com/DanielXMoore/Civet/tree/main/source/unplugin)
using the `comptime: true` option.
If not enabled, `comptime` blocks will execute at runtime.

Expand Down
2 changes: 1 addition & 1 deletion integration/example/build.civet
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is used in test/integration.civet to test the esbuild plugin
esbuild from esbuild

civetPlugin from ../../dist/esbuild.mjs
civetPlugin from ../../dist/unplugin/esbuild.mjs

esbuild.build {
+sourcemap
Expand Down
7 changes: 0 additions & 7 deletions integration/unplugin/package.json

This file was deleted.

24 changes: 0 additions & 24 deletions integration/unplugin/src/astro.ts

This file was deleted.

3 changes: 0 additions & 3 deletions integration/unplugin/src/esbuild.ts

This file was deleted.

3 changes: 0 additions & 3 deletions integration/unplugin/src/rollup.ts

This file was deleted.

3 changes: 0 additions & 3 deletions integration/unplugin/src/vite.ts

This file was deleted.

3 changes: 0 additions & 3 deletions integration/unplugin/src/webpack.ts

This file was deleted.

27 changes: 0 additions & 27 deletions integration/unplugin/tsup.config.ts

This file was deleted.

Loading
Loading