Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable browser / cross-platform support #18

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ jobs:
debug-${{ runner.os }}-
- name: Run tests
run: cargo test --verbose

- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
- run: wasm-pack build --target nodejs
- name: Install wasm-opt
run: cargo install wasm-opt
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'npm'
- run: npm ci
- run: ./build.sh

- run: npm test
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

# remove scope or update it once we decide exactly where we want to deploy this thing
- run: wasm-pack build --target nodejs
- run: npm run build
- uses: actions/setup-node@v3
with:
node-version: 20
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pkg/
/target
/node_modules
/node_modules
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Tests:

Build wasm package:

`wasm-pack build --target=nodejs`
`npm run build`

which will output your wasm package in `./pkg`

Expand All @@ -36,4 +36,4 @@ and replace:

```
$1 = { path = "../swc/crates/$1"
```
```
2 changes: 1 addition & 1 deletion Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]

[profile.release]
lto = true
opt-level = 'z'
Copy link
Contributor Author

@NullVoxPopuli NullVoxPopuli Jul 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the browser wasm module:

  • with only lto = true: 1MB
  • with only opt-level = 'z': 741K
  • with neither: 1MB
  • with both: 741K
  • with wee_alloc: 741K
  • with lol_alloc's LockedAllocator<FreeListAllocator>: 741K
  • with lol_alloc's AssumeSingleThreaded<FreeListAllocator>: 741K
  • with lol_alloc's FailAllocator: 374K (but also doesn't work 🙈 )

Copy link
Contributor Author

@NullVoxPopuli NullVoxPopuli Jul 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a separate machine now, it seems I'm getting different results.
I had thought that the current version of the optimization settings would give 741K, but on this machine:

❯ du -r ./pkg/
 2.6M └─┬ pkg                         │██████████████████████████ │ 100%
 1.3M   ├─┬ node                      │             █████████████ │  50%
 1.3M   │ ├── content_tag_bg.wasm     │             █████████████ │  49%
  16K   │ ├── content_tag.cjs         │             ░░░░░░░░░░░░█ │   1%
 4.0K   │ ├── content_tag_bg.wasm.d.ts│             ░░░░░░░░░░░░█ │   0%
 4.0K   │ ├── content_tag.d.ts        │             ░░░░░░░░░░░░█ │   0%
 4.0K   │ └── .gitignore              │             ░░░░░░░░░░░░█ │   0%
 1.3M   ├─┬ browser                   │             █████████████ │  50%
 1.2M   │ ├── content_tag_bg.wasm     │             █████████████ │  48%
  16K   │ ├── content_tag.js          │             ░░░░░░░░░░░░█ │   1%
 4.0K   │ ├── content_tag_bg.wasm.d.ts│             ░░░░░░░░░░░░█ │   0%
 4.0K   │ ├── content_tag.d.ts        │             ░░░░░░░░░░░░█ │   0%
 4.0K   │ └── .gitignore              │             ░░░░░░░░░░░░█ │   0%
 4.0K   ├── package.json              │                         █ │   0%
 4.0K   ├── README.md                 │                         █ │   0%
 4.0K   └── LICENSE                   │                         █ │   0%


the browser wasm is 1.2MB...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ended up being about 1.35MB today:
image

Hopefully it can shrink in the future (or I can ditch more and more babel stuff / not use @babel/standalone, perhaps)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GH says less:
image

🤷 could be MiB vs MB or something.

but, 487kB isn't terrible since accuracy is what we're doing here. I can finally divide numbers before <template>, which is good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that I opted out of all the different allocators -- seemed too risky, the more I read about them

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today, tho:

❯ du -r ./pkg/
2.7M └─┬ pkg                         │█████████████████████ │ 100%
1.3M   ├─┬ standalone                │          ███████████ │  50%
1.3M   │ ├── content_tag_bg.wasm     │          ███████████ │  48%
 20K   │ ├── content_tag.js          │          ░░░░░░░░░░█ │   1%
4.0K   │ ├── standalone.js           │          ░░░░░░░░░░█ │   0%
4.0K   │ ├── standalone.d.ts         │          ░░░░░░░░░░█ │   0%
4.0K   │ ├── content_tag_bg.wasm.d.ts│          ░░░░░░░░░░█ │   0%
4.0K   │ ├── content_tag.d.ts        │          ░░░░░░░░░░█ │   0%
4.0K   │ └── .gitignore              │          ░░░░░░░░░░█ │   0%
1.3M   ├─┬ node                      │          ███████████ │  50%
1.3M   │ ├── content_tag_bg.wasm     │          ███████████ │  48%
 16K   │ ├── content_tag.cjs         │          ░░░░░░░░░░█ │   1%
4.0K   │ ├── content_tag_bg.wasm.d.ts│          ░░░░░░░░░░█ │   0%
4.0K   │ ├── content_tag.d.ts        │          ░░░░░░░░░░█ │   0%
4.0K   │ └── .gitignore              │          ░░░░░░░░░░█ │   0%
4.0K   ├── package.json              │                    █ │   0%
4.0K   ├── README.md                 │                    █ │   0%
4.0K   └── LICENSE                   │                    █ │   0%

with:
codegen-units = 1

❯ du -r ./pkg/
2.6M └─┬ pkg                         │████████████████████████████████ │ 100%
1.3M   ├─┬ standalone                │                ████████████████ │  50%
1.2M   │ ├── content_tag_bg.wasm     │                ████████████████ │  48%
 20K   │ ├── content_tag.js          │                ░░░░░░░░░░░░░░░█ │   1%
4.0K   │ ├── standalone.js           │                ░░░░░░░░░░░░░░░█ │   0%
4.0K   │ ├── standalone.d.ts         │                ░░░░░░░░░░░░░░░█ │   0%
4.0K   │ ├── content_tag_bg.wasm.d.ts│                ░░░░░░░░░░░░░░░█ │   0%
4.0K   │ ├── content_tag.d.ts        │                ░░░░░░░░░░░░░░░█ │   0%
4.0K   │ └── .gitignore              │                ░░░░░░░░░░░░░░░█ │   0%
1.3M   ├─┬ node                      │                ████████████████ │  49%
1.2M   │ ├── content_tag_bg.wasm     │                ████████████████ │  48%
 16K   │ ├── content_tag.cjs         │                ░░░░░░░░░░░░░░░█ │   1%
4.0K   │ ├── content_tag_bg.wasm.d.ts│                ░░░░░░░░░░░░░░░█ │   0%
4.0K   │ ├── content_tag.d.ts        │                ░░░░░░░░░░░░░░░█ │   0%
4.0K   │ └── .gitignore              │                ░░░░░░░░░░░░░░░█ │   0%
4.0K   ├── package.json              │                               █ │   0%
4.0K   ├── README.md                 │                               █ │   0%
4.0K   └── LICENSE                   │                               █ │   0%

codegen-units = 1

[dependencies]
swc_common = { git = "https://github.com/ef4/swc.git", branch = "content-tag", features=["tty-emitter"] }
swc = { git = "https://github.com/ef4/swc.git", branch = "content-tag" }
Expand Down
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,33 @@ npm install content-tag

## Usage

### Node (CommonJS)

```js
let { Preprocessor } = require('content-tag');
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');

