facebook / create-react-app

Set up a modern web app by running one command.
https://create-react-app.dev
MIT License
102.75k stars 26.86k forks source link

Create React App inside a non-empty folder #7802

Open erdemkeren opened 5 years ago

erdemkeren commented 5 years ago

I use create-react-app to create a react app inside an existing backend project which will serve the react application. And I have to run the eject command to:

To automate this process, I took a fork from react-scripts and I defined my folder structure. A

My final file system hierarchy should be something like:

- project directory
    - foo
    - bar
    - baz
    - resources
        - react-app
            - index.js
    - package.json 
    - qux.php

So I edited the template folder to match these requirements and published the scripts to give a try.

npx create-react-app . --scripts-version my-react-scripts

And as you guessed, I got

The directory . contains files that could conflict: Backend FSH.

However:

Describe the solution you'd like

Maybe by setting a constant from my scripts or using an option after npx create-react-app call.

Describe alternatives you've considered

I considered to update create-react-app to support it as well but that doesn't make sense since it would be almost impossible to follow up the updates and upgrades later.

Additional context

https://github.com/facebook/create-react-app/issues/334 https://github.com/facebook/create-react-app/issues/2776

JacobMGEvans commented 5 years ago

Have you tried having another package.json local to the React app?

🤷‍♂ I am just taking a wild shot at this lol

erdemkeren commented 5 years ago

Hello, Thanks for your reply @JacobMGEvans!

Haha! That is one of my first shots as well :) I also considered working with:


But:

All of them programmatically and on every save (backend serves the output).

If I include the package.json inside root/React, then my watch and build commands will write the output to a higher level then the package.json and that doesn't make sense to me. I really feel like it belongs to the whole project since it affects a lot of places out of the React directory in the suggested example.

That is why It makes perfect sense to move the configuration to the root directory. I hope it makes sense to you too.

Having rmdir is great but I am looking for rm -rf at the moment =)

JacobMGEvans commented 5 years ago

Well, how I normally handle a small frontend like that is have a small server, just serving the frontend bundle... Separate from any backend I may have in the same fullstack repo. I don't think this will help you either but its worth a shot... lol 😆 It might look something like this

const express = require(`express`);
require(`dotenv`).config();

express()
  .use(express.static(`${__dirname}/dist`))
  .get(`*`, (req, res) => res.sendFile(`${__dirname}/dist/index.html`))
  .listen(process.env.PORT || 1234, () => console.log(`__SERVER_RUNNING__`, process.env.PORT),
  );
JacobMGEvans commented 5 years ago

Actually I think of it that would be useless unless you are building locally... or have the hot reload setup for it.

I think that it's such a large configuration change that utilizing eject here is what it was made for.

I know that is not the help or suggestion you were looking for, maybe someone with a deeper knowledge can give a better suggestion 😆

erdemkeren commented 5 years ago

@JacobMGEvans It looks like you misunderstood me.

I have a fairly big react SPA frontend and additionally, some single page exports for the non-frontend, backend-served template pages for the same look and feel (which utilizes the same layout, header, footer etc).  😉

And for the "Large configuration change",

I have three or four changes:

I can do everything listed here except initializing the create-react-app app in a non-empty folder.

erdemkeren commented 5 years ago

Here is the export part of my webpack.config.js:

const spaConfig = function (webpackEnv) {
    // [...]

    return {
        ...webpackConfig(webpackEnv),
        name: 'spa-configuration',
        entry: './resources/js/app.jsx',
        output: {
             filename: 'public/js/app.js',
             chunkFilename: isEnvProduction
                ? 'public/js/[name].chunk.js'
                : isEnvDevelopment && 'public/js/[name].chunk.js',
            // [...]
        },
        plugins: [
            // [...]
            new HtmlWebpackLaravelAssetPlugin({ options: '' }),
    };
};

const spConfig = function (webpackEnv) {
    return {
        ...webpackConfig(webpackEnv),
        name: 'sp-configuration',
        entry: {
            403: './resources/js/sp/403.jsx',
            500: './resources/js/sp/500.jsx',
            PostLogoutPage: './resources/js/sp/PostLogoutPage.jsx',
        },
        // [...]
    };
};

module.exports = [spConfig, spaConfig];
JacobMGEvans commented 5 years ago

I see. I think what you are doing is exactly why the eject is available to "own" the configurations. I would definitely be interested in what the maintainers have to say. Pretty slick that you got that to work though!