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

Add civet unplugin #632

Merged
merged 15 commits into from
Aug 31, 2023
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
8 changes: 8 additions & 0 deletions build/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/bash
set -euo pipefail

# clean build
rm -rf dist

# normal files
civet --no-config build/esbuild.civet

Expand All @@ -16,5 +19,10 @@ cp source/babel-plugin.mjs dist/babel-plugin.mjs
# types
cp types/types.d.ts dist/types.d.ts

# unplugin
yarn --cwd ./integration/unplugin build
cp ./integration/unplugin/dist/* ./dist

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Windows, I realize chmod a-x dist/*.*[jt]s would be helpful.

Also, what is this new dist/chunk-UZRN3RFA.mjs file? Presumably it's not desired?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the esm build, tsup splits the common chunks into a separate file by default. As all the bundler entrypoints /esbuild, /rollup etc. import the central civetUnplugin it splits that into this chunk and imports it in them. Mostly helps keep the bundle size smaller as its not duplicated

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that is nice. Can we choose a better name for that file? Given that there's only one, maybe a custom name rule would work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can choose a better name for it https://esbuild.github.io/api/#chunk-names, but i'm not sure if we should. There currently only exists one of these, but might be more than that in the future. I would suggest keeping it as is, or just turning off splitting

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to rename it for now; we can always change it if we need multiple chunks in the future (seems unlikely for the unplugin?). Maybe unplugin-shared?

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

88 changes: 88 additions & 0 deletions integration/unplugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# unplugin-civet

Use Civet in your projects with Vite, Webpack, Rspack, Rollup and esbuild, with `dts` generation supported.

## Usage

The only setup required is adding it your bundler's config:

### Vite

```ts
// vite.config.ts
import { defineConfig } from 'vite';
import civetVitePlugin from '@danielx/civet/vite';

export default defineConfig({
// ...
plugins: [
civetVitePlugin({
// options
}),
],
});
```

### Rollup

```ts
// rollup.config.ts
import civetRollupPlugin from '@danielx/civet/rollup';

export default {
// ...
plugins: [
civetRollupPlugin({
// options
}),
],
};
```

### ESBuild

```ts
import esbuild from 'esbuild';
import civetEsbuildPlugin from '@danielx/civet/esbuild';

esbuild
.build({
// ...
plugins: [civetEsbuildPlugin()],
})
.catch(() => process.exit(1));
```

### Webpack

```js
const civetWebpackPlugin = require('@danielx/civet/webpack');

module.exports = {
// ...
plugins: [
civetWebpackPlugin({
// options
}),
],
};
```

## Options

```ts
interface PluginOptions {
dts?: boolean;
outputExtension?: string;
js?: boolean;
transformOutput?: (
code: string,
id: string
) => TransformResult | Promise<TransformResult>;
}
```

- `dts`: `unplugin-civet` also supports generating `.d.ts` type definition files from the civet source, which is useful for building libraries. Default: `false`.
- `outputExtension`: Output filename extension to use. Default: `.civet.tsx`, or uses `.civet.jsx` if `js` is `true`.
- `js`: Whether to transpile to JS or TS. Default: `false`.
- `transformOutput(code, id)`: Adds a custom transformer over jsx/tsx code produced by `civet.compile`. It gets passed the jsx/tsx source (`code`) and filename (`id`), and should return valid jsx/tsx code.
21 changes: 21 additions & 0 deletions integration/unplugin/examples/astro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# build output
dist/
# generated types
.astro/

# dependencies
node_modules/

# logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*


# environment variables
.env
.env.production

# macOS-specific files
.DS_Store
4 changes: 4 additions & 0 deletions integration/unplugin/examples/astro/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"recommendations": ["astro-build.astro-vscode"],
"unwantedRecommendations": []
}
11 changes: 11 additions & 0 deletions integration/unplugin/examples/astro/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "./node_modules/.bin/astro dev",
"name": "Development server",
"request": "launch",
"type": "node-terminal"
}
]
}
47 changes: 47 additions & 0 deletions integration/unplugin/examples/astro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Astro Starter Kit: Minimal

```
npm create astro@latest -- --template minimal
```

[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/withastro/astro/tree/latest/examples/minimal)
[![Open with CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/withastro/astro/tree/latest/examples/minimal)
[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/withastro/astro?devcontainer_path=.devcontainer/minimal/devcontainer.json)

> 🧑‍🚀 **Seasoned astronaut?** Delete this file. Have fun!

## 🚀 Project Structure

Inside of your Astro project, you'll see the following folders and files:

```
/
├── public/
├── src/
│ └── pages/
│ └── index.astro
└── package.json
```

Astro looks for `.astro` or `.md` files in the `src/pages/` directory. Each page is exposed as a route based on its file name.

There's nothing special about `src/components/`, but that's where we like to put any Astro/React/Vue/Svelte/Preact components.

Any static assets, like images, can be placed in the `public/` directory.

## 🧞 Commands

All commands are run from the root of the project, from a terminal:

| Command | Action |
| :------------------------ | :----------------------------------------------- |
| `npm install` | Installs dependencies |
| `npm run dev` | Starts local dev server at `localhost:3000` |
| `npm run build` | Build your production site to `./dist/` |
| `npm run preview` | Preview your build locally, before deploying |
| `npm run astro ...` | Run CLI commands like `astro add`, `astro check` |
| `npm run astro -- --help` | Get help using the Astro CLI |

## 👀 Want to learn more?

Feel free to check [our documentation](https://docs.astro.build) or jump into our [Discord server](https://astro.build/chat).
12 changes: 12 additions & 0 deletions integration/unplugin/examples/astro/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { defineConfig } from 'astro/config';
import civetVitePlugin from '@danielx/civet/vite';

import solidJs from '@astrojs/solid-js';

// https://astro.build/config
export default defineConfig({
vite: {
plugins: [civetVitePlugin({})],
},
integrations: [solidJs()],
});
20 changes: 20 additions & 0 deletions integration/unplugin/examples/astro/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "astro-example",
"type": "module",
"version": "0.0.1",
"scripts": {
"dev": "astro dev",
"start": "astro dev",
"build": "astro build",
"preview": "astro preview",
"astro": "astro"
},
"dependencies": {
"@astrojs/solid-js": "^2.2.0",
"astro": "^2.10.7",
"solid-js": "^1.7.11"
},
"devDependencies": {
"@danielx/civet": "*"
}
}
9 changes: 9 additions & 0 deletions integration/unplugin/examples/astro/public/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions integration/unplugin/examples/astro/src/component.civet
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{createSignal} from 'solid-js'


export default function Counter()
[count, setCount] := createSignal 0

console.log "rendering"

<button onClick={=>setCount(count() + 1)}>
{count()}
1 change: 1 addition & 0 deletions integration/unplugin/examples/astro/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="astro/client" />
17 changes: 17 additions & 0 deletions integration/unplugin/examples/astro/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
import Counter from '../component.civet';
---

<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Astro</title>
</head>
<body>
<h1>Astro</h1>
<Counter client:load />
</body>
</html>
7 changes: 7 additions & 0 deletions integration/unplugin/examples/astro/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "astro/tsconfigs/base",
"compilerOptions": {
"jsx": "preserve",
"jsxImportSource": "solid-js"
}
}
5 changes: 5 additions & 0 deletions integration/unplugin/examples/rollup/main.civet
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{a} from "./utils/module.civet"

export b = a

export * from "./utils/module.civet"
8 changes: 8 additions & 0 deletions integration/unplugin/examples/rollup/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scripts": {
"build": "npx rollup --config rollup.config.js"
},
"devDependencies": {
"@danielx/civet": "*"
}
edemaine marked this conversation as resolved.
Show resolved Hide resolved
}
14 changes: 14 additions & 0 deletions integration/unplugin/examples/rollup/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import civetRollupPlugin from '@danielx/civet/rollup';

export default {
input: 'main.civet',
output: {
dir: 'dist',
format: 'cjs',
},
plugins: [
civetRollupPlugin({
dts: true,
}),
],
};
18 changes: 18 additions & 0 deletions integration/unplugin/examples/rollup/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"baseUrl": ".",
"isolatedModules": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "Node",
"noEmit": false,
"checkJs": false,
"declarationMap": false,
"skipLibCheck": true,
"target": "ESNext",
"allowJs": true,
"jsx": "preserve",
"emitDeclarationOnly": true,
"declaration": true
}
}
1 change: 1 addition & 0 deletions integration/unplugin/examples/rollup/utils/module.civet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export a = "a"
13 changes: 13 additions & 0 deletions integration/unplugin/examples/vite-lib/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Hello world</h1>
<script src="./src/main.civet" type="module"></script>
</body>
</html>
8 changes: 8 additions & 0 deletions integration/unplugin/examples/vite-lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"scripts": {
"build": "npx vite build"
},
"devDependencies": {
"@danielx/civet": "*"
}
}
3 changes: 3 additions & 0 deletions integration/unplugin/examples/vite-lib/src/main.civet
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{a} from "./module.civet"

console.log a
1 change: 1 addition & 0 deletions integration/unplugin/examples/vite-lib/src/module.civet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export a = "a"
18 changes: 18 additions & 0 deletions integration/unplugin/examples/vite-lib/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"baseUrl": ".",
"isolatedModules": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "Node",
"noEmit": false,
"checkJs": false,
"declarationMap": false,
"skipLibCheck": true,
"target": "ESNext",
"allowJs": true,
"jsx": "preserve",
"emitDeclarationOnly": true,
"declaration": true
}
}
13 changes: 13 additions & 0 deletions integration/unplugin/examples/vite-lib/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import civetVitePlugin from '@danielx/civet/vite';
import { defineConfig } from 'vite';

export default defineConfig({
build: {
lib: {
entry: './src/main.civet',
fileName: 'main',
formats: ['cjs', 'es'],
},
},
plugins: [civetVitePlugin({ dts: true })],
});
Loading