Skip to content

Commit

Permalink
feat: tree-shaking, native es6, and start of HMR work
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharygolba committed Jun 12, 2016
1 parent 82eab32 commit 7d0463a
Show file tree
Hide file tree
Showing 45 changed files with 873 additions and 331 deletions.
2 changes: 0 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"plugins": [
"syntax-flow",
"transform-es2015-modules-commonjs",
"transform-decorators-legacy",
"transform-flow-strip-types",
"transform-class-properties",
"transform-es2015-classes",
"transform-es2015-destructuring",
"transform-es2015-parameters",
"transform-es2015-spread",
Expand Down
3 changes: 1 addition & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ test/*
node_modules/*

# misc
decl/*
examples/*
src/**/decl.js
interfaces/*
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"node": true
},
"globals": {
"T": true,
"Map": true,
"Set": true,
"Promise": true,
Expand Down
6 changes: 5 additions & 1 deletion .flowconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
interfaces

[ignore]
.*/\(node_modules\|dist\|examples\|test\|docs\)/.*
.*/dist/.*
.*/docs/.*
.*/examples/.*
.*/node_modules/.*
.*/test/.*

[options]
esproposal.class_instance_fields=enable
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
dist/
/docs

# dependencies
Expand Down
14 changes: 10 additions & 4 deletions interfaces/server.js → interfaces/http.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage } from 'http';
import { http$IncomingMessage, http$ServerResponse } from 'http';

import type { Model } from '../src/packages/database';
import { Model } from '../src/packages/database';

