Skip to content

Commit

Permalink
Prove out basic stable type generation
Browse files Browse the repository at this point in the history
This isn't remotely ready for primetime, but it does prove out the
basic viability of @chadhietala's suggestion that we may not need to
fix all the circularities before we can publish. We in fact can just
publish as is: none of the circularities are *hard* blockers, at least
for *this* part of the effort (though they will be for getting docs
published with these as the source of truth).

The things we will need to do to be able to execute the rest of the way
on this, given our goals:

- Fix a couple cases where we are using private names in public types,
  specifically around the `OWNER` from `@glimmer/owner`.

- Put the hacky `generate-tsconfigs.mjs` script somewhere besides the
  root, expand its capabilities to include wrapping generated modules
  in a `declare module` statement and running Prettier on the result,
  and rename it accordingly.

- Land @wagenet's in-progress PR (#20175) to convert the `ember`
  package to TS so we can publish types for it, which will unblock some
  of the other Ember packages as well.

- Properly exclude the parts of Ember's APIs we *don't* want to be
  publishing (all the purely-internal and private stuff).
  • Loading branch information
chriskrycho committed Oct 26, 2022
1 parent d61d713 commit 9812772
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ npm-debug.log
*.log
/.vscode

# These are automatically generated by our build process.
# TODO: make that *fully* true: The root types/stable directory is *not*
# automatically generated yet, and accordingly we have explicitly committed a
# couple of the files. Once it is, we can switch this over to just ignoring
# `types/stable` entirely.
types/stable
49 changes: 49 additions & 0 deletions generate-tsconfigs.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env node
// @ts-check

import fs from 'node:fs';
import { join } from 'node:path';
import { cwd } from 'node:process';

const TYPES_DIR = join(cwd(), 'types', 'stable');

fs.writeFileSync(
join(TYPES_DIR, 'tsconfig.json'),
`{
"extends": "@tsconfig/ember/tsconfig.json",
"compilerOptions": {
"baseUrl": "."
}
}
`
);

fs.writeFileSync(
join(TYPES_DIR, 'ember', 'tsconfig.json'),
`{
"extends": "../tsconfig.json"
}
`
);

const NAMESPACE_CONFIG = `{
"extends": "../../tsconfig.json"
}
`;

[join(TYPES_DIR, '@ember'), join(TYPES_DIR, '@glimmer')]
.flatMap((dirName) =>
fs.readdirSync(dirName, { withFileTypes: true }).map((dirent) => ({ dirName, dirent }))
)
.filter(({ dirent }) => dirent.isDirectory())
.forEach(({ dirName, dirent: { name } }) => {
let fileName = join(dirName, name, 'tsconfig.json');
// eslint-disable-next-line no-console, no-undef
console.log(`printing to ${fileName}`);
try {
fs.writeFileSync(fileName, NAMESPACE_CONFIG);
} catch (e) {
// eslint-disable-next-line no-console, no-undef
console.error(`error writing to ${fileName}: ${e}`);
}
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"scripts": {
"build": "ember build --environment production",
"docs": "ember ember-cli-yuidoc",
"types": "tsc --project tsconfig.types.json && node generate-tsconfigs.mjs",
"link:glimmer": "node bin/yarn-link-glimmer.js",
"start": "ember serve",
"lint": "npm-run-all --continue-on-error --aggregate-output --parallel \"lint:!(fix)\"",
Expand Down
6 changes: 6 additions & 0 deletions packages/@ember/object/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ import { _WeakSet as WeakSet } from '@glimmer/util';
import { destroy, isDestroying, isDestroyed, registerDestructor } from '@glimmer/destroyable';
import { OWNER } from '@glimmer/owner';

export {
/** @internal */
OWNER,
};

type EmberClassConstructor<T> = new (owner?: Owner) => T;

type MergeArray<Arr extends any[]> = Arr extends [infer T, ...infer Rest]
Expand Down Expand Up @@ -235,6 +240,7 @@ interface CoreObject {
_super(...args: any[]): any;
}
class CoreObject {
/** @internal */
[OWNER]?: Owner;

constructor(owner?: Owner) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
// Compilation Configuration
"target": "es2017",
"target": "es2019",
"sourceMap": true,
"outDir": "dist",
"baseUrl": "packages",
Expand Down
28 changes: 28 additions & 0 deletions tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"extends": "./tsconfig.json",
"compilerOptions": {
"noEmit": false,
"declaration": true,
"emitDeclarationOnly": true,
"declarationMap": true,
"declarationDir": "types/stable"
},
"include": [
// Note: these will also pull on all their transitive dependencies, so we
// will end up publishing the (private!) types for packages not named here
// until we update the actual internals to avoid referencing them!
"packages/@ember/**/*",
"packages/ember/**/*",
"packages/@glimmer/**/*"
],
"exclude": [
"dist",
"node_modules",
"tmp",
"types",
"packages/ember-template-compiler",
"packages/ember-testing",
"packages/internal-test-helpers"
]
}

0 comments on commit 9812772

Please sign in to comment.