Skip to content

Commit 84a9697

Browse files
Merge pull request #1014 from mainmatter/typescript
chore(typescript): initial Typescript setup
2 parents 7df74b6 + 4dc0a83 commit 84a9697

File tree

28 files changed

+725
-499
lines changed

28 files changed

+725
-499
lines changed

packages/ember-cookies/.eslintignore

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/ember-cookies/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# compiled output
44
/dist
5+
/declarations/
56

67
# dependencies
78
/node_modules
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
'use strict';
2+
13
const { addonV1Shim } = require('@embroider/addon-shim');
24
module.exports = addonV1Shim(__dirname);

packages/ember-cookies/babel.config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{
22
"plugins": [
3+
[
4+
"@babel/plugin-transform-typescript",
5+
{ "allExtensions": true, "onlyRemoveTypeImports": true }
6+
],
37
"@embroider/addon-dev/template-colocation-plugin",
48
["@babel/plugin-proposal-decorators", { "legacy": true }],
59
"@babel/plugin-proposal-class-properties"

packages/ember-cookies/eslint.config.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ember from 'eslint-plugin-ember';
22
import prettier from 'eslint-plugin-prettier/recommended';
33
import globals from 'globals';
4-
import babelParser from '@babel/eslint-parser';
4+
import typescriptParser from '@typescript-eslint/parser';
55
import n from 'eslint-plugin-n';
66
import js from '@eslint/js';
77

@@ -15,6 +15,7 @@ export default [
1515
'vendor/',
1616
'dist/',
1717
'tmp/',
18+
'declarations/',
1819
'bower_components/',
1920
'node_modules/',
2021
'coverage/',
@@ -28,13 +29,14 @@ export default [
2829
plugins: {
2930
ember,
3031
},
32+
files: ['**/*.{ts,js}'],
3133

3234
languageOptions: {
3335
globals: {
3436
...globals.browser,
3537
},
3638

37-
parser: babelParser,
39+
parser: typescriptParser,
3840
ecmaVersion: 2020,
3941
sourceType: 'module',
4042

@@ -53,7 +55,7 @@ export default [
5355
'**/.eslintrc.js',
5456
'**/.eslintrc.js',
5557
'**/.prettierrc.js',
56-
'**/addon-main.js',
58+
'**/addon-main.cjs',
5759
'blueprints/*/index.js',
5860
'config/**/*.js',
5961
],

packages/ember-cookies/index.d.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

packages/ember-cookies/package.json

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@
88
"repository": "https://github.com/simplabs/ember-cookies",
99
"license": "MIT",
1010
"author": "",
11-
"types": "./index.d.ts",
1211
"exports": {
1312
".": {
14-
"types": "./index.d.ts",
13+
"types": "./declarations/index.d.ts",
1514
"default": "./dist/index.js"
1615
},
1716
"./*": {
18-
"types": "./index.d.ts",
19-
"default": "./dist/*"
17+
"types": "./declarations/*.d.ts",
18+
"default": "./dist/*.js"
2019
},
2120
"./test-support": {
22-
"types": "./index.d.ts",
21+
"types": "./declarations/*.d.ts",
2322
"default": "./dist/test-support/index.js"
2423
},
25-
"./addon-main.js": "./addon-main.js"
24+
"./addon-main.js": "./addon-main.cjs"
2625
},
2726
"typesVersions": {
2827
"*": {
@@ -36,16 +35,17 @@
3635
"test": "tests"
3736
},
3837
"files": [
39-
"addon-main.js",
40-
"dist",
41-
"index.d.ts"
38+
"addon-main.cjs",
39+
"declarations",
40+
"dist"
4241
],
4342
"scripts": {
4443
"build": "rollup --config",
4544
"copyDoc": "cp ../../*.md .",
46-
"lint": "concurrently \"pnpm lint:js\"",
45+
"lint": "concurrently \"pnpm lint:js\" \"pnpm lint:types\"",
4746
"lint:js": "eslint . --cache",
4847
"lint:js:fix": "eslint . --fix",
48+
"lint:types": "glint",
4949
"prepublishOnly": "concurrently \"pnpm copyDoc\" \"pnpm build\"",
5050
"start": "rollup --config --watch",
5151
"test": "echo 'A v2 addon does not have tests, run tests in test-app'",
@@ -56,14 +56,21 @@
5656
},
5757
"devDependencies": {
5858
"@babel/core": "7.25.8",
59-
"@babel/eslint-parser": "7.25.9",
6059
"@babel/plugin-proposal-class-properties": "7.18.6",
6160
"@babel/plugin-proposal-decorators": "7.25.9",
61+
"@babel/plugin-transform-typescript": "^7.26.3",
6262
"@embroider/addon-dev": "7.1.1",
6363
"@eslint/eslintrc": "3.1.0",
6464
"@eslint/js": "9.12.0",
65+
"@glint/core": "^1.5.0",
66+
"@glint/environment-ember-loose": "^1.5.0",
67+
"@glint/environment-ember-template-imports": "^1.5.0",
6568
"@rollup/plugin-babel": "6.0.4",
69+
"@tsconfig/ember": "^3.0.8",
6670
"@types/ember": "4.0.11",
71+
"@types/ember__utils": "^4.0.7",
72+
"@typescript-eslint/eslint-plugin": "^8.18.1",
73+
"@typescript-eslint/parser": "^8.18.1",
6774
"concurrently": "9.0.1",
6875
"eslint": "9.12.0",
6976
"eslint-config-prettier": "9.1.0",
@@ -72,7 +79,8 @@
7279
"eslint-plugin-prettier": "5.2.1",
7380
"globals": "15.11.0",
7481
"prettier": "3.3.3",
75-
"rollup": "4.29.1"
82+
"rollup": "4.29.1",
83+
"typescript": "^5.7.2"
7684
},
7785
"engines": {
7886
"node": ">= 16.*"
@@ -81,7 +89,7 @@
8189
"edition": "octane"
8290
},
8391
"ember-addon": {
84-
"main": "addon-main.js",
92+
"main": "addon-main.cjs",
8593
"type": "addon",
8694
"version": 2,
8795
"versionCompatibility": {

packages/ember-cookies/rollup.config.js renamed to packages/ember-cookies/rollup.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default {
1515
// These are the modules that users should be able to import from your
1616
// addon. Anything not listed here may get optimized away.
1717
addon.publicEntrypoints([
18+
'**/*.js',
1819
'services/**/*.js',
1920
'test-support/**/*.js',
2021
'utils/**/*.js',
@@ -33,6 +34,7 @@ export default {
3334
// By default, this will load the actual babel config from the file
3435
// babel.config.json.
3536
babel({
37+
extensions: ['.js', '.ts'],
3638
babelHelpers: 'bundled',
3739
}),
3840

@@ -44,6 +46,9 @@ export default {
4446
// Ensure that standalone .hbs files are properly integrated as Javascript.
4547
addon.hbs(),
4648

49+
// Emit .d.ts declarations
50+
addon.declarations('declarations'),
51+
4752
// addons are allowed to contain imports of .css files, which we want rollup
4853
// to leave alone and keep in the published output.
4954
addon.keepAssets(['**/*.css']),

packages/ember-cookies/src/services/cookies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { get } from '@ember/object';
33
import { assert } from '@ember/debug';
44
import { getOwner } from '@ember/application';
55
import Service from '@ember/service';
6-
import { serializeCookie } from '../utils/serialize-cookie';
6+
import { serializeCookie } from '../utils/serialize-cookie.ts';
77
const { keys } = Object;
88
const DEFAULTS = { raw: false };
99
const MAX_COOKIE_BYTE_LENGTH = 4096;

packages/ember-cookies/src/test-support/clear-all-cookies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert } from '@ember/debug';
22
import { isEmpty } from '@ember/utils';
3-
import { serializeCookie } from '../utils/serialize-cookie';
3+
import { serializeCookie } from '../utils/serialize-cookie.ts';
44

55
export default function clearAllCookies(options = {}) {
66
assert('Cookies cannot be set to be HTTP-only from a browser!', !options.httpOnly);

packages/ember-cookies/src/utils/serialize-cookie.js renamed to packages/ember-cookies/src/utils/serialize-cookie.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { typeOf, isEmpty } from '@ember/utils';
22

3-
export const serializeCookie = (name, value, options = {}) => {
3+
interface Options {
4+
maxAge?: number | string;
5+
domain?: string;
6+
expires?: Date;
7+
secure?: boolean;
8+
httpOnly?: boolean;
9+
path?: string;
10+
sameSite?: string;
11+
partitioned?: boolean;
12+
}
13+
14+
export const serializeCookie = (name: string, value: string, options: Options = {}) => {
415
let cookie = `${name}=${value}`;
516

617
if (!isEmpty(options.domain)) {
718
cookie = `${cookie}; domain=${options.domain}`;
819
}
9-
if (typeOf(options.expires) === 'date') {
20+
if (options.expires && typeOf(options.expires) === 'date') {
1021
cookie = `${cookie}; expires=${options.expires.toUTCString()}`;
1122
}
1223
if (!isEmpty(options.maxAge)) {

packages/ember-cookies/tsconfig.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
{
2+
"extends": "@tsconfig/ember/tsconfig.json",
3+
"include": [
4+
"src/**/*",
5+
"unpublished-development-types/**/*"
6+
],
7+
"glint": {
8+
"environment": ["ember-loose", "ember-template-imports"]
9+
},
10+
"compilerOptions": {
11+
"allowJs": true,
12+
"declarationDir": "declarations",
13+
/**
14+
https://www.typescriptlang.org/tsconfig#noEmit
15+
16+
We want to emit declarations, so this option must be set to `false`.
17+
@tsconfig/ember sets this to `true`, which is incompatible with our need to set `emitDeclarationOnly`.
18+
@tsconfig/ember is more optimized for apps, which wouldn't emit anything, only type check.
19+
*/
20+
"noEmit": false,
21+
/**
22+
https://www.typescriptlang.org/tsconfig#emitDeclarationOnly
23+
We want to only emit declarations as we use Rollup to emit JavaScript.
24+
*/
25+
"emitDeclarationOnly": true,
26+
27+
/**
28+
https://www.typescriptlang.org/tsconfig#noEmitOnError
29+
Do not block emit on TS errors.
30+
*/
31+
"noEmitOnError": false,
32+
33+
/**
34+
https://www.typescriptlang.org/tsconfig#rootDir
35+
"Default: The longest common path of all non-declaration input files."
36+
37+
Because we want our declarations' structure to match our rollup output,
38+
we need this "rootDir" to match the "srcDir" in the rollup.config.mjs.
39+
40+
This way, we can have simpler `package.json#exports` that matches
41+
imports to files on disk
42+
*/
43+
"rootDir": "./src",
44+
45+
/**
46+
https://www.typescriptlang.org/tsconfig#allowImportingTsExtensions
47+
48+
We want our tooling to know how to resolve our custom files so the appropriate plugins
49+
can do the proper transformations on those files.
50+
*/
51+
"allowImportingTsExtensions": true,
52+
53+
"types": [
54+
"ember-source/types"
55+
]
56+
}
57+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import '@types/ember__utils';

packages/ember-cookies/vendor/.gitkeep

Whitespace-only changes.

packages/test-app/.eslintignore

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/test-app/app/components/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)