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

Support swagger module with AMD imports? #3247

Closed
chenatu opened this issue Oct 6, 2017 · 9 comments
Closed

Support swagger module with AMD imports? #3247

chenatu opened this issue Oct 6, 2017 · 9 comments

Comments

@chenatu
Copy link

chenatu commented Oct 6, 2017

I am using swagger-codegen to generate swagger js client. However, it is hard to import the client module.

Environment

  1. node -v: 8.1.4
  2. npm -v: 5.4.2
  3. yarn --version (if you use Yarn): 1.1.0
  4. npm ls react-scripts (if you haven’t ejected): 1.0.14

Then, specify:

  1. Operating system: MAC OSX
  2. Browser and version (if relevant): chrome

Steps to Reproduce

  1. install swagger-codegen and generate javascript project according to some api spec
  2. install local swagger module according to README.md file in the generated swagger project
  3. import the module in the react project ant it shows:
Module not found: Can't resolve 'ApiClient' in /path/to/API

Expected Behavior

No error

Actual Behavior

image

In swagger issue 3537. It discusses this issue and bring 2 solutions:

Disabling directly edit the code source :

// commenting the first IF branch
  //if (typeof define === 'function' && define.amd) {
  //  // AMD. Register as an anonymous module.
  //  define(['superagent'], factory);
  //} else
  if (typeof module === 'object' && module.exports) {
    // CommonJS-like environments that support module.exports, like Node.
    module.exports = factory(require('superagent'));
  }

or via Webpack configs:

npm install --save-dev imports-loader
// and then add this loader to the webpack configs
{ test: /\.js/, loader: 'imports?define=>false'}

Since, it is not advised to revise the react-scripts, could create-react-app support swagger client imports?

@gaearon
Copy link
Contributor

gaearon commented Nov 3, 2017

Sorry, we don't intend to support AMD. It's time the ecosystem moves forward from a format that isn't used by any system (neither Node nor browsers) and only exists as a legacy.

@john-goldsmith
Copy link

john-goldsmith commented Apr 16, 2018

@gaearon Sorry for rehashing the past here, but it seems like the logical thread to pull with regards to this topic. While I absolutely agree with your statement above about not supporting AMD, my issue is centered around Swagger's ES6 generated output (specifically the usage of static class properties) and CRA's usage of related polyfills. Following the TC39 rabbit hole reveals:

Placement: Static vs instance -- static postponed to follow-on proposal

(source)

However, the CRA README says:

Supported Language Features and Polyfills

  • Class Fields and Static Properties (part of stage 3 proposal).

While we recommend using experimental proposals with some caution, Facebook heavily uses these features in the product code, so we intend to provide codemods if any of these proposals change in the future.

Maybe I'm just overly concerned with the semantics, but the CRA README would lead me to believe that Swagger's usage of static class properties should play nicely with CRA, but my terminal says otherwise. At the same time, however, CRA not supporting an early Stage 2 proposal also seems to make sense. So, really, this is just a long-winded way of saying "I'm confused."

FWIW, you're obviously not responsible for Swagger's implementation. Given the incredible usefulness and popularity of both Swagger and React, having them live in harmony makes for a killer development setup.

@mjameswh
Copy link

mjameswh commented Apr 20, 2018

Facing the same issue as the OP here.

Now, the original title of this issue is incorrect. Swagger's generated .js files does not require AMD modules support. It autodetect the proper modules semantics using a cascade of if statements. Now the problem is that by default, webpack loaders give hints that AMD modules are supported, though they are not really. Not sure about the details here, but in the end, since the generated code checks for AMD support before anything else, and is misled to believe that AMD is supported, then it initialize itself incorrectly.

The proposed fix is to tell webpack's module loader to completely disable support for AMD modules, by passing the argument define=>false to imports-loader. For example:

module: {
  rules: [
    {
      test: /my_client\/.*\.js$/,
      use: 'imports-loader?define=>false'
    }
  ]
}

This change seems to me as being compatible with CRA's policy regarding AMD modules... Could it be considered?

@gaearon
Copy link
Contributor

gaearon commented Apr 22, 2018

Is there a simpler way to disable AMD support that doesn’t involve adding a loader? If not maybe you could propose this to webpack?

@mjameswh
Copy link

I could be wrong on that point, but from my understanding, the proposed configuration does not actually add the module, but only declares it explicitly with some parameter. I believe Webpack registers that same module implicitly if it is not declared.

Now about having a simpler way to disable AMD support... One way or another, that would still involve modifying the webpack configuration file, which, as of now, implies either ejecting from CRA or cloning the CRA script project...

@gaearon
Copy link
Contributor

gaearon commented Apr 23, 2018

No, I’m suggesting that you propose this feature to webpack, and if webpack implements such an option, we’ll enable it by default in CRA. No ejecting.

@mezzario
Copy link

@gaearon Webpack implemented this feature:

https://webpack.js.org/configuration/module/#rule-parser

module: {
  rules: [
    {
      parser: {
        amd: false
      }
    }
  ]
}

@mjameswh you can generate ES6 javascript using swagger, set in config:

https://generator.swagger.io/api/gen/clients/javascript

{
  useES6: true
}

martnu added a commit to martnu/create-react-app that referenced this issue Dec 11, 2018
Disable AMD and solve swagger client issues since there is no intention of supporting it:

facebook#3247 (comment)
@martnu martnu mentioned this issue Dec 11, 2018
@seanlaff
Copy link
Contributor

seanlaff commented Jan 3, 2019

Using the useES6 flag in the swagger generation is a way to get around the AMD problem, however Im seeing the same errors @john-goldsmith saw about class fields
Support for the experimental syntax 'classProperties' isn't currently enabled

Which is odd because I know CRA supports that transform

[
        require("@babel/plugin-proposal-class-properties").default,
        {
          loose: true,
        },
      ],

@martnu
Copy link

martnu commented Jan 3, 2019

@seanlaff When it comes to node_modules, CRA only compiles standard language features (not class properties). See #3776

@lock lock bot locked and limited conversation to collaborators Jan 8, 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

7 participants