Skip to content

Commit

Permalink
feat(bindgen): Generate browser ESM bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Jan 18, 2023
1 parent 88ebd0a commit 9c2a112
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 57 deletions.
52 changes: 1 addition & 51 deletions doc/content/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions src/bindgen/demo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
:root {
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
font-size: 16px;
line-height: 20px;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

li {
text-align: left;
}

#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 12em;
padding: 1.1em;
will-change: filter;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vanilla:hover {
filter: drop-shadow(0 0 2em #3178c6aa);
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
18 changes: 18 additions & 0 deletions src/bindgen/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as <bindgenPackageNameCamelCase> from '../<bindgenPackageName>.js'
console.log('demo stuff')

const packageFunctions = []
for (const [key, val] of Object.entries(<bindgenPackageNameCamelCase>)) {
if (typeof val == 'function') {
packageFunctions.push(key)
}
}

const pipelineFunctionsList = document.getElementById('pipeline-functions-list')
pipelineFunctionsList.innerHTML = `
<li>
${packageFunctions.join('</li>\n<li>')}
</li>
`
console.log(packageFunctions)
console.log(<bindgenPackageNameCamelCase>)
30 changes: 30 additions & 0 deletions src/bindgen/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/demo/logo.svg" />
<link rel="stylesheet" href="/demo/style.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title><bindgenPackageName></title>
</head>
<body>
<div id="app">
<a href="https://wasm.itk.org/" target="_blank">
<img src="/demo/logo.svg" class="logo vanilla" alt="ITK Wasm logo" />
</a>
<h1><bindgenPackageName></h1>
package
<div class="card">
<h2>functions</h2>
<ul id="pipeline-functions-list">

</ul>
</div>
<p class="read-the-docs">
Click on the ITK Wasm logo to learn more
</p>
</div>

<script type="module" src="/demo/app.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions src/bindgen/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions src/bindgen/rollup.browser.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { nodeResolve } from '@rollup/plugin-node-resolve'
import copy from 'rollup-plugin-copy'
import typescript from '@rollup/plugin-typescript'
import commonjs from '@rollup/plugin-commonjs'
import nodePolyfills from 'rollup-plugin-polyfill-node'
import ignore from 'rollup-plugin-ignore'
import { terser } from 'rollup-plugin-terser'
import packageJson from './package.json' assert { type: 'json' }
import json from '@rollup/plugin-json'

export default {
input: './src/index.ts',
output: [
{
file: `./dist/${packageJson.name}.js`,
format: 'es',
sourcemap: true,
// plugins: [terser(),],
},
],
plugins: [
copy({
targets: [
{ src: 'node_modules/itk-wasm/dist/web-workers/bundles/pipeline.worker.js', dest: 'dist/pipelines/web-workers/' },
{ src: 'node_modules/itk-wasm/dist/web-workers/min-bundles/pipeline.worker.js', dest: 'dist/pipelines/web-workers/', rename: 'pipeline.min.worker.js' },
],
hook: 'writeBundle'
}),
ignore(['crypto']),
nodeResolve({
preferBuiltins: false,
browser: true,
}),
commonjs({
transformMixedEsModules: true
}),
nodePolyfills(),
typescript(),
json(),
],
}
18 changes: 16 additions & 2 deletions src/bindgen/template.package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
"version": "0.1.0",
"description": "",
"type": "module",
"main": "./dist/index.js",
"module": "./dist/package-name.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"browser": "./dist/package-name.js",
"node": "./dist/package-name.node.js",
"default": "./dist/package-name.js"
}
},
"scripts": {
"start": "http-server ./dist/ -c-1",
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npm run build:tsc && npm run build:node",
"build": "npm run build:tsc && npm run build:node && npm run build:browser",
"build:node": "rollup -c ./rollup.node.config.js",
"build:browser": "rollup -c ./rollup.browser.config.js",
"build:tsc": "tsc --pretty"
},
"keywords": [
Expand All @@ -26,7 +36,11 @@
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.0.1",
"@rollup/plugin-typescript": "^10.0.1",
"http-server": "^14.1.1",
"rollup": "^3.9.0",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-ignore": "^1.0.10",
"rollup-plugin-polyfill-node": "^0.11.0",
"typescript": "^4.9.4"
}
}
49 changes: 45 additions & 4 deletions src/itk-wasm-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,40 +285,81 @@ function typescriptBindings(outputDir, buildDir, wasmBinaries, options, forNode=
}

