From 219685c7526c99165f191cca29b857036a5a0206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alvarez?= Date: Wed, 13 Dec 2023 11:50:04 +0100 Subject: [PATCH] feat(packages/sui-ssr): add ssr dev support --- packages/sui-ssr/README.md | 57 +++++++++++++++--------- packages/sui-ssr/bin/sui-ssr.js | 1 + packages/sui-ssr/server/utils/factory.js | 13 ++++-- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/packages/sui-ssr/README.md b/packages/sui-ssr/README.md index e2cffdb56..cf13d8e7f 100644 --- a/packages/sui-ssr/README.md +++ b/packages/sui-ssr/README.md @@ -13,25 +13,40 @@ SSR can be tought to configure and maintain. SSR handles that for you providing: npm install @s-ui/ssr --save ``` +## Development + +Starts a development server. + +``` +Usage: sui-ssr-dev [options] + +Options: + -L, --link-all [monorepo] Link all packages inside of monorepo multipackage + -l, --link-package [package] Replace each occurrence of this package with an absolute path to this folder (default: []) + -h, --help display help for command + +Examples: + + $ sui-ssr dev + $ sui-ssr dev --link-package ./domain --link-all ./components +``` + ## Build Generate a static version of the server w/out dependencies in the server folder. ``` - Usage: sui-ssr-build [options] - - Options: +Usage: sui-ssr-build [options] - -C, --clean Remove build folder before create a new one - -V, --verbose Verbose output - -h, --help output usage information - Description: +Options: - Build a production ready ssr server + -C, --clean Remove build folder before create a new one + -V, --verbose Verbose output + -h, --help output usage information - Examples: +Examples: - $ sui-ssr build + $ sui-ssr build ``` ## Archive @@ -41,21 +56,21 @@ Create a zip file with all assets needed to run the server in any infra. It will, over parameter, make that the express server run over a username and password in a htpasswd way. ``` - Usage: sui-ssr-archive [options] +Usage: sui-ssr-archive [options] - Options: +Options: - -C, --clean Remove previous zip - -R, --docker-registry Custom registry to be used as a proxy or instead of the Docker Hub registry - -E, --entry-point Relative path to an entry point script to replace the current one -> https://bit.ly/3e4wT8C - -h, --help Output usage information - -A, --auth Will build the express definition under authentication htpassword like. - -O, --outputFileName A string that will be used to set the name of the output filename. Keep in mind that the outputFilename will have the next suffix -sui-ssr.zip + -C, --clean Remove previous zip + -R, --docker-registry Custom registry to be used as a proxy or instead of the Docker Hub registry + -E, --entry-point Relative path to an entry point script to replace the current one -> https://bit.ly/3e4wT8C + -h, --help Output usage information + -A, --auth Will build the express definition under authentication htpassword like. + -O, --outputFileName A string that will be used to set the name of the output filename. Keep in mind that the outputFilename will have the next suffix -sui-ssr.zip - Examples: +Examples: - $ sui-ssr archive - $ sui-ssr archive --outputFileName=myFile // output: myFile-sui-ssr.zip + $ sui-ssr archive + $ sui-ssr archive --outputFileName=myFile // output: myFile-sui-ssr.zip ``` ### IMPORTANT!! diff --git a/packages/sui-ssr/bin/sui-ssr.js b/packages/sui-ssr/bin/sui-ssr.js index d758e3ebc..e0a4d0e8f 100755 --- a/packages/sui-ssr/bin/sui-ssr.js +++ b/packages/sui-ssr/bin/sui-ssr.js @@ -9,5 +9,6 @@ program.version(version, ' --version') program.command('build', 'Build a ssr server').alias('b') program.command('archive', 'Create a server.zip file').alias('a') program.command('release', 'Release new version of the server').alias('r') +program.command('dev', 'Start a ssr server in development mode').alias('d') program.parse(process.argv) diff --git a/packages/sui-ssr/server/utils/factory.js b/packages/sui-ssr/server/utils/factory.js index 897825c9d..1d53e8cd0 100644 --- a/packages/sui-ssr/server/utils/factory.js +++ b/packages/sui-ssr/server/utils/factory.js @@ -1,6 +1,6 @@ -const IS_PRODUCTION = process.env.NODE_ENV === 'production' const DEFAULT_SITE_HEADER = 'X-Serve-Site' -const DEFAULT_PUBLIC_FOLDER = IS_PRODUCTION ? 'public' : '.sui/public' +const DEFAULT_PUBLIC_FOLDER = 'public' +const DEFAULT_DEV_PUBLIC_FOLDER = '.sui/public' const DEFAULT_MULTI_SITE_KEY = 'default' const EXPRESS_STATIC_CONFIG = {index: false} @@ -29,8 +29,15 @@ export default ({path, fs, config: ssrConf = {}, assetsManifest}) => { } const publicFolder = req => { + if (process.env.NODE_ENV !== 'production') { + return DEFAULT_DEV_PUBLIC_FOLDER + } + const site = siteByHost(req) - if (!site || !IS_PRODUCTION) return DEFAULT_PUBLIC_FOLDER + + if (!site) { + return DEFAULT_PUBLIC_FOLDER + } return multiSitePublicFolder(site) }