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

separate transformer packages #94

Merged
merged 6 commits into from
Dec 2, 2023
Merged
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
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ node_modules
/test/CMakeFiles
/packages/runtime/lib
/packages/emnapi/lib
/packages/emnapi/transformer/out
/packages/core/src/index.js
/packages/**/test/**/input
/packages/**/test/**/expected
/out
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ jobs:
shell: bash
run: npm run build --workspaces --if-present

- name: Test Emscripten ESM library
shell: bash
run: npm run test -w packages/ts-transform-emscripten-esm-library

# - name: Lint
# run: npm run lint

Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This doc will explain the structure of this project and some points need to note
- `packages/core` (`dependencies`)

This package is designed for using emnapi on non-Emscripten platform, it is a trasformed output of `packages/emnapi`
by using `packages/emnapi/transformer`.
by using `packages/ts-transform-emscripten-parse-tools`.

For common WebAssembly loading case, we can't use the Emscripten JavaScript library file built in `packages/emnapi` package.
We need to manually provide imported symbols to the second parameter of `WebAssembly.instantiate`,
Expand Down Expand Up @@ -52,4 +52,4 @@ This doc will explain the structure of this project and some points need to note
Macro is heavily used in `packages/emnapi`, there are two kinds of macro.

- `$macroName(...)`: transformed to `{{{ macroName(...) }}}`
- `$CUSTOM_MACRO!(...)`: powered by `packages/emnapi/transformer/src/macro.ts`
- `$CUSTOM_MACRO!(...)`: powered by `packages/ts-transform-macro`
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"test": "npm run test -w packages/test",
"test:version": "node ./packages/test/script/test-build-all-napi-version.js",
"release": "node ./script/release.js",
"bump": "npm version -w packages/emnapi -w packages/runtime -w packages/core -w packages/node",
"bump": "npm version -w packages/emnapi -w packages/runtime -w packages/core -w packages/node -w packages/ts-transform-emscripten-parse-tools",
"lint": "eslint \"packages/**/*.ts\""
},
"author": "toyobayashi",
Expand Down Expand Up @@ -48,10 +48,13 @@
"eslint-plugin-promise": "^6.1.1",
"fs-extra": "^10.1.0",
"rollup": "^4.5.2",
"ts-clone-node": "^3.0.0",
"typescript": "~5.0.4"
},
"workspaces": [
"packages/ts-transform-macro",
"packages/ts-transform-emscripten-parse-tools",
"packages/ts-transform-emscripten-esm-library",
"packages/rollup-plugin-emscripten-esm-library",
"packages/runtime",
"packages/node",
"packages/emnapi",
Expand Down
1 change: 0 additions & 1 deletion packages/emnapi/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ tsconfig*
/src/**/*.ts
/build
/dist/emnapi-core.js
/transformer
13 changes: 5 additions & 8 deletions packages/emnapi/script/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const fs = require('fs')
const path = require('path')
const { createRequire } = require('module')
const ts = require('typescript')
const { compile } = require('@tybys/tsapi')
const rollupTypescript = require('@rollup/plugin-typescript').default
const rollupAlias = require('@rollup/plugin-alias').default
const { rollup } = require('rollup')
Expand All @@ -12,9 +11,6 @@ const { rollup } = require('rollup')
// } = require('../../runtime/script/build.js')

async function build () {
const transformerTsconfigPath = path.join(__dirname, '../transformer/tsconfig.json')
compile(transformerTsconfigPath)

const libTsconfigPath = path.join(__dirname, '../tsconfig.json')
// compile(libTsconfigPath)
const libTsconfig = JSON.parse(fs.readFileSync(libTsconfigPath, 'utf8'))
Expand All @@ -40,7 +36,7 @@ async function build () {
before: [
{
type: 'program',
factory: require('../transformer/out/macro.js').default
factory: require('@emnapi/ts-transform-macro').createTransformerFactory
},
{
type: 'program',
Expand All @@ -65,9 +61,10 @@ async function build () {
{ find: 'emnapi:shared', replacement: path.join(__dirname, '../src/emscripten/init.ts') }
]
}),
require('./rollup-plugin-emscripten.js').default({
require('@emnapi/rollup-plugin-emscripten-esm-library').default({
defaultLibraryFuncsToInclude: ['$emnapiInit'],
exportedRuntimeMethods: ['emnapiInit'],
processDirective: true,
modifyOutput (output) {
return output
.replace(/\$POINTER_SIZE/g, '{{{ POINTER_SIZE }}}')
Expand Down Expand Up @@ -114,12 +111,12 @@ async function build () {
before: [
{
type: 'program',
factory: require('../transformer/out/macro.js').default
factory: require('@emnapi/ts-transform-macro').createTransformerFactory
},
{
type: 'program',
factory (program) {
return require('../transformer/out/index.js').default(program, {
return require('@emnapi/ts-transform-emscripten-parse-tools').createTransformerFactory(program, {
defines: {
MEMORY64: 0
}
Expand Down
59 changes: 0 additions & 59 deletions packages/emnapi/script/rollup-plugin-emscripten.js

This file was deleted.

24 changes: 0 additions & 24 deletions packages/emnapi/transformer/tsconfig.json

This file was deleted.

1 change: 1 addition & 0 deletions packages/rollup-plugin-emscripten-esm-library/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib
7 changes: 7 additions & 0 deletions packages/rollup-plugin-emscripten-esm-library/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.vscode
node_modules
/src
.gitignore
.npmignore
tsconfig.json
*.map
34 changes: 34 additions & 0 deletions packages/rollup-plugin-emscripten-esm-library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @emnapi/rollup-plugin-emscripten-esm-library

See https://github.com/toyobayashi/emnapi/blob/main/packages/ts-transform-emscripten-esm-library#readme

```js
// rollup.config.mjs
import { defineConfig } from 'rollup'
import rollupEmscriptenEsmLibrary from '@emnapi/rollup-plugin-emscripten-esm-library'

export default defineConfig({
/* ... */
output: {
/* ... */

/**
* required,
* ensure no import declaration left in the final output
*/
format: 'esm'
},
plugins: [
rollupEmscriptenEsmLibrary({
/** @type {string[]} */
defaultLibraryFuncsToInclude: [ /* ... */],

/** @type {string[]} */
exportedRuntimeMethods: [ /* ... */ ],

/** @type {boolean} */
processDirective: true
})
]
})
```
42 changes: 42 additions & 0 deletions packages/rollup-plugin-emscripten-esm-library/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@emnapi/rollup-plugin-emscripten-esm-library",
"version": "1.0.0",
"description": "Using ESM to write Emscripten JavaScript library",
"type": "commonjs",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"exports": {
".": {
"import": {
"types": "./lib/index.d.mts",
"default": "./lib/index.mjs"
},
"require": {
"types": "./lib/index.d.js",
"default": "./lib/index.js"
}
}
},
"scripts": {
"build": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/toyobayashi/emnapi.git"
},
"author": "toyobayashi",
"license": "MIT",
"bugs": {
"url": "https://github.com/toyobayashi/emnapi/issues"
},
"homepage": "https://github.com/toyobayashi/emnapi/tree/main/packages/rollup-plugin-emscripten-esm-library#readme",
"publishConfig": {
"access": "public"
},
"peerDependencies": {
"rollup": ">= 2.0.0"
},
"dependencies": {
"@emnapi/ts-transform-emscripten-esm-library": "^1.0.0"
}
}
18 changes: 18 additions & 0 deletions packages/rollup-plugin-emscripten-esm-library/src/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Plugin } from 'rollup'
import { transformWithOptions, type TransformOptions } from '@emnapi/ts-transform-emscripten-esm-library'

export interface PluginOptions extends TransformOptions {
modifyOutput?: (output: string) => string
}

export default function (options?: PluginOptions): Plugin {
function defaultModify<T> (_: T): T { return _ }
const modifyOutput = options?.modifyOutput ?? defaultModify

return {
name: '@emnapi/rollup-plugin-emscripten-esm-library',
renderChunk: function (code, chunk) {
return modifyOutput(transformWithOptions(chunk.fileName, code, options))
}
}
}
18 changes: 18 additions & 0 deletions packages/rollup-plugin-emscripten-esm-library/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Plugin } from 'rollup'
import { transformWithOptions, type TransformOptions } from '@emnapi/ts-transform-emscripten-esm-library'

export interface PluginOptions extends TransformOptions {
modifyOutput?: (output: string) => string
}

export default function (options?: PluginOptions): Plugin {
function defaultModify<T> (_: T): T { return _ }
const modifyOutput = options?.modifyOutput ?? defaultModify

return {
name: '@emnapi/rollup-plugin-emscripten-esm-library',
renderChunk: function (code, chunk) {
return modifyOutput(transformWithOptions(chunk.fileName, code, options))
}
}
}
17 changes: 17 additions & 0 deletions packages/rollup-plugin-emscripten-esm-library/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../shared/tsconfig.base.json",
"compilerOptions": {
"target": "ES2019",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true,
"declaration": true,
"sourceMap": true,
"declarationMap": true,
"outDir": "lib"
},
"include": [
"./src/**/*",
"../../runtime/src/typings/*.d.ts"
]
}
2 changes: 2 additions & 0 deletions packages/ts-transform-emscripten-esm-library/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/lib
/test/actual
8 changes: 8 additions & 0 deletions packages/ts-transform-emscripten-esm-library/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.vscode
node_modules
/src
.gitignore
.npmignore
tsconfig.json
*.map
/test
Loading