type params = {
data?: {
Expand Down Expand Up @@ -34,8 +34,14 @@ type url = {
href: string;
};

declare module 'server' {
declare class Request mixins IncomingMessage {
declare module 'http' {
declare class IncomingMessage extends http$IncomingMessage {
params: params;
record?: Model;
url: url;
}

declare class ServerResponse extends http$ServerResponse {
params: params;
record?: Model;
url: url;
Expand Down
6 changes: 6 additions & 0 deletions interfaces/webpack.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare module 'webpack' {
declare class Compiler {}
declare function factory(config: Object): Compiler;

declare var exports: factory;
}
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
"lux": "bin/lux"
},
"scripts": {
"build": "npm run clean && npm run lint && npm run type-check && webpack",
"build": "npm run clean && npm run type-check && webpack",
"clean": "rm -rf dist",
"docs": "rm -rf docs && documentation build src -p -o docs -f html",
"docs": "rm -rf docs && documentation build src -o docs -f html",
"lint": "eslint src/**/*.js",
"start": "lux serve",
"test": "npm run build && mocha --timeout 60000 --compilers=js:babel-core/register test/helper.js test/unit/**/*.js test/integration/**/*.js",
Expand All @@ -32,39 +32,40 @@
},
"dependencies": {
"ansi-regex": "2.0.0",
"babel-eslint": "6.0.4",
"babel-loader": "6.2.4",
"bluebird": "3.4.0",
"chalk": "1.1.3",
"commander": "2.9.0",
"eslint": "2.11.0",
"eslint-loader": "1.3.0",
"inflection": "1.10.0",
"json-loader": "0.5.4",
"memory-fs": "0.3.0",
"moment": "2.13.0",
"ora": "0.2.3",
"source-map-support": "0.4.0"
"require-reload": "0.2.2",
"source-map-support": "0.4.0",
"webpack": "2.1.0-beta.7"
},
"devDependencies": {
"babel-core": "6.9.0",
"babel-eslint": "6.0.4",
"babel-loader": "6.2.4",
"babel-plugin-syntax-flow": "6.8.0",
"babel-plugin-syntax-trailing-function-commas": "6.8.0",
"babel-plugin-transform-async-to-generator": "6.8.0",
"babel-plugin-transform-class-properties": "6.9.0",
"babel-plugin-transform-decorators": "6.8.0",
"babel-plugin-transform-decorators-legacy": "1.3.4",
"babel-plugin-transform-es2015-classes": "6.9.0",
"babel-plugin-transform-es2015-destructuring": "6.9.0",
"babel-plugin-transform-es2015-modules-commonjs": "6.8.0",
"babel-plugin-transform-es2015-parameters": "6.9.0",
"babel-plugin-transform-es2015-spread": "6.8.0",
"babel-plugin-transform-exponentiation-operator": "6.8.0",
"babel-plugin-transform-flow-strip-types": "6.8.0",
"babel-plugin-transform-object-rest-spread": "6.8.0",
"chai": "3.5.0",
"documentation": "4.0.0-beta3",
"eslint": "2.11.0",
"flow-bin": "0.25.0",
"isomorphic-fetch": "2.2.1",
"json-loader": "0.5.4",
"mocha": "2.5.3",
"webpack": "2.1.0-beta.7"
"mocha": "2.5.3"
}
}
120 changes: 113 additions & 7 deletions src/packages/application/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import Promise from 'bluebird';
import cluster from 'cluster';
import { pluralize, singularize } from 'inflection';
import requireReload from 'require-reload';
import { basename, join as joinPath } from 'path';
import { dasherize, pluralize, singularize } from 'inflection';

import Server from '../server';
import Router from '../router';
import Database from '../database';

import loader from '../loader';

import bound from '../../decorators/bound';

import tryCatch from '../../utils/try-catch';
import underscore from '../../utils/underscore';

import {
ControllerMissingError,
SerializerMissingError
} from './errors';

const { defineProperties } = Object;

const { assign, defineProperties } = Object;
const { env: { PWD, PORT } } = process;
const { isMaster } = cluster;
const reload = requireReload(external);

class Application {
path;
Expand All @@ -26,8 +33,14 @@ class Application {
logger;
router;

models: Map<string, Object> = new Map();

controllers: Map<string, Object> = new Map();

serializers: Map<string, Object> = new Map();

constructor({
path = PWD,
appPath = PWD,
port = PORT || 4000,
domain = 'http://localhost',
logger
Expand All @@ -40,14 +53,14 @@ class Application {
});

const store = new Database({
path,
path: appPath,
logger,
config: external(`${path}/config/database`).default
config: external(joinPath(appPath, 'dist', 'config', 'database')).default
});

defineProperties(this, {
path: {
value: path,
value: appPath,
writable: false,
enumerable: true,
configurable: false
Expand Down Expand Up @@ -96,6 +109,8 @@ class Application {
}
});

process.on('update', this.refresh);

return this;
}

Expand Down Expand Up @@ -183,8 +198,99 @@ class Application {
}
});

assign(this, {
models,
controllers,
serializers
});

return this;
}

@bound
refresh(changed: Array<{}> = []): void {
tryCatch(async () => {
const types = /(models|controllers|serializers)/g;
let controllerDidChange = false;

const {
store,
domain,
router,
models,
controllers,
serializers,
path: appPath
} = this;

changed
.map(({ name: path }: { name: string }) => {
const [type] = path.match(types) || [];

return {
path,
type: type ? singularize(type) : null
};
})
.filter(({ type }: { type: ?string }) => Boolean(type))
.forEach(({ path, type }: { path: string, type: string }) => {
let mod = reload(joinPath(appPath, 'dist', path));
const key = dasherize(underscore(basename(path)));

switch (type) {
case 'model':
break;

case 'controller':
const match = controllers.get(key);

controllerDidChange = true;

if (match) {
const {
model,
serializer,
parentController
} = match;

mod = new mod({
store,
model,
domain,
serializer,
serializers,
parentController
});

controllers.set(key, mod);

if (key === 'application') {
controllers.forEach(controller => {
if (controller !== mod) {
controller.parentController = mod;
}
});
}
}

break;

case 'serializers':
break;
}
});

if (controllerDidChange) {
const routes = await loader(appPath, 'routes');

routes.get('routes').call(null, router.route, router.resource);
}
}, ({ message }: { message: string }) => {
if (message) {
this.logger.error(message);
}
});
}
}

export default Application;
9 changes: 7 additions & 2 deletions src/packages/cli/commands/serve.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os from 'os';
import path from 'path';
import cluster from 'cluster';

import { cyan } from 'chalk';
Expand All @@ -13,8 +14,12 @@ const {
} = process;

export default async function serve(port = 4000) {
const Application = external(`${PWD}/bin/app`);
const config = external(`${PWD}/config/environments/${NODE_ENV}`).default;
const dist = path.join(PWD, 'dist');
const Application = external(path.join(dist, 'app', 'index')).default;

const config = external(
path.join(dist, 'config', 'environments', NODE_ENV)
).default;

const logger = await Logger.create({
enabled: config.log,
Expand Down
31 changes: 26 additions & 5 deletions src/packages/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
} from './commands';

import tryCatch from '../../utils/try-catch';
import { createCompiler, displayStats } from '../../utils/compiler';

import { version as VERSION } from '../../../package.json';

export default function CLI() {
Expand Down Expand Up @@ -63,13 +65,32 @@ export default function CLI() {
.option('-e, --environment [env]', '(Default: development)')
.option('-p, --port [port]', '(Default: 4000)')
.action(async ({ environment = 'development', port = 4000 } = {}) => {
await tryCatch(async () => {
process.env.NODE_ENV = environment;
await serve(port);
}, err => {
const rescue = (err) => {
console.error(err);
exit(1);
});
};

await tryCatch(async () => {
const compiler = await createCompiler(PWD, environment);
let isRunning = false;

compiler.watch({
poll: false,
aggregateTimeout: 300
}, async (err, stats) => {
if (err) {
return rescue(err);
}

if (!isRunning) {
process.env.NODE_ENV = environment;
await serve(port);
isRunning = true;
}

displayStats(stats);
});
}, rescue);
});

cli
Expand Down
Loading

0 comments on commit 7d0463a

Please sign in to comment.