You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Feature]: Support transpiling external modules #1423
What is the new or updated feature that you are suggesting?
It would be great to have the ability to have the remix compiler transpile local modules when working in a monorepo. Essentially this would be a remix equivalent to next-transpile-modules.
One way to get this done currently is via a patch outlined in this gist. In the patch, there's a new config option in remix.config.js.
I believe this issue is also related to #1398. Looking at the compiler code, if we had the ability the bundle all server dependencies that might also solve the issue.
The exact implementation is not super important to me -- I'm more interested in the ability to do this than the api itself.
Why should this feature be included?
It would be very useful for monorepos containing a remix app to be able to have the remix compiler transpile local modules from the monorepo. This makes it really simple & easy to create internal packages to share across multiple apps without the added friction of setting up a build step for each one.
This change also doesn't require opening up esbuild configuration options directly. The patch does touch remix's custom externals plugin, but this shouldn't impact remix's ability to switch compilers. If the remix team decided to switch another compiler, this could be done without breaking the api for this feature (though the functionality would have to be replicated with the other compiler).
thanks to this solution given to overide esbuild config i was able to build my local packages.
i created esbuild-overrides.js
added following code to allow custom packages
constesbuild=require('esbuild');constModule=require('module');functionmanualExternalsPlugin(){return{name: 'manual-externals-overide',setup(build){build.onResolve({filter: /@YourNamespaceOrPackageName/,},(args)=>{return{external: false,namespace: args.path,};},);},};}constoriginalRequire=Module.prototype.require;constoriginalBuild=esbuild.build;functionbuild(options){if(options.platform==='node'){const{ plugins }=options;constexternalPlugin=plugins.find((plugin)=>plugin.name==='manual-externals',);constlocalPlugins=plugins.filter((plugin)=>plugin.name!=='manual-externals',);localPlugins.push(manualExternalsPlugin());localPlugins.push(externalPlugin);returnoriginalBuild({
...options,plugins: localPlugins,});}returnoriginalBuild({
...options,});}Module.prototype.require=function(id){// when remix requires esbuild, it will get our modified build function from aboveif(id==='esbuild'){return{ ...esbuild, build };}returnoriginalRequire.apply(this,arguments);};
fyi - transpile modules can be added to remix by just updating a few lines of code I came up with this a few months ago https://gist.github.com/cj/c6d7a3b1fcf238073951762747c201ba you can now just add transpileModules to your remix.config.js i.e.:
thanks to this solution given to overide esbuild config i was able to build my local packages.
i created esbuild-overrides.js
added following code to allow custom packages
constesbuild=require('esbuild');constModule=require('module');functionmanualExternalsPlugin(){return{name: 'manual-externals-overide',setup(build){build.onResolve({filter: /@YourNamespaceOrPackageName/,},(args)=>{return{external: false,namespace: args.path,};},);},};}constoriginalRequire=Module.prototype.require;constoriginalBuild=esbuild.build;functionbuild(options){if(options.platform==='node'){const{ plugins }=options;constexternalPlugin=plugins.find((plugin)=>plugin.name==='manual-externals',);constlocalPlugins=plugins.filter((plugin)=>plugin.name!=='manual-externals',);localPlugins.push(manualExternalsPlugin());localPlugins.push(externalPlugin);returnoriginalBuild({
...options,plugins: localPlugins,});}returnoriginalBuild({
...options,});}Module.prototype.require=function(id){// when remix requires esbuild, it will get our modified build function from aboveif(id==='esbuild'){return{ ...esbuild, build };}returnoriginalRequire.apply(this,arguments);};
This did not work for me. I get an error SyntaxError: Unexpected token 'export' it looks like esbuild does not transpile the typescript file before loading it. any idea on how to get it to do that from esbuild?
Which feature is this in reference to? #1806 was closed without merge.
transpileModules was the originally proposed config setting. I can see a reference to serverDependenciesToBundle from #1839 but that doesn't sound like the right setting and doesn't seem to be working for me (transpiling typescript from another package in a monorepo).
I cannot get this to work. I'm working with pnpm in a turborepo monorepo. I have the transpileModules setting set in Remix to include my package .. but I get a SyntaxError: Unexpected token 'export' .. when it's main field is pointed to a .ts file .. both have ES2019 as targets .. if I include the package in serverDependenciesToBundle .. I get a different error where it can't find the path I setup in the tsconfig.json ..
I'm trying to port over an existing Next.js application where it was working using next-transpile-modules. Any guidance would be much appreciated.
Edit: I see now that transpileModules is not actually supported. It was a little confusing since there is a majority of 🚀 on that comment 😅 With that said, the question regarding serverDependenciesToBundle still stands. It's not recognizing the tsconfig.json for transpiled packages.. which may be intentional but now how next-transpile-modules functioned. Is there a way around using custom paths for transpiled packages apart from using relative directories in the Remix app tsconfig.json? I also notice that Remix warns about dependencies not being installed that are dependencies in transpiled packages.. What's the workflow I should be adopting for using Remix in a monorepo? Every package has its own build step?
@garretteklof not sure if this will help with your issue, but I just ran into it: I'm working in a monorepo and remix is one of many packages laid out via npm workspaces. I had serverDependenciesToBundle set to be an array of strings of the package names of each package defined in the repo. That stopped working after a recent update. I was able to fix it by changing those strings to RegExps. I believe it's matching individual filenames rather than the package.json names that worked previously.
Reacted by Jascha Brinkmann, Luke Phillippi, Thomas Howlett and Kristofer Giltvedt Selbekk
I am getting this warning in the console (though the dev server seems to work nonetheless):
The path "@shared/ui/styles" is imported in app/routes/index.tsx but "@shared/ui/styles" was not found in your node_modules. Did you forget to install it?
If I change the import in the route to import styles from "@shared/ui/styles.ts" then the warning goes away, but TypeScript says I shouldn't be importing a file with the ts extension. I'm not bothering transpiling the shared UI library since it's only being consumed inside my monorepo and I can control the transpilation.
I had added serverDependenciesToBundle: [/^@shared\//] to my remix config, doesn't seem to be affecting things at a glance. Thoughts on how to get rid of this warning?
Activity
hannadrehman commentedon Jan 12, 2022
i m having same issue. need an ability to transpile local modules in a monorepo.
hannadrehman commentedon Feb 3, 2022
thanks to this solution given to overide esbuild config i was able to build my local packages.
i created
esbuild-overrides.jsadded following code to allow custom packages
then added following scripts
cj commentedon Feb 4, 2022
fyi - transpile modules can be added to remix by just updating a few lines of code I came up with this a few months ago https://gist.github.com/cj/c6d7a3b1fcf238073951762747c201ba you can now just add
transpileModulesto your remix.config.js i.e.:cj commentedon Feb 5, 2022
@airjp73 @hannadrehman I created a pull request here #1806
hannadrehman commentedon Feb 7, 2022
@cj this is great. thanks.
badeAdebayo commentedon Feb 16, 2022
This did not work for me. I get an error
SyntaxError: Unexpected token 'export'it looks like esbuild does not transpile the typescript file before loading it. any idea on how to get it to do that from esbuild?adonaicandido commentedon Feb 22, 2022
This feature has been implemented in the latest release, although would be also helpful to watch those files and hot reload the browser.
Someone currently has a workaround #here.
machour commentedon Mar 18, 2022
Implemented.
If the new implementation still doesn't fit your needs, please open a new issue 🙏🏼
penx commentedon Apr 1, 2022
Which feature is this in reference to? #1806 was closed without merge.
transpileModuleswas the originally proposed config setting. I can see a reference toserverDependenciesToBundlefrom #1839 but that doesn't sound like the right setting anddoesn't seem to be working for me (transpiling typescript from another package in a monorepo).If I addI still get(Where the entry point to this package is a ts file)Edit: not sure what I was doing wrong earlier,
serverDependenciesToBundleis working for me nowgarretteklof commentedon May 4, 2022
I cannot get this to work. I'm working with
pnpmin aturborepomonorepo.I have theif I include the package intranspileModulessetting set in Remix to include my package .. but I get aSyntaxError: Unexpected token 'export'.. when it'smainfield is pointed to a.tsfile .. both haveES2019as targets ..serverDependenciesToBundle.. I get a different error where it can't find thepathI setup in thetsconfig.json..I'm trying to port over an existing Next.js application where it was working using
next-transpile-modules. Any guidance would be much appreciated.Edit: I see now that
transpileModulesis not actually supported. It was a little confusing since there is a majority of 🚀 on that comment 😅 With that said, the question regardingserverDependenciesToBundlestill stands. It's not recognizing thetsconfig.jsonfor transpiled packages.. which may be intentional but now hownext-transpile-modulesfunctioned. Is there a way around using custom paths for transpiled packages apart from using relative directories in the Remix apptsconfig.json? I also notice that Remix warns about dependencies not being installed that are dependencies in transpiled packages.. What's the workflow I should be adopting for using Remix in a monorepo? Every package has its own build step?ianwremmel commentedon May 5, 2022
@garretteklof not sure if this will help with your issue, but I just ran into it: I'm working in a monorepo and remix is one of many packages laid out via npm workspaces. I had
serverDependenciesToBundleset to be an array of strings of the package names of each package defined in the repo. That stopped working after a recent update. I was able to fix it by changing those strings to RegExps. I believe it's matching individual filenames rather than the package.json names that worked previously.osdiab commentedon Sep 3, 2022
I am getting this warning in the console (though the dev server seems to work nonetheless):
If I change the import in the route to
import styles from "@shared/ui/styles.ts"then the warning goes away, but TypeScript says I shouldn't be importing a file with thetsextension. I'm not bothering transpiling the shared UI library since it's only being consumed inside my monorepo and I can control the transpilation.I had added
serverDependenciesToBundle: [/^@shared\//]to my remix config, doesn't seem to be affecting things at a glance. Thoughts on how to get rid of this warning?