From bc2cc48efb6291ddbd7c25766d17c81cb9c806ec Mon Sep 17 00:00:00 2001 From: Jeff Carbonella Date: Wed, 2 Nov 2016 10:09:07 -0400 Subject: [PATCH] Used hashed assets in index/iframe html files --- src/server/build.js | 20 ++++++----- src/server/config/webpack.config.js | 2 +- src/server/config/webpack.config.prod.js | 2 +- src/server/iframe.html.js | 42 ++++++++++++++++++++++-- src/server/index.html.js | 35 ++++++++++++++++++-- 5 files changed, 84 insertions(+), 17 deletions(-) diff --git a/src/server/build.js b/src/server/build.js index bb1ab7bf8c05..5dc2a5cecdda 100644 --- a/src/server/build.js +++ b/src/server/build.js @@ -6,7 +6,6 @@ import path from 'path'; import fs from 'fs'; import chalk from 'chalk'; import shelljs from 'shelljs'; -import uuid from 'uuid'; import packageJson from '../../package.json'; import getBaseConfig from './config/webpack.config.prod'; import loadConfig from './config'; @@ -73,19 +72,22 @@ if (program.staticDir) { }); } -const cacheKey = uuid.v4(); -// Write both the storybook UI and IFRAME HTML files to destination path. -const headHtml = getHeadHtml(configDir); -const publicPath = config.output.publicPath; -fs.writeFileSync(path.resolve(outputDir, 'index.html'), getIndexHtml(publicPath, cacheKey)); -fs.writeFileSync(path.resolve(outputDir, 'iframe.html'), getIframeHtml(headHtml, publicPath, cacheKey)); - // compile all resources with webpack and write them to the disk. logger.log('Building storybook ...'); -webpack(config).run(function (err) { +webpack(config).run(function (err, stats) { if (err) { logger.error('Failed to build the storybook'); logger.error(err.message); process.exit(1); } + + const data = { + publicPath: config.output.publicPath, + assets: stats.toJson().assetsByChunkName, + }; + const headHtml = getHeadHtml(configDir); + + // Write both the storybook UI and IFRAME HTML files to destination path. + fs.writeFileSync(path.resolve(outputDir, 'index.html'), getIndexHtml(data)); + fs.writeFileSync(path.resolve(outputDir, 'iframe.html'), getIframeHtml({ ...data, headHtml })); }); diff --git a/src/server/config/webpack.config.js b/src/server/config/webpack.config.js index 5d789ee3f46d..ea89a5c713dd 100644 --- a/src/server/config/webpack.config.js +++ b/src/server/config/webpack.config.js @@ -28,7 +28,7 @@ export default function () { }, output: { path: path.join(__dirname, 'dist'), - filename: 'static/[name].bundle.[chunkhash].js', + filename: 'static/[name].bundle.js', publicPath: '/', }, plugins: [ diff --git a/src/server/config/webpack.config.prod.js b/src/server/config/webpack.config.prod.js index acf402b56944..0fecfc591be6 100644 --- a/src/server/config/webpack.config.prod.js +++ b/src/server/config/webpack.config.prod.js @@ -26,7 +26,7 @@ export default function () { devtool: '#cheap-module-source-map', entry: entries, output: { - filename: 'static/[name].bundle.[chunkhash].js', + filename: 'static/[name].[chunkhash].bundle.js', // Here we set the publicPath to ''. // This allows us to deploy storybook into subpaths like GitHub pages. // This works with css and image loaders too. diff --git a/src/server/iframe.html.js b/src/server/iframe.html.js index 7c0f01075c55..f8f977c332c8 100644 --- a/src/server/iframe.html.js +++ b/src/server/iframe.html.js @@ -1,7 +1,42 @@ import url from 'url'; -export default function (headHtml, publicPath, cacheKey) { - const previewUrl = cacheKey ? `static/preview.bundle.js?${cacheKey}` : 'static/preview.bundle.js'; +// assets.preview will be: +// - undefined +// - string e.g. 'static/preview.9adbb5ef965106be1cc3.bundle.js' +// - array of strings e.g. +// [ 'static/preview.9adbb5ef965106be1cc3.bundle.js', +// 'preview.0d2d3d845f78399fd6d5e859daa152a9.css', +// 'static/preview.9adbb5ef965106be1cc3.bundle.js.map', +// 'preview.0d2d3d845f78399fd6d5e859daa152a9.css.map' ] +const previewUrlsFromAssets = (assets) => { + if (!assets) { + return { + js: 'static/preview.bundle.js', + }; + } + + if (typeof assets.preview === 'string') { + return { + js: assets.preview, + }; + } + + return { + js: assets.preview.find(filename => filename.match(/\.js$/)), + css: assets.preview.find(filename => filename.match(/\.css$/)), + }; +}; + +export default function (data) { + const { assets, headHtml, publicPath } = data; + + const previewUrls = previewUrlsFromAssets(assets); + + let previewCssTag; + if (previewUrls.css) { + previewCssTag = ``; + } + return ` @@ -15,11 +50,12 @@ export default function (headHtml, publicPath, cacheKey) { React Storybook ${headHtml} + ${previewCssTag}
- + `; diff --git a/src/server/index.html.js b/src/server/index.html.js index 5e1d60d75143..5465c7c31c64 100644 --- a/src/server/index.html.js +++ b/src/server/index.html.js @@ -1,7 +1,36 @@ import url from 'url'; -export default function (publicPath, cacheKey) { - const managerUrl = cacheKey ? `static/manager.bundle.js?${cacheKey}` : 'static/manager.bundle.js'; +// assets.manager will be: +// - undefined +// - string e.g. 'static/manager.9adbb5ef965106be1cc3.bundle.js' +// - array of strings e.g. +// assets.manager will be something like: +// [ 'static/manager.c6e6350b6eb01fff8bad.bundle.js', +// 'static/manager.c6e6350b6eb01fff8bad.bundle.js.map' ] +const managerUrlsFromAssets = (assets) => { + if (!assets) { + return { + js: 'static/manager.bundle.js', + }; + } + + if (typeof assets.manager === 'string') { + return { + js: assets.manager, + }; + } + + return { + js: assets.manager.find(filename => filename.match(/\.js$/)), + css: assets.manager.find(filename => filename.match(/\.css$/)), + }; +}; + +export default function (data) { + const { assets, publicPath } = data; + + const managerUrls = managerUrlsFromAssets(assets); + return ` @@ -41,7 +70,7 @@ export default function (publicPath, cacheKey) {
- + `;