Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typescript support #111

Merged
merged 1 commit into from Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 12 additions & 7 deletions addon/index.js → addon/index.ts
@@ -1,5 +1,7 @@
/* global requirejs:false, require:false */
function resolveInitializer(moduleName) {
import Engine from '@ember/engine';
import require from 'require';

function resolveInitializer(moduleName: string) {
var module = require(moduleName, null, null, true);
if (!module) {
throw new Error(moduleName + ' must export an initializer.');
Expand All @@ -11,30 +13,33 @@ function resolveInitializer(moduleName) {
return initializer;
}

function registerInitializers(app, moduleNames) {
function registerInitializers(app: typeof Engine, moduleNames: string[]) {
rwjblue marked this conversation as resolved.
Show resolved Hide resolved
for (var i = 0; i < moduleNames.length; i++) {
app.initializer(resolveInitializer(moduleNames[i]));
}
}

function registerInstanceInitializers(app, moduleNames) {
function registerInstanceInitializers(app: typeof Engine, moduleNames: string[]) {
for (var i = 0; i < moduleNames.length; i++) {
app.instanceInitializer(resolveInitializer(moduleNames[i]));
}
}

function _endsWith(str, suffix) {
function _endsWith(str: string, suffix: string): boolean {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

export default function (app, prefix) {
/**
* Configure your application as it boots
*/
export default function loadInitializers(app: typeof Engine, prefix: string): void {
var initializerPrefix = prefix + '/initializers/';
var instanceInitializerPrefix = prefix + '/instance-initializers/';
var initializers = [];
var instanceInitializers = [];
// this is 2 pass because generally the first pass is the problem
// and is reduced, and resolveInitializer has potential to deopt
var moduleNames = Object.keys(requirejs._eak_seen);
var moduleNames = Object.keys(self.requirejs._eak_seen);
for (var i = 0; i < moduleNames.length; i++) {
var moduleName = moduleNames[i];
if (moduleName.lastIndexOf(initializerPrefix, 0) === 0) {
Expand Down
20 changes: 17 additions & 3 deletions package.json
Expand Up @@ -33,19 +33,32 @@
"lint:js": "eslint .",
"start": "ember serve",
"test": "ember test",
"test:all": "ember try:each"
"test:all": "ember try:each",
"prepublishOnly": "ember ts:precompile",
"postpublish": "ember ts:clean"
},
"dependencies": {
"ember-cli-babel": "^7.9.0"
"ember-cli-babel": "^7.9.0",
"ember-cli-typescript": "^2.0.0"
},
"devDependencies": {
"@ember/optional-features": "^0.7.0",
"@types/ember": "^3.0.29",
"@types/ember-qunit": "^3.4.6",
"@types/ember-resolver": "^5.0.6",
"@types/ember-test-helpers": "^1.0.5",
"@types/ember-testing-helpers": "^0.0.3",
"@types/ember__engine": "^3.0.4",
"@types/ember__test-helpers": "^0.7.8",
"@types/qunit": "^2.5.4",
"@types/rsvp": "^4.0.2",
"ember-cli": "~3.9.0",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-eslint": "^4.2.3",
"ember-cli-htmlbars": "^3.1.0",
"ember-cli-htmlbars-inline-precompile": "^2.0.0",
"ember-cli-inject-live-reload": "^2.0.1",
"ember-cli-typescript-blueprints": "^2.0.0",
"ember-disable-prototype-extensions": "^1.1.3",
"ember-maybe-import-regenerator": "^0.1.6",
"ember-qunit": "^4.5.1",
Expand All @@ -56,7 +69,8 @@
"eslint-plugin-ember": "^6.10.0",
"eslint-plugin-node": "^8.0.0",
"lerna-changelog": "^0.8.2",
"loader.js": "^4.7.0"
"loader.js": "^4.7.0",
"typescript": "^3.3.3333"
},
"ember-addon": {
"configPath": "tests/dummy/config"
Expand Down
8 changes: 8 additions & 0 deletions tests/dummy/app/app.js → tests/dummy/app/app.ts
Expand Up @@ -3,12 +3,20 @@ import Resolver from './resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

declare global {
interface Window {
fooInitializeWasCalled: any;
barInitializeWasCalled: any;
}
}

const App = Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
Resolver,
destroy() {
this._super(...arguments);

delete self.fooInitializeWasCalled;
delete self.barInitializeWasCalled;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/dummy/app/config/environment.d.ts
@@ -0,0 +1,16 @@
export default config;

/**
* Type declarations for
* import config from './config/environment'
*
* For now these need to be managed by the developer
* since different ember addons can materialize new entries.
*/
declare const config: {
environment: any;
modulePrefix: string;
podModulePrefix: string;
locationType: string;
rootURL: string;
};
File renamed without changes.
55 changes: 55 additions & 0 deletions tsconfig.json
@@ -0,0 +1,55 @@
{
"compilerOptions": {
"target": "es2017",
"allowJs": true,
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"noImplicitAny": true,
"noImplicitThis": true,
"alwaysStrict": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noEmitOnError": false,
"noEmit": true,
"inlineSourceMap": true,
"inlineSources": true,
"baseUrl": ".",
"module": "es6",
"paths": {
"dummy/tests/*": [
"tests/*"
],
"dummy/*": [
"tests/dummy/app/*",
"app/*"
],
"ember-load-initializers": [
"addon"
],
"ember-load-initializers/*": [
"addon/*"
],
"ember-load-initializers/test-support": [
"addon-test-support"
],
"ember-load-initializers/test-support/*": [
"addon-test-support/*"
],
"*": [
"types/*"
]
}
},
"include": [
"app/**/*",
"addon/**/*",
"tests/**/*",
"types/**/*",
"test-support/**/*",
"addon-test-support/**/*"
]
}
1 change: 1 addition & 0 deletions types/dummy/index.d.ts
@@ -0,0 +1 @@

12 changes: 12 additions & 0 deletions types/loader.js/index.d.ts
@@ -0,0 +1,12 @@
export {}

declare global {
interface Window {

requirejs: {
_eak_seen: {
[s: string]: any
}
}
}
}
5 changes: 5 additions & 0 deletions types/loader.js/require.d.ts
@@ -0,0 +1,5 @@
declare module 'require' {
function require(moduleName: string, ...args: any[]): any;

export default require;
}