Skip to content

How to bundle all js, css and images files as one file using react-scripts? #3365

Closed

Description

@willowTank

Dear Sir!
I would like to bundle all assets files as one js file.
I am using react-scripts.
Please help me.
best Regards

Activity

  1. Timer commented on Oct 31, 2017

    @Timer
    Contributor

    Unfortunately, we do not allow tuning like this -- you'd need to eject.

    What specific use case are you trying to accomplish?

  2. willowTank commented on Oct 31, 2017

    @willowTank
    Author

    Thanks for your reply. I want all assets files are combined as one file.

  3. Timer commented on Oct 31, 2017

    @Timer
    Contributor

    Yes, but for what reason? Why do you have this requirement?

    Also, if you eject you can configure all assets to go through file-loader instead of url-loader, & disable extract css plugin.

  4. gaearon commented on Nov 3, 2017

    @gaearon
    Contributor

    What do you mean when you say “one file”? Which particular assets do you want to see combined and why?

  5. samueledirito commented on Mar 27, 2018

    @samueledirito

    Sorry to reopen this old thread, but I have the same question.
    Has anything changed? Do I still need ejecting the app to have it all in one bundle.js file?

    I'd like to use Zalando's Tailor to move into microfrontend world. This lib requests to serve a single file with js and css all together.

    Thank you ;)

  6. msurdi commented on Mar 27, 2018

    @msurdi

    I've finally worked around this problem, without the need to eject, by just creating a webpack.config.js file at the root of my project with:

    const path = require("path")
    const UglifyJsPlugin = require("uglifyjs-webpack-plugin")
    const glob = require("glob")
    
    module.exports = {
      entry: {
        "bundle.js": glob.sync("build/static/?(js|css)/main.*.?(js|css)").map(f => path.resolve(__dirname, f)),
      },
      output: {
        filename: "build/static/js/bundle.min.js",
      },
      module: {
        rules: [
          {
            test: /\.css$/,
            use: ["style-loader", "css-loader"],
          },
        ],
      },
      plugins: [new UglifyJsPlugin()],
    }
    

    And adapting the build script in my package.json file like:

    ...
        "build": "npm run build:react && npm run build:bundle", 
        "build:react": "react-scripts build", 
        "build:bundle": "webpack --config webpack.config.js", 
    ...
    

    Hope this helps others.

  7. samueledirito commented on Mar 27, 2018

    @samueledirito
  8. mreliopez commented on Aug 31, 2018

    @mreliopez

    @msurdi good sir, you're a life saver

  9. xyklex commented on Sep 10, 2018

    @xyklex

    Just to give an use case for this is that I would like to use Cloudfront signed url to deliver access to just one file (index.html) and without the need to request others access, because that will require that I create a signed url for each one, or without using signed cookies, because right now API gateway does not allow to return multiple Set-Cookie headers.

  10. AlBannaTechno commented on Sep 28, 2018

    @AlBannaTechno

    Thanks
    and for the best case we will need just one bundle.js file
    when we use electron

  11. robertclarkson commented on Nov 22, 2018

    @robertclarkson

    I've finally worked around this problem, without the need to eject, by just creating a webpack.config.js file at the root of my project with:

    const path = require("path")
    const UglifyJsPlugin = require("uglifyjs-webpack-plugin")
    const glob = require("glob")
    
    module.exports = {
      entry: {
        "bundle.js": glob.sync("build/static/?(js|css)/main.*.?(js|css)").map(f => path.resolve(__dirname, f)),
      },
      output: {
        filename: "build/static/js/bundle.min.js",
      },
      module: {
        rules: [
          {
            test: /\.css$/,
            use: ["style-loader", "css-loader"],
          },
        ],
      },
      plugins: [new UglifyJsPlugin()],
    }
    

    And adapting the build script in my package.json file like:

    ...
        "build": "npm run build:react && npm run build:bundle", 
        "build:react": "react-scripts build", 
        "build:bundle": "webpack --config webpack.config.js", 
    ...
    

    Hope this helps others.

    This was great but my app was not bundling correctly as there were 3 JS files that had to be bundled, not just main.hash.chunk.js

    I just had to tweak the webpack config to include all files:

    entry: {
        "bundle.js": glob.sync("build/static/?(js|css)/*.?(js|css)").map(f => path.resolve(__dirname, f)),
      },
    
  12. npomfret commented on Jan 3, 2019

    @npomfret

    Is there a way to inline everything (all css and js) into a single html file?

  13. locked and limited conversation to collaborators on Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions