@actview/plugin-babel 2.0.0 → 2.1.0

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/README.md CHANGED
@@ -42,10 +42,32 @@ const result = babel.transformSync(code, {
42
42
  | 导出 | 说明 |
43
43
  |---|---|
44
44
  | `defineComponentPlugin`(默认导出同) | Babel 插件工厂:组件 → `defineComponent` 转换 |
45
- | `createBabelTransform(plugin)` | 宿主壳工厂:模块级创建一次 ConfigItem,返回 `(code, filename) => { code, map } \| null` |
45
+ | `createBabelTransform(plugin, options?)` | 宿主壳工厂:模块级创建一次 ConfigItem,返回 `(code, filename) => { code, map } \| null` |
46
46
  | `createBabelItem(plugin)` | 把插件工厂预编译为 ConfigItem(Babel 8 同步) |
47
- | `transformWithBabel(code, filename, pluginItem)` | 统一参数的 `transformSync` |
48
- | 类型:`BabelPlugin` / `BabelHostResult` | 宿主壳类型 |
47
+ | `transformWithBabel(code, filename, pluginItem, options?)` | 统一参数的 `transformSync` |
48
+ | `isExcludedTransform(filename, options?)` | 排除判定:node_modules 硬排除 / 未命中 include / 命中 exclude |
49
+ | 类型:`BabelPlugin` / `BabelHostResult` / `BabelTransformOptions` | 宿主壳类型 |
50
+
51
+ ## 排除规则(node_modules 硬排除)
52
+
53
+ 宿主壳**硬排除 node_modules 下的文件**(返回 `null` 不转换):依赖是第三方代码,属依赖管线(esbuild 预构建),不是源码管线——`babel-loader` / `@rollup/plugin-babel` 同款默认。`BabelTransformOptions`:
54
+
55
+ ```ts
56
+ {
57
+ include?: Array<string | RegExp>, // 白名单:命中任一才转换(默认不过滤;node_modules 硬排除优先)
58
+ exclude?: Array<string | RegExp>, // 黑名单:命中任一即跳过(优先于 include)
59
+ }
60
+ ```
61
+
62
+ **源码分发库包 / 主题包(node_modules 下)需要现场编译时**,不在插件层开洞,而是在宿主构建配置里做**路径转换**,让文件解析路径脱离 node_modules 段、进入源码管线:
63
+
64
+ ```ts
65
+ // vite.config.ts
66
+ resolve: {
67
+ alias: { 'my-lib': 'node_modules/my-lib/src' }, // 路径不再含 node_modules 段 → 正常转换
68
+ },
69
+ optimizeDeps: { exclude: ['my-lib'] }, // 不预构建,保持源码形态
70
+ ```
49
71
 
50
72
  ## 依赖关系
51
73
 
package/dist/index.d.ts CHANGED
@@ -21,15 +21,24 @@ interface BabelHostResult {
21
21
  code: string;
22
22
  map: unknown;
23
23
  }
24
+ /** 宿主壳排除规则(createBabelTransform / transformWithBabel 第二/四参) */
25
+ interface BabelTransformOptions {
26
+ /** 白名单:文件命中任一规则才转换(默认不过滤;node_modules 硬排除优先) */
27
+ include?: Array<string | RegExp>;
28
+ /** 黑名单:命中任一规则即跳过(优先级高于 include) */
29
+ exclude?: Array<string | RegExp>;
30
+ }
31
+ /** 是否应跳过转换:node_modules 硬排除 / 未命中 include / 命中 exclude */
32
+ declare function isExcludedTransform(filename: string, options?: BabelTransformOptions): boolean;
24
33
  /** 把插件工厂预编译为 ConfigItem(Babel 8 同步版本,可跨多次 transformSync 复用) */
25
34
  declare function createBabelItem(plugin: BabelPlugin): PluginItem;
26
- /** 统一参数的 transformSync:失败返回 null,成功返回 { code, map } */
27
- declare function transformWithBabel(code: string, filename: string, pluginItem: PluginItem): BabelHostResult | null;
35
+ /** 统一参数的 transformSync:排除命中或失败返回 null,成功返回 { code, map } */
36
+ declare function transformWithBabel(code: string, filename: string, pluginItem: PluginItem, options?: BabelTransformOptions): BabelHostResult | null;
28
37
  /**
29
38
  * 静态插件的便捷工厂:模块级调用一次,ConfigItem 只创建一次。
30
39
  * 适用于插件对象不随文件变化的场景(如 defineComponentPlugin)。
31
40
  */
32
- declare function createBabelTransform(plugin: BabelPlugin | BabelPlugin[]): (code: string, filename: string) => BabelHostResult | null;
41
+ declare function createBabelTransform(plugin: BabelPlugin | BabelPlugin[], options?: BabelTransformOptions): (code: string, filename: string) => BabelHostResult | null;
33
42
 
34
43
  declare function solidPlugin(): {
35
44
  visitor: {
@@ -40,4 +49,4 @@ declare function solidPlugin(): {
40
49
  };
41
50
  };
42
51
 
43
- export { type BabelHostResult, createBabelItem, createBabelTransform, defineComponentPlugin as default, defineComponentPlugin, solidPlugin, transformWithBabel };
52
+ export { type BabelHostResult, type BabelTransformOptions, createBabelItem, createBabelTransform, defineComponentPlugin as default, defineComponentPlugin, isExcludedTransform, solidPlugin, transformWithBabel };
package/dist/index.js CHANGED
@@ -437,10 +437,24 @@ function buildSolidMark(el, state) {
437
437
 
438
438
  // src/babel-host.ts
439
439
  import * as babel from "@babel/core";
440
+ var NODE_MODULES_RE = /(^|[\\/])node_modules[\\/]/;
441
+ function matchesRule(rule, filename) {
442
+ return typeof rule === "string" ? filename.includes(rule) : rule.test(filename);
443
+ }
444
+ function isExcludedTransform(filename, options) {
445
+ const norm = filename.replace(/\\/g, "/");
446
+ if (NODE_MODULES_RE.test(norm)) return true;
447
+ if (options?.include?.length && !options.include.some((r) => matchesRule(r, norm))) {
448
+ return true;
449
+ }
450
+ if (options?.exclude?.some((r) => matchesRule(r, norm))) return true;
451
+ return false;
452
+ }
440
453
  function createBabelItem(plugin) {
441
454
  return babel.createConfigItemSync(plugin, { type: "plugin" });
442
455
  }
443
- function transformWithBabel(code, filename, pluginItem) {
456
+ function transformWithBabel(code, filename, pluginItem, options) {
457
+ if (isExcludedTransform(filename, options)) return null;
444
458
  const result = babel.transformSync(code, {
445
459
  filename,
446
460
  plugins: [pluginItem],
@@ -453,9 +467,10 @@ function transformWithBabel(code, filename, pluginItem) {
453
467
  if (!result) return null;
454
468
  return { code: result.code || code, map: result.map };
455
469
  }
456
- function createBabelTransform(plugin) {
470
+ function createBabelTransform(plugin, options) {
457
471
  const items = (Array.isArray(plugin) ? plugin : [plugin]).map((p) => createBabelItem(p));
458
472
  return (code, filename) => {
473
+ if (isExcludedTransform(filename, options)) return null;
459
474
  const result = babel.transformSync(code, {
460
475
  filename,
461
476
  plugins: items,
@@ -770,6 +785,7 @@ export {
770
785
  createBabelTransform,
771
786
  defineComponentPlugin as default,
772
787
  defineComponentPlugin,
788
+ isExcludedTransform,
773
789
  solidPlugin,
774
790
  transformWithBabel
775
791
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@actview/plugin-babel",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "type": "module",
5
5
  "description": "Babel 插件:JSX 组件自动 defineComponent 转换(ActView 编译核心,独立于 Vite 宿主)",
6
6
  "exports": {