-
Notifications
You must be signed in to change notification settings - Fork 393
unocss升级 esm-only #1963
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
Open
cwyyue
wants to merge
11
commits into
didi:master
Choose a base branch
from
cwyyue:feat-update-unocss-esm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
unocss升级 esm-only #1963
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9f2566b
unocss升级 esm-only
cwyyue 4dffe76
fix: lint
cwyyue e0a6cde
feat: init 单测
cwyyue 1b641ab
fix lint
cwyyue 7c6ab73
feat: 优化test demo&删除本地文件
cwyyue 4c4d509
fix: base升级
cwyyue 83e6ca5
fix: lint
cwyyue 569a9d8
feat: 更新test
cwyyue cc41bb1
feat: 更新锁包
cwyyue 68062a6
feat: createContext异步规范化写法
cwyyue de03b53
Merge remote-tracking branch 'origin/master' into feat-update-unocss-esm
cwyyue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
37 changes: 37 additions & 0 deletions
37
packages/unocss-plugin/__tests__/__snapshots__/plugin.test.js.snap
This file contains hidden or 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,37 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`test plugin > test-template 1`] = `"<view class="translate-_bl_-50_p__2c_-50_p__br_ text-12px bg-_h_fff_s_10" />"`; | ||
|
|
||
| exports[`test plugin > test-template 2`] = ` | ||
| [ | ||
| "translate-[-50%,-50%]", | ||
| "text-12px", | ||
| "bg-#fff/10", | ||
| ] | ||
| `; | ||
|
|
||
| exports[`test plugin > test-template 3`] = ` | ||
| "/* layer: default */ | ||
| .translate-_bl_-50_p__2c_-50_p__br_{--un-translate-x:-50%;--un-translate-y:-50%;transform:translateX(var(--un-translate-x)) translateY(var(--un-translate-y)) translateZ(var(--un-translate-z)) rotate(var(--un-rotate)) rotateX(var(--un-rotate-x)) rotateY(var(--un-rotate-y)) rotateZ(var(--un-rotate-z)) skewX(var(--un-skew-x)) skewY(var(--un-skew-y)) scaleX(var(--un-scale-x)) scaleY(var(--un-scale-y)) scaleZ(var(--un-scale-z));} | ||
| .bg-_h_fff_s_10{background-color:rgb(255 255 255 / 0.1);} | ||
| .text-12px{font-size:12px;}" | ||
| `; | ||
|
|
||
| exports[`test plugin > test-template 4`] = `"<view wx:class="{{(['translate-_bl_-50_p__br_',{'text-12px text-16px': false},'bg-_h_fff_s_10'])}}" />"`; | ||
|
|
||
| exports[`test plugin > test-template 5`] = ` | ||
| [ | ||
| "translate-[-50%]", | ||
| "text-12px", | ||
| "text-16px", | ||
| "bg-#fff/10", | ||
| ] | ||
| `; | ||
|
|
||
| exports[`test plugin > test-template 6`] = ` | ||
| "/* layer: default */ | ||
| .translate-_bl_-50_p__br_{--un-translate-x:-50%;--un-translate-y:-50%;transform:translateX(var(--un-translate-x)) translateY(var(--un-translate-y)) translateZ(var(--un-translate-z)) rotate(var(--un-rotate)) rotateX(var(--un-rotate-x)) rotateY(var(--un-rotate-y)) rotateZ(var(--un-rotate-z)) skewX(var(--un-skew-x)) skewY(var(--un-skew-y)) scaleX(var(--un-scale-x)) scaleY(var(--un-scale-y)) scaleZ(var(--un-scale-z));} | ||
| .bg-_h_fff_s_10{background-color:rgb(255 255 255 / 0.1);} | ||
| .text-12px{font-size:12px;} | ||
| .text-16px{font-size:16px;}" | ||
| `; |
This file contains hidden or 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,51 @@ | ||
| import MpxUnocssPlugin from '../lib/index' | ||
| import { describe, expect, test } from 'vitest' | ||
| import { getRawSource } from '../lib/source.js' | ||
| import { e as cssEscape } from '@unocss/core' | ||
| import { mpEscape } from '../lib/transform.js' | ||
| import presetMpx from '@mpxjs/unocss-base' | ||
| // const { presetLegacyCompat } = require('@unocss/preset-legacy-compat') | ||
|
|
||
| // import testpage from './123.mpx?resolve' | ||
| describe('test plugin', async () => { | ||
| const mode = 'wx' | ||
| const plugin = new MpxUnocssPlugin({ | ||
| config: { | ||
| presets: [ | ||
| presetMpx() | ||
| ] | ||
| } | ||
| }) | ||
| const mockCompilation = { | ||
| fileDependencies: new Set() | ||
| } | ||
| const uno = await plugin.createContext(mockCompilation, mode) | ||
| const parseTemplate = plugin.getTemplateParser(uno) | ||
| async function testTemplate (content, generateOptions = { | ||
| preflights: false, | ||
| safelist: false | ||
| }) { | ||
| const source = getRawSource(content) | ||
| const classmap = {} | ||
| const { newsource } = parseTemplate(source, (className) => { | ||
| if (!className) { | ||
| return className | ||
| } | ||
| classmap[className] = true | ||
| return mpEscape(cssEscape(className), plugin.options.escapeMap) | ||
| }) | ||
| // 测试模板是否转义 | ||
| expect(newsource.source()).toMatchSnapshot() | ||
| const classList = Object.keys(classmap) | ||
| // 测试类名是否正确识别 | ||
| expect(classList).toMatchSnapshot() | ||
| const list = new Set(classList) | ||
| const unoFileContent = await plugin.generateStyle(uno, classList, generateOptions) | ||
| // 测试css输出是否正确 | ||
| expect(unoFileContent).toMatchSnapshot() | ||
| } | ||
| test('test-template', async () => { | ||
| await testTemplate('<view class="translate-[-50%,-50%] text-12px bg-#fff/10" />') | ||
| await testTemplate(`<view wx:class="{{['translate-[-50%]',{'text-12px text-16px': false},'bg-#fff/10']}}" />`) | ||
| }) | ||
| }) |
This file contains hidden or 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,33 +1,22 @@ | ||
| const path = require('path') | ||
| const { minimatch } = require('minimatch') | ||
| const unoConfig = require('@unocss/config') | ||
| const core = require('@unocss/core') | ||
| const mpxConfig = require('@mpxjs/webpack-plugin/lib/config') | ||
| const toPosix = require('@mpxjs/webpack-plugin/lib/utils/to-posix') | ||
| const fixRelative = require('@mpxjs/webpack-plugin/lib/utils/fix-relative') | ||
| const parseRequest = require('@mpxjs/webpack-plugin/lib/utils/parse-request') | ||
| const { has } = require('@mpxjs/webpack-plugin/lib/utils/set') | ||
| const MpxWebpackPlugin = require('@mpxjs/webpack-plugin') | ||
| const UnoCSSWebpackPlugin = require('./web-plugin') | ||
| const transformerDirectives = require('@unocss/transformer-directives').default | ||
| const transformerVariantGroup = require('@unocss/transformer-variant-group') | ||
| const { | ||
| parseClasses, | ||
| parseStrings, | ||
| parseMustache, | ||
| stringifyAttr, | ||
| parseComments, | ||
| parseCommentConfig | ||
| } = require('./parser') | ||
| const { getReplaceSource, getConcatSource, getRawSource } = require('./source') | ||
| const { | ||
| transformStyle, | ||
| buildAliasTransformer, | ||
| transformGroups, | ||
| mpEscape, | ||
| cssRequiresTransform | ||
| } = require('./transform') | ||
| const platformPreflightsMap = require('./platform') | ||
| import { loadConfig } from '@unocss/config' | ||
| import { createGenerator, e as cssEscape } from '@unocss/core' | ||
| import transformerDirectives from '@unocss/transformer-directives' | ||
| import transformerVariantGroup from '@unocss/transformer-variant-group' | ||
| import path from 'path' | ||
| import { minimatch } from 'minimatch' | ||
| import mpxConfig from '@mpxjs/webpack-plugin/lib/config.js' | ||
| import toPosix from '@mpxjs/webpack-plugin/lib/utils/to-posix.js' | ||
| import fixRelative from '@mpxjs/webpack-plugin/lib/utils/fix-relative.js' | ||
| import parseRequest from '@mpxjs/webpack-plugin/lib/utils/parse-request.js' | ||
| import set from '@mpxjs/webpack-plugin/lib/utils/set.js' | ||
| import MpxWebpackPlugin from '@mpxjs/webpack-plugin' | ||
| import { parseClasses, parseStrings, parseMustache, stringifyAttr, parseComments, parseCommentConfig } from './parser.js' | ||
| import { getReplaceSource, getConcatSource, getRawSource } from './source.js' | ||
| import { transformStyle, buildAliasTransformer, transformGroups, mpEscape, cssRequiresTransform } from './transform.js' | ||
| import platformPreflightsMap from './platform.js' | ||
| import UnoCSSWebpackPlugin from './web-plugin/index.js' | ||
| const { has } = set | ||
|
|
||
| const PLUGIN_NAME = 'MpxUnocssPlugin' | ||
|
|
||
| function filterFile (file, scan) { | ||
|
|
@@ -206,16 +195,16 @@ class MpxUnocssPlugin { | |
|
|
||
| async createContext (compilation, mode) { | ||
| const { root, config, configFiles } = this.options | ||
| const { config: resolved, sources } = await unoConfig.loadConfig(root, config, configFiles) | ||
| const { config: resolved, sources } = await loadConfig(root, config, configFiles) | ||
| sources.forEach((item) => { | ||
| compilation.fileDependencies.add(item) | ||
| // fix jiti require cache for watch | ||
| delete require.cache[item] | ||
| // delete require.cache[item] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 测试watch状态下配置变更是否能够生效 |
||
| }) | ||
|
|
||
| const platformPreflights = platformPreflightsMap[mode] || [] | ||
|
|
||
| return core.createGenerator({ | ||
| return await createGenerator({ | ||
| ...resolved, | ||
| preflights: [ | ||
| ...(resolved.preflights || []), | ||
|
|
@@ -224,6 +213,54 @@ class MpxUnocssPlugin { | |
| }) | ||
| } | ||
|
|
||
| getTemplateParser (uno) { | ||
| // process classes | ||
| const transformAlias = buildAliasTransformer(uno.config.alias) | ||
| const transformClasses = (source, classNameHandler = c => c) => { | ||
| // pre process | ||
| source = transformAlias(source) | ||
| if (this.options.transformGroups) { | ||
| source = transformGroups(source, this.options.transformGroups) | ||
| } | ||
| const content = source.source() | ||
| // escape & fill classesMap | ||
| return content.split(/\s+/).map(classNameHandler).join(' ') | ||
| } | ||
| return (source, classNameHandler) => { | ||
| source = getReplaceSource(source) | ||
| const content = source.original().source() | ||
| parseClasses(content).forEach(({ result, start, end }) => { | ||
| let { replaced, val } = parseMustache(result, (exp) => { | ||
| const expSource = getReplaceSource(exp) | ||
| parseStrings(exp).forEach(({ result, start, end }) => { | ||
| result = transformClasses(result, classNameHandler) | ||
| expSource.replace(start, end, result) | ||
| }) | ||
| return expSource.source() | ||
| }, str => transformClasses(str, classNameHandler)) | ||
| if (replaced) { | ||
| val = stringifyAttr(val) | ||
| source.replace(start - 1, end + 1, val) | ||
| } | ||
| }) | ||
| // process comments | ||
| const commentConfig = {} | ||
| parseComments(content).forEach(({ result, start, end }) => { | ||
| Object.assign(commentConfig, parseCommentConfig(result)) | ||
| source.replace(start, end, '') | ||
| }) | ||
| if (commentConfig.safelist) { | ||
| this.getSafeListClasses(commentConfig.safelist).forEach((className) => { | ||
| classNameHandler(className) | ||
| }) | ||
| } | ||
| return { | ||
| newsource: source, | ||
| commentConfig | ||
| } | ||
| } | ||
| } | ||
|
|
||
| apply (compiler) { | ||
| this.minify = isProductionLikeMode(compiler.options) | ||
| // 处理web | ||
|
|
@@ -311,29 +348,15 @@ class MpxUnocssPlugin { | |
| const commentConfigMap = {} | ||
|
|
||
| const mainClassesMap = packageClassesMaps.main | ||
| const cssEscape = core.e | ||
| // config中的safelist视为主包classes | ||
| const safeListClasses = this.getSafeListClasses(config.safelist) | ||
|
|
||
| safeListClasses.forEach((className) => { | ||
| mainClassesMap[className] = true | ||
| }) | ||
| const transformAlias = buildAliasTransformer(config.alias) | ||
| const transformClasses = (source, classNameHandler = c => c) => { | ||
| // pre process | ||
| source = transformAlias(source) | ||
| if (this.options.transformGroups) { | ||
| source = transformGroups(source, this.options.transformGroups) | ||
| } | ||
| const content = source.source() | ||
| // escape & fill classesMap | ||
| return content.split(/\s+/).map(classNameHandler).join(' ') | ||
| } | ||
| const parseTemplate = this.getTemplateParser(uno) | ||
|
|
||
| const processTemplate = async (file, source) => { | ||
| source = getReplaceSource(source) | ||
| const content = source.original().source() | ||
|
|
||
| const packageName = getPackageName(file) | ||
| const filename = file.slice(0, -templateExt.length) | ||
| const currentClassesMap = packageClassesMaps[packageName] = packageClassesMaps[packageName] || {} | ||
|
|
@@ -351,35 +374,9 @@ class MpxUnocssPlugin { | |
| } | ||
| return mpEscape(cssEscape(className), this.options.escapeMap) | ||
| } | ||
| parseClasses(content).forEach(({ result, start, end }) => { | ||
| let { replaced, val } = parseMustache(result, (exp) => { | ||
| const expSource = getReplaceSource(exp) | ||
| parseStrings(exp).forEach(({ result, start, end }) => { | ||
| result = transformClasses(result, classNameHandler) | ||
| expSource.replace(start, end, result) | ||
| }) | ||
| return expSource.source() | ||
| }, str => transformClasses(str, classNameHandler)) | ||
| if (replaced) { | ||
| val = stringifyAttr(val) | ||
| source.replace(start - 1, end + 1, val) | ||
| } | ||
| }) | ||
| // process comments | ||
| const commentConfig = {} | ||
| parseComments(content).forEach(({ result, start, end }) => { | ||
| Object.assign(commentConfig, parseCommentConfig(result)) | ||
| source.replace(start, end, '') | ||
| }) | ||
| if (commentConfig.safelist) { | ||
| this.getSafeListClasses(commentConfig.safelist).forEach((className) => { | ||
| classNameHandler(className) | ||
| }) | ||
| } | ||
|
|
||
| const { newsource, commentConfig } = parseTemplate(source, classNameHandler) | ||
| commentConfigMap[filename] = commentConfig | ||
|
|
||
| assets[file] = source | ||
| assets[file] = newsource | ||
| } | ||
|
|
||
| await Promise.all(Object.entries(assets).map(([file, source]) => { | ||
|
|
@@ -538,4 +535,4 @@ class MpxUnocssPlugin { | |
| } | ||
| } | ||
|
|
||
| module.exports = MpxUnocssPlugin | ||
| export default MpxUnocssPlugin | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有啥区别
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@unocss/preset wind和@unocss/preset uno已被弃用,并重命名为@unocss/preset-wind3