make alias resolver independent of package/app

This commit is contained in:
Brendan Allan 2023-01-20 12:21:20 +08:00
parent c2ab9466f5
commit 77fb8a27e8
4 changed files with 20 additions and 15 deletions

View file

@ -1,9 +1,9 @@
// WARNING: BE CAREFUL SAVING THIS FILE WITH A FORMATTER ENABLED. The import order is important and goes against prettier's recommendations.
import React, { Suspense } from 'react';
import ReactDOM from 'react-dom/client';
// THIS MUST GO BEFORE importing the App
import '~/patches';
import './patches'; // THIS MUST GO BEFORE importing the App
import App from './App';
import '@sd/ui/style';

View file

@ -2,7 +2,12 @@
"extends": "../../packages/config/base.tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"declarationDir": "dist"
"declarationDir": "dist",
"paths": {
"~/*": [
"./src/*"
]
}
},
"include": [
"src"

View file

@ -1,8 +1,9 @@
import { relativeAliasResolver } from '@sd/config/vite';
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import svgr from 'vite-plugin-svgr';
import tsconfigPaths from 'vite-tsconfig-paths';
import { relativeAliasResolver } from "@sd/config/vite"
import { name, version } from './package.json';
// https://vitejs.dev/config/

View file

@ -1,24 +1,23 @@
const fs = require("fs/promises")
const path = require("path")
const fs = require('fs/promises');
const path = require('path');
// only supports files rn
module.exports = {
relativeAliasResolver: {
find: /^(~\/.+)/,
replacement: "$1",
replacement: '$1',
async customResolver(source, importer) {
const [repo, filePath] = importer.split("/packages/");
const [pkg] = filePath.split("/src/")
const [pkg] = importer.split('/src/');
const sourcePath = source.substring(2)
const [_, sourcePath] = source.split('~/');
const absolutePath = `${repo}/packages/${pkg}/src/${sourcePath}`;
const absolutePath = `${pkg}/src/${sourcePath}`;
const folderItems = await fs.readdir(path.join(absolutePath, "../"));
const folderItems = await fs.readdir(path.join(absolutePath, '../'));
const item = folderItems.find(i => i.startsWith(sourcePath.split("/").at(-1)))
const item = folderItems.find((i) => i.startsWith(sourcePath.split('/').at(-1)));
return absolutePath + path.extname(item)
return absolutePath + path.extname(item);
}
}
}
};