Skip to content

Commit

Permalink
Allow environment overrides specifically for Storybook
Browse files Browse the repository at this point in the history
Stories are rendered as full Ember Apps including the default
Environment which may have settings that aren't appropriate for only
rendering a single story in an iframe.

Now those can be overridden with a .storybook/environment.js file
  • Loading branch information
DingoEatingFuzz committed Oct 6, 2021
1 parent b399835 commit c207500
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
19 changes: 18 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const YUIDocsGenerator = require('ember-cli-addon-docs-yuidoc/lib/broccoli/generator');
const Funnel = require('broccoli-funnel');
const mergeTrees = require('broccoli-merge-trees');
const { parse, generatePreviewHead } = require('./lib/util');
const { parse, generatePreviewHead, overrideEnvironment } = require('./lib/util');

module.exports = {
name: require('./package').name,
Expand Down Expand Up @@ -74,6 +74,7 @@ module.exports = {
const previewHeadFilePath = path.resolve(process.cwd(), '.storybook/preview-head.html');
const previewHeadDirectory = path.dirname(previewHeadFilePath);
const envFilePath = path.resolve(process.cwd(), '.env');
const environmentOverridePath = path.resolve(process.cwd(), '.storybook/environment.js');

let fileContents = '';

Expand All @@ -93,6 +94,22 @@ module.exports = {

this.ui.writeDebugLine('Generating preview-head.html');

const environment = parsedConfig.meta.find(meta => meta.name.endsWith('config/environment'));

if (environment) {
// From the Ember App's environment.js file
const original = JSON.parse(decodeURIComponent(environment.content));

// When rootURL is anything other than "/" routing can't be started without erroring, so
// this is a sensible default.
const defaultOverride = { rootURL: '/' };

// Allow arbitrary overriding in the storybook environment
const environmentOverride = fs.existsSync(environmentOverridePath) && require(environmentOverridePath)(process.env);

environment.content = encodeURIComponent(JSON.stringify(overrideEnvironment(original, defaultOverride, environmentOverride)));
}

if(config) {
this.ui.writeDebugLine('Setting up overrides.');

Expand Down
6 changes: 6 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const cheerio = require('cheerio');
const merge = require('lodash.merge');

const lookupTable = {
meta: [{
Expand Down Expand Up @@ -146,9 +147,14 @@ function generatePreviewHead(parsedConfig) {
return doc.join('\n')
}

function overrideEnvironment(env, ...overrides) {
return merge(env, ...overrides);
}

module.exports = {
getDocumentValues,
parse,
objectToHTMLAttributes,
generatePreviewHead,
overrideEnvironment,
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@
"cheerio": "^1.0.0-rc.2",
"ember-cli-addon-docs-yuidoc": "^1.0.0",
"ember-cli-babel": "^7.23.0",
"ember-cli-htmlbars": "^5.3.1"
"ember-cli-htmlbars": "^5.3.1",
"lodash.merge": "^4.6.2"
},
"publishConfig": {
"access": "public"
Expand Down

0 comments on commit c207500

Please sign in to comment.