Skip to content

perf: don't use middleware if has build #1148

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

Merged
merged 9 commits into from
Jul 9, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion test-fixtures/skeleton-app/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module.exports = function(environment) {
return {
environment,
modulePrefix: 'skeleton-app'
modulePrefix: 'skeleton-app',
rootURL: '/'
};
};
1 change: 1 addition & 0 deletions test-fixtures/skeleton-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"ember-cli-htmlbars": "*",
"ember-cli-babel": "*",
"ember-source": "*",
"ember-qunit": "*",
"loader.js": "*",
"typescript": "*"
},
Expand Down
23 changes: 23 additions & 0 deletions test-fixtures/skeleton-app/testem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

module.exports = {
test_page: 'tests/index.html?hidepassed',
disable_watching: true,
launch_in_ci: ['Chrome'],
launch_in_dev: ['Chrome'],
browser_start_timeout: 120,
browser_args: {
Chrome: {
ci: [
// --no-sandbox is needed when running Chrome inside a container
process.env.CI ? '--no-sandbox' : null,
'--headless',
'--disable-dev-shm-usage',
'--disable-software-rasterizer',
'--mute-audio',
'--remote-debugging-port=0',
'--window-size=1440,900'
].filter(Boolean)
}
}
};
33 changes: 33 additions & 0 deletions test-fixtures/skeleton-app/tests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Dummy Tests</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">

{{content-for "head"}}
{{content-for "test-head"}}

<link rel="stylesheet" href="{{rootURL}}assets/vendor.css">
<link rel="stylesheet" href="{{rootURL}}assets/skeleton-app.css">
<link rel="stylesheet" href="{{rootURL}}assets/test-support.css">

{{content-for "head-footer"}}
{{content-for "test-head-footer"}}
</head>
<body>
{{content-for "body"}}
{{content-for "test-body"}}

<script src="/testem.js" integrity=""></script>
<script src="{{rootURL}}assets/vendor.js"></script>
<script src="{{rootURL}}assets/test-support.js"></script>
<script src="{{rootURL}}assets/skeleton-app.js"></script>
<script src="{{rootURL}}assets/tests.js"></script>

{{content-for "body-footer"}}
{{content-for "test-body-footer"}}
</body>
</html>
3 changes: 3 additions & 0 deletions test-fixtures/skeleton-app/tests/test-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { start } from 'ember-qunit';

start();
21 changes: 17 additions & 4 deletions ts/addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import TypecheckMiddleware from './lib/typechecking/middleware';
import { Application } from 'express';
import walkSync from 'walk-sync';
import fs from 'fs-extra';
import logger from 'debug';

const debug = logger('ember-cli-typescript:addon');

export const ADDON_NAME = 'ember-cli-typescript';

Expand Down Expand Up @@ -45,12 +48,22 @@ export default addon({
return `${__dirname}/blueprints`;
},

serverMiddleware({ app }) {
this._addTypecheckMiddleware(app);
serverMiddleware({ app, options }) {
if (!options || !options.path) {
debug('Installing typecheck server middleware');
this._addTypecheckMiddleware(app);
} else {
debug('Skipping typecheck server middleware');
}
},

testemMiddleware(app) {
this._addTypecheckMiddleware(app);
testemMiddleware(app, options) {
if (!options || !options.path) {
debug('Installing typecheck testem middleware');
this._addTypecheckMiddleware(app);
} else {
debug('Skipping typecheck testem middleware');
}
},

async postBuild() {
Expand Down
26 changes: 26 additions & 0 deletions ts/tests/acceptance/build-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,32 @@ describe('Acceptance: build', function () {
);
});

it("doesn't launch type checking for `ember serve` when --path is used", async () => {
await app.build();

let server = app.serve({
args: ['--path', 'dist'],
env: { DEBUG: 'ember-cli-typescript:addon' },
});

let result = await server.waitForOutput('Serving on');

expect(result).to.include('ember-cli-typescript:addon Skipping typecheck server middleware');
});

it("doesn't launch type checking for `ember test` when --path is used", async () => {
await app.build({ args: ['--environment', 'test'] });

let result = await app.test({
args: ['--path', 'dist'],
env: { DEBUG: 'ember-cli-typescript:addon' },
});

expect(result.all).to.include(
'ember-cli-typescript:addon Skipping typecheck testem middleware'
);
});

it('fails the build when noEmitOnError is set and an error is emitted', async () => {
app.writeFile('app/app.ts', `import { foo } from 'nonexistent';`);

Expand Down
31 changes: 20 additions & 11 deletions ts/tests/helpers/skeleton-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ const getEmberPort = (() => {
return () => lastPort++;
})();

interface EmberCliOptions {
args?: string[];
env?: Record<string, string>;
}

export default class SkeletonApp {
port = getEmberPort();
watched: WatchedBuild | null = null;
watched: WatchedEmberProcess | null = null;
cleanupTempDir = () => rimraf(this.root, (error) => error && console.error(error));
root = path.join(process.cwd(), `test-skeleton-app-${Math.random().toString(36).slice(2)}`);

Expand All @@ -25,18 +30,22 @@ export default class SkeletonApp {
process.on('beforeExit', this.cleanupTempDir);
}

build() {
return this._ember(['build']);
build({ args = [], env }: EmberCliOptions = {}) {
return this._ember({ args: ['build', ...args], env });
}

test({ args = [], env }: EmberCliOptions = {}) {
return this._ember({ args: ['test', '--test-port', `${this.port}`, ...args], env });
}

serve() {
serve({ args = [], env }: EmberCliOptions = {}) {
if (this.watched) {
throw new Error('Already serving');
}
return (this.watched = new WatchedBuild(
this._ember(['serve', '--port', `${this.port}`]),
this.port
));

let childProcess = this._ember({ args: ['serve', '--port', `${this.port}`, ...args], env });

return (this.watched = new WatchedEmberProcess(childProcess, this.port));
}

updatePackageJSON(callback: (arg: any) => any) {
Expand Down Expand Up @@ -68,13 +77,13 @@ export default class SkeletonApp {
process.off('beforeExit', this.cleanupTempDir);
}

_ember(args: string[]) {
_ember({ args, env }: EmberCliOptions) {
let ember = require.resolve('ember-cli/bin/ember');
return execa.node(ember, args, { cwd: this.root, all: true });
return execa.node(ember, args, { cwd: this.root, all: true, env });
}
}

class WatchedBuild extends EventEmitter {
class WatchedEmberProcess extends EventEmitter {
constructor(protected ember: execa.ExecaChildProcess, protected port: number) {
super();
this.ember.stdout?.on('data', (data) => {
Expand Down
11 changes: 9 additions & 2 deletions ts/types/ember-cli/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare module 'ember-cli/lib/models/addon' {
import UI from 'console-ui';
import { Application } from 'express';
import Project from 'ember-cli/lib/models/project';
import { TaskOptions } from 'ember-cli/lib/models/task';
import Command from 'ember-cli/lib/models/command';
import EmberApp from 'ember-cli/lib/broccoli/ember-app';
import PreprocessRegistry from 'ember-cli-preprocess-registry';
Expand All @@ -36,8 +37,8 @@ declare module 'ember-cli/lib/models/addon' {
includedCommands(): Record<string, typeof Command | ExtendOptions<Command>> | void;
shouldIncludeChildAddon(addon: Addon): boolean;
isDevelopingAddon(): boolean;
serverMiddleware(options: { app: Application }): void | Promise<void>;
testemMiddleware(app: Application): void;
serverMiddleware(options: { app: Application; options?: TaskOptions }): void | Promise<void>;
testemMiddleware(app: Application, options?: TaskOptions): void;
setupPreprocessorRegistry(type: 'self' | 'parent', registry: PreprocessRegistry): void;
}
}
Expand All @@ -49,6 +50,12 @@ declare module 'ember-cli/lib/models/blueprint' {
export = Blueprint;
}

declare module 'ember-cli/lib/models/task' {
export interface TaskOptions {
path?: string;
}
}

declare module 'ember-cli/lib/models/command' {
import CoreObject from 'core-object';
import UI from 'console-ui';
Expand Down