chiasm-project / chiasm

A browser based environment for interactive data visualizations.
MIT License
184 stars 27 forks source link

Remove Browserify-Shim transform from package.json in all Chiasm subprojects #49

Open curran opened 8 years ago

curran commented 8 years ago

There is a problem now with bundling Chiasm applications using Browserify. When a project requires Chiasm as a dependency, and uses Browserify to bundle the application, the fact that browserify-shim is listed in the transforms in the package.json of Chiasm and each component causes Browserify to try running those transforms in the outer project that requires Chiasm as a dependency. I didn't realize this would happen, I added the browserify-shim transform with the idea that it would only apply locally, and not when the package is a dependency. For example, model.js should not be shimmed in the parent project (which may want model.js included in the bundle), but should be shimmed in the Browser build of Chiasm (which is for use in bl.ocks.org examples and should not have model.js in the bundle).

To remedy this situation, the package.json of all Chiasm subprojects needs to be changed such that there are no browserify transforms listed. For example, consider the following package.json:

{
  "name": "chiasm",
...
  "scripts": {
    "pretest": "browserify -o chiasm.js -s Chiasm src/index.js",
    "test": "mocha"
  },
...
  "browserify": {
    "transform": [
      "browserify-shim"
    ]
  },
  "browserify-shim": {
    "lodash": "global:_",
    "model-js": "global:Model"
  }
}

This needs to be changed around such that the transforms are specified in the Browserify command rather than the "browserify" field in package.json. After the change, it looks like this:

{
  "name": "chiasm",
...
  "scripts": {
    "pretest": "browserify -t browserify-shim -o chiasm.js -s Chiasm src/index.js",
    "test": "mocha"
  },
 ...
  "browserify-shim": {
    "lodash": "global:_",
    "model-js": "global:Model"
  }
}

After this change is made, new NPM releases must be made so the changes come through when adding Chiasm as a dependency.

curran commented 8 years ago

Done for Chiasm itself in 154c1305323a19f5d99d2e6dec54bb51b1898eb6

Needs to be done in:

curran commented 8 years ago

Done for chiasm-component in https://github.com/chiasm-project/chiasm-component/commit/775401f336d193d3dd50e533e94ecd1bea5ad139

Done for chiasm-layout in https://github.com/chiasm-project/chiasm-layout/commit/75262fc571b9c5a6455947298dc9d95deb34f65c

curran commented 8 years ago

Done for chiasm-links in https://github.com/chiasm-project/chiasm-links/commit/fc25ae796c2cf874a68c6930f1e139aeb2581a26

curran commented 8 years ago

Done for chiasm-dsv-dataset in https://github.com/chiasm-project/chiasm-dsv-dataset/commit/7c33b2fb34c26798ff1f5ed0c73c9e269a5674cd

curran commented 8 years ago

Done for chiasm-data-reduction in https://github.com/chiasm-project/chiasm-data-reduction/commit/639c879b0c5582b09896983c6c0fc0ccaff5a672

curran commented 8 years ago

chiasm-component is still being shimmed out for some reason. I think this is because the components are depending on an older version and need to be updated. I'm going to make one more pass through all the modules and update their dependencies to the latest using npm-check-updates.

curran commented 8 years ago

Here's the sequence of commands used to upgrade dependencies for a package and push out a new version.

Step 1. Upgrade dependencies and ensure tests pass:

ncu --upgradeAll
git diff
npm install
npm test

Some tests may fail - watch out for the jsdom package, which needs to be pegged at version 3.1.2 because it is has some funky Node.js version compatibility issues.

Step 2. If everything looks good, publish a new version:

git commit -m "Upgrade dependencies" -a
npm version patch
git push && git push --tags
npm publish

After doing this for all modules, I see the following beautiful upgrade set for the Magic Bar Chart (Browserified) example.

$ ncu --upgradeAll
 chiasm                 ^0.2.2  →  ^0.2.3 
 chiasm-component       ^0.2.2  →  ^0.2.3 
 chiasm-data-reduction  ^0.2.1  →  ^0.2.2 
 chiasm-dsv-dataset     ^0.2.1  →  ^0.2.2 
 chiasm-layout          ^0.2.3  →  ^0.2.4 
 chiasm-links           ^0.2.2  →  ^0.2.3 
curran commented 8 years ago

The shim situation is horrible using browserify-shim. The top-level package being bundled cannot specify the set of modules to be shimmed. This is because browserify-shim looks in the package.json of all dependency packages to find out which modules to be shimmed.

Example scenario - I want to have a UMD bundle that has a bunch of Chiasm modules AND model.js included, but has D3 and Lodash shimmed out. If the top-level package only specifies D3 and Lodash to be shimmed, model.js is also shimmed, because it is configured to be shimmed out within one of the chiasm modules. This is so frustrating - I want to be able to include model.js in the bundle, but it seems there is no way to get browserify-shim to include it if any dependencies want to shim it out in their own browser builds.

Rollup seems to have a better story in terms of specifying shims. I think all of this will go away after adopting Rollup https://github.com/chiasm-project/chiasm/issues/54

Hypercubed commented 8 years ago

Can you quickly explain the reason the shim transform is needed? I was going to bump lodash v4 and thought it might be a good idea also replace each require with the specific lodash module. For example var keys = require('lodash/keys'). But I found that the bundle got bigger... which might be due to browserify-shim always including lodash as a global.

Also, related, what is your target for chiasm. Object.keys has pretty good browser and node support now. Can we start using this? How about string literal templates (to replace _.template)?

Hypercubed commented 8 years ago

Perhaps I have that backwords? The browserify-shim ensures that lodash is not included in the bundle?

curran commented 8 years ago

Ahh yes. The reason why these were included in the first place is to exclude D3 and Lodash from the bundles. The target here was a Rails app, which included D3 and Lodash separately through the Rails asset pipeline. We wanted to be able to just include one copy of D3 and Lodash for the entire app, rather than have two copies, one in the Chiasm bundle (not exposed globally) and one via the Rails asset pipeline.

We can definitely make use of Object.keys and string literal templates. I was just not aware of them at the time of writing Chiasm.