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

require.context doesn't work in version 3.0.2 #6920

Closed
zeroooooooo opened this issue Jul 6, 2020 · 4 comments · Fixed by #7492
Closed

require.context doesn't work in version 3.0.2 #6920

zeroooooooo opened this issue Jul 6, 2020 · 4 comments · Fixed by #7492
Assignees
Labels
A-cli Area - CLI 相关 question Further information is requested V-3 Version - 3.x
Milestone

Comments

@zeroooooooo
Copy link

相关平台

微信小程序

小程序基础库: 2.11.1
使用框架: React

复现步骤

  1. work in version 3.0.0-beta.5
  2. upgrade to version 3.0.2 will cause an error
  3. code like this
    const test = require.context( '@/store/test', true, /config.\w .js$/);

期望结果

require.context can work as usual

实际结果

Module not found: Error: Can't resolve '@/store/test' in '/demos./src/store'
resolve '@/store/test' in '/demos./src/store'
  Parsed request is a module
  using description file: /demos./package.json (relative path: ./src/store)
    aliased with mapping '@': '/demos./src' to '/demos./src/store/test'
      using description file: /demos./package.json (relative path: ./src/store)
        resolve multi platform file path
          using description file: /demos./package.json (relative path: ./src/store)
            using description file: /demos./package.json (relative path: ./src/store/test/index.js)
              as directory
                /demos./src/store/test/index.js is not a directory
[/demos./src/store/test/index.js]
 @ ./src/store/index.js 4:19-135
 @ ./node_modules/@tarojs/mini-runner/node_modules/babel-loader/lib!./node_modules/linaria/loader.js??ref--9-1!./src/app.tsx
 @ ./src/app.tsx

环境信息

 Taro CLI 3.0.2 environment info:
    System:
      OS: macOS 10.15.5
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 12.16.2 - ~/.nvm/versions/node/v12.16.2/bin/node
      Yarn: 1.22.0 - /usr/local/bin/yarn
      npm: 6.14.4 - ~/.nvm/versions/node/v12.16.2/bin/npm
    npmPackages:
      @tarojs/cli: 3.0.2 => 3.0.2
      @tarojs/components: 3.0.2 => 3.0.2
      @tarojs/mini-runner: 3.0.2 => 3.0.2
      @tarojs/react: 3.0.2 => 3.0.2
      @tarojs/runtime: 3.0.2 => 3.0.2
      @tarojs/taro: 3.0.2 => 3.0.2
      @tarojs/webpack-runner: 3.0.2 => 3.0.2
      babel-preset-taro: 3.0.2 => 3.0.2
      eslint-config-taro: 3.0.2 => 3.0.2
      react: ^16.10.0 => 16.13.1
      taro-ui: 3.0.0-alpha.3 => 3.0.0-alpha.3

补充信息

related issues : #6720

@digiaries
Copy link
Contributor

试试最后用 / 结尾

@zeroooooooo
Copy link
Author

试试最后用 / 结尾

试过了的 这样也会报错 当扫描的目录的根目录存在index.js文件时就会出现错误

@Chen-jj Chen-jj added A-cli Area - CLI 相关 V-3 Version - 3.x labels Jul 30, 2020
@Chen-jj Chen-jj modified the milestones: 3.0.7, 3.0.8 Jul 30, 2020
@Chen-jj Chen-jj modified the milestones: 3.0.8, 3.0.9 Aug 18, 2020
@Chen-jj
Copy link
Contributor

Chen-jj commented Sep 1, 2020

@zeroooooooo 复现不了,升级到最新版本再试试,或者提供一下 demo 吧。

// Taro 项目配置
config: {
  alias: {
    '@/src': path.resolve(__dirname, '../src')
  }
  ...
}
// 读取 src 下所有 xxx.config.js
const r = require.context('@/src', true, /(\w)+\.config\.js$/)
console.log('r: ', r.keys())

image

// 像 #6720 一样,读取当前页面文件夹下的配置文件
const r = require.context('./', true, /(\w)+\.config\.js$/)
console.log('r: ', r.keys())

image

@Chen-jj Chen-jj removed this from the 3.0.9 milestone Sep 1, 2020
@Chen-jj Chen-jj added the question Further information is requested label Sep 1, 2020
@zeroooooooo
Copy link
Author

试试最后用 / 结尾

试过了的 这样也会报错 当扫描的目录的根目录存在index.js文件时就会出现错误

@Chen-jj

hello, 当扫描的一级目录下存在index.js的时候才会出错,如图
Screen Shot 2020-09-01 at 4 28 58 PM

已创建最小复现demo 如下

taro.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli Area - CLI 相关 question Further information is requested V-3 Version - 3.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants