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

WIP: Enable custom html in iframe <head> #77

Merged
merged 3 commits into from
Apr 11, 2016
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
25 changes: 25 additions & 0 deletions dist/server/get_head_html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});

exports.default = function (configDirPath) {
var headHtmlPath = _path2.default.resolve(configDirPath, 'head.html');
var headHtml = '';
if (_fs2.default.existsSync(headHtmlPath)) {
headHtml = _fs2.default.readFileSync(headHtmlPath, 'utf8');
}

return headHtml;
};

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
4 changes: 2 additions & 2 deletions dist/server/iframe.html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

exports.default = function () {
return "\n <!DOCTYPE html>\n <html>\n <head>\n <title>React Storybook</title>\n </head>\n <body>\n <div id=\"root\" />\n <script src=\"/static/preview.bundle.js\"></script>\n </body>\n </html>\n ";
exports.default = function (headHtml) {
return "\n <!DOCTYPE html>\n <html>\n <head>\n <title>React Storybook</title>\n " + headHtml + "\n </head>\n <body>\n <div id=\"root\" />\n <script src=\"/static/preview.bundle.js\"></script>\n </body>\n </html>\n ";
};
7 changes: 6 additions & 1 deletion dist/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _get_head_html = require('./get_head_html');

var _get_head_html2 = _interopRequireDefault(_get_head_html);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

process.env.NODE_ENV = 'production';
Expand Down Expand Up @@ -133,8 +137,9 @@ app.get('/', function (req, res) {
res.send((0, _index2.default)());
});

var headHtml = (0, _get_head_html2.default)(configDirPath);
app.get('/iframe', function (req, res) {
res.send((0, _iframe2.default)());
res.send((0, _iframe2.default)(headHtml));
});

app.listen(_commander2.default.port, function (error) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"eslint-plugin-babel": "^3.1.0",
"eslint-plugin-react": "^4.2.3",
"mocha": "^2.4.5",
"mock-fs": "^3.8.0",
"nodemon": "^1.9.1",
"react": "^0.14.8",
"react-dom": "^0.14.8",
Expand Down
44 changes: 44 additions & 0 deletions src/server/__tests__/get_head_html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const { describe, it, beforeEach, afterEach } = global;
import { expect } from 'chai';
import getHeadHtml from '../get_head_html';
import mock from 'mock-fs';

const HEAD_HTML_CONTENTS = '<script>console.log("custom script!");</script>';

describe('server.getHeadHtml', () => {
describe('when .storybook/head.html does not exist', () => {
beforeEach(() => {
mock({
config: {},
});
});

afterEach(() => {
mock.restore();
});

it('return an empty string', () => {
const result = getHeadHtml('./config');
expect(result).to.be.equal('');
});
});

describe('when .storybook/head.html exists', () => {
beforeEach(() => {
mock({
config: {
'head.html': HEAD_HTML_CONTENTS,
},
});
});

afterEach(() => {
mock.restore();
});

it('return the contents of the file', () => {
const result = getHeadHtml('./config');
expect(result).to.be.equal(HEAD_HTML_CONTENTS);
});
});
});
12 changes: 12 additions & 0 deletions src/server/get_head_html.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'path';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we move this into a file called src/server/utils.js and export the following function as getHeadHtml.

import fs from 'fs';

export default function (configDirPath) {
const headHtmlPath = path.resolve(configDirPath, 'head.html');
let headHtml = '';
if (fs.existsSync(headHtmlPath)) {
headHtml = fs.readFileSync(headHtmlPath, 'utf8');
}

return headHtml;
}
3 changes: 2 additions & 1 deletion src/server/iframe.html.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export default function () {
export default function (headHtml) {
return `
<!DOCTYPE html>
<html>
<head>
<title>React Storybook</title>
${headHtml}
</head>
<body>
<div id="root" />
Expand Down
4 changes: 3 additions & 1 deletion src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import packageJson from '../../package.json';
import config from './webpack.config';
import path from 'path';
import fs from 'fs';
import getHeadHtml from './get_head_html';

const logger = console;

Expand Down Expand Up @@ -106,8 +107,9 @@ app.get('/', function (req, res) {
res.send(getIndexHtml());
});

const headHtml = getHeadHtml(configDirPath);
app.get('/iframe', function (req, res) {
res.send(getIframeHtml());
res.send(getIframeHtml(headHtml));
});

app.listen(program.port, function (error) {
Expand Down