mirror of
https://github.com/swc-project/swc.git
synced 2024-11-24 02:06:08 +03:00
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:
parent
f500e9528c
commit
901b32fa05
1
wasm/example/.gitignore
vendored
Normal file
1
wasm/example/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
node_modules
|
47
wasm/example/index.js
Normal file
47
wasm/example/index.js
Normal 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
15
wasm/example/package.json
Normal 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
21
wasm/example/readme.txt
Normal 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
|
||||
|
||||
|
||||
|
||||
|
||||
|
10
wasm/example/webpack.config.js
Normal file
10
wasm/example/webpack.config.js
Normal 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"
|
||||
};
|
4
wasm/scripts/build_nodejs_release.sh
Executable file
4
wasm/scripts/build_nodejs_release.sh
Executable 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
|
4
wasm/scripts/build_web_release.sh
Executable file
4
wasm/scripts/build_web_release.sh
Executable 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
|
Loading…
Reference in New Issue
Block a user