Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 8f11040

Browse files
committed
feat: Automatic alias mapping based on baseDir in tsconfig.json (ts)
1 parent 079a95f commit 8f11040

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed

docs/development/configuration.md

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -631,14 +631,6 @@ FuseBox.init({
631631
})
632632
```
633633

634-
Alternatively you can use the chainable API
635-
636-
```js
637-
fuse.bundle("app")
638-
.alias("foo", "~/foo/f")
639-
.alias("bar", "~/bar/b")
640-
```
641-
642634

643635
In your code, you would use it in a way similar to this:
644636
```js
@@ -648,6 +640,39 @@ import faraway from "faraway";
648640
console.log(utils, faraway);
649641
```
650642

643+
644+
`baseUrl` option will automatically list your home directory and create aliases, unless `automaticAlias` is set
645+
```json
646+
{
647+
"compilerOptions": {
648+
"baseUrl": "."
649+
}
650+
}
651+
```
652+
653+
## Automatic alias
654+
655+
The option `automaticAlias` is enabled by default, and works only if `tsconfig.json` is found and you are dealing with typescript
656+
657+
```json
658+
{
659+
"compilerOptions": {
660+
"baseUrl": "."
661+
}
662+
}
663+
```
664+
665+
FuseBox will automatically list all your homeDir directories and map aliases for you. Note, that baseURL should be equal `.` and located right in your home directory (for example `src/`)
666+
667+
668+
```
669+
→ Applying automatic alias based on baseUrl in tsconfig.json
670+
671+
components => "~/components"
672+
foo => "~/foo"
673+
index => "~/index"
674+
ui => "~/ui"
675+
```
651676
## Extension Overrides
652677

653678
You can optionally override how file extensions are resolved. This is useful if you want to create platform specific bundles:

src/core/FuseBox.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface FuseBoxOptions {
5555
output?: string;
5656
emitHMRDependencies?: boolean;
5757
filterFile?: (file: File) => boolean;
58+
automaticAlias: boolean;
5859
allowSyntheticDefaultImports?: boolean;
5960
debug?: boolean;
6061
files?: any;
@@ -107,6 +108,11 @@ export class FuseBox {
107108
if (opts.writeBundles !== undefined) {
108109
this.context.userWriteBundles = opts.writeBundles;
109110
}
111+
112+
if (opts.automaticAlias !== undefined) {
113+
this.context.automaticAlias = opts.automaticAlias;
114+
}
115+
110116
// setting targets
111117
opts.target = opts.target || "browser";
112118
const [target, languageLevel] = opts.target.toLowerCase().split("@");

src/core/TypescriptConfig.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class TypescriptConfig {
1313
// the actual typescript config
1414
private config: any;
1515
private customTsConfig: string;
16+
private configFile: string;
1617

1718
constructor(public context: WorkFlowContext) { }
1819

@@ -30,6 +31,25 @@ export class TypescriptConfig {
3031
if (this.context.forcedLanguageLevel) {
3132
this.forceCompilerTarget(this.context.forcedLanguageLevel);
3233
}
34+
35+
if (compilerOptions.baseUrl === "." && this.context.automaticAlias) {
36+
let aliasConfig = {};
37+
let log = [];
38+
fs.readdirSync(this.context.homeDir).forEach(file => {
39+
const extension = path.extname(file);
40+
if (!extension || extension === ".ts" || extension === ".tsx") {
41+
let name = file;
42+
if (extension) {
43+
name = file.replace(/\.tsx?/, "")
44+
}
45+
log.push(`\t${name} => "~/${name}"`)
46+
aliasConfig[name] = `~/${name}`;
47+
}
48+
});
49+
this.context.log.echoInfo(`Applying automatic alias based on baseUrl in tsconfig.json`)
50+
this.context.log.echoInfo(`\n ${log.join("\n")}`)
51+
this.context.addAlias(aliasConfig);
52+
}
3353
}
3454

3555
public forceCompilerTarget(level: ScriptTarget) {
@@ -68,27 +88,27 @@ export class TypescriptConfig {
6888
if (CACHED[cacheKey]) {
6989
this.config = CACHED[cacheKey];
7090
} else {
71-
let url, configFile;
91+
let url;
7292
let config: any = {
7393
compilerOptions: {},
7494
};;
7595
let configFileFound = false;
7696
let tsConfigOverride: any;
7797
if (typeof this.customTsConfig === "string") {
78-
configFile = ensureUserPath(this.customTsConfig);
98+
this.configFile = ensureUserPath(this.customTsConfig);
7999
} else {
80100
url = path.join(this.context.homeDir, "tsconfig.json");
81101
let tsconfig = findFileBackwards(url, this.context.appRoot);
82102
if (tsconfig) {
83103
configFileFound = true;
84-
configFile = tsconfig;
104+
this.configFile = tsconfig;
85105
}
86106
}
87-
if (configFile) {
88-
const configFileRelPath = configFile.replace(this.context.appRoot, "");
107+
if (this.configFile) {
108+
const configFileRelPath = this.configFile.replace(this.context.appRoot, "");
89109
this.context.log.echoInfo(`Typescript config file: ${configFileRelPath}`);
90110
configFileFound = true;
91-
const res = ts.readConfigFile(configFile, (p) => fs.readFileSync(p).toString());
111+
const res = ts.readConfigFile(this.configFile, (p) => fs.readFileSync(p).toString());
92112
config = res.config;
93113
if (res.error) {
94114
this.context.log.echoError(`Errors in ${configFileRelPath}`);
@@ -108,8 +128,8 @@ export class TypescriptConfig {
108128
config.compilerOptions = Object.assign(config.compilerOptions, tsConfigOverride);
109129
}
110130
// allowSyntheticDefaultImports
111-
if( config.compilerOptions.allowSyntheticDefaultImports !== undefined ){
112-
if( this.context.fuse && this.context.fuse.producer ) {
131+
if (config.compilerOptions.allowSyntheticDefaultImports !== undefined) {
132+
if (this.context.fuse && this.context.fuse.producer) {
113133
this.context.fuse.producer.allowSyntheticDefaultImports = config.compilerOptions.allowSyntheticDefaultImports;;
114134
}
115135
}

src/core/WorkflowContext.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ export class WorkFlowContext {
7171

7272
public dynamicImportsEnabled = true;
7373

74+
public automaticAlias = true;
75+
7476
public shim: any;
7577

7678
public writeBundles = true;

0 commit comments

Comments
 (0)