Repository navigation
Creating Packages
Packaging up a JavaScript library for use in ClojureScript is usually not too difficult.
While it's the rare case please note that some JS libraries are written in a Google Closure compatible way and do require a slightly different process. See this discussion
The following list goes through the process, step-by-step. If you
encounter any problems, consider asking for help at #clojurescript
on freenode or submitting an issue here.
You'll do your work in this branch in order to submit the package later.
This will usually be via a service like GitHub, CDNJS or the library's homepage. A zip file with the source is fine.
Use the library's name (without "js" or ".js") as the top-level directory and create the following directory structure:
└── resources
└── cljsjs
└── <library-name>
└── common
If you aren't familiar with externs, read about what they are and how to create them.
Add your externs file to the following location:
└── resources
└── cljsjs
└── <library-name>
└── common
└── <library-name>.ext.js
Note: In addition to your externs file, any additional assets (css, images) that ship with the JS library can be added to the common directory.
These can be copied from existing packages to get started.
This file will describe how the package can be built and should reside in the library's top-level directory.
All builds should minimally contain the following:
- Version vars:
(def +lib-version+ "x.y.z")
(def +version+ (str +lib-version+ "-0"))
The names of vars should be these, as our build script uses simple regex to parse versions from build.boot files.
The lib version should be exactly the same as version used by the library. You can use +lib-version+ var to construct the URL to be downloaded. Cljsjs version (used for Clojars) is based on lib version with a build identifier appended.
- a pom file
(task-options! pom {...}) - a package task
(deftask package [] ...)
See next step for details.
This file should reside in the library's top-level directory and contain the following:
- the package's name and a placeholder for the version number.
- any specific instructions for its usage
Consult existing library packages for examples.
After running the package task in any CLJSJS package, you should
have a new target package directory looking like this:
target/
├── cljsjs
│ └── <library-name>
│ ├── common
│ │ └── <library-name>.ext.js
│ ├── development
│ │ └── <library-name>.inc.js
│ └── production
│ └── <library-name>.min.inc.js
└── deps.cljs
File details:
-
*.inc.jsand*.min.inc.jswere copied from a downloaded file -
*.ext.jswas copied from your resources directory -
deps.cljswas generated by boot, and required by cljs compiler
Thus, the package task basically needs to be a program doing the following:
- download your library from a URL, verify checksum, and unzip if needed
- move downloaded files to the target directory
- minify files from the target directory to another location in target
- directory generate the deps.cljs
The boot-cljsjs project aims to provide a comprehensive set of helpers to make the process described above as effortless as possible. Install boot by following the instructions on the project homepage, then run:
boot -d cljsjs/boot-cljsjs -h
Boot will print all available tasks and the last section of tasks should
contain tasks that belong to boot-cljsjs. It will look like this:
checksum No description provided.
deps-cljs Creates a deps.cljs file based on information in the files...
download No description provided.
minify Minifies .js and .css files based on their file extension
unzip No description provided.
Using commands like boot -d cljsjs/boot-cljsjs deps-cljs --help
you can print more information about each of those tasks.
Note that the
-d cljsjs/boot-cljsjsis only neccessary if it's not specified as a dependency by yourbuild.bootfile already.
Also ensure that cljsjs/boot-cljsjs is up to date by running boot show -u.
You can try to locally install your package now with the following command:
boot package build-jar
This will install the jar to your local ~/.m2 repository. The dependency
information will be inserted into the packages README file. You can add that
to the :dependencies vector in your build.boot or project.clj.
Try to require it in a ClojureScript project (eg. (:require cljsjs.react)) and see that the expected
global variables (eg. js/React) exist in the compiled build's
page. For best results, test with both {:optimizations :none} and
{:optimizations :advanced} compilations.
It's really important to run
boot package build-jar. The package task downloads JavaScript files you want in the jar, thus if you omit it those files will be missing in the jar.
That's it! Thank you for making the effort! Please do open issues if you have any suggestions how to improve the packaging process or have other ideas you'd like to discuss.