Skip to content

Commit

Permalink
seperate transformer packages
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Dec 1, 2023
1 parent 04d2db5 commit 158676e
Show file tree
Hide file tree
Showing 29 changed files with 248 additions and 63 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ node_modules
/test/CMakeFiles
/packages/runtime/lib
/packages/emnapi/lib
/packages/emnapi/transformer/out
/packages/core/src/index.js
/out
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`
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
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
12 changes: 4 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,7 +61,7 @@ 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'],
modifyOutput (output) {
Expand Down Expand Up @@ -114,12 +110,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
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
1 change: 1 addition & 0 deletions packages/rollup-plugin-emscripten-esm-library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @emnapi/rollup-plugin-emscripten-esm-library
30 changes: 30 additions & 0 deletions packages/rollup-plugin-emscripten-esm-library/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"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",
"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"
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const { EOL } = require('os')
const ts = require('typescript')
const createTransformer = require('../transformer/out/esm.js').default

/**
* @param {string} fileName
* @param {string} sourceText
* @returns {string}
*/
function transform (fileName, sourceText) {
import { EOL } from 'os'
import ts = require('typescript')
import type { Plugin } from 'rollup'
import { createTransformerFactory } from '@emnapi/ts-transform-emscripten-esm-library'

export interface PluginOptions {
defaultLibraryFuncsToInclude?: string[]
exportedRuntimeMethods?: string[]
modifyOutput?: (output: string) => string
}

function transform (fileName: string, sourceText: string): string {
const compilerOptions = {
allowJs: true,
module: ts.ModuleKind.ESNext,
Expand All @@ -23,7 +25,7 @@ function transform (fileName, sourceText) {
host
})

const transformerFactory = createTransformer(program)
const transformerFactory = createTransformerFactory(program)

const transformResult = ts.transform(source, [transformerFactory])
const printer = ts.createPrinter({
Expand All @@ -32,14 +34,11 @@ function transform (fileName, sourceText) {
return printer.printNode(ts.EmitHint.SourceFile, transformResult.transformed[0], transformResult.transformed[0])
}

/**
* @param {{ defaultLibraryFuncsToInclude?: string[]; exportedRuntimeMethods?: string[]; modifyOutput?: (output: string) => string }=} options
*/
exports.default = function (options) {
const defaultLibraryFuncsToInclude = (options ? options.defaultLibraryFuncsToInclude : []) || []
const exportedRuntimeMethods = (options ? options.exportedRuntimeMethods : []) || []
const defaultModify = _ => _
const modifyOutput = (options ? options.modifyOutput : defaultModify) || defaultModify
export default function (options?: PluginOptions): Plugin {
const defaultLibraryFuncsToInclude = options?.defaultLibraryFuncsToInclude ?? []
const exportedRuntimeMethods = options?.exportedRuntimeMethods ?? []
const defaultModify = <T>(_: T): T => _
const modifyOutput = options?.modifyOutput ?? defaultModify

return {
name: 'ts-transform-emscriten',
Expand Down
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/**/*.ts",
"../../runtime/src/typings/*.d.ts"
]
}
1 change: 1 addition & 0 deletions packages/ts-transform-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/ts-transform-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
1 change: 1 addition & 0 deletions packages/ts-transform-emscripten-esm-library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @emnapi/ts-transform-emscripten-esm-library
24 changes: 24 additions & 0 deletions packages/ts-transform-emscripten-esm-library/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@emnapi/ts-transform-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",
"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/ts-transform-emscripten-esm-library#readme",
"publishConfig": {
"access": "public"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,12 @@ class Transformer {
}
}

export default function createTransformer (program: Program): TransformerFactory<SourceFile> {
function createTransformerFactory (program: Program): TransformerFactory<SourceFile> {
return (context) => {
const transformer = new Transformer(context, program)

return transformer.transform
}
}

export { createTransformerFactory }
17 changes: 17 additions & 0 deletions packages/ts-transform-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/**/*.ts",
"../../runtime/src/typings/*.d.ts"
]
}
1 change: 1 addition & 0 deletions packages/ts-transform-emscripten-parse-tools/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib
7 changes: 7 additions & 0 deletions packages/ts-transform-emscripten-parse-tools/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.vscode
node_modules
/src
.gitignore
.npmignore
tsconfig.json
*.map
25 changes: 25 additions & 0 deletions packages/ts-transform-emscripten-parse-tools/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@emnapi/ts-transform-emscripten-parse-tools",
"version": "0.44.0",
"private": true,
"description": "",
"type": "commonjs",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"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/ts-transform-emscripten-parse-tools",
"publishConfig": {
"access": "public"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import type {
Statement
} from 'typescript'

import * as ts from 'typescript'
import ts = require('typescript')
import { join } from 'path'

export interface DefineOptions {
defines?: Record<string, any>
defines?: {
MEMORY64?: 0 | 1
}
}

function isEmscriptenMacro (text: string): boolean {
Expand Down Expand Up @@ -733,7 +735,7 @@ function createTransformerFactory (_program: Program, config: DefineOptions): Tr
// inject HEAP_DATA_VIEW
const injectedSrc = ts.visitEachChild(transformedSrc, transform.functionLikeDeclarationVisitor, context)

const doNotInsertImport = join(__dirname, '../../src/core/init.ts')
const doNotInsertImport = join(__dirname, '../../emnapi/src/core/init.ts')
if (src.fileName === doNotInsertImport) {
return injectedSrc
}
Expand Down Expand Up @@ -781,4 +783,4 @@ function createTransformerFactory (_program: Program, config: DefineOptions): Tr
}
}

export default createTransformerFactory
export { createTransformerFactory }
17 changes: 17 additions & 0 deletions packages/ts-transform-emscripten-parse-tools/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/**/*.ts",
"../../runtime/src/typings/*.d.ts"
]
}
1 change: 1 addition & 0 deletions packages/ts-transform-macro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib
7 changes: 7 additions & 0 deletions packages/ts-transform-macro/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.vscode
node_modules
/src
.gitignore
.npmignore
tsconfig.json
*.map
1 change: 1 addition & 0 deletions packages/ts-transform-macro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @emnapi/ts-transform-macro
Loading

0 comments on commit 158676e

Please sign in to comment.