diff --git a/.eslintignore b/.eslintignore index b66e7fdb..5a697bdc 100644 --- a/.eslintignore +++ b/.eslintignore @@ -8,6 +8,5 @@ node_modules /test/CMakeFiles /packages/runtime/lib /packages/emnapi/lib -/packages/emnapi/transformer/out /packages/core/src/index.js /out diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 81bec9d7..23ec87fb 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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`, @@ -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` diff --git a/package.json b/package.json index 94554835..67e88ec3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/emnapi/.npmignore b/packages/emnapi/.npmignore index 56c81700..7807f435 100644 --- a/packages/emnapi/.npmignore +++ b/packages/emnapi/.npmignore @@ -3,4 +3,3 @@ tsconfig* /src/**/*.ts /build /dist/emnapi-core.js -/transformer diff --git a/packages/emnapi/script/build.js b/packages/emnapi/script/build.js index ae55ec73..a115456a 100644 --- a/packages/emnapi/script/build.js +++ b/packages/emnapi/script/build.js @@ -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') @@ -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')) @@ -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', @@ -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) { @@ -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 } diff --git a/packages/emnapi/transformer/tsconfig.json b/packages/emnapi/transformer/tsconfig.json deleted file mode 100644 index 6b20b6e6..00000000 --- a/packages/emnapi/transformer/tsconfig.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "compilerOptions": { - "target": "ES2019", - "module": "CommonJS", - "moduleResolution": "node", - "strict": true, - "noImplicitAny": true, - "noImplicitReturns": true, - "noImplicitThis": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "strictNullChecks": true, - "strictPropertyInitialization": true, - "strictBindCallApply": true, - "strictFunctionTypes": true, - "declaration": true, - "sourceMap": true, - "outDir": "out" - }, - "include": [ - "./src/**/*.ts", - "../../runtime/src/typings/*.d.ts" - ] -} diff --git a/packages/rollup-plugin-emscripten-esm-library/.gitignore b/packages/rollup-plugin-emscripten-esm-library/.gitignore new file mode 100644 index 00000000..502167fa --- /dev/null +++ b/packages/rollup-plugin-emscripten-esm-library/.gitignore @@ -0,0 +1 @@ +/lib diff --git a/packages/rollup-plugin-emscripten-esm-library/.npmignore b/packages/rollup-plugin-emscripten-esm-library/.npmignore new file mode 100644 index 00000000..a6f4a228 --- /dev/null +++ b/packages/rollup-plugin-emscripten-esm-library/.npmignore @@ -0,0 +1,7 @@ +.vscode +node_modules +/src +.gitignore +.npmignore +tsconfig.json +*.map diff --git a/packages/rollup-plugin-emscripten-esm-library/README.md b/packages/rollup-plugin-emscripten-esm-library/README.md new file mode 100644 index 00000000..f71ab926 --- /dev/null +++ b/packages/rollup-plugin-emscripten-esm-library/README.md @@ -0,0 +1 @@ +# @emnapi/rollup-plugin-emscripten-esm-library diff --git a/packages/rollup-plugin-emscripten-esm-library/package.json b/packages/rollup-plugin-emscripten-esm-library/package.json new file mode 100644 index 00000000..baf54243 --- /dev/null +++ b/packages/rollup-plugin-emscripten-esm-library/package.json @@ -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" + } +} diff --git a/packages/emnapi/script/rollup-plugin-emscripten.js b/packages/rollup-plugin-emscripten-esm-library/src/index.ts similarity index 65% rename from packages/emnapi/script/rollup-plugin-emscripten.js rename to packages/rollup-plugin-emscripten-esm-library/src/index.ts index f3e9c7c1..001d39f8 100644 --- a/packages/emnapi/script/rollup-plugin-emscripten.js +++ b/packages/rollup-plugin-emscripten-esm-library/src/index.ts @@ -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, @@ -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({ @@ -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 => _ + const modifyOutput = options?.modifyOutput ?? defaultModify return { name: 'ts-transform-emscriten', diff --git a/packages/rollup-plugin-emscripten-esm-library/tsconfig.json b/packages/rollup-plugin-emscripten-esm-library/tsconfig.json new file mode 100644 index 00000000..63242fe6 --- /dev/null +++ b/packages/rollup-plugin-emscripten-esm-library/tsconfig.json @@ -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" + ] +} diff --git a/packages/ts-transform-emscripten-esm-library/.gitignore b/packages/ts-transform-emscripten-esm-library/.gitignore new file mode 100644 index 00000000..502167fa --- /dev/null +++ b/packages/ts-transform-emscripten-esm-library/.gitignore @@ -0,0 +1 @@ +/lib diff --git a/packages/ts-transform-emscripten-esm-library/.npmignore b/packages/ts-transform-emscripten-esm-library/.npmignore new file mode 100644 index 00000000..a6f4a228 --- /dev/null +++ b/packages/ts-transform-emscripten-esm-library/.npmignore @@ -0,0 +1,7 @@ +.vscode +node_modules +/src +.gitignore +.npmignore +tsconfig.json +*.map diff --git a/packages/ts-transform-emscripten-esm-library/README.md b/packages/ts-transform-emscripten-esm-library/README.md new file mode 100644 index 00000000..95a3368e --- /dev/null +++ b/packages/ts-transform-emscripten-esm-library/README.md @@ -0,0 +1 @@ +# @emnapi/ts-transform-emscripten-esm-library diff --git a/packages/ts-transform-emscripten-esm-library/package.json b/packages/ts-transform-emscripten-esm-library/package.json new file mode 100644 index 00000000..6f0a96f6 --- /dev/null +++ b/packages/ts-transform-emscripten-esm-library/package.json @@ -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" + } +} diff --git a/packages/emnapi/transformer/src/esm.ts b/packages/ts-transform-emscripten-esm-library/src/index.ts similarity index 99% rename from packages/emnapi/transformer/src/esm.ts rename to packages/ts-transform-emscripten-esm-library/src/index.ts index cd2f8c5b..0151aa5d 100644 --- a/packages/emnapi/transformer/src/esm.ts +++ b/packages/ts-transform-emscripten-esm-library/src/index.ts @@ -328,10 +328,12 @@ class Transformer { } } -export default function createTransformer (program: Program): TransformerFactory { +function createTransformerFactory (program: Program): TransformerFactory { return (context) => { const transformer = new Transformer(context, program) return transformer.transform } } + +export { createTransformerFactory } diff --git a/packages/ts-transform-emscripten-esm-library/tsconfig.json b/packages/ts-transform-emscripten-esm-library/tsconfig.json new file mode 100644 index 00000000..63242fe6 --- /dev/null +++ b/packages/ts-transform-emscripten-esm-library/tsconfig.json @@ -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" + ] +} diff --git a/packages/ts-transform-emscripten-parse-tools/.gitignore b/packages/ts-transform-emscripten-parse-tools/.gitignore new file mode 100644 index 00000000..502167fa --- /dev/null +++ b/packages/ts-transform-emscripten-parse-tools/.gitignore @@ -0,0 +1 @@ +/lib diff --git a/packages/ts-transform-emscripten-parse-tools/.npmignore b/packages/ts-transform-emscripten-parse-tools/.npmignore new file mode 100644 index 00000000..a6f4a228 --- /dev/null +++ b/packages/ts-transform-emscripten-parse-tools/.npmignore @@ -0,0 +1,7 @@ +.vscode +node_modules +/src +.gitignore +.npmignore +tsconfig.json +*.map diff --git a/packages/ts-transform-emscripten-parse-tools/package.json b/packages/ts-transform-emscripten-parse-tools/package.json new file mode 100644 index 00000000..524b158f --- /dev/null +++ b/packages/ts-transform-emscripten-parse-tools/package.json @@ -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" + } +} diff --git a/packages/emnapi/transformer/src/index.ts b/packages/ts-transform-emscripten-parse-tools/src/index.ts similarity index 99% rename from packages/emnapi/transformer/src/index.ts rename to packages/ts-transform-emscripten-parse-tools/src/index.ts index 7f089c90..0dd01d3b 100644 --- a/packages/emnapi/transformer/src/index.ts +++ b/packages/ts-transform-emscripten-parse-tools/src/index.ts @@ -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 + defines?: { + MEMORY64?: 0 | 1 + } } function isEmscriptenMacro (text: string): boolean { @@ -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 } @@ -781,4 +783,4 @@ function createTransformerFactory (_program: Program, config: DefineOptions): Tr } } -export default createTransformerFactory +export { createTransformerFactory } diff --git a/packages/ts-transform-emscripten-parse-tools/tsconfig.json b/packages/ts-transform-emscripten-parse-tools/tsconfig.json new file mode 100644 index 00000000..63242fe6 --- /dev/null +++ b/packages/ts-transform-emscripten-parse-tools/tsconfig.json @@ -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" + ] +} diff --git a/packages/ts-transform-macro/.gitignore b/packages/ts-transform-macro/.gitignore new file mode 100644 index 00000000..502167fa --- /dev/null +++ b/packages/ts-transform-macro/.gitignore @@ -0,0 +1 @@ +/lib diff --git a/packages/ts-transform-macro/.npmignore b/packages/ts-transform-macro/.npmignore new file mode 100644 index 00000000..a6f4a228 --- /dev/null +++ b/packages/ts-transform-macro/.npmignore @@ -0,0 +1,7 @@ +.vscode +node_modules +/src +.gitignore +.npmignore +tsconfig.json +*.map diff --git a/packages/ts-transform-macro/README.md b/packages/ts-transform-macro/README.md new file mode 100644 index 00000000..79848051 --- /dev/null +++ b/packages/ts-transform-macro/README.md @@ -0,0 +1 @@ +# @emnapi/ts-transform-macro diff --git a/packages/ts-transform-macro/package.json b/packages/ts-transform-macro/package.json new file mode 100644 index 00000000..38332db9 --- /dev/null +++ b/packages/ts-transform-macro/package.json @@ -0,0 +1,27 @@ +{ + "name": "@emnapi/ts-transform-macro", + "version": "1.0.0", + "description": "C style define macros for TypeScript", + "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-macro#readme", + "publishConfig": { + "access": "public" + }, + "dependencies": { + "ts-clone-node": "^3.0.0" + } +} diff --git a/packages/emnapi/transformer/src/macro.ts b/packages/ts-transform-macro/src/index.ts similarity index 99% rename from packages/emnapi/transformer/src/macro.ts rename to packages/ts-transform-macro/src/index.ts index 35bf6ebb..5ef3e6b5 100644 --- a/packages/emnapi/transformer/src/macro.ts +++ b/packages/ts-transform-macro/src/index.ts @@ -23,7 +23,7 @@ import type { Declaration } from 'typescript' -import * as ts from 'typescript' +import ts = require('typescript') import { cloneNode } from 'ts-clone-node' function preserveMultiLine (node: Node, sourceNode: Node): Node { @@ -447,4 +447,4 @@ function createTransformerFactory (program: Program, config: TransformOptions): } } -export default createTransformerFactory +export { createTransformerFactory } diff --git a/packages/ts-transform-macro/tsconfig.json b/packages/ts-transform-macro/tsconfig.json new file mode 100644 index 00000000..e9e556f3 --- /dev/null +++ b/packages/ts-transform-macro/tsconfig.json @@ -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" + ] +}