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(webpack): support pre transformer #1133

Merged
merged 9 commits into from Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion examples/vue-cli/vue.config.js
@@ -1,9 +1,14 @@
const UnoCSS = require('@unocss/webpack').default
const { transformerVariantGroup } = require('unocss')

module.exports = {
configureWebpack: {
plugins: [
UnoCSS(),
UnoCSS({
transformers: [
transformerVariantGroup(),
],
}),
],
},
chainWebpack(config) {
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/types.ts
Expand Up @@ -536,8 +536,6 @@ export interface PluginOptions {

/**
* Custom transformers to the source code
*
* Currently only supported in Vite
*/
transformers?: SourceCodeTransformer[]
}
Expand Down
36 changes: 32 additions & 4 deletions packages/webpack/src/index.ts
Expand Up @@ -2,6 +2,7 @@ import type { UserConfig, UserConfigDefaults } from '@unocss/core'
import type { ResolvedUnpluginOptions, UnpluginOptions } from 'unplugin'
import { createUnplugin } from 'unplugin'
import WebpackSources from 'webpack-sources'
import MagicString from 'magic-string'
import {
HASH_PLACEHOLDER_RE,
LAYER_MARK_ALL,
Expand Down Expand Up @@ -29,7 +30,7 @@ export default function WebpackPlugin<Theme extends {}>(
defaults?: UserConfigDefaults,
) {
return createUnplugin(() => {
const context = createContext(configOrPath, defaults)
const context = createContext<WebpackPluginOptions>(configOrPath as any, defaults)
const { uno, tokens, filter, extract, onInvalidate } = context

let timer: any
Expand All @@ -48,9 +49,13 @@ export default function WebpackPlugin<Theme extends {}>(
transformInclude(id) {
return filter('', id)
},
transform(code, id) {
tasks.push(extract(code, id))
return null
async transform(code, id) {
const result = await transform(code, id)
if (result === null)
tasks.push(extract(code, id))
else
tasks.push(extract(result.code, id))
return result
},
resolveId(id) {
const entry = resolveId(id)
Expand Down Expand Up @@ -109,6 +114,29 @@ export default function WebpackPlugin<Theme extends {}>(
},
} as Required<ResolvedUnpluginOptions>

async function transform(code: string, id: string) {
const transformers = (context.uno.config.transformers || []).filter(i => i.enforce === plugin.enforce)
if (!transformers.length)
return null
const s = new MagicString(code)
for (const t of transformers) {
if (t.idFilter) {
if (!t.idFilter(id))
continue
}
else if (!context.filter(code, id)) {
continue
}
await t.transform(s, id, context)
}
if (s.hasChanged()) {
return {
code: s.toString(),
map: s.generateMap({ hires: true, source: id }),
}
}
return null
}
async function updateModules() {
if (!plugin.__vfsModules)
return
Expand Down