oklas / react-app-alias

:label: Alias and multiple src directory for craco or rewired create-react-app
MIT License
174 stars 18 forks source link

autoscan #70

Open oklas opened 2 years ago

oklas commented 2 years ago

Warn: Experimental feature. It is not documented and may be subject to remove.

Please ask a questions, share opinions and suggestions. Share your experience if you tried to use autoscan.

const options = {
  autoscan: 'src',
}
const options = {
  autoscan: {
    path: 'src',
    prefix: '@', // optional
    suffix: '_', // optional
  }
}
const options = {
  autoscan: [
    'lib',
    {
      path: 'src',
    },
  ]
}
oklas commented 2 years ago

As of now this feature is not working for typescript. It requires to specify alias in tsconfig.paths.json anyway.

oakfang commented 2 years ago

Great feature, please keep it in!

SPiCaRiA commented 1 year ago

Hi, thank you for the awesome feature! Not sure if this is the right place to ask related questions but:

The example usage demonstrates how it can make alias (Internal/index) for the folders under src. Apart from this, is it possible for autoscan to make alias for files under src? For example, imagine having the file structure:

- src
  - App.js
  - Foo.js

And I want to use absolute imports as if I set baseUrl as src so I can write code like this in App.js:

// In App.js.
import Foo from 'Foo';
oklas commented 1 year ago

Hi @SPiCaRiA, what is the problem, what is actually not worked for you? Error or something else.

SPiCaRiA commented 1 year ago

Hi @SPiCaRiA, what is the problem, what is actually not worked for you? Error or something else.

Hi, sorry I didn't make it clear:

In my config-override.js:

const options = {
  autoscan: 'src',
}

module.exports = aliasWebpack(options)

jsconfig.json and jsconfig.path.json:

{
  "extends": "./jsconfig.paths.json",
  "compilerOptions": {
    "baseUrl": "."
  },
  "include": ["src"]
}

{
  "compilerOptions": {
    "paths": {
      "*": ["src/*"]
    }
  }
}

The usage:

import foo from 'my_dir/foo' // works
import bar from 'bar' // does not work, needs to be ./bar

Yes it throws an error: Can't resolve 'bar' in '/dir/to/root/src' Did you mean './bar'?

oklas commented 1 year ago

Yes it requires a little fix, track it #89.

As of now you can try to use file import, like this:

- import bar from 'bar'
+ import bar from 'bar/index'

May be it need to specify extension (.js or .ts)

SPiCaRiA commented 1 year ago

89

Hmm, not sure if I'm understanding it correctly, but in my case bar is just a JavaScript module, not the index.js 🤔

Am I wrong about what autoscan does at the very beginning? I thought it does something similar as the CRA absolute imports. :x