console.log(output);
```

### Node (ESM)

```js
import { Preprocessor } from 'content-tag';
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');

console.log(output);
```

### Browser (ESM)

```js
import { createPreprocessor } from 'content-tag/standalone';
let p = await createPreprocessor();
let output = p.process('<template>Hi</template>');

console.log(output);
```

Expand Down
26 changes: 26 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

# we need npm packages for the post-wasm phase
npm install

rm -rf pkg

# wasm-pack knows to use wasm-opt, when present
# NOTE: wasm-pack does not support multi-target building
# so we'll build twice, and then tweak package.json
# "exports" to point at the correct build depending on node or browser
wasm-pack build --target web --out-dir pkg/standalone --weak-refs --no-pack --release
wasm-pack build --target nodejs --out-dir pkg/node --weak-refs --no-pack --release

# Rename the node js file to cjs, because we emit type=module
mv pkg/node/content_tag.js pkg/node/content_tag.cjs

# generate the rest of the package for npm, since
# we disabled package.json generation above with --no-pack.
# this needs to be done because we're targeting
# both browser and node, which wasm-packg doesn't natively support.
node ./build/post-wasm-build.mjs

# ---------
cp LICENSE pkg/LICENSE
cp README.md pkg/README.md
47 changes: 47 additions & 0 deletions build/post-wasm-build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// This post-wasm-build.js script is called from build.sh
import fs from "node:fs/promises";
import path from "node:path";
import url from "node:url";
import toml from "toml";

let cargo = await fs.readFile("Cargo.toml", "utf8");
let config = toml.parse(cargo);

const manifest = {
name: config.package.name,
description: config.package.description,
version: config.package.version,
license: config.package.license,
repository: {
type: "git",
url: "https://github.com/embroider-build/content-tag",
},
files: ["standalone", "node"],
type: "module",
exports: {
".": {
types: "./node/content_tag.d.ts",
default: "./node/content_tag.cjs",
},
"./standalone": {
types: "./standalone/standalone.d.ts",
import: "./standalone/standalone.js",
},
},
};

const content = JSON.stringify(manifest, null, 2);

const here = url.fileURLToPath(new URL(".", import.meta.url));
const root = path.join(here, "..");
const output = path.join(root, "pkg");

await fs.writeFile(path.join(output, "package.json"), content);
await fs.copyFile(
path.join(here, "src/standalone.js"),
path.join(output, "standalone/standalone.js")
);
await fs.copyFile(
path.join(here, "src/standalone.d.ts"),
path.join(output, "standalone/standalone.d.ts")
);
5 changes: 5 additions & 0 deletions build/src/standalone.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Preprocessor } from './content_tag';

export * from "./content_tag";

export function createPreprocessor(): Promise<Preprocessor>;
12 changes: 12 additions & 0 deletions build/src/standalone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export * from "./content_tag.js";

import init, { Preprocessor } from "./content_tag.js";

export async function createPreprocessor() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a tiny wrapper function since the default way rust wants us to build a wasm thing is ... goofy. Like, I get it, two things need to happen, load the wasm file and then instantiate the stuff.
maybe I should PR to them.

// This no-ops if it's already ran
await init();

let processor = new Preprocessor();

return processor;
}
25 changes: 25 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<html>

<!-- View this file with `npm run web` -->

<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
<script type="module">
import {createPreprocessor} from './pkg/standalone/standalone.js';

async function run() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is how I'll be using it in my REPL, tho it'll be import { createPreprocessor } from 'content-tag/standalone'

const p = await createPreprocessor();
const output = p.process('<template>Hi from createPreprocessor</template>');

document.querySelector('#output').innerHTML = output;
}

run();
</script>
</head>

<body>
<pre id="output"></pre>
</body>

</html>
20 changes: 19 additions & 1 deletion package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
"url": "git@github.com:embroider-build/content-tag.git"
},
"scripts": {
"web": "npx html-pages . --no-cache",
"build": "./build.sh",
"test": "mocha"
},
"devDependencies": {
"@release-it-plugins/lerna-changelog": "^6.0.0",
"@release-it/bumper": "^5.1.0",
"chai": "^4.3.7",
"code-equality-assertions": "github:mansona/code-equality-assertions#add-chai-build",
"content-tag": "file:./pkg",
"eslint": "^8.44.0",
"mocha": "^10.2.0",
"toml": "^3.0.0",
"release-it": "^16.2.1"
},
"publishConfig": {
Expand Down
65 changes: 65 additions & 0 deletions test/node-api-esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import chai from "chai";
import { codeEquality } from "code-equality-assertions/chai";

chai.use(codeEquality);

const { expect } = chai;

import { Preprocessor } from "content-tag";
const p = new Preprocessor();

describe("Node ESM", function () {
it("works for a basic example", function () {
let output = p.process("<template>Hi</template>");

expect(output).to
.equalCode(`import { template } from "@ember/template-compiler";
export default template(\`Hi\`, {
eval () {
return eval(arguments[0]);
}
});`);
});

