@knotx/build-config 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/LICENSE +21 -0
- package/package.json +42 -0
- package/src/index.ts +27 -0
- package/src/plugins/index.ts +1 -0
- package/src/plugins/jsx-transform.ts +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 格桑
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@knotx/build-config",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "Build config for Knotx",
|
|
6
|
+
"author": "boenfu",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://github.com/boenfu/knotx#readme",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/boenfu/knotx.git",
|
|
12
|
+
"directory": "packages/build-config"
|
|
13
|
+
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./src/index.ts",
|
|
21
|
+
"import": "./src/index.ts",
|
|
22
|
+
"require": "./src/index.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"main": "./src/index.ts",
|
|
26
|
+
"module": "./src/index.ts",
|
|
27
|
+
"types": "./src/index.ts",
|
|
28
|
+
"files": [
|
|
29
|
+
"src"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"magic-string": "^0.30.5",
|
|
33
|
+
"rollup": "^4.9.5"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@knotx/typescript-config": "0.0.1",
|
|
37
|
+
"@knotx/eslint-config": "0.0.1"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"lint": "eslint ."
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { BuildConfig } from 'unbuild'
|
|
2
|
+
|
|
3
|
+
// import { jsxTransform } from './plugins'
|
|
4
|
+
|
|
5
|
+
export const buildConfig: BuildConfig = {
|
|
6
|
+
entries: [
|
|
7
|
+
'src/index',
|
|
8
|
+
],
|
|
9
|
+
declaration: true,
|
|
10
|
+
clean: true,
|
|
11
|
+
rollup: {
|
|
12
|
+
emitCJS: true,
|
|
13
|
+
esbuild: {
|
|
14
|
+
target: 'es2015',
|
|
15
|
+
jsx: 'automatic',
|
|
16
|
+
jsxImportSource: '@knotx/jsx',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
failOnWarn: false,
|
|
20
|
+
// 没想好,暂时不转换,默认使用 react-jsx
|
|
21
|
+
// hooks: {
|
|
22
|
+
// 'rollup:options': (_, options) => {
|
|
23
|
+
// options.plugins.push(jsxTransform())
|
|
24
|
+
// },
|
|
25
|
+
// },
|
|
26
|
+
externals: [/^@knotx\/.+$/],
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './jsx-transform'
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Plugin } from 'rollup'
|
|
2
|
+
|
|
3
|
+
import MagicString from 'magic-string'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Rollup 插件:将 jsx(...args) 转换为 jsx.apply(this, ...args)
|
|
7
|
+
*/
|
|
8
|
+
export function jsxTransform(): Plugin {
|
|
9
|
+
return {
|
|
10
|
+
name: 'jsx-transform',
|
|
11
|
+
transform(code, id) {
|
|
12
|
+
// 只处理 JS/TS/JSX/TSX 文件
|
|
13
|
+
if (!id.match(/\.[tj]sx?$/)) {
|
|
14
|
+
return null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 检查文件是否包含 jsx 调用
|
|
18
|
+
if (!code.includes('jsx(')) {
|
|
19
|
+
return null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const magicString = new MagicString(code)
|
|
23
|
+
|
|
24
|
+
// 简单的正则表达式匹配替换
|
|
25
|
+
// 注意:这种方法可能需要根据实际代码进行调整
|
|
26
|
+
const jsxCallRegex = /jsx\(([^)]*)\)/g
|
|
27
|
+
let hasChanged = false
|
|
28
|
+
|
|
29
|
+
let match
|
|
30
|
+
// eslint-disable-next-line no-cond-assign
|
|
31
|
+
while (match = jsxCallRegex.exec(code)) {
|
|
32
|
+
const startPos = match.index
|
|
33
|
+
const endPos = startPos + match[0].length
|
|
34
|
+
const args = match[1].trim()
|
|
35
|
+
|
|
36
|
+
magicString.overwrite(startPos, endPos, `jsx.call(this, ${args})`)
|
|
37
|
+
hasChanged = true
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (hasChanged) {
|
|
41
|
+
return {
|
|
42
|
+
code: magicString.toString(),
|
|
43
|
+
map: magicString.generateMap({ hires: true }),
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return null
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
}
|