Posts

skip scalajs-bundler, use npm with sbt/scalajs.md

skip scalajs-bundler, use npm with sbt/scalajs.mdMany people are using scalajs-bundler to specify their javascript dependencies using npm-style (sbt first, npm second) declarations.You don’t need to use scalajs-bundler, which can be confusing to use or perhaps less useful in complex bundling scenarios. If all you need is some simpler webpack config, use scalajs-bundler and ignore the content below. If not, you can still retain the flexibility of running npm after your compile triggers on source code change using the standard approach shown below.First, set up a webpack config. Only the relevant parts are shown:// config file myapp.webpack.config.jsconst common ={...whatever you want....}// I typically parameterize the scala.js output based on the// build kind but you can also be explicit if you wishconst prod ={ mode:"production",// prod by default, webpack 4 entry: path.resolve(__dirname,"target/scala-2.12/me-opt.js"), plugins:[newwebpack.DefinePlugin(…

thougthfulness of facebook's reasonml and reasonreact

Facebook recently released 0.3 version of reason and reasonreact for creating UIs. Instead of using typescript, you would use reasonml. reasonml is based on OCaml which has both functional and OOP capabilities. Why is how facebook is integrating reasonml into their ecosystem brillant? facebook recognizes that the ocaml syntax is a burden to learn. reasonml is a more javascript oriented syntax of ocaml. ocaml syntax is, as many functional languages are, harder to learn than say google’s go or really it’s difficult if you are not used to functional syntax. syntax is a big burden for migration of existing devs. The great part is that they are making it easy for a certain group, such as js programmers, to migrate.facebook has created reasonreact using only a subset of ocaml. No OOP components. And they reduce the surface area of the reactjs APIs to something appropriate for the functional world. The API is small and easy to learn. It does not offer everything that the reactjs API offers b…

codemirror, react and height sizing.md

codemirror, react and height sizing.mdIf you use codemirror and react together, say through react-codemirror2, then you may have run into sizing issues.Out of the box, codemirror has a style height setting to 300px on the outer div with classname CodeMirror. react-codemirror2 wraps this with another div called react-codemirror2.If you don’t load the CSS correctly, the default height of the component is 300px, which is rarely what you want. You generally want the editor to resize based on your overall layout. However, you may have found it difficult to do so. I certrainly did.The secret is to realize that codemirror add its own scrollbars and manages scrolling in order to optimize content rendering. There are really two modes for setting the height:Set the size of the outer container explicitly with a hight, say 500px using editor.setSize() however this does not refresh when the page changes size.Allow for dynamic resizing but ensure that the the component knows how tall it is.Generall…

pass through from typescript to babel.md

pass through from typescript to babel.mdI often use typescript just for types and not for bundling or transpilation to different ES targets.Typescript can play a few different roles that when used with a complex stack, can cause tooling confusion. To avoid confusion, I generally have typescript do as little as possible.To have typescript passthrough everything to babel, which is configured to transpile down to my target, I set tsconfig.json to:module: esnext => target an ES version that understands import/export natively.moduleResolution: node => resolve modules using node semantics. This is the default, but when “module” is used, it seems to get reset to a bad value.target: esnext => target the latest es environment for destructuring, default args, etc.jsx: preserve: => Keep brackets (see below).importHelpers; true => If typescript does happen to transpile, import tslib instead of writing some boilerplate into each output module. This saves spacenoEmitHelpers: true =&g…

typescript ambient declarations, global.d.ts, lib and typeRoot.md

typescript ambient declarations, global.d.ts, lib and typeRoot.mdWhen using typescript, you may have global ambient declarations you need to add.For example, my HTML defines several variables prior to my main .js file being loaded and executed. The .js file, packed using webscript and written in typescript, needs access to those variables.Accesing the HTML variables is much like accessing “window” inside of a .ts file. The typescript compiler declares a window object for us and makes it available without import in our .ts files.We clearly need something like that.google searching did not yield alot of insight, but the key thoughts you need to know are:typescript reads all your files in your compiler path e.g. src then compiles the file you want to compile and output.typescript has a search path algorithm that is used when importing modules. This search path is not relevant to solving the global ambient problem.Global ambient can be declared in a global.d.ts file:declareconst myvar:boo…

attributes with react and typescript.md

If you use typescript with react you may wonder what the right signature is for your “properties” interface.Obviously, your component will have few to many specific properties that reflect your domain. However, you may wish to also provide for the ability to add other attributes, such as those valid for a div property. In other words, you want to extend your props interface with that of the properties from a div element. How do you do that?There are really two issues:How do you extend your props interfacesHow do you filter the props once you receive them to just those that apply.Extend Your Prop Interface or Use Sum TypesIt’s not uncommon to seeinterfaceProps{ className?:string yourProp?:number}with the idea of using it in class MyComponent extends React.Component<Props, any> somewhere. If your component will have an outer div, you may also pass in a react element so that its configurable. But let’s say your outer element is a div and you want to be able to allow clients to ad…

creating extensible components.md

We would like to create a flexible set of functions for rendering a view on checkboxes. Many libraries try to take “control” of the rendering process and introduce brittleness into the component by making too many assumptions and then trying to provide too many “options” to allow you to customize the more monolithic comopnent. By breaking apart our functions into smaller ones, while offering defaults with all the bells and whistles, we can make a much more flexible library that can be used in many different scenarios.You can use ramda and recompose “in the small” assuming you are using it elswhere in your library that justifies its inclusion in your bundle.Here is a simple example of how we can use these extra tools to create a very simple, non-aria enabled check box list. It is not optimized for large lists either i.e. its not “virtualized.”import React from"react"import{ defaultProps }from"recompose"import R from"ramda"import{ firstOrElse, noop, compose…