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

Send minified client binary in production environment #2

Merged
merged 1 commit into from
Jan 20, 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ BABEL=$(NODE_MODULES_BIN)/babel
BABEL_NODE=$(NODE_MODULES_BIN)/babel-node
WEBPACK_CLIENT_CONFIG=webpack-client.config.js

.PHONY: test test-w dev-install build build-module lint clean
.PHONY: test test-w dev-install build build-module lint clean serve

dist:
mkdir dist
Expand Down
6 changes: 4 additions & 2 deletions server/main.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const dom = require('nomplate').dom;

function main(options, renderView) {
// Use minified sources for production environment
const scriptSource = options.settings && options.settings.env === 'production' ? '/dist/client.min.js' : '/dist/client.js';

return dom.html({lang: 'en'}, () => {
dom.head(() => {
dom.meta({charset: 'utf8'});
dom.title('Nomplate Project');
dom.link({rel: 'stylesheet', href: 'static/styles.css'});
// TODO(lbayes): If production mode, use minified and gzipped binary.
dom.script({src: '/dist/client.js', type: 'text/javascript'});
dom.script({src: scriptSource, type: 'text/javascript'});
});
dom.body(() => {
dom.div({id: 'app-context'}, () => {
Expand Down
31 changes: 27 additions & 4 deletions test/server_test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
const assert = require('chai').assert;
const dom = require('nomplate').dom;
const server = require('../server');
const {JSDOM} = require('jsdom');
const main = require('../server/main');
const renderElement = require('nomplate').renderElement;

describe('Server', () => {
let instance;

beforeEach(() => {
});
function render(options) {
const fakeDoc = new JSDOM('').window.document;
return renderElement(main(options || {}, () => {
return dom.div({id: 'fake-client'});
}), fakeDoc);
}

it('is instantiable', () => {
assert(false, "Expected failure");
assert.isNotNull(render());
});

it('renders expected elements', () => {
const appContext = render().querySelector('#app-context');
assert.isNotNull(appContext);
});

it('renders dev script source', () => {
const scriptTag = render({settings: {env: 'development'}}).querySelector('script');
assert.isNotNull(scriptTag);
assert.equal(scriptTag.src, '/dist/client.js');
});

it('renders minified script source', () => {
const scriptTag = render({settings: {env: 'production'}}).querySelector('script');
assert.isNotNull(scriptTag);
assert.equal(scriptTag.src, '/dist/client.min.js');
});
});