Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vite, webpack): add
extraContent
option (#1963)
* feat(vite): add `watchExternal` option * chore: lint * refactor: api Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
22 changed files
with
323 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# build output | ||
dist/ | ||
.output/ | ||
|
||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineConfig } from 'astro/config' | ||
import UnoCSS from 'unocss/astro' | ||
import vue from '@astrojs/vue' | ||
|
||
export default defineConfig({ | ||
integrations: [ | ||
vue(), | ||
UnoCSS(), | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "unocss-astro", | ||
"private": true, | ||
"scripts": { | ||
"dev": "astro dev", | ||
"start": "astro dev", | ||
"build": "astro build", | ||
"preview": "astro preview", | ||
"astro": "astro" | ||
}, | ||
"dependencies": { | ||
"vue": "^3.2.30" | ||
}, | ||
"devDependencies": { | ||
"@astrojs/vue": "^1.2.1", | ||
"astro": "^1.6.11", | ||
"unocss": "link:../../packages/unocss" | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
export interface Props { | ||
title: string; | ||
body: string; | ||
href: string; | ||
} | ||
const { href, title, body } = Astro.props as Props; | ||
--- | ||
|
||
<li class="border rounded inline-block px4 py2 m2 hover:shadow"> | ||
<a href={href}> | ||
<h2 class="font-bold"> | ||
{title} | ||
<span>→</span> | ||
</h2> | ||
<p> | ||
{body} | ||
</p> | ||
</a> | ||
</li> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<template> | ||
<div class=":uno: text-(xl red-400) mt-8"> | ||
Example Vue component | ||
</div> | ||
</template> | ||
|
||
<style scoped> | ||
div { | ||
@apply mt-8; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/// <reference types="astro/client" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
--- | ||
export interface Props { | ||
title: string; | ||
} | ||
const { title } = Astro.props as Props; | ||
--- | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width" /> | ||
<meta name="generator" content={Astro.generator} /> | ||
<title>{title}</title> | ||
</head> | ||
<body class="font-sans text-center"> | ||
<slot /> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
import Layout from '../layouts/Layout.astro'; | ||
import Card from '../components/Card.astro'; | ||
--- | ||
|
||
<Layout title="Foo Page"> | ||
<div class="text-center text-lg mt-10">Foo</div> | ||
<ul role="list" class="link-card-grid"> | ||
<Card | ||
href="/" | ||
title="Home" | ||
body="Go home." | ||
/> | ||
</ul> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
import Layout from '../layouts/Layout.astro'; | ||
import Card from '../components/Card.astro'; | ||
import Example from '../components/Example.vue'; | ||
--- | ||
|
||
<Layout title="Welcome to Astro."> | ||
<main class="p10 text-center"> | ||
<div> | ||
<div class="i-logo"></div> | ||
</div> | ||
<h1>Welcome to <span class="text-gradient">Astro</span></h1> | ||
<p class="instructions"> | ||
Check out the <code>src/pages</code> directory to get started.<br /> | ||
</p> | ||
<ul role="list" class="link-card-grid"> | ||
<Card | ||
href="/foo" | ||
title="/foo" | ||
body="Go to page foo." | ||
/> | ||
<Example client:only="vue" /> | ||
</ul> | ||
</main> | ||
</Layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"module": "ESNext", | ||
"moduleResolution": "node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { | ||
defineConfig, | ||
presetIcons, | ||
presetUno, | ||
transformerCompileClass, | ||
transformerDirectives, | ||
transformerVariantGroup, | ||
} from 'unocss' | ||
|
||
export default defineConfig({ | ||
shortcuts: [ | ||
{ 'i-logo': 'i-logos-astro w-6em h-6em transform transition-800' }, | ||
], | ||
transformers: [ | ||
transformerDirectives(), | ||
transformerCompileClass(), | ||
transformerVariantGroup(), | ||
], | ||
presets: [ | ||
presetUno(), | ||
presetIcons({ | ||
extraProperties: { | ||
'display': 'inline-block', | ||
'vertical-align': 'middle', | ||
}, | ||
}), | ||
], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import fs from 'fs/promises' | ||
import { resolve } from 'path' | ||
import fg from 'fast-glob' | ||
import type { UnocssPluginContext } from '@unocss/core' | ||
import { applyTransformers } from './transformers' | ||
|
||
export async function setupExtraContent(ctx: UnocssPluginContext, shouldWatch = false) { | ||
const { extraContent } = await ctx.getConfig() | ||
const { extract, tasks, root, filter } = ctx | ||
|
||
// plain text | ||
if (extraContent?.plain) { | ||
await Promise.all( | ||
extraContent.plain.map((code, idx) => { | ||
return extract(code, `__extra_content_${idx}__`) | ||
}), | ||
) | ||
} | ||
|
||
// filesystem | ||
if (extraContent?.filesystem) { | ||
const files = await fg(extraContent.filesystem, { cwd: root }) | ||
|
||
async function extractFile(file: string) { | ||
const code = await fs.readFile(file, 'utf-8') | ||
if (!filter(code, file)) | ||
return | ||
const preTransform = await applyTransformers(ctx, code, file, 'pre') | ||
const defaultTransform = await applyTransformers(ctx, preTransform?.code || code, file) | ||
await applyTransformers(ctx, defaultTransform?.code || preTransform?.code || code, file, 'post') | ||
return await extract(preTransform?.code || code, file) | ||
} | ||
|
||
if (shouldWatch) { | ||
const { watch } = await import('chokidar') | ||
const ignored = ['**/{.git,node_modules}/**'] | ||
|
||
const watcher = watch(files, { | ||
ignorePermissionErrors: true, | ||
ignored, | ||
cwd: root, | ||
ignoreInitial: true, | ||
}) | ||
|
||
watcher.on('all', (type, file) => { | ||
if (type === 'add' || type === 'change') { | ||
const absolutePath = resolve(root, file) | ||
tasks.push(extractFile(absolutePath)) | ||
} | ||
}) | ||
} | ||
|
||
await Promise.all(files.map(extractFile)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.