-
Notifications
You must be signed in to change notification settings - Fork 48
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
Using NX libs not possible in Strapi project #49
Comments
I don't have any active strapi project atm so i'm not quite sure, I do see that they are working on typescript support so it should then be a matter of updating that config with the aliases / extend the root one. Maybe soon i will have a use case again for Strapi and will than also check the typescript implementation. |
I was facing this exact issue and needed to get Strapi working in the monorepo with integration for nx libraries. I've decided to create scripts to get this to work with an nx setup. More on the scripts below. There are two key components to get Strapi to work with nx libraries: 1. Handle the new folder structure due to the compiled librariesBefore, the dist folder looked like this:
Now, since we include the libraries, this folder structure changes to:
This needs to be accounted for in various places, e.g. when resolving local plugins: // config/plugins.ts
// ...
'my-plugin': {
enabled: true,
resolve: `./${isRunningInServeMode ? '' : 'apps/my-app/'}src/plugins/my-plugin`,
},
// ... 2. Use of tsconfig-pathsI'm making heavy use of the tsconfig-paths plugin in order to make the imports of the nx libraries map to the respective files. For admin panel plugins to access nx libraries, we need to include the tsconfig-paths-webpack-plugin plugin in the admin webpack configuration. // apps/my-app/src/admin/webpack.config.js
const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
module.exports = (config, webpack) => {
config.resolve.plugins = [
new TsconfigPathsPlugin({
configFile: 'tsconfig.base.json',
baseUrl: process.env.NODE_ENV === 'production' ? 'dist/apps/my-app' : 'apps/my-app/dist',
extensions: ['.ts', '.js'],
}),
];
return config;
};
Simply omit the extension, e.g.: // tsconfig.base.json
"@my-project/shared/example": ["libs/shared/src/example.ts"] // fails
"@my-project/shared/example": ["libs/shared/src/example"] // works We also need to update the tsconfig files for both the admin panel as well as the server-side one. // apps/my-app/tsconfig.json
{
// We need to extend our base tsconfig file
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
// Add all other compilerOptions from https://github.com/strapi/strapi/blob/main/packages/utils/typescript/tsconfigs/server.json
// ...
},
} // apps/my-app/src/admin/tsconfig.json
{
// We need to extend our base tsconfig file
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"module": "ES2020",
// Add all other compilerOptions from https://github.com/strapi/strapi/blob/main/packages/utils/typescript/tsconfigs/admin.json
// ...
},
} Scripts to serve, build and start (a production build)To serve and build the project using the local script, I updated the project configuration: // apps/my-app/project.json
// ...
"targets": {
"serve": {
"executor": "nx:run-commands",
"options": {
"command": "node apps/my-app/scripts/serve.js",
"envFile": "apps/my-app/.env"
}
},
"build": {
"executor": "nx:run-commands",
"options": {
"command": "node apps/my-app/scripts/build.js"
}
}
}
// ... Serve the Strapi project locally: // apps/my-app/scripts/serve.js
const tsConfig = require('../../../tsconfig.base.json');
const tsConfigPaths = require('tsconfig-paths');
const strapi = require('@strapi/strapi');
const path = require('path');
const { buildAdmin } = require('@strapi/strapi/lib/commands/builders');
const tsUtils = require('@strapi/typescript-utils');
const appName = 'my-app';
const strapiRoot = path.join(__dirname, '..');
const distDirRoot = path.join(strapiRoot, 'dist');
const distDirApp = path.join(distDirRoot, 'apps', appName);
tsConfigPaths.register({
baseUrl: distDirRoot,
paths: tsConfig.compilerOptions.paths,
});
(async () => {
await tsUtils.compile(strapiRoot, { watch: false });
await buildAdmin({ forceBuild: true, buildDestDir: distDirApp, srcDir: strapiRoot });
const app = strapi({ appDir: strapiRoot, distDir: distDirApp });
app.start();
})(); Build it for production: // apps/my-app/scripts/build.js
const tsConfig = require('../../../tsconfig.base.json');
const tsConfigPaths = require('tsconfig-paths');
const path = require('path');
const { buildAdmin } = require('@strapi/strapi/lib/commands/builders');
const tsUtils = require('@strapi/typescript-utils');
const appName = 'my-app';
const strapiRoot = path.join(__dirname, '..');
const distDirRoot = path.join(strapiRoot, '../../dist/apps', appName);
const distDirApp = path.join(distDirRoot, 'apps', appName);
tsConfigPaths.register({
baseUrl: distDirRoot,
paths: tsConfig.compilerOptions.paths,
});
(async () => {
await tsUtils.compile(strapiRoot, {
watch: false,
configOptions: {
options: {
outDir: distDirRoot,
},
},
});
await buildAdmin({ forceBuild: true, buildDestDir: distDirApp, srcDir: strapiRoot });
})(); Run the built version in production: // apps/my-app/scripts/start.js
const tsConfig = require('../tsconfig.base.json');
const tsConfigPaths = require('tsconfig-paths');
const strapi = require('@strapi/strapi');
const path = require('path');
const appName = 'my-app';
const strapiRoot = path.join(__dirname, '..');
const distDirRoot = strapiRoot;
const distDirApp = path.join(distDirRoot, 'apps', appName);
tsConfigPaths.register({
baseUrl: distDirRoot,
paths: tsConfig.compilerOptions.paths,
});
(async () => {
const app = strapi({ appDir: strapiRoot, distDir: distDirApp });
app.start();
})(); Running in productionA few things need to copied to the dist folder
Caveats:
I hope this insight into my experience with setting this up is helpful to somebody else facing the same or similar issues. |
I decided to throw together an example project using the scripts mentioned above: florianmrz/nx-strapi-with-libraries-example @TriPSs I hope this is helpful to maybe get this supported through this package. Even though it was quite a few things that needed to be changed in order to get this to work, it might be feasible to enable this behind an option flag? |
@florianmrz first of all thanks for figuring all this shit out! I already tried a bit to make it work with everything you mentioned, it then was able to build but the output become very weird (keeping the folder structure of the libs/apps) causing it not to start. When I have some time again I will try to continue to implement/make something work with what you mentioned above so we can add proper support for it through this package. |
I finally found the way to run strapi with the Nx libs but with the provided scripts there aren't any ways to edit the content type. Please help :( |
I found the way. Adding
After save some content type update the process dies:
I suppose this is normal because strapi use internally the node clusters feature and now the process is managed by us. Simply start again the server after crash works. |
After some time trying to replicate the strapi develop script I found the way the server restarts automatically:
|
Seeing the develop strapi script It's possible to run with watch mode. Here is the script with watch mode and restart capabilities:
|
@icastillejogomez where you able to produce a actual prod build with the scripts? The issues that I encountered was once I create a prod build it was unable to start as the build output was completely wrong. |
Hi,
thanks for the strapi extension!
Currently it is not possible to use nx-libs in the strapi-server.
eg. I have a JS/ lib
@my-nx-workspace/utils
, then I can't require it likeconst utils = require('@my-nx-workspace/utils');
inside strapi.Any ideas how to solve that mix of old require and new ES6 importing?
Thanks
The text was updated successfully, but these errors were encountered: