Skip to content
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

Add AWS module support by including the JSON loader #1650

Closed
kellyjanderson opened this issue Feb 26, 2017 · 17 comments
Closed

Add AWS module support by including the JSON loader #1650

kellyjanderson opened this issue Feb 26, 2017 · 17 comments

Comments

@kellyjanderson
Copy link

The AWS JavaScript SDK recently added support for JS modules and Webpack Webpack Support - issue the one thing that the AWS JavaScript SDK requires, that create-react-app does not provide, is the JSON loader Bundling Applications with Webpack. Considering the prevalence of the AWS stack this would be a significant improvement allowing the use of the AWS JavaScript API without having to eject.

@kellyjanderson kellyjanderson changed the title Add ASW module support by including the JSON loader Add AWS module support by including the JSON loader Feb 26, 2017
@gaearon
Copy link
Contributor

gaearon commented Feb 26, 2017

Create React App already uses JSON loader. Have you tried to use the library? What issue are you getting?

@kellyjanderson
Copy link
Author

Oh, my mistake, I could not find any documentation indicating that the json-loader was included in the box. I checked the documentation and I assumed by this section https://github.com/facebookincubator/create-react-app#whats-inside that only the style-loader was included; I also searched the page for any reference to the json-loader being included. Perhaps the use of json-loader is mentioned somewhere I missed?

@gaearon
Copy link
Contributor

gaearon commented Feb 26, 2017

It's not really documented because this is implementation detail. Our average reader won't know what json-loader is anyway, so it probably doesn't make sense to mention as a "feature". Handling JSON works out of the box in all other bundlers so we just thought we need to fix it up for our Webpack setup as well. Let me know if you have any issues!

@gaearon gaearon closed this as completed Feb 26, 2017
@kellyjanderson
Copy link
Author

I was able to use the AWS SDK without any difficulty, well other than the difficulty of the SDK itself. :-D

@flaviocarmo
Copy link

Did you use json-loader without ejecting the webpack configuration? I need to do this, too, because i have external configurations, like API URL

@gaearon
Copy link
Contributor

gaearon commented Mar 6, 2017

@flaviocarmo

Have you had a chance to read my comment in #1650 (comment)? We already use json-loader in our config by default, you don’t need to do anything extra to get it working. Do you have any issues?

@flaviocarmo
Copy link

i dont know how to use it with create-react-app. I my own webpack.config.js, i use like this:

module.exports = {
  entry: [
    './src/index.jsx'
  ],
  externals: {
    'Config': JSON.stringify(require('./config.dev.json'))
  },
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'js/bundle.js'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel'
    }]
  },
  resolve: {
    extensions: ['', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './output/'
  }
};

How can i do this with create-react-app?

@gaearon
Copy link
Contributor

gaearon commented Mar 6, 2017

You don’t need to do anything. It’s already there. If you use Create React App, importing JSON already works. Have you tried it?

@flaviocarmo
Copy link

@gaearon

I'm sorry, but I could not explain correctly what problem I'm facing. Today I use the webpack in my application to store the development and production environment settings in two json files: config.dev.json and config.prod.json.

When running the npm scripts for development using the dev-server webpack, it loads the notes into the corresponding environment. When I go to production, I simply run the npm build, and in addition to the minification, the webpack uses the other json file.

So, my problem is how, using create-react-app, I use the webpack to load configuration files into my development and production environments, according to the npm script I run.

@Timer
Copy link
Contributor

Timer commented Mar 6, 2017

@flaviocarmo So it sounds like you want to switch what you're importing based on whether you're building for development or production.
You can try this:

const Config = require(process.env.NODE_ENV === 'production' ? 'config.prod.json' : 'config.dev.json')

@flaviocarmo
Copy link

@Timer Yes, something like that. But i need to this file by file or at index.js level?

@Timer
Copy link
Contributor

Timer commented Mar 6, 2017

That would need to be file by file, you can go ahead and make it available globally by attaching it to the window, or by creating a stub file which hides the logic and require that stub in every file. I'd probably do the latter.

edit: see @gaearon stub to achieve a properly eliminated code segment

@gaearon
Copy link
Contributor

gaearon commented Mar 6, 2017

Note that I don't recommend dynamic requires. Write it like this:

var config;
if (process.env.NODE_ENV === 'production') {
  config = require('./config.prod');
} else {
  config = require('./config.dev');
}

This way it will be properly eliminated in production.

You can do this in any file.

@Timer
Copy link
Contributor

Timer commented Mar 6, 2017

I believe this is missing from our User Guide, especially the part about not using a ternary operator.
We touch on it very lightly here.

Would you like to submit a PR detailing this suggestion, @flaviocarmo ? 😄

@gaearon
Copy link
Contributor

gaearon commented Mar 6, 2017

IMO it’s a bit too specific for user guide—the problem isn’t really unique to ternaries, but occurs with any sort of dynamic requires.

@Timer
Copy link
Contributor

Timer commented Mar 6, 2017

I guess what I meant to say is that we don't detail how to dynamically configure anything based on NODE_ENV, require or configuration/text otherwise.
If this is too advanced of a section we'd like to leave out, that's fine too.

@gaearon
Copy link
Contributor

gaearon commented Mar 6, 2017

OK, maybe we can include this more explicitly.

@lock lock bot locked and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants