Skip to content

Commit

Permalink
Merge pull request #65 from cormacrelf/wasm-packaging
Browse files Browse the repository at this point in the history
Publish WASM builds to NPM
  • Loading branch information
cormacrelf authored Sep 18, 2020
2 parents 431b1cf + f92057b commit ec9b385
Show file tree
Hide file tree
Showing 17 changed files with 5,234 additions and 115 deletions.
88 changes: 81 additions & 7 deletions .github/workflows/build-wasm.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
name: Build WASM
name: Publish WASM
on:
push:
push:
branches:
- master

jobs:
build_wasm:
name: Build WebAssembly Package & deploy demo
publish_wasm_demo:
name: Publish WebAssembly Demo
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@master
- uses: hecrj/setup-rust-action@v1
- uses: actions/checkout@v2
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
rust-version: nightly
toolchain: nightly
override: true
- name: Cache cargo directories
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
# use "-wasm-target" to cache these separately from the CI workflow
key: ${{ runner.os }}-cargo-wasm-target-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-wasm-target-
- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f
- name: Yarn install
Expand All @@ -32,3 +46,63 @@ jobs:
PUBLISH_DIR: ./crates/wasm/js-demo/dist
EXTERNAL_REPOSITORY: cormacrelf/citeproc-wasm-demo
PUBLISH_BRANCH: gh-pages

publish_npm_canary:
name: "Publish to the 'canary' npm dist-tag"
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- name: Cache cargo directories
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
# use "-wasm-target" to cache these separately from the CI workflow
key: ${{ runner.os }}-cargo-wasm-target-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-wasm-target-
- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f

- name: Get version info
id: version
run: |
SHORT_SHA=$(git rev-parse --short "$GITHUB_SHA")
echo "::set-output name=short_sha::${SHORT_SHA}"
echo "::set-output name=npm_version::v0.0.0-canary-${SHORT_SHA}"
- name: Build for all targets
working-directory: crates/wasm
run: |
./scripts/all-targets.sh --features console
- name: Publish to NPM canary dist tag
working-directory: crates/wasm
run: |
./scripts/all-targets.sh --package-only --canary-sha ${{steps.version.outputs.short_sha}}
# log in to the NPM repository
echo "//registry.npmjs.org/:_authToken=${{secrets.NPM_PUBLISH_TOKEN}}" >> .npmrc
npm publish dist --tag canary --access public
- name: Publish to GitHub Packages canary dist tag
working-directory: crates/wasm
run: |
# GitHub packages must be scoped by their repo owner
# This configures a publishConfig field pointing to GitHub in the package.json
./scripts/all-targets.sh --package-only \
--canary-sha ${{steps.version.outputs.short_sha}} \
--github-packages \
--set-name "@cormacrelf/citeproc-wasm"
# log in to the NPM repository
echo "//npm.pkg.github.com/:_authToken=${{secrets.GITHUB_TOKEN}}" >> .npmrc
npm publish dist --tag canary --access public
99 changes: 74 additions & 25 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ jobs:
name: CSL Test Suite Regressions
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
with:
submodules: recursive

- name: Extract branch name
shell: bash
run: |
Expand All @@ -29,13 +30,12 @@ jobs:
GITHUB_HEAD=${GITHUB_HEAD#refs/heads/}
GITHUB_HEAD=$(echo ${GITHUB_HEAD#refs/} | tr '/' '-')
echo "GITHUB_HEAD = ${GITHUB_HEAD}"
echo "##[set-output name=head;]${GITHUB_HEAD}"
echo "::set-output name=head::${GITHUB_HEAD}"
GITHUB_BASE=${GITHUB_BASE#refs/heads/}
GITHUB_BASE=$(echo ${GITHUB_BASE#refs/} | tr '/' '-')
echo "GITHUB_BASE = ${GITHUB_BASE}"
echo "##[set-output name=base;]${GITHUB_BASE}"
echo "::set-output name=base::${GITHUB_BASE}"
id: branch
- name: Download base output
Expand All @@ -48,29 +48,22 @@ jobs:
run: |
mkdir -p .snapshots/branches
aws s3 cp s3://citeproc-rs-test-results/.snapshots/branches/$GITHUB_BASE .snapshots/branches/$GITHUB_BASE
- uses: hecrj/setup-rust-action@v1
with:
rust-version: nightly
- name: Create ~/.cargo directory
run: |
mkdir -p ~/.cargo/registry
mkdir -p ~/.cargo/git
mkdir -p ./target
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo index
uses: actions/cache@v1
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
- name: Cache cargo build
uses: actions/cache@v1
toolchain: nightly
override: true
- name: Cache cargo directories
uses: actions/cache@v2
with:
path: target
key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
path: |
~/.cargo/registry
~/.cargo/git
target
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: "Build tools package"
run: cargo build --package tools
- name: "Pull locales"
Expand All @@ -96,3 +89,59 @@ jobs:
aws s3 cp .snapshots/current s3://citeproc-rs-test-results/.snapshots/branches/$GITHUB_HEAD
aws s3 cp .snapshots/current s3://citeproc-rs-test-results/.snapshots/commits/$GITHUB_SHA
wasm_tests:
name: "Test the WASM package"
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
- name: Cache cargo directories
uses: actions/cache@v2
with:
path: |
~/.cargo/registry
~/.cargo/git
target
# use "-wasm-target" to cache these separately from the CI workflow
key: ${{ runner.os }}-cargo-wasm-target-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-wasm-target-
- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f

- name: Build WASM
working-directory: crates/wasm
run: wasm-pack build --target nodejs --out-dir pkg-node --scope citeproc-rs --dev

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Restore yarn cache
uses: actions/cache@v2
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: Yarn install
working-directory: crates/wasm/js-tests
run: yarn install

- name: Run tests
working-directory: crates/wasm/js-tests
run: yarn test

# otherwise, the yarn cache will contain a different @citeproc-rs/wasm
# entry every time.
- name: Clean yarn cache dir of wasm output
run: |
cd "$(yarn cache dir)"
ls | grep citeproc-rs-wasm | xargs rm -rf
42 changes: 21 additions & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/wasm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pkg-scratch
pkg
dist
5 changes: 4 additions & 1 deletion crates/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
# Copyright © 2018 Corporation for Digital Scholarship

[package]
name = "citeproc-wasm"
# package name on NPM = @citeproc-rs/ + this field = @citeproc-rs/wasm
name = "wasm"
version = "0.0.1"
authors = ["Cormac Relf <[email protected]>"]
license = "MPL-2.0"
edition = "2018"
repository = "https://github.com/cormacrelf/citeproc-rs"
description = "citeproc-rs, compiled to WebAssembly"

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
54 changes: 2 additions & 52 deletions crates/wasm/README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,7 @@
# `citeproc-wasm`
# `@citeproc-rs/wasm`

This is a build of `citeproc` that is suitable for use in Node.js, a browser or
a Firefox/Chromium-based application like Zotero. It consists of a WebAssembly
(WASM) binary, and a fairly lightweight JavaScript wrapper for that binary.

The wrapper consists of:

* A JS class to wrap `citeproc::Driver`. The core processing code is
synchronous, but it will generally be sufficiently fast to run in any
interactive context.
* A JS interface for library consumers to asynchronously fetch locales and
style modules at specific points in Driver's lifecycle. The fetching must
happen and complete before the processing actually begins, because processing
cannot be paused to wait for the JS library consumer to fetch something. So
the processor will analyse which locales and modules will be needed, and
request all of them at once.
* Style, locale and style-module inputs are all XML strings.
* The library does its own parsing, and validates it at the same time. Any
validation errors are reported, with line/column positions, the text at that
location, a descriptive and useful message (only in English at the moment)
and sometimes even a hint for how to fix it, for common errors.
* Input libraries are only CSL-JSON, serialized as a string. The other input
formats that `citeproc` may recognise will be disabled, to save bundle size.
* A TypeScript definition file auto-generated from the Rust types and
interfaces.

The library is intended to replace `citeproc-js`, but the interface will be
different.

* All fetching is via Promises, but if you have a string already you can just
`return Promise.resolve(string)`.

* Not all features are supported (at least for now).
* The `citeproc-js` abbreviations API currently requires a huge
implementation by library consumers in order to get the behaviour
envisioned by the spec. Some of this functionality needs to be made more
consistent by having the processor take responsibility for it. (Like
tokenizing/slugifying strings and requesting My, My_Abbreviated,
My_Abbreviated_Name in order, etc. Maybe feed all your abbreviations to the
processor and allow it to build its own
[trie](https://en.wikipedia.org/wiki/Trie) for much faster lookup.)
* Some of it doesn't make much sense, like requiring users to import Juris-M
abbreviation lists into each document rather than linking abbreviations to
styles or jurisdictions or the Courts that each item in those huge lists
refers to.

* You cannot deserialize your own XML. Any tree modifications you want to do
should be implemented as transforms on the Style AST within `citeproc`,
which, it should be noted, is dramatically different from the XML such that
it is not possible to represent invalid CSL. There isn't any API for such
transforms at present, but it could be done. It's possible but unlikely that
such an API could be bridged to JavaScript.

Drop-in API compatibility will not be a goal, and given that, the whole
interface may as well be improved.

README for newer version of the API forthcoming.
Loading

0 comments on commit ec9b385

Please sign in to comment.