if (options.packageName) {
const packageName = options.packageName
const packageJsonPath = path.join(outputDir, 'package.json')
if (!fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(bindgenResource('template.package.json')))
packageJson.name = options.packageName
packageJson.name = packageName
if (options.packageDescription) {
packageJson.description = options.packageDescription
}
packageJson.module = `./dist/${packageName}.js`
packageJson.exports['.'].browser = `./dist/${packageName}.js`
packageJson.exports['.'].node = `./dist/${packageName}.node.js`
packageJson.exports['.'].default = `./dist/${packageName}.js`
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))

const readmePath = path.join(outputDir, 'README.md')
if (!fs.existsSync(readmePath)) {
let readme = `# ${options.packageName}\n`
let readme = `# ${packageName}\n`
if (options.packageDescription) {
readme += `\n${options.packageDescription}\n`
}
fs.writeFileSync(readmePath, readme)
}
}

if (!forNode) {
try {
fs.mkdirSync(path.join(outputDir, 'dist', 'demo'), { recursive: true })
} catch (err) {
if (err.code !== 'EEXIST') throw err
}

const logoPath = path.join(outputDir, 'dist', 'demo', 'logo.svg')
if (!fs.existsSync(logoPath)) {
fs.copyFileSync(bindgenResource('logo.svg'), logoPath)
}

const demoStylePath = path.join(outputDir, 'dist', 'demo', 'style.css')
if (!fs.existsSync(demoStylePath)) {
fs.copyFileSync(bindgenResource('demo.css'), demoStylePath)
}

const indexPath = path.join(outputDir, 'dist', 'index.html')
if (!fs.existsSync(indexPath)) {
let indexContent = fs.readFileSync(bindgenResource('index.html'), { encoding: 'utf8', flag: 'r' })
indexContent = indexContent.replaceAll('<bindgenPackageName>', packageName)
fs.writeFileSync(indexPath, indexContent)
}

const demoPath = path.join(outputDir, 'dist', 'demo', 'app.js')
if (!fs.existsSync(demoPath)) {
let demoContent = fs.readFileSync(bindgenResource('demo.js'), { encoding: 'utf8', flag: 'r' })
demoContent = demoContent.replaceAll('<bindgenPackageName>', options.packageName)
demoContent = demoContent.replaceAll('<bindgenPackageNameCamelCase>', camelCase(packageName))
fs.writeFileSync(demoPath, demoContent)
}

const rollupConfigPath = path.join(outputDir, 'rollup.browser.config.js')
if (!fs.existsSync(rollupConfigPath)) {
fs.copyFileSync(bindgenResource('rollup.browser.config.js'), rollupConfigPath)
}
}
}

if (forNode) {
const rollupConfigPath = path.join(outputDir, 'rollup.node.config.js')
if (!fs.existsSync(rollupConfigPath)) {
fs.copyFileSync(bindgenResource('rollup.node.config.js'), rollupConfigPath)
}
} else {

}

const tsConfigPath = path.join(outputDir, 'tsconfig.json')
if (!fs.existsSync(tsConfigPath)) {
fs.copyFileSync(bindgenResource('tsconfig.json'), tsConfigPath)
}

wasmBinaries.forEach((wasmBinaryName) => {
let wasmBinaryRelativePath = `${buildDir}/${wasmBinaryName}`
if (!fs.existsSync(wasmBinaryRelativePath)) {
Expand Down

0 comments on commit 9c2a112

Please sign in to comment.