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

feat: support plugin of unocss #6665

Merged
merged 7 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 examples/with-unocss/ice.config.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from '@ice/app';
import Unocss from '@ice/plugin-unocss';

export default defineConfig(() => ({
plugins: [
Unocss(),
ClarkXia marked this conversation as resolved.
Show resolved Hide resolved
]
}));
20 changes: 20 additions & 0 deletions examples/with-unocss/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@examples/with-unocss",
"private": true,
"version": "1.0.0",
"scripts": {
"start": "ice start",
"build": "ice build"
},
"dependencies": {
"@ice/runtime": "workspace:*",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
"devDependencies": {
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@ice/app": "workspace:*",
"@ice/plugin-unocss": "workspace:*"
}
}
3 changes: 3 additions & 0 deletions examples/with-unocss/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineAppConfig } from 'ice';

export default defineAppConfig(() => ({}));
22 changes: 22 additions & 0 deletions examples/with-unocss/src/document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Meta, Title, Links, Main, Scripts } from 'ice';

function Document() {
return (
<html>
<head>
<meta charSet="utf-8" />
<meta name="description" content="with-web-worker" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Title />
<Links />
</head>
<body>
<Main />
<Scripts />
</body>
</html>
);
}

export default Document;
7 changes: 7 additions & 0 deletions examples/with-unocss/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home() {
return (
<h1 className="text-3xl font-bold underline w-6em h-6em">
Hello world!
</h1>
);
}
76 changes: 76 additions & 0 deletions packages/plugin-unocss/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# @ice/plugin-unocss

A plugin for enable unocss in your app based on `@ice/app`.


## Usage

Install `@ice/plugin-unocss`:

```bash
$ npm install @ice/plugin-unocss --save-dev
```

Configure it in `ice.config.mts`:

```ts
import { defineConfig } from '@ice/app';
import Unocss from '@ice/plugin-unocss';

export default defineConfig(() => ({
plugins: [
Unocss(),
]
}));
```

## Plugin Options

Plugin options is as same as [UnoCSS ConfigFle](https://unocss.dev/guide/config-file).

Plugin has a default preset `@unocss/preset-uno` for UnoCSS. You can pass options of presets to override the default preset:

```ts
import { defineConfig } from '@ice/app';
import Unocss from '@ice/plugin-unocss';
import {
defineConfig,
presetAttributify,
presetIcons,
presetTypography,
presetUno,
presetWebFonts,
transformerDirectives,
transformerVariantGroup
} from 'unocss';

export default defineConfig(() => ({
plugins: [
Unocss({
shortcuts: [
// ...
],
theme: {
colors: {
// ...
}
},
presets: [
presetUno(),
presetAttributify(),
presetIcons(),
presetTypography(),
presetWebFonts({
fonts: {
// ...
},
}),
],
transformers: [
transformerDirectives(),
transformerVariantGroup(),
],
}),
],
}));
```
43 changes: 43 additions & 0 deletions packages/plugin-unocss/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@ice/plugin-unocss",
"version": "1.0.0",
"description": "",
ClarkXia marked this conversation as resolved.
Show resolved Hide resolved
"license": "MIT",
"type": "module",
"exports": {
".": {
"types": "./esm/index.d.ts",
"import": "./esm/index.js",
"default": "./esm/index.js"
}
},
"main": "./esm/index.js",
"types": "./esm/index.d.ts",
"files": [
"esm",
"!esm/**/*.map",
"*.d.ts"
],
"dependencies": {
"@unocss/config": "^0.57.6",
"@unocss/core": "^0.57.6",
"@unocss/reset": "^0.57.6",
"@unocss/webpack": "^0.57.6",
"unocss": "^0.57.6"
},
"devDependencies": {
"@ice/app": "workspace:^",
"@nuxt/schema": "^3.8.1"
},
"repository": {
"type": "http",
"url": "https://github.com/alibaba/ice/tree/master/packages/plugin-unocss"
},
"scripts": {
"watch": "tsc -w --sourceMap",
"build": "tsc"
},
"publishConfig": {
"access": "public"
}
}
34 changes: 34 additions & 0 deletions packages/plugin-unocss/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Plugin } from '@ice/app/types';
import UnocssWebpackPlugin from '@unocss/webpack';
import type { UserConfig } from '@unocss/core';
import { presetUno } from 'unocss';

const PLUGIN_NAME = '@ice/plugin-unocss';

const plugin: Plugin<UserConfig> = (options) => ({
name: PLUGIN_NAME,
setup: ({ generator, onGetConfig }) => {
// Import uno.css in entry, when uno mode is global or dist-chunk.
generator.addEntryImportAhead({
source: 'uno.css',
});

// Register webpack plugin of unocss.
const unoConfig: UserConfig = options || {
presets: [
// Add default preset if option is null.
presetUno(),
],
};
onGetConfig((config) => {
config.configureWebpack ??= [];
config.configureWebpack.push((webpackConfig) => {
// @ts-expect-error
webpackConfig.plugins.push(UnocssWebpackPlugin({}, unoConfig));
return webpackConfig;
});
});
},
});

export default plugin;
9 changes: 9 additions & 0 deletions packages/plugin-unocss/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"baseUrl": "./",
"rootDir": "src",
"outDir": "esm"
},
"include": ["src"]
}
Loading
Loading