it("Emits parse errors with anonymous file", function () {
expect(function () {
p.process(`const thing = "face";
<template>Hi`);
}).to.throw(`Parse Error at <anon>:2:15: 2:15`);
});

it("Emits parse errors with real file", function () {
expect(function () {
p.process(
`const thing = "face";
<template>Hi`,
"path/to/my/component.gjs"
);
}).to.throw(`Parse Error at path/to/my/component.gjs:2:15: 2:15`);
});

it("Offers source_code snippet on parse errors", function () {
let parseError;
try {
p.process(`class {`);
} catch (err) {
parseError = err;
}
expect(parseError)
.to.have.property("source_code")
.matches(/Expected ident.*class \{/s);
});

it("Offers source_code_color snippet on parse errors", function () {
let parseError;
try {
p.process(`class {`);
} catch (err) {
parseError = err;
}
// eslint-disable-next-line no-control-regex
expect(parseError)
.to.have.property("source_code_color")
.matches(/Expected ident.*[\u001b].*class \{/s);
});
});
4 changes: 1 addition & 3 deletions test/node-api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Preprocessor } = require("../pkg");
const { Preprocessor } = require("content-tag");
const chai = require("chai");
const { codeEquality } = require("code-equality-assertions/chai");

Expand Down Expand Up @@ -217,10 +217,8 @@ describe("process", function () {
}
};`
);

});


it("Emits parse errors with anonymous file", function () {
expect(function () {
p.process(`const thing = "face";
Expand Down