Skip to content

Commit

Permalink
feat: autoscan #70
Browse files Browse the repository at this point in the history
  • Loading branch information
oklas committed Mar 15, 2022
1 parent 125b072 commit 7f04ddb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
4 changes: 3 additions & 1 deletion example/main/config-overrides.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const {aliasWebpack, aliasJest} = require('react-app-alias')

const options = {}
const options = {
autoscan: 'src',
}

module.exports = aliasWebpack(options)
module.exports.jest = aliasJest(options)
6 changes: 6 additions & 0 deletions example/main/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ test('renders text', () => {
expect(linkElement).toBeInTheDocument();
});

test('renders internal component from src', () => {
render(<App />);
const nearElement = screen.getByText(/Internal/i);
expect(nearElement).toBeInTheDocument();
});

test('renders component from near src', () => {
render(<App />);
const nearElement = screen.getByText(/Near src/i);
Expand Down
2 changes: 2 additions & 0 deletions example/main/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React from 'react'
import NearSrc from 'near-src/NearSrc'
import Internal from 'Internal/index'
import './App.css'

function App() {
return (
<div className="App">
<h2>Main</h2>
<Internal />
<NearSrc />
</div>
);
Expand Down
9 changes: 9 additions & 0 deletions example/main/src/Internal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'

function Internal() {
return (
<h3>Internal</h3>
)
}

export default Internal
35 changes: 33 additions & 2 deletions packages/react-app-alias/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,30 @@ function aliasJest(options) {
}
}

function autoscan(tasks) {
const dirlist = dir =>
fs.readdirSync(dir).filter(
file => fs.statSync(path.resolve(dir, file)).isDirectory())
if(!Array.isArray(tasks)) tasks = tasks ? [tasks] : []
tasks = tasks.map(task => (task===task+'') ? {path: task} : task)
tasks = tasks.map(task => ({
prefix: '',
suffix: '',
...task,
}))
const aliasMap = tasks.map(task => (
dirlist(task.path).reduce(
(a, alias) => ({
...a,
[task.prefix + alias + task.suffix]:
path.join(task.path, alias)
}),
{}
)
)).reduce((a, map) => ({...a, ...map}), {})
return aliasMap
}

function configFilePath(configPath = '') {
if(
configPath.length > 0 && fs.existsSync(path.resolve(paths.appPath, configPath))
Expand Down Expand Up @@ -140,22 +164,28 @@ function configPathsRaw(confPath) {
function configPaths(configPath = '') {
const confPath = configFilePath(configPath)
const paths = configPathsRaw(confPath)
return Object.keys(paths).reduce( (a, path) => {
const aliasMap = Object.keys(paths).reduce( (a, path) => {
const value = paths[path]
const target = Array.isArray(value) ? value[0] : value
a[path.replace(/\/\*$/,'')] = target.replace(/\/\*$/,'')
return a
}, {})
return aliasMap
}

function defaultOptions(options) {
const configPath = configFilePath(
options.tsconfig || options.jsconfig
)
const aliasMap = options.alias || configPaths(configPath)
const aliasAutoMap = autoscan(options.autoscan)

const opts = {
...options,
aliasMap,
aliasMap: {
...aliasAutoMap,
...aliasMap,
},
}
return opts
}
Expand All @@ -182,6 +212,7 @@ const CracoAliasPlugin = {
module.exports = {
aliasWebpack,
aliasJest,
autoscan,
configFilePath,
configPathsRaw,
configPaths,
Expand Down

0 comments on commit 7f04ddb

Please sign in to comment.