Skip to content

Commit

Permalink
use HtmlWebpackPlugin for vue
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Bertet committed Sep 4, 2017
1 parent ae0e1ee commit 9aaecd9
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 203 deletions.
1 change: 1 addition & 0 deletions app/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"file-loader": "^0.11.1",
"find-cache-dir": "^1.0.0",
"global": "^4.3.2",
"html-webpack-plugin": "^2.30.1",
"json-loader": "^0.5.4",
"json-stringify-safe": "^5.0.1",
"json5": "^0.5.1",
Expand Down
26 changes: 6 additions & 20 deletions app/vue/src/server/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import shelljs from 'shelljs';
import packageJson from '../../package.json';
import getBaseConfig from './config/webpack.config.prod';
import loadConfig from './config';
import getIndexHtml from './index.html';
import getIframeHtml from './iframe.html';
import { getPreviewHeadHtml, getManagerHeadHtml, parseList, getEnvConfig } from './utils';
import { parseList, getEnvConfig } from './utils';

process.env.NODE_ENV = process.env.NODE_ENV || 'production';

Expand Down Expand Up @@ -78,24 +76,12 @@ if (program.staticDir) {
// compile all resources with webpack and write them to the disk.
logger.log('Building storybook ...');
webpack(config).run((err, stats) => {
if (err) {
if (err || stats.hasErrors()) {
logger.error('Failed to build the storybook');
logger.error(err.message);
// eslint-disable-next-line no-unused-expressions
err && logger.error(err.message);
// eslint-disable-next-line no-unused-expressions
stats.hasErrors() && stats.toJson().errors.forEach(e => logger.error(e));
process.exit(1);
}

const data = {
publicPath: config.output.publicPath,
assets: stats.toJson().assetsByChunkName,
};

// Write both the storybook UI and IFRAME HTML files to destination path.
fs.writeFileSync(
path.resolve(outputDir, 'index.html'),
getIndexHtml({ ...data, headHtml: getManagerHeadHtml(configDir) })
);
fs.writeFileSync(
path.resolve(outputDir, 'iframe.html'),
getIframeHtml({ ...data, headHtml: getPreviewHeadHtml(configDir) })
);
});
10 changes: 7 additions & 3 deletions app/vue/src/server/config/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ export function loadEnv(options = {}) {
PUBLIC_URL: JSON.stringify(options.production ? '.' : ''),
};

Object.keys(process.env).filter(name => /^STORYBOOK_/.test(name)).forEach(name => {
env[name] = JSON.stringify(process.env[name]);
});
Object.keys(process.env)
.filter(name => /^STORYBOOK_/.test(name))
.forEach(name => {
env[name] = JSON.stringify(process.env[name]);
});

return {
'process.env': env,
};
}

export const getConfigDir = () => process.env.SBCONFIG_CONFIG_DIR || './.storybook';
29 changes: 28 additions & 1 deletion app/vue/src/server/config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import path from 'path';
import webpack from 'webpack';
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import WatchMissingNodeModulesPlugin from './WatchMissingNodeModulesPlugin';
import { includePaths, excludePaths, nodeModulesPaths, loadEnv, nodePaths } from './utils';
import {
getConfigDir,
includePaths,
excludePaths,
nodeModulesPaths,
loadEnv,
nodePaths,
} from './utils';
import { getPreviewHeadHtml, getManagerHeadHtml } from '../utils';
import babelLoaderConfig from './babel';
import { version } from '../../../package.json';

export default function() {
const config = {
Expand All @@ -22,6 +32,23 @@ export default function() {
publicPath: '/',
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
chunks: ['manager'],
data: {
managerHead: getManagerHeadHtml(getConfigDir()),
version,
},
template: require.resolve('../index.html.ejs'),
}),
new HtmlWebpackPlugin({
filename: 'iframe.html',
excludeChunks: ['manager'],
data: {
previewHead: getPreviewHeadHtml(getConfigDir()),
},
template: require.resolve('../iframe.html.ejs'),
}),
new webpack.DefinePlugin(loadEnv()),
new webpack.HotModuleReplacementPlugin(),
new CaseSensitivePathsPlugin(),
Expand Down
22 changes: 21 additions & 1 deletion app/vue/src/server/config/webpack.config.prod.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import path from 'path';
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import babelLoaderConfig from './babel.prod';
import { includePaths, excludePaths, loadEnv, nodePaths } from './utils';
import { getConfigDir, includePaths, excludePaths, loadEnv, nodePaths } from './utils';
import { getPreviewHeadHtml, getManagerHeadHtml } from '../utils';
import { version } from '../../../package.json';

export default function() {
const entries = {
Expand All @@ -23,6 +26,23 @@ export default function() {
publicPath: '',
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
chunks: ['manager'],
data: {
managerHead: getManagerHeadHtml(getConfigDir()),
version,
},
template: require.resolve('../index.html.ejs'),
}),
new HtmlWebpackPlugin({
filename: 'iframe.html',
excludeChunks: ['manager'],
data: {
previewHead: getPreviewHeadHtml(getConfigDir()),
},
template: require.resolve('../iframe.html.ejs'),
}),
new webpack.DefinePlugin(loadEnv({ production: true })),
new webpack.optimize.UglifyJsPlugin({
compress: {
Expand Down
20 changes: 20 additions & 0 deletions app/vue/src/server/iframe.html.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<base target="_parent">
<script>
if (window.parent !== window) {
window.__VUE_DEVTOOLS_GLOBAL_HOOK__ = window.parent.__VUE_DEVTOOLS_GLOBAL_HOOK__;
window.parent.__VUE_DEVTOOLS_CONTEXT__ = window.document;
}
</script>
<title>Storybook</title>
<%= htmlWebpackPlugin.options.data.previewHead %>
</head>
<body>
<div id="root"></div>
<div id="error-display"></div>
</body>
</html>
87 changes: 0 additions & 87 deletions app/vue/src/server/iframe.html.js

This file was deleted.

44 changes: 44 additions & 0 deletions app/vue/src/server/index.html.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="storybook-version" content="<%= htmlWebpackPlugin.options.data.version %>">
<meta content="IE=edge" http-equiv="X-UA-Compatible" />
<title>Storybook</title>
<style>
/*
When resizing panels, the drag event breaks if the cursor
moves over the iframe. Add the 'dragging' class to the body
at drag start and remove it when the drag ends.
*/
.dragging iframe {
pointer-events: none;
}
/* Styling the fuzzy search box placeholders */
.searchBox::-webkit-input-placeholder { /* Chrome/Opera/Safari */
color: #ddd;
font-size: 16px;
}
.searchBox::-moz-placeholder { /* Firefox 19+ */
color: #ddd;
font-size: 16px;
}
.searchBox:focus{
border-color: #EEE !important;
}
.btn:hover{
background-color: #eee
}
</style>
<%= htmlWebpackPlugin.options.data.managerHead %>

</head>
<body style="margin: 0;">
<div id="root"></div>
</body>
</html>
79 changes: 0 additions & 79 deletions app/vue/src/server/index.html.js

This file was deleted.

Loading

0 comments on commit 9aaecd9

Please sign in to comment.