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 a React example authored in TSX #1045

Closed
linuxerwang opened this issue Dec 13, 2017 · 5 comments
Closed

Add a React example authored in TSX #1045

linuxerwang opened this issue Dec 13, 2017 · 5 comments
Labels
Can Close? We will close this in 30 days if there is no further activity enhancement

Comments

@linuxerwang
Copy link

Is it possible for rules_typescript to compile ReactJS? I tried a bit and found that it can generate the bundle.js but in the devserver it's not running.

Here is the generated bundle.js:

(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define("__main__/streamline/fe/src/entry", ["require", "exports", "react", "react-dom"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var React = require("react");
    var ReactDOM = require("react-dom");
    var App = function () {
        return (React.createElement("div", null,
            React.createElement("p", null, "Hello world!")));
    };
    ReactDOM.render(React.createElement(App, null), document.getElementById('app'));
});

Looks like the anonymous factory function was never called. Both requirejs and amd was installed with yarn.

Any help? Thanks.

appsforartists referenced this issue in appsforartists/react Jan 14, 2018
```js
define('', ['require', 'exports', 'react', 'react-dom'] // …
  var React = require('react');
```

throws a [mismatched define error](https://requirejs.org/docs/errors.html#mismatch) when used with RequireJS because the current AMD definitions don't specify module names.  This patch passes the bundle names into `rollup`, so the AMD definitions can use them.

By naming the builds, they can be [consumed from the Bazel build tool](https://github.com/bazelbuild/rules_typescript/issues/102).

I have minimal familiarity with AMD.  I don't believe adding module names should affect other environments.
@appsforartists
Copy link
Contributor

appsforartists commented Jan 14, 2018

There are two bits to it. First, you need to ensure ts_devserver serves the modules. Then, you need to ensure that the generated RequireJS dependency can find them.

Here's a BUILD file that will ensure React is included in your bundle. (You still need to yarn add react react-dom.)

package(default_visibility = ["//visibility:public"])

load("@build_bazel_rules_typescript//:defs.bzl", "ts_devserver", "ts_library")

ts_library(
  name = "playground",
  module_name = "my-lib-playground",
  module_root = "src",
  srcs = glob(
    include = ["src/**/*.ts", "src/**/*.tsx"],
    exclude = ["**/__tests__/*"],
  ),
  deps = ["//packages/core"],
  tsconfig = "//:tsconfig.json",
)

ts_devserver(
  name = "devserver",
  deps = [
    "//:node_modules/react/umd/react.development.js",
    "//:node_modules/react-dom/umd/react-dom.development.js",
    ":playground",
  ],
  entry_module = 'my_root/packages/playground/src/index',
  serving_path = "/dist/bundle.js",
)

Unfortunately, Facebook doesn't currently include module names in its AMD bundles, so you'd also need to edit /node_modules/react/umd/react.development.js and /node_modules/react-dom/umd/react-dom.development.js to add module names to those definitions, e.g.:

  //           notice the first argument below is now 'react-dom' 
  typeof define === 'function' && define.amd ? define('react-dom', ['react'], factory) :

I've opened a PR for Facebook to fix this upstream. When that is merged, this BUILD file alone should take care of the "making sure RequireJS can find the imported modules bit."


In the mean time, you may be able to work around it by serving the React AMD bundles as static files (rather than deps) and using this syntax in your HTML to depend on them:

<script>
  requirejs.config({    
    paths: {    
      'react': '/node_modules/react/umd/react.development.js',   
      'react-dom': '/node_modules/react-dom/umd/react-dom.development',
    }   
  });
</script>

appsforartists referenced this issue in appsforartists/react Jan 14, 2018
```js
define('', ['require', 'exports', 'react', 'react-dom'] // …
  var React = require('react');
```

throws a [mismatched define error](https://requirejs.org/docs/errors.html#mismatch) when used with RequireJS because the current AMD definitions don't specify module names.  This patch passes the bundle names into `rollup`, so the AMD definitions can use them.

By naming the builds, they can be [consumed from the Bazel build tool](https://github.com/bazelbuild/rules_typescript/issues/102).

I have minimal familiarity with AMD.  I don't believe adding module names should affect other environments.
@alexeagle
Copy link
Collaborator

@appsforartists they are looking for you over in facebook/react#12019

@alexeagle
Copy link
Collaborator

re-purposing this issue to make an example for react

@alexeagle alexeagle changed the title Make rules_typescript able to compile ReactJS Add a React example authored in TSX Aug 21, 2019
@alexeagle alexeagle transferred this issue from bazelbuild/rules_typescript Aug 21, 2019
@github-actions
Copy link

github-actions bot commented Dec 3, 2020

This issue has been automatically marked as stale because it has not had any activity for 90 days. It will be closed if no further activity occurs in two weeks. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs!

@github-actions github-actions bot added the Can Close? We will close this in 30 days if there is no further activity label Dec 3, 2020
@github-actions
Copy link

This issue was automatically closed because it went two weeks without a reply since it was labeled "Can Close?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Can Close? We will close this in 30 days if there is no further activity enhancement
Projects
None yet
Development

No branches or pull requests

3 participants