-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Files outside of app directory will not be transpiled #5666
Comments
Sounds like this is a duplicate of #706 |
Just for reference, this is the extremely ugly workaround I found for this: // NOTE(swatinem): we just assume the typescript loader is configured last
const tsxRules = config.module.rules[config.module.rules.length - 1]
// By default next-typescript only includes things in the app root for no real reason -_-
// See: https://github.com/zeit/next-plugins/blob/be21851f63e82845387e576f5f2ed3e5c448cb97/packages/next-typescript/index.js#L51
// See: https://github.com/zeit/next-plugins/issues/234
tsxRules.include = undefined
// Also, apparently babel does not search for the `.babelrc` based on the `root`
// option correctly when the file to transpile is outside of the root.
// See: https://babeljs.io/docs/en/options#root
// So we just pass a path to the babelrc manually here
defaultLoaders.babel.options.configFile = path.join(__dirname, '.babelrc') |
Thanks @Swatinem! Made it slightly more resilient by searching for the relevant loader rather than relying on index: config.module.rules.forEach((rule) => {
const ruleContainsTs = rule.test.toString().includes('ts|tsx');
if (ruleContainsTs && rule.use && rule.use.loader === 'next-babel-loader') {
rule.include = undefined;
}
}); |
@Swatinem, Thanx for the workaround I've spent few hours till found this thread. |
For easier debug, you can |
Going to track this in #706 |
Apparently someone changed the regex in the rules to |
I feel like this should be re-opened because it's a different issue. This one is for files outside of the nextjs root, and #706 is for files within the nextjs root, but in the |
I seems that transpileModules works for me. module.exports = withBundleAnalyzer(
withTranspileModules({
// To force Next.js to transpile code from other workspace packages.
// https://github.com/martpie/next-transpile-modules
transpileModules: ['evolu'],
// We need server for local next export for integration tests.
// We can detects ZEIT Now runtime via AWS_REGION.
target: process.env.AWS_REGION ? 'serverless' : 'server',
// Enforce relative paths for integration tests.
// https://github.com/zeit/next.js/issues/2581
assetPrefix: './',
}),
); |
You can also specify a path to an external folder.
|
@jakemmarsh This works for me: const ruleString = rule.test ? rule.test.toString() : ''
const ruleContainsTs = /tsx?/.test(ruleString)
if (ruleContainsTs && rule.use && rule.use.loader === 'next-babel-loader') {
rule.include = undefined
} |
Does Next dev mode rebuilds on change of the external files? |
We made this library to do this - it's not 100% reliable but it's pretty good. |
@brysonandrew It looks like that we would also need to look for the loader const hasNextBabelLoader = (r) => {
if (Array.isArray(r.use)) {
return r.use.find(l => l?.loader === 'next-babel-loader')
}
return r.use?.loader === 'next-babel-loader';
}
if (/(ts|tsx)/.test(String(rule.test)) && hasNextBabelLoader(rule)) {
rule.include = [
...rule.include,
path.join(__dirname, '..', 'portal')
];
return rule;
} |
I made even more simple. The fact is that
|
The solution by @spanwair did work however 👍 |
NextJS introduced an experimental option to allow imports from outside the project directory. I tried it out in our mono repo and was able to get it to work |
If if you attempt to transpile In this case, Webpack fails to import The solution was to add Webpack aliases for the missing modules: config.resolve.alias["react/jsx-runtime"] = require.resolve("react/jsx-runtime")
config.resolve.alias["react/jsx-dev-runtime"] = require.resolve("react/jsx-dev-runtime")
// you may want to add more depending on your use case (take it from the error description) so that now my full config is: // next.config.js
module.exports = {
experimental: {
externalDir: true,
},
webpack: config => {
config.resolve.alias["react/jsx-runtime"] = require.resolve("react/jsx-runtime")
config.resolve.alias["react/jsx-dev-runtime"] = require.resolve("react/jsx-dev-runtime")
return config
},
} |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
I have a monorepo usecase, where I want to share code between the next app and other modules that are located outside of the app folder.
It will end up with this error:
To Reproduce
I have set up a simple repo here, based on the next-typescript example:
https://github.com/Swatinem/next-monorepo/tree/master
Expected behavior
Whatever I import should be transpiled like everything else, period.
System information
Additional context
Related issue (possibly a duplicate?): vercel/next-plugins#234
Also related maybe:
The text was updated successfully, but these errors were encountered: