-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
@nrwl/next:server with customServerTarget does not seem to work with typescript libs in monorepo #12032
Comments
I believe it related to this #11858, try changing the |
Till @jaysoo rolls out something permanent, here is a quick change to get those who are blocked moving forward. It works for the dev server (watch) as well as the production builds. If you have not changed the Hope this helps to unblock those who need a nextjs custom server for enabling //@ts-check
const path = require('path');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { withNx } = require('@nrwl/next/plugins/with-nx');
// supported loaders
const supportedLoaders = ['next-babel-loader', 'next-swc-loader'];
// paths outside next we want to include and watch
const externalPaths = [path.join(__dirname, '../..', 'libs')];
/**
* Checks if module rule has supported loader
* @param rule module rule
* @returns true if rule has a supported loader
*/
const hasNextSupportedLoader = (rule) => {
if (Array.isArray(rule.use)) {
return rule.use.find((l) => supportedLoaders.includes(l?.loader));
}
return supportedLoaders.includes(rule.use?.loader);
};
/**
* Includes external module if they have supported loader
* @param rule module rule
*/
const appendExternalPaths = (rule) => {
const pattern = String(rule.test);
if ((pattern && pattern.includes('tsx|ts')) || pattern.includes('ts|tsx')) {
if (hasNextSupportedLoader(rule)) {
rule.include = [...(rule?.include || []), ...externalPaths];
}
}
};
/**
* @type {import('@nrwl/next/plugins/with-nx').WithNxOptions}
**/
const nextConfig = {
nx: {
// Set this to true if you would like to to use SVGR
// See: https://github.com/gregberge/svgr
svgr: false,
},
reactStrictMode: true,
webpack: (config) => {
config.module.rules.forEach((rule) => {
if (rule.test) {
appendExternalPaths(rule);
} else if (rule.oneOf) {
rule.oneOf.forEach((rule) => {
if (rule.test) {
appendExternalPaths(rule);
}
});
}
});
return config;
},
};
module.exports = withNx(nextConfig); |
@un33k thx for the snippet. It helped me import libs in custom server pages. I'm having an issue tho with using libs in the custom server files for example |
Just add transpilePackages to your next.config.js file
To use transpilePackages you need the Next version up to 13.1.0 |
@olafkrawczyk I'm having exactly the same issue. Have you found a solution? |
@olafkrawczyk @kenleytomlin I was facing this blocker as well. My solution was to switch executors on An example config that works for me. My app is called "build-custom-server": {
"executor": "@nx/webpack:webpack",
"defaultConfiguration": "production",
"options": {
"outputPath": "dist/packages/next-app",
"main": "packages/next-app/server/main.ts",
"tsConfig": "packages/next-app/tsconfig.server.json",
"compiler": "swc",
"target": "node",
"deleteOutputPath": false,
"generatePackageJson": true,
"assets": []
},
"configurations": {
"development": {
"outputPath": "packages/next-app/tmp"
},
"production": {}
}
}, |
@nsmith7989 thanks for the update, will try it! |
HI @olafkrawczyk did that resolved your issue . I am facing the same issue in our mono repo app. |
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
Current Behavior
I'm trying to integrate a custom server into my existing nextjs app in a monorepo with multiple libs.
I ran the generator
nx generate @nrwl/next:custom-server
to create the custom server and project.json scaffolding.However, every time I try to serve it in development mode with
nx serve
, which uses@nrwl/next:server
, I'm met with the following:It seems like whatever webpack config is used under the hood isn't properly transpiling my typescript files when I use a customServerTarget.
If I remove the target and just build as normal, everything works fine.
My hunch is there's a disconnect in the code path between when you want to also build a custom server. It's apparent from the code that it branches here, and it looks like the custom server portion does much less heavy lifting.
If this is expected, it would really help to have more documentation to understand exactly how to handle this scenario when you have ts libs.
Expected Behavior
I expect the custom server to start up successfully with the same webpack config used in the normal dev server, with no typescript issues. OR some extra documentation on how to exactly use the customServerTarget option.
Steps to Reproduce
nx generate @nrwl/next:custom-server <app-name>
to add a custom server into your appnx serve
You will see a log similar to the following:
Failure Logs
Environment
The text was updated successfully, but these errors were encountered: