generated from sonofmagic/monorepo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: commit js case * refact: type and plugin * feat: use config to control plugin * chore: allow test for tailwindcss patch * chore: add ci codecov
- Loading branch information
1 parent
b735483
commit 0f5ab74
Showing
28 changed files
with
261 additions
and
240 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
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
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
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 @@ | ||
// Unterminated string constant | ||
if (isSvg) { | ||
let source = serializeString(svgNode); | ||
// add xml declaration | ||
source = '<?xml version="1.0" standalone="no"?>\r\n' + source; | ||
// convert svg source to URI data scheme. | ||
url = "data:image/svg+xml;charset=utf-8," + encodeURIComponent(source); | ||
saveAs(url, "graph.svg"); | ||
onAlreadySerialized(); | ||
return; | ||
} |
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 @@ | ||
// Missing semicolon. | ||
if (typeof parent === 'string') { | ||
parent = d.querySelectorAll(config.parent); | ||
if (parent.length > 1) { | ||
console.warn('WARNING: the given `parent` query(' + config.parent + ') matched more than one element, the first one will be used'); | ||
} | ||
if (parent.length === 0) { | ||
throw 'ERROR: the given `parent` doesn\'t exists!'; | ||
} | ||
parent = parent[0]; | ||
} |
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 |
---|---|---|
@@ -1,4 +0,0 @@ | ||
// import 'tailwindcss-patch/cli' | ||
import { TailwindcssPatcher } from './dist' | ||
const twPatcher = new TailwindcssPatcher() | ||
twPatcher.patch() | ||
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 |
---|---|---|
@@ -1,3 +1 @@ | ||
export const pluginName = 'unplugin-tailwindcss-mangle' | ||
|
||
export const webpackCacheKey = pluginName + '-sources-cache-key' |
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,85 @@ | ||
import fs from 'node:fs' | ||
import { resolve } from 'node:path' | ||
import { ClassGenerator } from '@tailwindcss-mangle/shared' | ||
import { getConfig, getDefaultMangleUserConfig } from '@tailwindcss-mangle/config' | ||
import type { MangleUserConfig } from '@tailwindcss-mangle/config' | ||
import { sort } from 'fast-sort' | ||
import defu from 'defu' | ||
import { createGlobMatcher, defaultMangleClassFilter } from '@/utils' | ||
|
||
export class Context { | ||
options: MangleUserConfig | ||
includeMatcher: (file: string) => boolean | ||
excludeMatcher: (file: string) => boolean | ||
classSet: Set<string> | ||
replaceMap: Map<string, string> | ||
classGenerator: ClassGenerator | ||
constructor(opts: MangleUserConfig) { | ||
this.options = defu(opts, getDefaultMangleUserConfig()) | ||
this.classSet = new Set() | ||
this.replaceMap = new Map() | ||
this.includeMatcher = createGlobMatcher(this.options.include, true) | ||
this.excludeMatcher = createGlobMatcher(this.options.exclude, false) | ||
this.classGenerator = new ClassGenerator(this.options.classGenerator) | ||
} | ||
|
||
mergeOptions(opts?: MangleUserConfig) { | ||
// 插件选项优先 | ||
this.options = defu(this.options, opts) | ||
this.includeMatcher = createGlobMatcher(this.options.include, true) | ||
this.excludeMatcher = createGlobMatcher(this.options.exclude, false) | ||
this.classGenerator = new ClassGenerator(this.options.classGenerator) | ||
} | ||
|
||
isInclude(file: string) { | ||
return this.includeMatcher(file) && !this.excludeMatcher(file) | ||
} | ||
|
||
currentMangleClassFilter(className: string) { | ||
return (this.options.mangleClassFilter ?? defaultMangleClassFilter)(className) | ||
} | ||
|
||
getClassSet() { | ||
return this.classSet | ||
} | ||
|
||
getReplaceMap() { | ||
return this.replaceMap | ||
} | ||
|
||
addToUsedBy(key: string, file: string) { | ||
const hit = this.classGenerator.newClassMap[key] | ||
if (hit) { | ||
hit.usedBy.add(file) | ||
} | ||
} | ||
|
||
async initConfig() { | ||
const { config } = await getConfig() | ||
const mangleConfig = config?.mangle | ||
this.mergeOptions(mangleConfig) | ||
const jsonPath = this.options.classListPath ?? resolve(process.cwd(), config?.patch?.output?.filename as string) | ||
|
||
if (jsonPath && fs.existsSync(jsonPath)) { | ||
const rawClassList = fs.readFileSync(jsonPath, 'utf8') | ||
const list = JSON.parse(rawClassList) as string[] | ||
// why? | ||
// case bg-red-500 and bg-red-500/50 | ||
// transform bg-red-500/50 first | ||
const classList = sort(list).desc((c) => c.length) | ||
for (const className of classList) { | ||
if (this.currentMangleClassFilter(className)) { | ||
this.classSet.add(className) | ||
} | ||
} | ||
} | ||
for (const cls of this.classSet) { | ||
this.classGenerator.generateClassName(cls) | ||
} | ||
|
||
for (const x of Object.entries(this.classGenerator.newClassMap)) { | ||
this.replaceMap.set(x[0], x[1].name) | ||
} | ||
return config | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './plugin' | ||
export * from './options' | ||
export * from './context' |
Oops, something went wrong.