Skip to content

Commit

Permalink
#38: Restore TypeScript support for using a non-namespaced TS jsxFactory
Browse files Browse the repository at this point in the history
# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Wed Jan 26 19:11:24 2022 -0500
#
# On branch fix-jsx-types
# Your branch and 'origin/fix-jsx-types' have diverged,
# and have 6 and 7 different commits each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
# Changes to be committed:
#	modified:   package.json
#	modified:   src/index.ts
#	modified:   src/jsxTypes.ts
#	modified:   tsconfig.json
#

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Wed Jan 26 19:11:24 2022 -0500
#
# On branch fix-jsx-types
# Your branch and 'origin/fix-jsx-types' have diverged,
# and have 6 and 7 different commits each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
# Changes to be committed:
#	modified:   package.json
#	modified:   src/index.ts
#	modified:   src/jsxTypes.ts
#	modified:   tsconfig.json
#

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Wed Jan 26 19:11:24 2022 -0500
#
# On branch fix-jsx-types
# Your branch and 'origin/fix-jsx-types' have diverged,
# and have 6 and 7 different commits each, respectively.
#   (use "git pull" to merge the remote branch into yours)
#
# Changes to be committed:
#	modified:   package.json
#	modified:   src/index.ts
#	modified:   src/jsxTypes.ts
#	modified:   tsconfig.json
#
  • Loading branch information
spiffytech committed Jan 29, 2022
1 parent f8ef312 commit ad4d121
Show file tree
Hide file tree
Showing 4 changed files with 882 additions and 851 deletions.
11 changes: 3 additions & 8 deletions package.json
Expand Up @@ -3,18 +3,13 @@
"version": "2.1.3",
"main": "./dist/index.js",
"type": "module",
"types": "./dist/index.d.ts",
"author": "Jeswin Kumar<jeswinpk@agilehead.com>",
"repository": {
"type": "git",
"url": "https://github.com/forgojs/forgo"
},
"exports": {
".": {
"import": "./dist/forgo.min.js",
"types": "./dist/index.d.ts"
}
},
"exports": "./dist/forgo.min.js",
"types": "./dist/index.d.ts",
"devDependencies": {
"@types/jsdom": "^16.2.14",
"@types/mocha": "^9.1.0",
Expand All @@ -28,7 +23,7 @@
},
"scripts": {
"clean": "rimraf ./dist",
"build": "npm run clean && mkdir -p dist && npx tsc --emitDeclarationOnly && npx esbuild src/index.ts --minify --sourcemap --target=es2015 --outfile=dist/forgo.min.js",
"build": "npm run clean && npx tsc --emitDeclarationOnly && npx esbuild ./src/index.ts --minify --bundle --format=esm --sourcemap --outfile=dist/forgo.min.js",
"build-dev": "npx tsc",
"test": "npx tsc && npx mocha dist/test/test.js"
},
Expand Down
32 changes: 29 additions & 3 deletions src/index.ts
Expand Up @@ -1754,13 +1754,39 @@ function findNodeIndex(
}

/* JSX Types */
import type { JSXTypes } from "./jsxTypes";
/*
JSX typings expect a JSX namespace to be in scope for the forgo module (ifa
using a jsxFactory like forgo.createElement), or attached to the naked factory
function (if using a jsxFactory like createElement).
See: https://www.typescriptlang.org/docs/handbook/jsx.html#intrinsic-elements
Also: https://dev.to/ferdaber/typescript-and-jsx-part-ii---what-can-create-jsx-22h6
Note that importing a module turns it into a namespace on this side of the
import, so it doesn't need to be declared as a namespace inside jsxTypes.ts.
However, attempting to declare it as such causes no end of headaches either
when trying to reexport it here, or reexport it from a createElement
namespace. Some errors arise at comple or build time, and some are only
visible when a project attempts to consume forgo.
*/
// This covers a consuming project using the forgo.createElement JSX factory
export * as JSX from "./jsxTypes.js";

// If this uses named imports, esbuild doesn't know how to erase the imports andu
// gets pset that "JSX" isn't an actual literal value inside the jsxTypes.ts
// module. We have to import as a different name than the export within
// createElement because I can't find a way to export a namespace within a
// namespace without using import aliases.
import * as JSXTypes from "./jsxTypes.js";
// The createElement namespace exists so that users can set their TypeScript
// jsxFactory o createElement instead of forgo.createElement.
export namespace createElement {
export import JSX = JSXTypes;
}

declare global {
interface ChildNode {
__forgo?: NodeAttachedState;
__forgo_deleted?: boolean;
}
}

export { JSXTypes as JSX };

0 comments on commit ad4d121

Please sign in to comment.