Code Monkey home page Code Monkey logo

cyc's Introduction

cyc

https://img.shields.io/npm/v/cyc-cli.svg?style=flat-square https://img.shields.io/npm/dt/cyc-cli.svg?style=flat-square https://img.shields.io/npm/l/cyc-cli.svg?style=flat-square

cyc provides an intuitive and hassle-free starting point for Cycle.js applications. It comes with production and development webpack configurations, dynamic hot reloading, Babel transpilation, unintrusive long-term caching, and an isomorphic express server. The cyc boilerplate is scalable, convenient, and highly modular.

Live Preview

Contents

Features

  • production and development webpack configurations
  • project-wide babel transpilation
  • hot reloading with cycle-restart
  • dynamic isomorphic loading with dynamic-require
  • declarative server endpoints
  • long-term caching

Installing

$ npm install -g cyc-cli babel-cli

Getting Started

Run cyc in any directory, and you will be prompted for the name of your project, and what directory to put it in. The directory name defaults to the project name.

$ cyc
  Application Name myapp
  Directory (myapp)
  Copying...
  Populating...
  Done.
$ cd myapp
$ npm i

Then, you can run the server in development.

$ npm run dev

The website is now live on localhost:3000.

Have a look in ./src/js/ and play around with the files. An index page with a BMI calculator and a simple about page are automatically generated. Each file is hot reloadable.

You can also build the server and run it in production. Compiling the client bundles may take a while, because this build step utilizes heavy optimization.

$ npm run mk
$ PORT=80 npm start

Note: When actually hosting a live website, you should change ::1 to 0.0.0.0 or ::.

npm scripts

  • dev start dev server
  • start start production server (requires built server and client)
  • mkserver build production server (fast)
  • mkclient build production client (slow)
  • mk build production server and client

How It Works

cyc has some special plumbing that allows much of its code to be flexible and freely run in dev or production, client or server. This feature required close attention to details during the design process.

Server, Client, and Isomorphism

In development, the server is run entirely with babel-node, provided by babel-cli. In production, the server (output by the mkserver build step) is run with the default node installation. Application modules, or programs, are parsed with an isolated babel-registered require. Entry-level modules can check the global CLIENT variable to determine whether to use server or client logic. This is set to false on the server with global.CLIENT = false, while the webpack configuration sets it to true with a DefinePlugin.

Dynamic Require

Allowing the server to be built for production is one of the main design goals, but due to webpack's poor support for dynamic requires (i.e. require(variable)), some special tooling is required. The dynamic-require package provides dynamic requires, and supports babel transpilation.

Request Pipeline

For each request, the program is run, and its virtual DOM output is rendered and wrapped in an HTML template. A reference to the actual bundle is then placed at the end of the file. When the response reaches the client, the rendered content is immediately available while the program is loading. Once loaded, the program will bootstrap itself, and the application will be responsive.

Isomorphic Reloading

The server can hot reload modules for server-side rendering, just as the client can with webpack-hmr. Implemented in hot.js, the reloading mechanism hooks into the webpack compiler object in the same way that webpack-hot-middleware does. When a program dependency is modified, invalidation is trickled down the tree all the way to the entry-level module. Invalidated modules are deleted from the require cache, and re-required with dynamic-require, making the updated version of the program available.

Routing

Routes are configured in the routes.js file. Each configurable route contains a reference to the index file of its respective program, pug (formerly jade) template, and route path. These routes are then transformed to normalize paths and add compilation metadata for webpack. Each route is then linked to the server request handler. The routing configuration can be customized to make other specific logic available in the request handler.

Long-Term Caching

The server contains a hashes object that maps route IDs to their respective bundles. In development, this is reloaded in memory whenever a new compilation is performed. In production, the hashes are read from the hashes.json file produced by the mkclient build step.

cyc's People

Contributors

alkhe avatar widdershin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cyc's Issues

better directory name for assets

Currently app modules are placed in src/js and jade templates in src/html. This is a bit excessive, so an alternate name might be better, e.g. src/js => app, src/html => view.

