diff --git a/docs/blog/version-4.5-release-notes.md b/docs/blog/version-4.5-release-notes.md index b380d54653..17fd1edfa3 100644 --- a/docs/blog/version-4.5-release-notes.md +++ b/docs/blog/version-4.5-release-notes.md @@ -14,6 +14,14 @@ Version 4.5 of [Foal](https://foalts.org/) is out! +## CLI fixes + +When running `npx foal connect react` to connect the React application to the Foal application in development, the following features did not work: +- Proxify requests from the client to the server without needing to enable CORS or specify a different port in development. +- Build the client application in the server application's public directory. + +This is fixed in v4.5. + ## Global use of CLI deprecated In previous versions, the tutorial suggested installing the CLI globally to create a new application or generate files. However, it is considered bad practice to install a dependency globally for local use. diff --git a/docs/docs/frontend/angular-react-vue.md b/docs/docs/frontend/angular-react-vue.md index 6685c21997..dc12008ce8 100644 --- a/docs/docs/frontend/angular-react-vue.md +++ b/docs/docs/frontend/angular-react-vue.md @@ -70,6 +70,4 @@ One way to get around this, keeping the policy and the same codebase, is to conf ### Build Outpath -> *This feature only works with Angular and Vue.* - The `connect` command also modifies the build output path of your front so that its bundles are saved in the `public/` directory. This way, you can run the frontend and the backend build commands and directly ship the application to production. diff --git a/docs/docs/tutorials/real-world-example-with-react/14-production-build.md b/docs/docs/tutorials/real-world-example-with-react/14-production-build.md index 7358ae68f6..13799a123c 100644 --- a/docs/docs/tutorials/real-world-example-with-react/14-production-build.md +++ b/docs/docs/tutorials/real-world-example-with-react/14-production-build.md @@ -14,11 +14,7 @@ In your frontend directory, run the following command: npm run build ``` -This command builds the React application for production and saves the files in the `build` directory. - -Open it and copy all its contents to the `public` directory of your Foal application. - -> When you use `npx foal connect` with Angular or Vue, the frontend build will automatically save the files in `public`. +This command builds the React application for production and saves the files in the `public` directory of your Foal application. Now, if you navigate to [http://localhost:3001](http://localhost:3001), you will see the frontend application served by the backend server. diff --git a/packages/cli/src/generate/generators/react/connect-react.spec.ts b/packages/cli/src/generate/generators/react/connect-react.spec.ts index 2d16780763..a3425b876f 100644 --- a/packages/cli/src/generate/generators/react/connect-react.spec.ts +++ b/packages/cli/src/generate/generators/react/connect-react.spec.ts @@ -9,7 +9,7 @@ describe('connectReact', () => { afterEach(() => fs.tearDown()); - it('should update package.json to set up the proxy, install ncp and change the output dir.', () => { + it('should update package.json and create a .env.development file to set up the proxy.', () => { fs .ensureDir('connector-test/react') .copyFixture('react/package.json', 'connector-test/react/package.json'); @@ -17,9 +17,21 @@ describe('connectReact', () => { connectReact('./connector-test/react'); fs - .assertEqual('connector-test/react/package.json', 'react/package.json'); + .assertEqual('connector-test/react/package.json', 'react/package.json') + .assertEqual('connector-test/react/.env.development', 'react/env.development'); }); + it('should create a .env file with the path to the output dir.', () => { + fs + .ensureDir('connector-test/react') + .copyFixture('react/package.json', 'connector-test/react/package.json'); + + connectReact('./connector-test/react'); + + fs + .assertEqual('connector-test/react/.env', 'react/env'); + }) + it('should not throw if the path does not exist.', () => { connectReact('somewhere-that-does-not-exist'); }); diff --git a/packages/cli/src/generate/generators/react/connect-react.ts b/packages/cli/src/generate/generators/react/connect-react.ts index 6b632038be..9185e11d76 100644 --- a/packages/cli/src/generate/generators/react/connect-react.ts +++ b/packages/cli/src/generate/generators/react/connect-react.ts @@ -1,4 +1,4 @@ -import { join } from 'path'; +import { join, relative } from 'path'; import { red } from 'colors/safe'; import { FileSystem } from '../../file-system'; @@ -20,11 +20,17 @@ export function connectReact(path: string) { return; } + const outputPath = join(relative(path, process.cwd()), 'public') + // Make projects generated on Windows build on Unix. + .replace(/\\/g, '/'); + fs .cd(path) .modify('package.json', content => { const pkg = JSON.parse(content); pkg.proxy = 'http://localhost:3001'; return JSON.stringify(pkg, null, 2); - }); + }) + .copy('react/env.development', '.env.development') + .render('react/env', '.env', { path: outputPath }); } diff --git a/packages/cli/src/generate/generators/vue/connect-vue.spec.ts b/packages/cli/src/generate/generators/vue/connect-vue.spec.ts index 47f273a063..d1c188be54 100644 --- a/packages/cli/src/generate/generators/vue/connect-vue.spec.ts +++ b/packages/cli/src/generate/generators/vue/connect-vue.spec.ts @@ -9,7 +9,7 @@ describe('connectVue', () => { afterEach(() => fs.tearDown()); - it('should update package.json to set up the proxy, install ncp and change the output dir.', () => { + it('should update package.json to set up the proxy and change the output dir.', () => { fs .ensureDir('connector-test/vue') .copyFixture('vue/package.json', 'connector-test/vue/package.json'); diff --git a/packages/cli/src/generate/specs/react/env b/packages/cli/src/generate/specs/react/env new file mode 100644 index 0000000000..29b661e6e4 --- /dev/null +++ b/packages/cli/src/generate/specs/react/env @@ -0,0 +1 @@ +BUILD_PATH='../../public' diff --git a/packages/cli/src/generate/specs/react/env.development b/packages/cli/src/generate/specs/react/env.development new file mode 100644 index 0000000000..0ca677faea --- /dev/null +++ b/packages/cli/src/generate/specs/react/env.development @@ -0,0 +1,3 @@ +# Proxify requests to the development server. +# See https://github.com/facebook/create-react-app/issues/12304 +DANGEROUSLY_DISABLE_HOST_CHECK=true diff --git a/packages/cli/src/generate/templates/react/env b/packages/cli/src/generate/templates/react/env new file mode 100644 index 0000000000..2c2909855f --- /dev/null +++ b/packages/cli/src/generate/templates/react/env @@ -0,0 +1 @@ +BUILD_PATH='/* path */' diff --git a/packages/cli/src/generate/templates/react/env.development b/packages/cli/src/generate/templates/react/env.development new file mode 100644 index 0000000000..0ca677faea --- /dev/null +++ b/packages/cli/src/generate/templates/react/env.development @@ -0,0 +1,3 @@ +# Proxify requests to the development server. +# See https://github.com/facebook/create-react-app/issues/12304 +DANGEROUSLY_DISABLE_HOST_CHECK=true