Comments (12)
Low effort, high impact task maybe? 🙏 😅
from remix.
I like this because nested routes make imports super annoying, and I generally prefer flat file structures (but the nested routing is worth it).
If we had ~/home/imports
then all my frustration would be gone. And it would make moving routes around a lot easier because they don't ever import each other. You could literally just drag/drop a folder into another layout route folder and it would all still work.
What I don't like about these kinds of aliases is that it can break tooling, like editors "go to file" and "go to definition" functionality, or typescript being able to find the types, etc. Pretty sure most of these tools are configurable to understand our alias though, so might still be worth it.
from remix.
I made a simple patch to enable this. The Remix templates create a tsconfig.json
file in ./app
. The default should be updated to include:
...
"paths": {
"~/*": ["./*"]
}
This makes imports from ~/some/path
relative to the /app
folder.
The only reason the Remix compiler doesn't support this is because the function isBareModuleId()
checks to see if the import doesn't start with '.' (relative) or '/' absolute. If not, then it assumes it's a node module.
I simply updated the function like so
function isBareModuleId(id) {
return !id.startsWith(".") && !id.startsWith('~') && !path.isAbsolute(id);
}
And now both VS Code and esbuild compiles it just fine.
Before
import Json from '../../../../components/Json'
import { getUser } from '../../../../data/db'
import { getSessionFromCache, commitSession } from '../../../../session'
import { getFromCache } from '../../../../utils/cache'
After
import Json from '~/components/Json'
import { getUser } from '~/data/db'
import { getSessionFromCache, commitSession } from '~/session'
import { getFromCache } from '~/utils/cache'
Of course, this doesn't fully support the paths property. That is, it won't support something like @app
as the @ sign is still treated like a scoped node module. You'd have to parse tsconfig.json and compare the paths entries to be fully correct, but I think this is a good initial implementation.
from remix.
Can't wait for this to get released!
from remix.
I think this can be closed now
from remix.
Yep, typescript jest and eslint all have the ability to handle these kinds of aliases without any trouble with a little config. It would just need to be documented. Easy peasy
from remix.
I am wondering if this could be solved adding @esbuild-plugins/tsconfig-paths
?
from remix.
Sweet! Thanks @kiliman! Here's my patch (this includes a patch for dynamic import support):
diff --git a/node_modules/@remix-run/dev/compiler.js b/node_modules/@remix-run/dev/compiler.js
index 923d122..cbc40ce 100644
--- a/node_modules/@remix-run/dev/compiler.js
+++ b/node_modules/@remix-run/dev/compiler.js
@@ -239,7 +239,7 @@ async function createServerBuild(config, options) {
}
function isBareModuleId(id) {
- return !id.startsWith(".") && !path.isAbsolute(id);
+ return !id.startsWith(".") && !id.startsWith('~') && !path.isAbsolute(id);
}
function getNpmPackageName(id) {
diff --git a/node_modules/@remix-run/dev/compiler/assets.js b/node_modules/@remix-run/dev/compiler/assets.js
index 2e9bf3f..b465ff6 100644
--- a/node_modules/@remix-run/dev/compiler/assets.js
+++ b/node_modules/@remix-run/dev/compiler/assets.js
@@ -37,7 +37,7 @@ async function createAssetsManifest(config, metafile) {
module: resolveUrl(key),
imports: resolveImports(output.imports)
};
- } else {
+ } else if (output.entryPoint.startsWith("browser-route-module:")) {
let route = routesByFile.get(entryPointFile);
invariant['default'](route, `Cannot get route for entry point ${output.entryPoint}`);
let sourceExports = await routes.getRouteModuleExportsCached(config, route.id);
from remix.
So this feature does not work for me - I am using it as part of @shopify/hydrogen but I think Remix is handling this. I have changed my package.json type to "module" which I suspect is the issue though I find very little info online to support the idea that this prevents the alias from working. Anyone try this with "type":"module"
?. My tsconfig specifies the alias and Shopify's demo store is built with it but somewhere in my upgrades to ESM it broke.
from remix.
You may be interested in the new approach taken by the Epic Stack: https://github.com/epicweb-dev/epic-stack/blob/main/docs/decisions/031-imports.md
It's based on a Node.js standard.
from remix.
Thanks but shopify hydrogen already makes use of ~ and this only works for # prefix.
from remix.
It actually turns out that the code works. Remix just throws errors that are meaningless evidently.
from remix.
Related Issues (20)
- [vite] Files shared between Remix and Express are loaded twice HOT 2
- `useRouteLoaderData("root")` returns `undefined` in root's `ErrorBoundary`/`Layout` when non-existent page is loaded right away. HOT 10
- `TypeError: Can't modify immutable headers.` in handleDataRequestRR (Cloudflare Pages) HOT 2
- When use custom routes, pages use `root.tsx` Layout only and skip own by prefix pattern HOT 1
- Vulnerability detected in library Braces
- [Vite] Remix probably serves malformed response HOT 4
- Node Fetch: Cannot cancel a stream that already has a reader HOT 3
- Random preloads added for images HOT 1
- `assetsDir` not properly resolved when using `basename` when using Wrangler HOT 1
- Splat Routes that would have been prioritized can be overridden by Dynamic Segments. HOT 7
- remix hydration error when using gtag
- [Vite] [Cloudflare] dev server start failed with http_proxy environment variable
- Onclick handler not working in remix and problem installing tailwindcss HOT 5
- MetaFunction data type not inferred from clientLoader HOT 3
- Application Error: failed to execute insertBefore on Node HOT 18
- Vitest looks for package.json in every workspace when using vitePlugin HOT 2
- [BUG]: cloudflareDevProxyVitePlugin: Issue in DEV with Remix/Vite/Drizzle/Cloudflare Pages/Supabase/Postgres HOT 2
- Can't import node_module SVG HOT 2
- Streaming does work not on Vite/MacOS HOT 2
- Hydration issues in prod builds of Cloudflare starter template HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from remix.