Added example that shows how to run swc in the browser. Instructions can be found in swc/wasm/example/readme.txt (#871)

Co-authored-by: rickevry <rickard@rickardnilsson.com>
This commit is contained in:
RickEvry 2020-06-30 04:14:07 +02:00 committed by GitHub
parent f500e9528c
commit 901b32fa05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 102 additions and 0 deletions

1
wasm/example/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

47
wasm/example/index.js Normal file
View File

@ -0,0 +1,47 @@
let settings =
{
"jsc": {
"target": "es2016",
"parser": {
"syntax": "ecmascript",
"jsx": true,
"dynamicImport": false,
"numericSeparator": false,
"privateMethod": false,
"functionBind": false,
"exportDefaultFrom": false,
"exportNamespaceFrom": false,
"decorators": false,
"decoratorsBeforeExport": false,
"topLevelAwait": false,
"importMeta": false
}
}
};
let code = `
let a = 1;
let b = {
c: {
d: 1
}
};
console.log(b?.c?.d);
let MyComponent = () => {
return (<div a={10}>
<p>Hello World!</p>
</div>);
}
`;
const swc = import("./pkg/wasm.js");
swc.then(swc => {
console.log("SWC Loaded", swc);
let result = swc.transformSync(code, settings);
console.log("result from transformSync", result);
console.log(result.code);
document.getElementById("result").innerHTML = "<xmp>" + result.code + "</xmp>";
document.getElementById("source").innerHTML = "<xmp>" + code + "</xmp>";
});

15
wasm/example/package.json Normal file
View File

@ -0,0 +1,15 @@
{
"scripts": {
"copy-files": "copyfiles ../pkg/* ./pkg",
"dev-server": "webpack-dev-server",
"serve": "run-s copy-files dev-server"
},
"devDependencies": {
"copyfiles": "^2.3.0",
"npm-run-all": "^4.1.5",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10"
},
"dependencies": {}
}

21
wasm/example/readme.txt Normal file
View File

@ -0,0 +1,21 @@
To run this example, you first need to build swc for the web:
cd swc/wasm
./scripts/build_web_release.sh
Then run npm install:
cd swc/wasm/example
npm install
Finally, start the webpack-dev-server:
cd swc/wasm/example
npm run serve
Browse to http://localhost:8080

View File

@ -0,0 +1,10 @@
const path = require('path');
module.exports = {
entry: "./index.js",
output: {
path: path.resolve(__dirname, "dist"),
filename: "index.js",
},
mode: "development"
};

View File

@ -0,0 +1,4 @@
#!/bin/bash
# run this script from the wasm folder ./scripts/build_nodejs_release.sh
npx wasm-pack build --scope swc -t nodejs

View File

@ -0,0 +1,4 @@
#!/bin/bash
# run this script from the wasm folder ./scripts/build_web_release.sh
npx wasm-pack build --scope swc