@fairys/taro-tools-plugins 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/index.d.ts +3 -0
- package/esm/index.js +3 -0
- package/esm/postcss.plugin.d.ts +6 -0
- package/esm/postcss.plugin.js +15 -0
- package/esm/replace.plugin.d.ts +11 -0
- package/esm/replace.plugin.js +31 -0
- package/esm/utils.d.ts +25 -0
- package/esm/utils.js +22 -0
- package/package.json +32 -0
- package/src/index.ts +3 -0
- package/src/postcss.plugin.ts +24 -0
- package/src/replace.plugin.ts +45 -0
- package/src/utils.ts +42 -0
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type postcss from 'postcss';
|
|
2
|
+
import { ReplaceOptions } from './utils';
|
|
3
|
+
export interface PostcssClassProcessorOptions extends Omit<ReplaceOptions, 'content' | 'isPostcss'> {
|
|
4
|
+
replace?: (str: string, isPostcss: boolean) => string;
|
|
5
|
+
}
|
|
6
|
+
export declare const postcssClassProcessor: (options?: PostcssClassProcessorOptions) => (root: postcss.Root) => void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { replace } from "./utils.js";
|
|
2
|
+
const postcssClassProcessor = (options = {})=>{
|
|
3
|
+
const { replace: parentReplace, ...rest } = options || {};
|
|
4
|
+
return (root)=>{
|
|
5
|
+
root.walkRules((rule)=>{
|
|
6
|
+
const regx = new RegExp(`^\\.${rest.prefix || 'fairystaro__'}`);
|
|
7
|
+
if (regx.test(rule.selector)) rule.selector = parentReplace?.(rule.selector, true) || replace({
|
|
8
|
+
...rest,
|
|
9
|
+
content: rule.selector,
|
|
10
|
+
isPostcss: true
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export { postcssClassProcessor };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
2
|
+
import { ReplaceOptions } from './utils';
|
|
3
|
+
export interface ReplacePluginOptions extends Omit<ReplaceOptions, 'content' | 'isPostcss'> {
|
|
4
|
+
replace?: (str: string, isPostcss: boolean) => string;
|
|
5
|
+
}
|
|
6
|
+
export declare class ReplacePlugin implements RspackPluginInstance {
|
|
7
|
+
options: ReplacePluginOptions;
|
|
8
|
+
constructor(options?: ReplacePluginOptions);
|
|
9
|
+
_replace: (str: string) => string;
|
|
10
|
+
apply(compiler: Compiler): void;
|
|
11
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Compilation, sources } from "@rspack/core";
|
|
2
|
+
import { replace } from "./utils.js";
|
|
3
|
+
class ReplacePlugin {
|
|
4
|
+
options;
|
|
5
|
+
constructor(options = {}){
|
|
6
|
+
this.options = options;
|
|
7
|
+
}
|
|
8
|
+
_replace = (str)=>{
|
|
9
|
+
const { replace: parentReplace, ...rest } = this.options || {};
|
|
10
|
+
return parentReplace?.(str, false) || replace({
|
|
11
|
+
content: str,
|
|
12
|
+
...rest,
|
|
13
|
+
isPostcss: false
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
apply(compiler) {
|
|
17
|
+
compiler.hooks.compilation.tap('ReplacePlugin', (compilation)=>{
|
|
18
|
+
compilation.hooks.processAssets.tap({
|
|
19
|
+
name: 'ReplacePlugin',
|
|
20
|
+
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE
|
|
21
|
+
}, (assets)=>{
|
|
22
|
+
for (const filename of Object.keys(assets))if (filename.endsWith('.js') || filename.endsWith('.ts') || filename.endsWith('.tsx') || filename.endsWith('.jsx')) {
|
|
23
|
+
const source = assets[filename].source();
|
|
24
|
+
const sourceStr = source.toString();
|
|
25
|
+
assets[filename] = new sources.RawSource(this._replace(sourceStr));
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export { ReplacePlugin };
|
package/esm/utils.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 处理特殊字符正则表达式
|
|
3
|
+
*/
|
|
4
|
+
export declare const defaultReplaceRules: {
|
|
5
|
+
readonly ':': "_mh_";
|
|
6
|
+
readonly '/': "_xg_";
|
|
7
|
+
readonly '[': "_zkh1_";
|
|
8
|
+
readonly ']': "_zhk2_";
|
|
9
|
+
readonly '(': "_xkh1_";
|
|
10
|
+
readonly ')': "_xkh2_";
|
|
11
|
+
readonly '!': "_gqh_";
|
|
12
|
+
readonly '%': "_bfh_";
|
|
13
|
+
readonly '*': "_x_";
|
|
14
|
+
};
|
|
15
|
+
export interface ReplaceOptions {
|
|
16
|
+
/**内容*/
|
|
17
|
+
content: string;
|
|
18
|
+
/**是否是postcss*/
|
|
19
|
+
isPostcss?: boolean;
|
|
20
|
+
/**前缀*/
|
|
21
|
+
prefix?: string;
|
|
22
|
+
/**替换规则*/
|
|
23
|
+
replaceRules?: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
export declare const replace: (options: ReplaceOptions) => string;
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { defaultRules, transformEscapESelector, transformSelector } from "unplugin-transform-class/utils";
|
|
2
|
+
const defaultReplaceRules = {
|
|
3
|
+
...defaultRules,
|
|
4
|
+
':': '_mh_',
|
|
5
|
+
'/': '_xg_',
|
|
6
|
+
'[': '_zkh1_',
|
|
7
|
+
']': '_zhk2_',
|
|
8
|
+
'(': '_xkh1_',
|
|
9
|
+
')': '_xkh2_',
|
|
10
|
+
'!': '_gqh_',
|
|
11
|
+
'%': '_bfh_',
|
|
12
|
+
'*': '_x_'
|
|
13
|
+
};
|
|
14
|
+
const replace = (options)=>{
|
|
15
|
+
const { content, isPostcss = true, prefix = 'fairystaro__', replaceRules = defaultReplaceRules } = options;
|
|
16
|
+
return content.replace(new RegExp(`${prefix}(\\S+?)(?=[\\s'"]|$)`, 'g'), (match)=>{
|
|
17
|
+
let _newStr = match;
|
|
18
|
+
_newStr = isPostcss ? transformEscapESelector(_newStr, replaceRules) : transformSelector(_newStr, replaceRules);
|
|
19
|
+
return _newStr;
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
export { defaultReplaceRules, replace };
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fairys/taro-tools-plugins",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"author": "SunLxy <1011771396@qq.com>",
|
|
5
|
+
"description": "框架组件库",
|
|
6
|
+
"homepage": "https://github.com/autumn-fairy-tales/fairys-taro-react",
|
|
7
|
+
"version": "0.0.1",
|
|
8
|
+
"main": "esm/index.js",
|
|
9
|
+
"types": "esm/index.d.ts",
|
|
10
|
+
"module": "esm/index.js",
|
|
11
|
+
"license": "ISC",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "carefrees-rslib build --esm",
|
|
14
|
+
"watch": "carefrees-rslib build --watch --esm"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/autumn-fairy-tales/fairys-taro-react.git",
|
|
22
|
+
"directory": "packages/plugins"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"src",
|
|
26
|
+
"lib",
|
|
27
|
+
"esm"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"unplugin-transform-class": "^0.6.0"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type postcss from 'postcss';
|
|
2
|
+
import { replace, ReplaceOptions } from './utils';
|
|
3
|
+
// 这个解决了 tailwindcss 类名中的符号替换问题
|
|
4
|
+
export interface PostcssClassProcessorOptions extends Omit<ReplaceOptions, 'content' | 'isPostcss'> {
|
|
5
|
+
replace?: (str: string, isPostcss: boolean) => string;
|
|
6
|
+
}
|
|
7
|
+
export const postcssClassProcessor = (options: PostcssClassProcessorOptions = {}) => {
|
|
8
|
+
const { replace: parentReplace, ...rest } = options || {};
|
|
9
|
+
return (root: postcss.Root) => {
|
|
10
|
+
root.walkRules((rule: postcss.Rule) => {
|
|
11
|
+
// 或处理选择器中的 class(如 .my-class → .prefix-my-class)
|
|
12
|
+
const regx = new RegExp(`^\\.${rest.prefix || 'fairystaro__'}`);
|
|
13
|
+
if (regx.test(rule.selector)) {
|
|
14
|
+
rule.selector =
|
|
15
|
+
parentReplace?.(rule.selector, true) ||
|
|
16
|
+
replace({
|
|
17
|
+
...rest,
|
|
18
|
+
content: rule.selector,
|
|
19
|
+
isPostcss: true,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { RspackPluginInstance, Compiler, Compilation, sources } from '@rspack/core';
|
|
2
|
+
import { replace, ReplaceOptions } from './utils';
|
|
3
|
+
|
|
4
|
+
export interface ReplacePluginOptions extends Omit<ReplaceOptions, 'content' | 'isPostcss'> {
|
|
5
|
+
replace?: (str: string, isPostcss: boolean) => string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export class ReplacePlugin implements RspackPluginInstance {
|
|
9
|
+
options: ReplacePluginOptions;
|
|
10
|
+
|
|
11
|
+
constructor(options: ReplacePluginOptions = {}) {
|
|
12
|
+
this.options = options;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 替换内容
|
|
16
|
+
_replace = (str: string) => {
|
|
17
|
+
const { replace: parentReplace, ...rest } = this.options || {};
|
|
18
|
+
return parentReplace?.(str, false) || replace({ content: str, ...rest, isPostcss: false });
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
apply(compiler: Compiler) {
|
|
22
|
+
compiler.hooks.compilation.tap('ReplacePlugin', (compilation: Compilation) => {
|
|
23
|
+
compilation.hooks.processAssets.tap(
|
|
24
|
+
{
|
|
25
|
+
name: 'ReplacePlugin',
|
|
26
|
+
stage: Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
|
27
|
+
},
|
|
28
|
+
(assets) => {
|
|
29
|
+
for (const filename of Object.keys(assets)) {
|
|
30
|
+
if (
|
|
31
|
+
filename.endsWith('.js') ||
|
|
32
|
+
filename.endsWith('.ts') ||
|
|
33
|
+
filename.endsWith('.tsx') ||
|
|
34
|
+
filename.endsWith('.jsx')
|
|
35
|
+
) {
|
|
36
|
+
const source = assets[filename].source();
|
|
37
|
+
const sourceStr = source.toString();
|
|
38
|
+
assets[filename] = new sources.RawSource(this._replace(sourceStr));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { transformSelector, transformEscapESelector, defaultRules } from 'unplugin-transform-class/utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 处理特殊字符正则表达式
|
|
5
|
+
*/
|
|
6
|
+
export const defaultReplaceRules = {
|
|
7
|
+
...defaultRules,
|
|
8
|
+
':': '_mh_',
|
|
9
|
+
'/': '_xg_',
|
|
10
|
+
'[': '_zkh1_',
|
|
11
|
+
']': '_zhk2_',
|
|
12
|
+
'(': '_xkh1_',
|
|
13
|
+
')': '_xkh2_',
|
|
14
|
+
'!': '_gqh_',
|
|
15
|
+
'%': '_bfh_',
|
|
16
|
+
'*': '_x_',
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
export interface ReplaceOptions {
|
|
20
|
+
/**内容*/
|
|
21
|
+
content: string;
|
|
22
|
+
/**是否是postcss*/
|
|
23
|
+
isPostcss?: boolean;
|
|
24
|
+
/**前缀*/
|
|
25
|
+
prefix?: string;
|
|
26
|
+
/**替换规则*/
|
|
27
|
+
replaceRules?: Record<string, string>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const replace = (options: ReplaceOptions) => {
|
|
31
|
+
const { content, isPostcss = true, prefix = 'fairystaro__', replaceRules = defaultReplaceRules } = options;
|
|
32
|
+
// /fairystaro__(\S+?)(?=[\s'"]|$)/g
|
|
33
|
+
return content.replace(new RegExp(`${prefix}(\\S+?)(?=[\\s'"]|$)`, 'g'), (match) => {
|
|
34
|
+
let _newStr = match;
|
|
35
|
+
if (isPostcss) {
|
|
36
|
+
_newStr = transformEscapESelector(_newStr, replaceRules);
|
|
37
|
+
} else {
|
|
38
|
+
_newStr = transformSelector(_newStr, replaceRules);
|
|
39
|
+
}
|
|
40
|
+
return _newStr;
|
|
41
|
+
});
|
|
42
|
+
};
|