Skip to content

Commit

Permalink
feat: Add module-import.js and node-ambient.js to allow importing the…
Browse files Browse the repository at this point in the history
…re in the codebase with a bundler
  • Loading branch information
JumpLink committed Feb 17, 2024
1 parent 225583b commit 7208309
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 13 deletions.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
83 changes: 79 additions & 4 deletions packages/generator-typescript/src/type-definition-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,37 @@ export class TypeDefinitionGenerator implements Generator {
}
}

protected async exportModuleAmbientJS(
moduleTemplateProcessor: TemplateProcessor,
girModule: GirModule,
): Promise<void> {
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,
Expand Down Expand Up @@ -1294,6 +1325,37 @@ export class TypeDefinitionGenerator implements Generator {
}
}

protected async exportModuleImportJS(
moduleTemplateProcessor: TemplateProcessor,
girModule: GirModule,
): Promise<void> {
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<void> {
const template = 'module.d.ts'
let explicitTemplate = `${girModule.importName}.d.ts`
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {}
2 changes: 2 additions & 0 deletions packages/generator-typescript/templates/gjs/module-import.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const gi = globalThis.imports?.gi || {};
export default gi;
5 changes: 5 additions & 0 deletions packages/generator-typescript/templates/node-gtk/ambient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if(moduleType === 'esm'){ %>
export {};
<% } else { %>
module.exports = {};
<% } %>
10 changes: 10 additions & 0 deletions packages/generator-typescript/templates/node-gtk/module-import.js
Original file line number Diff line number Diff line change
@@ -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;
<% } %>
32 changes: 25 additions & 7 deletions packages/generator-typescript/templates/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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') { _%>
Expand Down
2 changes: 1 addition & 1 deletion types
Submodule types updated 4201 files

0 comments on commit 7208309

Please sign in to comment.