question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: Use JSImport annotations instead of requiring user to configure an 'mui' or similar variables

See original GitHub issue

Currently, the raw components for material-ui are contained in the Mui Scala object, which pulls its definition from an mui Javascript object which the library user must configure, e.g., as

var mui = require("material-ui");
mui.Styles = require("material-ui/styles");
mui.SvgIcons = require('material-ui/svg-icons/index');

window.mui = mui;

However, depending on the tool chain used to manage Javascript dependencies, getting such an object into the global namespace may be inconvenient and/or finicky, and it is also not obvious that it needs to be done to begin with.

Scala.js has an @JSImport annotation which is designed for this use case, that of importing definitions from modules. @JSName("mui") can be replaced with @JSImport("material-ui",Namespace) It has the same semantics as the original Mui object, so the facade classes don’t need to be changed. It also works out of the box with sbt plugins like scalajs-bundler that manage NPM dependencies. Making such a tool chain work with scalajs-react-components’ current method of getting component definitions is something I haven’t managed to do yet.

(the above also applies to the other libraries this library provides facades for).

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:7

github_iconTop GitHub Comments

1reaction
beikerncommented, Sep 2, 2017

Fixed!!!

It works using this config:

webpack.config.js


var webpack = require('webpack');

// Load the config generated by scalajs-bundler
var config = require('./scalajs.webpack.config');

// Exported modules (here, React and ReactDOM)
var globalModules = {
  "material-ui": "mui",
  "material-ui/styles" : "mui.Styles",
  "material-ui/svg-icons/index" : "mui.SvgIcons"
};

Object.keys(config.entry).forEach(function(key) {
  // Prepend each entry with the globally exposed JS dependencies
  config.entry[key] = Object.keys(globalModules).concat(config.entry[key]);
});

// Globally expose the JS dependencies
config.module.loaders = Object.keys(globalModules).map(function (pkg) {
  return {
    test: require.resolve(pkg),
    loader: "expose-loader?" + globalModules[pkg]
  }
});

module.exports = config;

build.sbt

lazy val client: Project = (project in file("client"))
  .settings(
    name := "client",
    version := Settings.version,
    scalaVersion := Settings.versions.scala,
    scalacOptions ++= Settings.scalacOptions,
    useYarn := true,
    mainClass in Compile := Some("client.SPAMain"),
    libraryDependencies ++= Settings.scalajsDependencies.value,
    npmDependencies in Compile := Seq(
      "react"                             -> Settings.versions.reactVersion,
      "react-dom"                         -> Settings.versions.reactVersion,
      "react-addons-create-fragment"      -> Settings.versions.reactVersion,
      "react-addons-css-transition-group" -> Settings.versions.reactVersion,
      "react-addons-pure-render-mixin"    -> Settings.versions.reactVersion,
      "react-addons-transition-group"     -> Settings.versions.reactVersion,
      "react-addons-update"               -> Settings.versions.reactVersion,
      "material-ui"                       -> Settings.versions.MuiVersion,
      "react-tap-event-plugin"            -> "2.0.1",
      "jquery"                            -> Settings.versions.jQuery,
      "bootstrap"                         -> Settings.versions.bootstrap
    ),
    npmDevDependencies in Compile += "expose-loader" -> "0.7.1",
    webpackConfigFile := Some(baseDirectory.value/"webpack.config.js"),
    scalaJSUseMainModuleInitializer := true,
    scalaJSUseMainModuleInitializer.in(Test) := false,
    webpackEmitSourceMaps := false,
    enableReloadWorkflow := false
  )
  .enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin, ScalaJSWeb)
  .dependsOn(sharedJS)
0reactions
oyvindbergcommented, Sep 4, 2017

Hey there! Yes, yes, this is the plan! In fact me and @rleibman have been working on it for a while in what will be version 1.0, see #89 . Feel free to give it a spin!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Server-side-rendered styles are missing and/or incorrect #17676
I'm working on an app based on Gatsby and Material UI. GitHub issues reporting similar, but not the same behavior that I...
Read more >
Some notes on using esbuild - Hacker News
Esbuild seems like the perfect tool for people like me. ... (and anyone else getting started now) to use all `import` instead of...
Read more >
Improve code inspection with annotations - Android Developers
Learn how annotations allow you to provide hints to code inspections tools like Lint, to help detect these more subtle code problems.
Read more >
Stop Overcomplicating Web Development - Try Svelte
Variables & Reactivity. Variables are created in the script tag. We can create a string variable and display it in the DOM very...
Read more >
can't resolve '@material-ui/core/styles/createmuitheme'
Don't know about createMuiTheme. but you can use this for importing Styles in Mui. ... I used the same command before but I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found