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

feat(maleo playground): add default server and client #29

Merged
merged 1 commit into from
Feb 14, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example/playground/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';

import { Document, Header, Main, Scripts } from '@airy/maleo/document';
import { default as Document, Header, Main, Scripts } from '@airy/maleo/document';
import { ReduxScript } from '@airy/maleo-redux-plugin';

export class MyDocument extends Document {
export default class MyDocument extends Document {
static getInitialProps = async (ctx) => {
const initialProps = await Document.getInitialProps(ctx);

Expand Down
6 changes: 3 additions & 3 deletions example/playground/_wrap.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';

import { _Wrap } from '@airy/maleo/wrap';
import Wrap from '@airy/maleo/wrap';
import pageWithStyles from '@airy/maleo-css-plugin/lib/pageWithStyles';
import { withRedux } from '@airy/maleo-redux-plugin';

import { makeStoreClient } from './store';

@pageWithStyles
@withRedux(makeStoreClient)
export class Wrap extends _Wrap {
export default class extends Wrap {
static getInitialProps = ({ store }) => {
console.log('\nGIP wrap store', store);
};
Expand All @@ -25,4 +25,4 @@ export class Wrap extends _Wrap {
</div>
);
}
}
}
6 changes: 0 additions & 6 deletions example/playground/client.ts

This file was deleted.

9 changes: 5 additions & 4 deletions example/playground/routes.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import Dynamic from '@airy/maleo/lib/utils/dynamicImport';
import Dynamic from '@airy/maleo/dynamic';
import { registerRoutes } from '@airy/maleo/routes';

import { RoomsMainApp } from './src/MainApp';

Expand All @@ -24,9 +25,8 @@ const RoomsDetail = Dynamic({
// import { RoomsDetail } from 'src/Detail';
// import { RoomsSearch } from 'src/Search';

export const routes = [
const routes = [
{
path: '/',
component: RoomsMainApp,
key: 'rootWrapper',
routes: [
Expand All @@ -48,7 +48,6 @@ export const routes = [
exact: true,
},
],
// exact: true,
},
{
path: '/detail',
Expand All @@ -64,3 +63,5 @@ export const routes = [
],
},
];

export default routes;
24 changes: 0 additions & 24 deletions example/playground/server.ts

This file was deleted.

1 change: 0 additions & 1 deletion example/playground/src/MainApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class RoomsMainApp extends React.Component<any, any> {
};

render() {
console.log('Data', this.props.data);
return (
<div>
rooms main app
Expand Down
72 changes: 42 additions & 30 deletions packages/Maleo.js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Readme below is the documentation for the `canary` (prerelease) branch. To view
- [Custom Document](#custom-document)
- [Custom Wrap](#custom-wrap)
- [Custom Configuration](#custom-configuration)
- [Customize Server](#customize-server)
- [Customize Webpack](#customize-webpack)
- [Customize Babel Config](#customize-babel-config)
- [CDN Support](#cdn-support)
Expand Down Expand Up @@ -75,36 +76,6 @@ export default [
];
```

And then create a `server.js` file

Here you can customize Maleo's server.
```js
import { Server } from '@airy/maleo/server';
import path from 'path';

import routeConfig from './routes';

const PORT = process.env.PORT || 8080;

const maleoServer = Server.init({
port: PORT,
routes: routeConfig,
});

maleoServer.run(() => {
console.log('Server running on port :', PORT);
});
```

And lastly create a `client.js` file
```js
import { init } from '@airy/maleo/client';

import routes from './routes';

init(routes, module);
```

After that you can now run `$ npm run dev` and go to `http://localhost:3000`.

You should now see your app running on your browser.
Expand Down Expand Up @@ -525,6 +496,47 @@ Here are the API's for the configuration:
</tr>
</table>

#### Customize Server

Create a `server.js` file on root directory where your `package.json` lives.
Here you can customize Maleo's server.
```js
import { Server } from '@airy/maleo/server';
import path from 'path';

import routeConfig from './routes';

const PORT = process.env.PORT || 8080;

const maleoServer = Server.init({
port: PORT,
});

maleoServer.run(() => {
console.log('Server running on port :', PORT);
});
```

Here are the API's for the configuration:

<table>
<tr>
<td>Key</td>
<td>Type</td>
<td>Description</td>
</tr>
<tr>
<td><code>port</code></td>
<td><code>Number?</code> [<code>3000</code>]</td>
<td>Port to run Maleo server</td>
</tr>
<tr>
<td><code>assetDir</code></td>
<td><code>String?</code> [<code>'<root>/.maleo/client'</code>]</td>
<td>Directory for all client related assets</td>
</tr>
</table>

#### Customize Webpack

You are able to extend Maleo.js' default webpack configuration by defining a function on `maleo.config.js`
Expand Down
8 changes: 7 additions & 1 deletion packages/Maleo.js/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ const aliases = {};

Object.keys(paths).forEach((item) => {
const key = item.replace('/*', '');
const path = paths[item][0].replace(/src\/(.+)\/\*/, './lib/$1/');

let path = '';
if (paths[item][0] === './*') {
path = './';
} else {
path = paths[item][0].replace(/src\/(.+)\/\*/, './lib/$1/');
}

aliases[key] = path;
});
Expand Down
2 changes: 1 addition & 1 deletion packages/Maleo.js/client.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/client/Client');
module.exports = require('./lib/client/client');
2 changes: 2 additions & 0 deletions packages/Maleo.js/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const paths = {
server: 'src/server/**',
client: 'src/client/**',
bin: 'src/bin/*.ts',
routes: 'src/routes/**',
default: 'src/default/*.ts',
};

let tasks = Object.keys(paths);
Expand Down
2 changes: 1 addition & 1 deletion packages/Maleo.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"es6-promise": "4.2.5",
"express": "4.16.4",
"figlet": "^1.2.1",
"friendly-errors-webpack-plugin": "^1.7.0",
"hard-source-webpack-plugin": "0.12.0",
"helmet": "3.14.0",
"isomorphic-fetch": "2.2.1",
Expand All @@ -50,7 +51,6 @@
"webpack-cli": "3.1.2",
"webpack-dev-middleware": "3.4.0",
"webpack-hot-middleware": "2.24.3",
"webpack-node-externals": "1.7.2",
"webpack-stats-plugin": "^0.2.1",
"webpackbar": "2.6.3"
},
Expand Down
1 change: 1 addition & 0 deletions packages/Maleo.js/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/routes/registerRoutes');
2 changes: 1 addition & 1 deletion packages/Maleo.js/server.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('./lib/server/Server.js');
module.exports = require('./lib/server/server.js');
9 changes: 9 additions & 0 deletions packages/Maleo.js/src/build/loaders/maleo-babel-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ export default babelLoader.custom((babel) => {
const configs = new Set();

return {
customOptions(opts) {
const loader = {
cacheCompression: false,
cacheDirectory: true,
...opts,
};

return { loader };
},
config(config) {
const options = { ...config.options };
if (config.hasFilesystemConfig()) {
Expand Down
23 changes: 23 additions & 0 deletions packages/Maleo.js/src/build/loaders/maleo-register-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { loader } from 'webpack';
import loaderUtils from 'loader-utils';
import { REGISTERS } from '@constants/index';

const maleoRegisterLoader: loader.Loader = function() {
const { absolutePagePath, page }: any = loaderUtils.getOptions(this);
const stringifiedAbsolutePagePath = JSON.stringify(absolutePagePath);
const stringifiedPage = JSON.stringify(page);

return `
(window.${REGISTERS.WINDOW_VAR_NAME}=window.${
REGISTERS.WINDOW_VAR_NAME
}||[]).push([${stringifiedPage}, function() {
var page = require(${stringifiedAbsolutePagePath});
if(module.hot) {
module.hot.accept(${stringifiedAbsolutePagePath});
}
return page.default || page;
}]);
`;
};

export default maleoRegisterLoader;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import { Compiler } from 'webpack';
import { AUTODLL_PATH } from '@src/constants';
import { AUTODLL_PATH } from '@constants/index';

const INDENT = 2;
const DEFAULT_TRANSFORM = async (data) => JSON.stringify(data, null, INDENT);
Expand Down
Loading