abstract isomorphic structure

The optimal isomorphic architecture is as follows:

  • application is context aware, behaves differently depending on global flag CLIENT
    • webpack config: webpack.DefinePlugin({ CLIENT: true })
    • server + babel-require2: global.CLIENT = false
  • export { source, drivers } from application root
  • server runs application by:
{ source, drivers } = babelRequire(app)
program = babelRequire(source)
server.get((...) => {
    run(program, drivers)
        .forEach(render)
})
// program may be hot reloaded by calling `babelRequire(source)` again

Work for this has been completed on https://github.com/edge/cyc/tree/abstraction, but I would like to minimize the boilerplate necessary for application roots (e.g. https://github.com/edge/cyc/blob/abstraction/skel/src/js/index.js) before merging to master for scalability concerns.

/skel/src/js/index.js

/* global CLIENT */
import { run } from '@cycle/core';
import { makeDOMDriver, makeHTMLDriver } from '@cycle/dom';
import { restart, restartable } from 'cycle-restart';

export let source = './main';
export let drivers = {};

if (CLIENT) {
    drivers = {
        DOM: restartable(makeDOMDriver('#root'), { pauseSinksWhileReplaying: false })
    };

-   let cycle = run(require('./main').default, drivers);
+   let cycle = run(require(source).default, drivers);

    if (module.hot) {
-       module.hot.accept('./main', () => {
-           restart(require('./main').default, drivers, cycle);
+       module.hot.accept(source, () => {
+           restart(require(source).default, drivers, cycle);
        });
    }
}
else {
-   let { join } = require('path');
-   source = require.resolve(join(__dirname, source));
    drivers = {
        DOM: makeHTMLDriver()
    };
}
  • Specifying './main' only once in the source is desired, but this requires webpack to understand dynamic requires.
  • The server can take care of require.resolve in dev mode, but will not be able to when compiled for production. Once again, because webpack cannot understand dynamic requires well.

add typescript support

Unless I am the only one interested, I think it would be cool to have an option (branch?) in order to setup as well the typescript boilerplate

Can not run

0 info it worked if it ends with ok
1 verbose cli [ '/usr/local/Cellar/node/5.1.0/bin/node',
1 verbose cli '/usr/local/bin/npm',
1 verbose cli 'owner',
1 verbose cli 'ls',
1 verbose cli 'Mycycapp' ]
2 info using [email protected]
3 info using [email protected]
4 silly mapToRegistry name Mycycapp
5 silly mapToRegistry using default registry
6 silly mapToRegistry registry https://registry.npmjs.org/
7 silly mapToRegistry uri https://registry.npmjs.org/Mycycapp
8 verbose request uri https://registry.npmjs.org/Mycycapp
9 verbose request no auth needed
10 info attempt registry request try #1 at 11:22:08 AM
11 verbose request id c27cc123b20bf120
12 http request GET https://registry.npmjs.org/Mycycapp
13 http 404 https://registry.npmjs.org/Mycycapp
14 verbose headers { 'content-type': 'application/json',
14 verbose headers 'cache-control': 'max-age=0',
14 verbose headers 'content-length': '2',
14 verbose headers 'accept-ranges': 'bytes',
14 verbose headers date: 'Wed, 25 Nov 2015 03:22:09 GMT',
14 verbose headers via: '1.1 varnish',
14 verbose headers age: '0',
14 verbose headers connection: 'keep-alive',
14 verbose headers 'x-served-by': 'cache-sin6924-SIN',
14 verbose headers 'x-cache': 'MISS',
14 verbose headers 'x-cache-hits': '0',
14 verbose headers 'x-timer': 'S1448421729.718299,VS0,VE253' }
15 silly get cb [ 404,
15 silly get { 'content-type': 'application/json',
15 silly get 'cache-control': 'max-age=0',
15 silly get 'content-length': '2',
15 silly get 'accept-ranges': 'bytes',
15 silly get date: 'Wed, 25 Nov 2015 03:22:09 GMT',
15 silly get via: '1.1 varnish',
15 silly get age: '0',
15 silly get connection: 'keep-alive',
15 silly get 'x-served-by': 'cache-sin6924-SIN',
15 silly get 'x-cache': 'MISS',
15 silly get 'x-cache-hits': '0',
15 silly get 'x-timer': 'S1448421729.718299,VS0,VE253' } ]
16 error owner ls Couldn't get owner data Mycycapp
17 verbose stack Error: Registry returned 404 for GET on https://registry.npmjs.org/Mycycapp
17 verbose stack at makeError (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:264:12)
17 verbose stack at CachingRegistryClient. (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:242:14)
17 verbose stack at Request._callback (/usr/local/lib/node_modules/npm/node_modules/npm-registry-client/lib/request.js:172:14)
17 verbose stack at Request.self.callback (/usr/local/lib/node_modules/npm/node_modules/request/request.js:198:22)
17 verbose stack at emitTwo (events.js:87:13)
17 verbose stack at Request.emit (events.js:172:7)
17 verbose stack at Request. (/usr/local/lib/node_modules/npm/node_modules/request/request.js:1082:10)
17 verbose stack at emitOne (events.js:82:20)
17 verbose stack at Request.emit (events.js:169:7)
17 verbose stack at IncomingMessage. (/usr/local/lib/node_modules/npm/node_modules/request/request.js:1009:12)
18 verbose statusCode 404
19 verbose pkgid Mycycapp
20 verbose cwd /Users/xnpeng/NodeJsProjects/Mycycapp
21 error Darwin 14.5.0
22 error argv "/usr/local/Cellar/node/5.1.0/bin/node" "/usr/local/bin/npm" "owner" "ls" "Mycycapp"
23 error node v5.1.0
24 error npm v3.4.1
25 error code E404
26 error 404 Registry returned 404 for GET on https://registry.npmjs.org/Mycycapp
26 error 404
26 error 404 'Mycycapp' is not in the npm registry.
26 error 404 Your package name is not valid, because
26 error 404
26 error 404 1. name can no longer contain capital letters
26 error 404
26 error 404 Note that you can also install from a
26 error 404 tarball, folder, http url, or git url.
27 verbose exit [ 1, true ]

use webpack for server reload

Webpack's lack of support for server reloading adds a lot of complexity to this project. Consider adding this feature to webpack.

related: #13 #14

Continued from #9:

Using dynamic-require is relatively primitive, but is necessary to make up for webpack's lack of support for interoperable dynamic requires. Not using webpack for server compilation may reduce this complexity.

Wepack loaders in mvi.js

I think it would be nice to be able to use webpack loaders in mvi.js, to do things like require('style.css'). This is currently not possible because the server loads mvi.js directly. Only the client-side bundle.js is produced by webpack.

An additional bundle for the server side would be needed for this to work. Would you consider a pull request?

optimize server reload

As applications grow larger, hotAccept may take longer to reload all required assets. Additional webpack integration may be required.

Examples with DB layer

How about an example with MongoDB or something? It would be nice if that were a bit more fleshed out.

ES6 relative path imports

Hey, nice project, thanks for your work.
Maybe I am doing something wrong but trying to import my own modules in /src/js/index.js or main.js will give me "Module not found" errors. This breaks the npm tasks import api from './api'; while import api from './src/js/api'; works but gives me errors further down the road โ€ฆ

Weird message: listening on http://::1:3000

When running npm run dev on a fresh project

[dev]
listening on http://::1:3000

Should be listening on http://localhost:3000

I found the bug in skel/index.js

app.listen(port, '::1', err => {
	if (err) {
		return console.err(err)
	}
	log(`listening on http://::1:${ port }`)
})

trigger server reload for all dependencies

Currently, isomorphic reload is only triggered when the primary application file is written. This can be fixed by recursively detecting all of its dependencies.

Alternatively, seeing as though isomorphic reload introduces so much complexity, we could abandon it until there is more tooling support.

related: #13

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.