Skip to content

Commit 1841f3f

Browse files
authored
Rollup & SSR, Release: 1.1.0 (#15)
1 parent 1bf74b4 commit 1841f3f

11 files changed

+173
-66
lines changed

README.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
🚋 Batteries Included View Framework
33

44
## Install
5-
```
5+
```sh
66
npm install tram-one --save
77
```
88

@@ -375,6 +375,9 @@ are combined, and the app is mounted onto the `selector`.
375375
`pathName` can be an initial path, if you don't want to check the browser's
376376
current path.
377377

378+
This method only works on the client. If you are running Tram on a server, then
379+
use `.toString()`.
380+
378381
Note: setting `pathName` is great for testing, but prevents other routes from
379382
being reached on page reload.
380383

@@ -434,10 +437,10 @@ if you want to manually attach the HTMLNode that Tram-One builds to whatever.
434437
### `app.toString(pathName, [state])`
435438

436439
`app.toString` returns a string of the app for a given route and state. It has
437-
the same interface at `app.toNode`, and basically just calls `.outerHTML` on that.
440+
the same interface at `app.toNode`, and basically just calls `.outerHTML` (or
441+
`toString` on the server) on the node.
438442

439-
This can be useful if you want to do server-sider rendering. Note, this really
440-
hasn't been explored too much, so, milage may vary.
443+
This can be useful if you want to do server-sider rendering or testing.
441444

442445
## Development
443446

@@ -451,10 +454,5 @@ If you decide to clone this repo, there are several commands included in the
451454

452455
## Todo
453456

457+
Check out our [Issues on Github](https://github.com/JRJurman/tram-one/issues).
454458
PRs welcome!
455-
456-
- badges on README
457-
- advertise build size
458-
- source maps
459-
- CI (probably with Circle-CI?)
460-
- List Repositories with Example Apps

configs/rollup.esm.config.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import uglify from 'rollup-plugin-uglify'
2+
3+
const babel = require('rollup-plugin-babel')
4+
const filesize = require('rollup-plugin-filesize')
5+
6+
const pkg = require('../package.json')
7+
const external = Object.keys(pkg.dependencies)
8+
9+
const plugins = [
10+
babel({
11+
presets: [
12+
'es2015-rollup'
13+
]
14+
}),
15+
uglify(),
16+
filesize()
17+
]
18+
19+
export default {
20+
entry: 'tram-one.js',
21+
external: external,
22+
dest: pkg.main,
23+
format: 'es',
24+
plugins: plugins,
25+
sourceMap: true
26+
}

configs/rollup.umd.config.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import uglify from 'rollup-plugin-uglify'
2+
3+
const commonjs = require('rollup-plugin-commonjs')
4+
const resolve = require('rollup-plugin-node-resolve')
5+
const babel = require('rollup-plugin-babel')
6+
const builtins = require('rollup-plugin-node-builtins')
7+
const globals = require('rollup-plugin-node-globals')
8+
const filesize = require('rollup-plugin-filesize')
9+
10+
const pkg = require('../package.json')
11+
12+
const plugins = [
13+
resolve({ main: true, preferBuiltins: true }),
14+
commonjs(),
15+
globals(),
16+
builtins(),
17+
babel({
18+
presets: [
19+
'es2015-rollup'
20+
]
21+
}),
22+
uglify(),
23+
filesize()
24+
]
25+
26+
export default {
27+
entry: 'tram-one.js',
28+
dest: pkg.browser,
29+
format: 'umd',
30+
plugins: plugins,
31+
moduleName: 'tram-one',
32+
sourceMap: true
33+
}

configs/testem.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
framework: jasmine
22
launch_in_dev:
3-
- phantomjs
3+
- PhantomJS
4+
- Node
5+
launchers:
6+
Node:
7+
command: npm run test-server
48
src_files:
59
- tram-one.js
610
- tests/specs/*.js

configs/webpack.config.js

-32
This file was deleted.

examples/ssr-example/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const Tram = require('../../tram-one')
2+
const app = new Tram()
3+
const html = Tram.html()
4+
const home = () => html`
5+
<div>
6+
<h1>This is the Home Page</h1>
7+
<div> This is rendered on a server, and then served up to you! </div>
8+
<div> We also have a number page here: <a href="/num">/num</a>
9+
</div>
10+
`
11+
const num = (state) => html`
12+
<div>
13+
<h1>This is a Number Page</h1>
14+
<div> This is rendered on a server, and then served up to you! </div>
15+
<div> We can even take in stuff from the server, like this number: ${state.number} </div>
16+
</div>
17+
`
18+
19+
app.addRoute('/', home)
20+
app.addRoute('/number', num)
21+
22+
module.exports = app

examples/ssr-example/server.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const express = require('express')
2+
const server = express()
3+
4+
const app = require('./index')
5+
6+
server.get('/', (req, res) => {
7+
res.send(app.toString('/'))
8+
})
9+
10+
server.get('/num', (req, res) => {
11+
const number = Math.random() * 10
12+
res.send(app.toString('/number', {number}))
13+
})
14+
15+
server.listen(5000, () => {
16+
console.log('ssr express server running on port 5000!')
17+
})

package.json

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
{
22
"name": "tram-one",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "🚋 Batteries Included View Framework",
5-
"main": "dist/tram-one.js",
6-
"files": ["dist/tram-one.js"],
5+
"main": "dist/tram-one.esm.js",
6+
"browser": "dist/tram-one.umd.js",
7+
"files": [
8+
"dist/tram-one.esm.js",
9+
"dist/tram-one.umd.js"
10+
],
711
"scripts": {
812
"lint": "eslint ./",
913
"example": "node dev-scripts/example-selector.js",
1014
"examples": "npm run example",
11-
"build": "webpack --config configs/webpack.config.js",
12-
"test-build": "webpack --config configs/webpack.config.js && webpack --config configs/webpack.testem.config.js",
15+
"build": "npm run build-esm && npm run build-umd",
16+
"build-esm": "rollup -c configs/rollup.esm.config.js",
17+
"build-umd": "rollup -c configs/rollup.umd.config.js",
18+
"test-build": "npm run build && webpack --config configs/webpack.testem.config.js",
1319
"test-dev": "testem -f configs/testem.yml",
20+
"test-server": "jasmine tests/specs/tram-spec.js",
1421
"test": "testem ci -f configs/testem.yml"
1522
},
1623
"author": {
@@ -32,14 +39,30 @@
3239
"devDependencies": {
3340
"babel-core": "^6.25.0",
3441
"babel-loader": "^7.0.0",
42+
"babel-plugin-external-helpers": "^6.22.0",
3543
"babel-preset-env": "^1.5.2",
44+
"babel-preset-es2015": "^6.24.1",
45+
"babel-preset-es2015-rollup": "^3.0.0",
3646
"eslint": "^3.19.0",
3747
"eslint-config-standard": "^10.2.1",
48+
"eslint-plugin-import": "^2.6.0",
49+
"eslint-plugin-node": "^5.0.0",
3850
"eslint-plugin-promise": "^3.5.0",
3951
"eslint-plugin-standard": "^3.0.1",
52+
"express": "^4.15.3",
4053
"inquirer": "^3.1.0",
4154
"internal-ip": "^1.2.0",
55+
"jasmine": "^2.6.0",
56+
"min-document": "^2.19.0",
4257
"opn": "^5.0.0",
58+
"rollup": "^0.43.0",
59+
"rollup-plugin-babel": "^2.7.1",
60+
"rollup-plugin-commonjs": "^8.0.2",
61+
"rollup-plugin-filesize": "^1.4.2",
62+
"rollup-plugin-node-builtins": "^2.1.2",
63+
"rollup-plugin-node-globals": "^1.1.0",
64+
"rollup-plugin-node-resolve": "^3.0.0",
65+
"rollup-plugin-uglify": "^2.0.1",
4366
"testem": "^1.16.2",
4467
"webpack": "^2.6.1",
4568
"webpack-dev-server": "^2.4.5"

tests/specs/tram-spec.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
const Tram = window['tram-one']
2-
const testemPath = window.location.pathname
1+
const Tram = require('../../dist/tram-one.esm')
2+
3+
const isBrowser = typeof window !== 'undefined'
4+
const testemPath = isBrowser ? window.location.pathname : '/'
5+
const document = isBrowser ? window.document : require('min-document')
6+
7+
const stringify = (node) => {
8+
if (node.outerHTML !== undefined) {
9+
return node.outerHTML
10+
}
11+
return node.toString()
12+
}
313

414
describe('Tram', () => {
515
let app
@@ -23,22 +33,22 @@ describe('Tram', () => {
2333
app = new Tram()
2434

2535
app.addRoute('/404', errorPage)
26-
expect(app.toString('/')).toEqual(errorPage().outerHTML)
36+
expect(app.toString('/')).toEqual(stringify(errorPage()))
2737
})
2838

2939
it('should take in a default route', () => {
3040
app = new Tram({defaultRoute: '/200'})
3141

3242
app.addRoute('/200', successPage)
33-
expect(app.toString('/')).toEqual(successPage().outerHTML)
43+
expect(app.toString('/')).toEqual(stringify(successPage()))
3444
})
3545

3646
it('should not always go to the default', () => {
3747
app = new Tram()
3848

3949
app.addRoute('/404', errorPage)
4050
app.addRoute('/200', successPage)
41-
expect(app.toString('/200')).not.toEqual(errorPage().outerHTML)
51+
expect(app.toString('/200')).not.toEqual(stringify(errorPage()))
4252
})
4353
})
4454

@@ -63,10 +73,10 @@ describe('Tram', () => {
6373
app.addRoute('/good', successPage)
6474
app.addRoute('/bad', errorPage)
6575
app.addRoute('/404', errorPage)
66-
expect(app.toString('/')).toEqual(successPage().outerHTML)
67-
expect(app.toString('/good')).toEqual(successPage().outerHTML)
68-
expect(app.toString('/bad')).toEqual(errorPage().outerHTML)
69-
expect(app.toString('/404')).toEqual(errorPage().outerHTML)
76+
expect(app.toString('/')).toEqual(stringify(successPage()))
77+
expect(app.toString('/good')).toEqual(stringify(successPage()))
78+
expect(app.toString('/bad')).toEqual(stringify(errorPage()))
79+
expect(app.toString('/404')).toEqual(stringify(errorPage()))
7080
})
7181

7282
it('should include the default state in app', () => {
@@ -86,6 +96,8 @@ describe('Tram', () => {
8696
})
8797

8898
describe('start', () => {
99+
if (!isBrowser) { return }
100+
89101
beforeEach(() => {
90102
const childDiv = document.createElement('div')
91103
childDiv.id = 'tram_test_container'
@@ -120,6 +132,8 @@ describe('Tram', () => {
120132
})
121133

122134
describe('mount', () => {
135+
if (!isBrowser) { return }
136+
123137
beforeEach(() => {
124138
const childDiv = document.createElement('div')
125139
childDiv.id = 'tram_test_container'
@@ -136,9 +150,9 @@ describe('Tram', () => {
136150

137151
app.addRoute('/', queryablePage)
138152
const target = document.getElementById('tram_test_container')
139-
app.mount(target, '/')
153+
app.mount(target, '/', undefined, document)
140154
const mountedTarget = document.querySelector(queryableSelector)
141-
expect(mountedTarget.outerHTML).toEqual(queryablePage().outerHTML)
155+
expect(mountedTarget.outerHTML).toEqual(stringify(queryablePage()))
142156
})
143157

144158
it('should use the default route', () => {
@@ -149,7 +163,7 @@ describe('Tram', () => {
149163
const target = document.getElementById('tram_test_container')
150164
app.mount(target)
151165
const mountedTarget = document.querySelector(queryableSelector)
152-
expect(mountedTarget.outerHTML).toEqual(queryablePage(200).outerHTML)
166+
expect(mountedTarget.outerHTML).toEqual(stringify(queryablePage(200)))
153167
})
154168

155169
it('should attach the app to a selector', () => {
@@ -158,7 +172,7 @@ describe('Tram', () => {
158172
app.addRoute('/', queryablePage)
159173
app.mount('#tram_test_container', '/')
160174
const mountedTarget = document.querySelector(queryableSelector)
161-
expect(mountedTarget.outerHTML).toEqual(queryablePage().outerHTML)
175+
expect(mountedTarget.outerHTML).toEqual(stringify(queryablePage()))
162176
})
163177

164178
it('should update the app on re-mount', () => {
@@ -169,15 +183,15 @@ describe('Tram', () => {
169183
app.mount('#tram_test_container', '/')
170184
app.mount('#tram_test_container', '/200')
171185
const mountedTarget = document.querySelector(queryableSelector)
172-
expect(mountedTarget.outerHTML).toEqual(queryablePage(200).outerHTML)
186+
expect(mountedTarget.outerHTML).toEqual(stringify(queryablePage(200)))
173187
})
174188
})
175189

176190
describe('toNode', () => {
177191
it('should resolve the path', () => {
178192
app = new Tram()
179193
app.addRoute('/', successPage)
180-
expect(app.toNode('/').outerHTML).toEqual(successPage().outerHTML)
194+
expect(stringify(app.toNode('/'))).toEqual(stringify(successPage()))
181195
})
182196

183197
it('should have the default state', () => {
@@ -198,7 +212,7 @@ describe('Tram', () => {
198212
it('should return a string', () => {
199213
app = new Tram()
200214
app.addRoute('/404', errorPage)
201-
expect(app.toString('/')).toEqual(errorPage().outerHTML)
215+
expect(app.toString('/')).toEqual(stringify(errorPage()))
202216
})
203217
})
204218

tests/testem.html

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.css">
2020
</head>
2121
<body>
22-
<script src="../dist/tram-one.js"></script>
2322
<script src="tram-spec.js"></script>
2423
<div id="jasmine_content"></div>
2524
</body>

0 commit comments

Comments
 (0)