diff --git a/NEWS.md b/NEWS.md index 5daf3233e..86e3a5476 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,7 +6,8 @@ # 3.2.8 - Upgrade dependencies - Update examples and removed deprecated function calls like `byteArray.toString()` -- Add `dom.js` and `ambient.js` to allow importing `dom.d.ts` and `ambient.d.ts` in the codebase with a bundler +- Add `dom.js`, `ambient.js` and `node-ambient.js` to allow importing there type definitions in the codebase with a bundler +- Update TSDoc documentation for `system` - node-gtk: Add new `gtk-4-application` example - node-gtk: Uses snake_case for property names at constructor, fixes #131 diff --git a/packages/generator-typescript/src/type-definition-generator.ts b/packages/generator-typescript/src/type-definition-generator.ts index 8ac909e27..e80d0fdd9 100644 --- a/packages/generator-typescript/src/type-definition-generator.ts +++ b/packages/generator-typescript/src/type-definition-generator.ts @@ -1263,6 +1263,37 @@ export class TypeDefinitionGenerator implements Generator { } } + protected async exportModuleAmbientJS( + moduleTemplateProcessor: TemplateProcessor, + girModule: GirModule, + ): Promise { + const template = 'module-ambient.js' + let target = `${girModule.importName}-ambient.js` + + if (this.overrideConfig.moduleType) { + if (this.overrideConfig.moduleType === 'cjs') { + target = `${girModule.importName}-ambient.cjs` + } else { + target = `${girModule.importName}-ambient.mjs` + } + } + + if (this.config.outdir) { + await moduleTemplateProcessor.create( + template, + this.config.outdir, + target, + undefined, + undefined, + undefined, + this.config, + ) + } else { + const { append, prepend } = await moduleTemplateProcessor.load(template, {}, this.config) + this.log.log(append + prepend) + } + } + protected async exportModuleImportTS( moduleTemplateProcessor: TemplateProcessor, girModule: GirModule, @@ -1294,6 +1325,37 @@ export class TypeDefinitionGenerator implements Generator { } } + protected async exportModuleImportJS( + moduleTemplateProcessor: TemplateProcessor, + girModule: GirModule, + ): Promise { + const template = 'module-import.js' + let target = `${girModule.importName}-import.js` + + if (this.overrideConfig.moduleType) { + if (this.overrideConfig.moduleType === 'cjs') { + target = `${girModule.importName}-import.cjs` + } else { + target = `${girModule.importName}-import.mjs` + } + } + + if (this.config.outdir) { + await moduleTemplateProcessor.create( + template, + this.config.outdir, + target, + undefined, + undefined, + undefined, + this.config, + ) + } else { + const { append, prepend } = await moduleTemplateProcessor.load(template, {}, this.config) + this.log.log(append + prepend) + } + } + protected async exportModuleTS(moduleTemplateProcessor: TemplateProcessor, girModule: GirModule): Promise { const template = 'module.d.ts' let explicitTemplate = `${girModule.importName}.d.ts` @@ -1612,14 +1674,20 @@ export class TypeDefinitionGenerator implements Generator { ) await this.exportModuleTS(moduleTemplateProcessor, girModule) + if (this.config.buildType === 'lib') { + await this.exportModuleJS(moduleTemplateProcessor, girModule) + } if (this.config.environment === 'gjs') { await this.exportModuleAmbientTS(moduleTemplateProcessor, girModule) + + if (this.config.buildType === 'lib') { + await this.exportModuleAmbientJS(moduleTemplateProcessor, girModule) + } } await this.exportModuleImportTS(moduleTemplateProcessor, girModule) - if (this.config.buildType === 'lib') { - await this.exportModuleJS(moduleTemplateProcessor, girModule) + await this.exportModuleImportJS(moduleTemplateProcessor, girModule) } if (this.config.package) { @@ -1768,11 +1836,15 @@ export class TypeDefinitionGenerator implements Generator { // Import ambient types await templateProcessor.create('ambient.d.ts', this.config.outdir, 'ambient.d.ts') - await templateProcessor.create('ambient.js', this.config.outdir, 'ambient.js') + if (this.config.buildType === 'lib') { + await templateProcessor.create('ambient.js', this.config.outdir, 'ambient.js') + } // DOM types await templateProcessor.create('dom.d.ts', this.config.outdir, 'dom.d.ts') - await templateProcessor.create('dom.js', this.config.outdir, 'dom.js') + if (this.config.buildType === 'lib') { + await templateProcessor.create('dom.js', this.config.outdir, 'dom.js') + } // Import ambient path alias if (this.config.generateAlias) { @@ -1844,6 +1916,9 @@ export class TypeDefinitionGenerator implements Generator { // Import ambient types await templateProcessor.create('ambient.d.ts', this.config.outdir, 'node-ambient.d.ts') + if (this.config.buildType === 'lib') { + await templateProcessor.create('ambient.js', this.config.outdir, 'node-ambient.js') + } // Package if (this.config.package) { diff --git a/packages/generator-typescript/templates/gjs/module-ambient.js b/packages/generator-typescript/templates/gjs/module-ambient.js new file mode 100644 index 000000000..693da49fc --- /dev/null +++ b/packages/generator-typescript/templates/gjs/module-ambient.js @@ -0,0 +1 @@ +export {} \ No newline at end of file diff --git a/packages/generator-typescript/templates/gjs/module-import.js b/packages/generator-typescript/templates/gjs/module-import.js new file mode 100644 index 000000000..b2bf6c686 --- /dev/null +++ b/packages/generator-typescript/templates/gjs/module-import.js @@ -0,0 +1,2 @@ +const gi = globalThis.imports?.gi || {}; +export default gi; \ No newline at end of file diff --git a/packages/generator-typescript/templates/node-gtk/ambient.js b/packages/generator-typescript/templates/node-gtk/ambient.js new file mode 100644 index 000000000..4351cbe54 --- /dev/null +++ b/packages/generator-typescript/templates/node-gtk/ambient.js @@ -0,0 +1,5 @@ +<% if(moduleType === 'esm'){ %> + export {}; +<% } else { %> + module.exports = {}; +<% } %> \ No newline at end of file diff --git a/packages/generator-typescript/templates/node-gtk/module-import.js b/packages/generator-typescript/templates/node-gtk/module-import.js new file mode 100644 index 000000000..aae833c57 --- /dev/null +++ b/packages/generator-typescript/templates/node-gtk/module-import.js @@ -0,0 +1,10 @@ +const gi = globalThis.imports?.gi || {}; +export default gi; + +<% if(moduleType === 'esm'){ %> + import * as gi from 'node-gtk'; + export default gi; +<% } else { %> + const gi = require('node-gtk'); + module.exports = gi; +<% } %> \ No newline at end of file diff --git a/packages/generator-typescript/templates/package.json b/packages/generator-typescript/templates/package.json index 692d54156..35ae6a95b 100644 --- a/packages/generator-typescript/templates/package.json +++ b/packages/generator-typescript/templates/package.json @@ -29,8 +29,11 @@ _%> <%_ if (packageName === 'Gjs' && generateAlias) { _%> "./tsconfig.alias.json": "./tsconfig.alias.json", <%_ } _%> - <%_ if (packageName === 'Gjs') { _%> - "./ambient": "./ambient.d.ts", + <%_ if (packageName === 'Gjs') { _%> + "./ambient": { + "types": "./ambient.d.ts", + "default": "./ambient.js" + }, "./gettext": { <%_ if (moduleType === 'esm') { _%> "import": { @@ -96,13 +99,28 @@ _%> }, <%_ } _%> <%_ if (entryPointName === 'gjs') { _%> - "./ambient": "./ambient.d.ts", - "./dom": "./dom.d.ts", + "./ambient": { + "types": "./ambient.d.ts", + "default": "./ambient.js" + }, + "./dom": { + "types": "./dom.d.ts", + "default": "./dom.js" + }, <%_ } else if (entryPointName === 'node-gtk') { _%> - "./ambient": "./node-ambient.d.ts", + "./ambient": { + "types": "./node-ambient.d.ts", + "default": "./node-ambient.js" + }, <%_ } else {_%> - "./ambient": "./<%- entryPointName %>-ambient.d.ts", - "./import": "./<%- entryPointName %>-import.d.ts", + "./ambient": { + "types": "./<%- entryPointName %>-ambient.d.ts", + "default": "./<%- entryPointName %>-ambient.js" + }, + "./import": { + "types": "./<%- entryPointName %>-import.d.ts", + "default": "./<%- entryPointName %>-import.js" + }, <%_ } _%> ".": { <%_ if (moduleType === 'esm') { _%> diff --git a/types b/types index 0037de12d..9104102dc 160000 --- a/types +++ b/types @@ -1 +1 @@ -Subproject commit 0037de12df188dacedef4c006617ea21a966df34 +Subproject commit 9104102dcdfc002d86b4ffaac69ff02ed778f503