Open
Description
This is an internal tracking ticket for infrastructure for compiling client code through Django-pipeline @tomka and I have been discussing for some time.
Motivation
- We would like to take advantage of the type safety and productivity benefits of TypeScript for central libraries and APIs in CATMAID, while allowing widgets and experimental features to still be prototyped quickly in plain JS as before.
- We would like to factor more components into modules to decrease complexity, better delegate responsibilities to components, make composition easier, and allow for reuse outside of the full CATMAID client.
Requirements
These are the capabilities any compiled pipeline must maintain:
- The client must recompile when edited in a development environment without any explicit action.
- Source mapping in the browser console.
- Likewise, no mangling or breaking of the API in the browser console for scripting use.
NOTE: The below plan is no longer valid. See the discussion beginning in 2019 below for a webpack-based approach.
Draft Plan
- TS Compilation
- Pipeline plugin
- Pipeline's development mode should recompile on page load, but if this is too slow there are alternatives, e.g, using gulp
- Source mapping
- The Atlantic has a PR adding source mapping support to pipeline (finally)
- Since we bundle multiple source files together, we need a compressor that is also source-map aware. The PR above uses closure's compressor. To use a compressor like closure we'd need to go ahead and annotate all our modules and exports so that they're still callable from the console. Non-mangling compressors like JSMin produce source maps but don't consume them from earlier steps, or if they do consume only one (like UglifyJS). This may be a non-issue if we can just compile a single TypeScript entry point file, so then there is only a single source map to pass through. Also annoying is that pipeline can't apply different compilers and compressors to different sets of files.
- Closure can take advantage of TypeScript's typing through JSDoc annotations, for example google has a project that does just that
- Dependencies
- We prefer to keep our dependencies in-repo rather than packaged from NPM or bower, but would also want matching TS definitions through Typings
Drawbacks
- Requires installation of TS and possibly closure compilers for non-docker servers
- No in-browser editing of compiled files
- Potentially more latency in the edit > test > edit cycle for developers
Alternatives
- Django pipeline is unergonomic for modern Javascript ecosystems for SPAs, feeling more oriented towards classic HTML + JS + CSS full page delivery. It might be better to adopt a different asset management strategy.