Skip to content

Commit

Permalink
Add WASM test (#76)
Browse files Browse the repository at this point in the history
* Add WASM test

* Add comment

* Fix CI command

* Fix test
  • Loading branch information
drwpow committed Oct 20, 2021
1 parent 3b78d4a commit f61bec7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 56 deletions.
53 changes: 30 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: Test

on:
push:
Expand All @@ -7,7 +7,7 @@ on:
branches: ['main']

jobs:
build-wasm:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -17,18 +17,10 @@ jobs:
with:
go-version: 1.17

- name: Get tinygo runtime
run: |
wget -P /tmp/ https://github.com/tinygo-org/tinygo/releases/download/v0.20.0/tinygo_0.20.0_amd64.deb
sudo dpkg -i /tmp/tinygo_0.20.0_amd64.deb
- name: Build WASM
run: |
mkdir -p /home/runner/go/src/github.com/snowpackjs/astro
cp -R . /home/runner/go/src/github.com/snowpackjs/astro/
tinygo build -o astro.wasm -target wasm ./cmd/astro-wasm/astro-wasm.go
- name: Test
run: go test -v ./internal/...

test:
test-wasm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -38,8 +30,31 @@ jobs:
with:
go-version: 1.17

- name: Test
run: go test -v ./internal/...
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 14.x

- name: Get tinygo runtime
run: |
wget -P /tmp/ https://github.com/tinygo-org/tinygo/releases/download/v0.20.0/tinygo_0.20.0_amd64.deb
sudo dpkg -i /tmp/tinygo_0.20.0_amd64.deb
- name: Build WASM
run: |
tinygo build -o ./lib/compiler/astro.wasm -target wasm ./cmd/astro-wasm/astro-wasm.go
cp ./lib/compiler/astro.wasm ./lib/compiler/deno/astro.wasm
- name: Install deps
env:
CI: true
run: yarn --frozen-lockfile --ignore-engines

- name: Build JS
run: yarn build:compiler

- name: Test WASM
run: yarn test

lint:
runs-on: ubuntu-latest
Expand All @@ -50,14 +65,6 @@ jobs:
with:
version: latest

build-js:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- run: yarn
- run: yarn build:compiler

lint-js:
runs-on: ubuntu-latest
steps:
Expand Down
16 changes: 7 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Changelog
name: Release

on:
push:
Expand All @@ -17,27 +17,25 @@ jobs:
with:
go-version: 1.17

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: 14.x

- name: Get tinygo runtime
run: |
wget -P /tmp/ https://github.com/tinygo-org/tinygo/releases/download/v0.20.0/tinygo_0.20.0_amd64.deb
sudo dpkg -i /tmp/tinygo_0.20.0_amd64.deb
- name: Build WASM
run: |
mkdir -p /home/runner/go/src/github.com/snowpackjs/astro
cp -R . /home/runner/go/src/github.com/snowpackjs/astro/
tinygo build -no-debug -o ./lib/compiler/astro.wasm -target wasm ./cmd/astro-wasm/astro-wasm.go
cp ./lib/compiler/astro.wasm ./lib/compiler/deno/astro.wasm
- name: Set up Node.js 14.x
uses: actions/setup-node@v2
with:
node-version: 14.x

- name: Install dependencies
run: yarn --frozen-lockfile --ignore-engines
env:
CI: true
run: yarn --frozen-lockfile --ignore-engines

- name: Create Release Pull Request or Publish to npm
id: changesets
Expand Down
1 change: 1 addition & 0 deletions cmd/astro-wasm/astro-wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func preprocessStyle(i int, style *astro.Node, transformOptions transform.Transf
defer cb()
attrs := wasm_utils.GetAttrs(style)
data, _ := wasm_utils.Await(transformOptions.PreprocessStyle.(js.Value).Invoke(style.FirstChild.Data, attrs))
// note: Rollup (and by extension our Astro Vite plugin) allows for "undefined" and "null" responses if a transform wishes to skip this occurrence
if data[0].IsUndefined() || data[0].IsNull() {
return
}
Expand Down
54 changes: 31 additions & 23 deletions lib/compiler/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

const sleep = (ms) => new Promise((res) => setTimeout(res, ms));

(async () => {
async function run() {
const { transform } = await import('@astrojs/compiler');
let i = 0;
try {
const result = await transform(
`---
const result = await transform(
`---
let value = 'world';
---
Expand All @@ -27,29 +26,38 @@ div {
}
</style>
`,
{
sourcemap: true,
// HOLY CRAP THIS ACTUALLY WORKS!
preprocessStyle: async (value, attrs) => {
let x = i++;
if (!attrs.lang) {
return null;
}
console.log(`Starting to preprocess style ${x} as ${attrs.lang}`);
await sleep(3000);
console.log(`Finished preprocessing ${x}`);
return { code: value.replace('color', 'background') };
},
}
);
console.log(result);
} catch (e) {
console.log(e);
{
sourcemap: true,
// HOLY CRAP THIS ACTUALLY WORKS!
preprocessStyle: async (value, attrs) => {
let x = i++;
if (!attrs.lang) {
return null;
}
// console.log(`Starting to preprocess style ${x} as ${attrs.lang}`);
// await sleep(3000);
// console.log(`Finished preprocessing ${x}`);
return { code: value.replace('color', 'background') };
},
}
);
// console.log(result);

// test
if (!result.code.includes('background:red')) {
throw new Error(`Styles didn’t transform as expected. Expected "background:red" to be present.`);
}

if (!result.code.includes('background:green')) {
throw new Error(`Styles didn’t transform as expected. Expected "background:green" to be present.`);
}

// const start = performance.now()
// const html = await compile(template);
// const end = performance.now()

// console.log('Compiled in ' + (start - end).toFixed(1) + 'ms');
// console.log(html);
})();
}

run();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"lint": "eslint \"lib/**/*.{js,jsx,ts,tsx}\"",
"format": "prettier -w .",
"prerelease": "yarn build:compiler",
"release": "changeset publish"
"release": "changeset publish",
"test": "node ./lib/compiler/test.js"
},
"workspaces": [
"lib/*"
Expand Down

0 comments on commit f61bec7

Please sign in to comment.