Skip to content
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

compile.inlcude 不起作用,没发转译 node_modules 下的包 #16024

Closed
axetroy opened this issue Jul 1, 2024 · 1 comment
Closed

compile.inlcude 不起作用,没发转译 node_modules 下的包 #16024

axetroy opened this issue Jul 1, 2024 · 1 comment

Comments

@axetroy
Copy link

axetroy commented Jul 1, 2024

相关平台

H5

浏览器版本: Firefox 128
使用框架: React

复现步骤

compile.include 有以下几个问题:

  1. 不能接受 RegExp 作为参数(无法经过 Validate)
  2. 函数类型不起作用(如下面的代码,函数只会触发 3 次,并且 modulePath 是空字符串)
  3. 纯字符指定的路径也不起作用 (查看编译出来的结果,并没有发生变化)
        compile: {
            include: [
                resolve('../../'), // ----------> 但是这条规则生效了,这个路径不在 node_modules 下
                resolve(__dirname, '../node_modules/@tarojs/components'),
                // (modulePath) => {
                //     这里只会触发 3 次,并且 modulePath 是空字符串,明显不对
                //     console.log('modulePath-------------->', modulePath)

                //     require('fs').appendFileSync('modules.txt', modulePath +  '\n')

                //     return /[\\/](@tarojs[\\/]components|jotai)[\\/]/.test(modulePath)
                // },
                resolve(__dirname, '../node_modules/jotai'),
                resolve(__dirname, '../node_modules/taro-code'),
                resolve(__dirname, '../node_modules/taro-ztree'),
            ],
        },

看起来,配置已经加进去了,但没有生效

截屏2024-07-01 16 59 53

期望结果

正常应该编译 node_modules 下的目录,并且函数的 include 规则,应该正常工作

实际结果

没法正常工作

环境信息


  Taro CLI 3.6.32 environment info:
    System:
      OS: Windows 10 10.0.19045
    Binaries:
      Node: 16.20.2 - C:\Program Files\nodejs\node.EXE  
      Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD    
      npm: 6.14.18 - C:\Program Files\nodejs\npm.CMD    
    npmPackages:
      @tarojs/cli: ^3.6.32 => 3.6.32 
      @tarojs/components: ^3.6.32 => 3.6.32 
      @tarojs/helper: ^3.6.32 => 3.6.32 
      @tarojs/plugin-framework-react: ^3.6.32 => 3.6.32 
      @tarojs/plugin-platform-alipay: ^3.6.32 => 3.6.32 
      @tarojs/plugin-platform-h5: ^3.6.32 => 3.6.32     
      @tarojs/plugin-platform-jd: ^3.6.32 => 3.6.32     
      @tarojs/plugin-platform-qq: ^3.6.32 => 3.6.32     
      @tarojs/plugin-platform-swan: ^3.6.32 => 3.6.32   
      @tarojs/plugin-platform-tt: ^3.6.32 => 3.6.32     
      @tarojs/plugin-platform-weapp: ^3.6.32 => 3.6.32  
      @tarojs/react: ^3.6.32 => 3.6.32
      @tarojs/runtime: ^3.6.32 => 3.6.32
      @tarojs/shared: ^3.6.32 => 3.6.32
      @tarojs/taro: ^3.6.32 => 3.6.32
      @tarojs/taro-h5: ^3.6.32 => 3.6.32
      @tarojs/taro-loader: ^3.6.32 => 3.6.32
      @tarojs/webpack5-runner: ^3.6.32 => 3.6.32
      babel-plugin-transform-taroapi: 3.6.17 => 3.6.17
      babel-preset-taro: ^3.6.32 => 3.6.32
      eslint-config-taro: 3.6.17 => 3.6.17
      react: ^18.2.0 => 18.2.0
      taro-ui: ^3.1.0 => 3.1.0
@axetroy axetroy closed this as completed Jul 1, 2024
@axetroy
Copy link
Author

axetroy commented Jul 1, 2024

手动 patch 了两个文件,解决

diff --git a/node_modules/@tarojs/helper/dist/constants.js b/node_modules/@tarojs/helper/dist/constants.js
index 490a869..926c6c1 100644
--- a/node_modules/@tarojs/helper/dist/constants.js
+++ b/node_modules/@tarojs/helper/dist/constants.js
@@ -96,15 +96,15 @@ exports.processTypeMap = {
 };
 exports.CSS_EXT = ['.css', '.scss', '.sass', '.less', '.styl', '.stylus', '.wxss', '.acss'];
 exports.SCSS_EXT = ['.scss'];
-exports.JS_EXT = ['.js', '.jsx'];
-exports.TS_EXT = ['.ts', '.tsx'];
+exports.JS_EXT = ['.js', '.jsx', '.mjs', '.mjsx'];
+exports.TS_EXT = ['.ts', '.tsx', '.mts', '.mtsx'];
 exports.UX_EXT = ['.ux'];
 exports.SCRIPT_EXT = exports.JS_EXT.concat(exports.TS_EXT);
 exports.VUE_EXT = ['.vue'];
-exports.REG_JS = /\.js(\?.*)?$/;
-exports.REG_SCRIPT = /\.(js|jsx)(\?.*)?$/;
-exports.REG_TYPESCRIPT = /\.(tsx|ts)(\?.*)?$/;
-exports.REG_SCRIPTS = /\.[tj]sx?$/i;
+exports.REG_JS = /\.m?js(\?.*)?$/;
+exports.REG_SCRIPT = /\.m?(js|jsx)(\?.*)?$/;
+exports.REG_TYPESCRIPT = /\.m?(tsx|ts)(\?.*)?$/;
+exports.REG_SCRIPTS = /\.m?[tj]sx?$/i;
 exports.REG_VUE = /\.vue$/i;
 exports.REG_SASS = /\.(s[ac]ss)$/;
 exports.REG_SASS_SASS = /\.sass$/;
diff --git a/node_modules/@tarojs/webpack5-runner/dist/webpack/H5WebpackModule.js b/node_modules/@tarojs/webpack5-runner/dist/webpack/H5WebpackModule.js
index 0e42ffb..94e3929 100644
--- a/node_modules/@tarojs/webpack5-runner/dist/webpack/H5WebpackModule.js
+++ b/node_modules/@tarojs/webpack5-runner/dist/webpack/H5WebpackModule.js
@@ -214,10 +214,13 @@ class H5WebpackModule {
         if (Array.isArray(compile.include)) {
             rule.include.unshift(...compile.include);
         }
-        rule.exclude = [filename => /@tarojs[\\/]components/.test(filename)];
+        rule.exclude = rule.exclude || [];
         if (Array.isArray(compile.exclude)) {
             rule.exclude.unshift(...compile.exclude);
         }
+
         return rule;
     }
     getMediaRule(staticDirectory) {

This was referenced Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant