Skip to content

Commit

Permalink
fix(cli): Expose whole JSDOM API to global scope (#147)
Browse files Browse the repository at this point in the history
* fix(cli): Expose whole JSDOM API to global scope

* Add missing package.json update

* Remove outdated assertion from test
  • Loading branch information
tchock authored Feb 9, 2021
1 parent 77d7882 commit 9498c6f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 123 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"ink-spinner": "^3.0.1",
"ink-text-input": "^3.3.0",
"jsdom": "^16.3.0",
"jsdom-global": "^3.0.2",
"memory-fs": "^0.5.0",
"node-emoji": "^1.10.0",
"open": "^7.3.0",
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/steps/analyze/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ScanStepOutput } from '../scan';
import { CompileStepOutput } from '../compile';
import { getStoriesMetadata } from './getStoriesMetadata';
import { setupFakeBrowserEnvironment } from './setupFakeBrowserEnvironment';
import { getStepOutputStories } from '../../utils/getOutputStories';

export type StoryItem = {
exportName: string;
Expand Down Expand Up @@ -38,9 +39,9 @@ type DependencyStepOutputs = {
};

async function action({ stepOutputs }: StepRunnerActionOptions<DependencyStepOutputs>) {
const componentModules = setupFakeBrowserEnvironment(global);
const { componentModules, cleanup } = setupFakeBrowserEnvironment(global);

stepOutputs.compile.stories.forEach(s =>
getStepOutputStories(stepOutputs).forEach(s =>
s.files.forEach(fileName => {
const foundFile = stepOutputs.compile.files
.filter(file => JS_REGEXP.test(file.name))
Expand All @@ -53,6 +54,8 @@ async function action({ stepOutputs }: StepRunnerActionOptions<DependencyStepOut

const storiesMetadata = getStoriesMetadata(stepOutputs.compile.stories, componentModules);

cleanup();

return {
storiesMetadata,
};
Expand Down
10 changes: 4 additions & 6 deletions packages/cli/src/steps/analyze/setupFakeBrowserEnvironment.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { JSDOM } from 'jsdom';

const jsDomGlobal = require('jsdom-global');

export function setupFakeBrowserEnvironment(internalGlobal: any) {
const componentModules = new Map<string, Record<string, any>>();

const { window } = new JSDOM('<body></body>');
const cleanup = jsDomGlobal();
// We need to extend global here, so we disable no-param-reassign rule here
/* eslint-disable no-param-reassign */
internalGlobal.window = window;
internalGlobal.react = React;
internalGlobal.reactDom = ReactDOM;
internalGlobal.document = window.document;
internalGlobal.registerComponent = (moduleName: string, moduleContent) => {
componentModules.set(moduleName, moduleContent);
};
/* eslint-enable no-param-reassign */

return componentModules;
return { componentModules, cleanup };
}
5 changes: 0 additions & 5 deletions packages/cli/src/utils/composeWebpackConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ const testCases = [
react: 'react',
'react-dom': 'reactDom',
});

// Plugins
expect(config.plugins[1].definitions).toEqual({
'process.env': { NODE_ENV: '"production"' },
});
},
},
{
Expand Down
15 changes: 2 additions & 13 deletions packages/cli/src/utils/composeWebpackConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as webpack from 'webpack';
import * as pathUtils from 'path';
import { mergeWithCustomize, unique } from 'webpack-merge';
import { merge } from 'webpack-merge';

const BLACKLISTED_PLUGINS = ['ManifestPlugin'];

Expand All @@ -17,13 +17,7 @@ const composeWebpackConfig = (
plugin => !BLACKLISTED_PLUGINS.includes(plugin.constructor?.name)
);

return mergeWithCustomize({
customizeArray: unique(
'plugins',
['DefinePlugin'],
plugin => plugin.constructor && plugin.constructor.name
),
})(
return merge(
// Bojagi decorator
{
module: {
Expand Down Expand Up @@ -86,11 +80,6 @@ const composeWebpackConfig = (
},
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production'),
},
}),
new webpack.NormalModuleReplacementPlugin(
/@storybook\/addons/,
require.resolve('@bojagi/cli/fakeStorybookAddons.js')
Expand Down
114 changes: 17 additions & 97 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7420,7 +7420,7 @@ debug@4, debug@^4.2.0:
dependencies:
ms "2.1.2"

debug@^3.0.0, debug@^3.1.0, debug@^3.1.1, debug@^3.2.5:
debug@^3.0.0, debug@^3.1.0, debug@^3.1.1:
version "3.2.6"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.2.6.tgz#e83d17de16d8a7efb7717edbe5fb10135eee629b"
integrity sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==
Expand Down Expand Up @@ -8814,14 +8814,7 @@ fault@^1.0.0:
dependencies:
format "^0.2.0"

faye-websocket@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/faye-websocket/-/faye-websocket-0.10.0.tgz#4e492f8d04dfb6f89003507f6edbf2d501e7c6f4"
integrity sha1-TkkvjQTftviQA1B/btvy1QHnxvQ=
dependencies:
websocket-driver ">=0.5.1"

faye-websocket@^0.11.3, faye-websocket@~0.11.1:
faye-websocket@^0.11.3:
version "0.11.3"
resolved "https://registry.yarnpkg.com/faye-websocket/-/faye-websocket-0.11.3.tgz#5c0e9a8968e8912c286639fde977a8b209f2508e"
integrity sha512-D2y4bovYpzziGgbHYtGCMjlJM36vAl/y+xUyn1C+FVx8szd1E+86KwVw6XvYSzOP8iMpm1X0I4xJD+QtUb36OA==
Expand Down Expand Up @@ -10287,7 +10280,7 @@ inherits@2.0.1:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.1.tgz#b17d08d326b4423e568eff719f91b0b1cbdf69f1"
integrity sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE=

inherits@2.0.4, inherits@^2.0.4:
inherits@2.0.4, inherits@^2.0.0, inherits@^2.0.4:
version "2.0.4"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
Expand Down Expand Up @@ -11622,6 +11615,11 @@ jsbn@~0.1.0:
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
integrity sha1-peZUwuWi3rXyAdls77yoDA7y9RM=

jsdom-global@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/jsdom-global/-/jsdom-global-3.0.2.tgz#6bd299c13b0c4626b2da2c0393cd4385d606acb9"
integrity sha1-a9KZwTsMRiay2iwDk81DhdYGrLk=

jsdom@^15.1.1:
version "15.2.1"
resolved "https://registry.yarnpkg.com/jsdom/-/jsdom-15.2.1.tgz#d2feb1aef7183f86be521b8c6833ff5296d07ec5"
Expand Down Expand Up @@ -11731,7 +11729,7 @@ json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1:
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus=

json3@^3.3.2, json3@^3.3.3:
json3@^3.3.3:
version "3.3.3"
resolved "https://registry.yarnpkg.com/json3/-/json3-3.3.3.tgz#7fc10e375fc5ae42c4705a5cc0aa6f62be305b81"
integrity sha512-c7/8mbUsKigAbLkD5B010BK4D9LZm7A1pNItkEwiUZRpIN66exu/e7YQWysGun+TRKaJp8MhemM+VkfWv42aCA==
Expand Down Expand Up @@ -13004,11 +13002,6 @@ node-fetch@^2.6.0, node-fetch@^2.6.1:
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==

node-forge@0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.9.0.tgz#d624050edbb44874adca12bb9a52ec63cb782579"
integrity sha512-7ASaDa3pD+lJ3WvXFsxekJQelBKRpne+GOVbLbtHYdd7pFspyeuJHnWfLplGf3SwKGbfs/aYl5V/JCIaHVUKKQ==

node-forge@^0.10.0:
version "0.10.0"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.10.0.tgz#32dea2afb3e9926f02ee5ce8794902691a676bf3"
Expand Down Expand Up @@ -15706,13 +15699,6 @@ select@^1.1.2:
resolved "https://registry.yarnpkg.com/select/-/select-1.1.2.tgz#0e7350acdec80b1108528786ec1d4418d11b396d"
integrity sha1-DnNQrN7ICxEIUoeG7B1EGNEbOW0=

selfsigned@^1.10.7:
version "1.10.7"
resolved "https://registry.yarnpkg.com/selfsigned/-/selfsigned-1.10.7.tgz#da5819fd049d5574f28e88a9bcc6dbc6e6f3906b"
integrity sha512-8M3wBCzeWIJnQfl43IKwOmC4H/RAp50S8DF60znzjW5GVqTcSe2vWclt7hmYVPkKPlHWOu5EaWOMZ2Y6W8ZXTA==
dependencies:
node-forge "0.9.0"

selfsigned@^1.10.8:
version "1.10.8"
resolved "https://registry.yarnpkg.com/selfsigned/-/selfsigned-1.10.8.tgz#0d17208b7d12c33f8eac85c41835f27fc3d81a30"
Expand Down Expand Up @@ -16043,18 +16029,6 @@ snapdragon@^0.8.1:
source-map-resolve "^0.5.0"
use "^3.1.0"

sockjs-client@1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.4.0.tgz#c9f2568e19c8fd8173b4997ea3420e0bb306c7d5"
integrity sha512-5zaLyO8/nri5cua0VtOrFXBPK1jbL4+1cebT/mmKA1E1ZXOvJrII75bPu0l0k843G/+iAbhEqzyKr0w/eCCj7g==
dependencies:
debug "^3.2.5"
eventsource "^1.0.7"
faye-websocket "~0.11.1"
inherits "^2.0.3"
json3 "^3.3.2"
url-parse "^1.4.3"

sockjs-client@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/sockjs-client/-/sockjs-client-1.5.0.tgz#2f8ff5d4b659e0d092f7aba0b7c386bd2aa20add"
Expand All @@ -16067,15 +16041,6 @@ sockjs-client@^1.5.0:
json3 "^3.3.3"
url-parse "^1.4.7"

sockjs@0.3.20:
version "0.3.20"
resolved "https://registry.yarnpkg.com/sockjs/-/sockjs-0.3.20.tgz#b26a283ec562ef8b2687b44033a4eeceac75d855"
integrity sha512-SpmVOVpdq0DJc0qArhF3E5xsxvaiqGNb73XfgBpK1y3UD5gs8DSo8aCTsuT5pX8rssdc2NDIzANwP9eCAiSdTA==
dependencies:
faye-websocket "^0.10.0"
uuid "^3.4.0"
websocket-driver "0.6.5"

sockjs@^0.3.21:
version "0.3.21"
resolved "https://registry.yarnpkg.com/sockjs/-/sockjs-0.3.21.tgz#b34ffb98e796930b60a0cfa11904d6a339a7d417"
Expand Down Expand Up @@ -17498,7 +17463,7 @@ url-loader@^4.0.0:
mime-types "^2.1.27"
schema-utils "^3.0.0"

url-parse@^1.4.3, url-parse@^1.4.4:
url-parse@^1.4.3, url-parse@^1.4.4, url-parse@^1.4.7:
version "1.4.7"
resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.4.7.tgz#a8a83535e8c00a316e403a5db4ac1b9b853ae278"
integrity sha512-d3uaVyzDB9tQoSXFvuSUNFibTd9zxd2bkVrDRvF5TmvWWQwqE4lgYJ5m+x1DbecWkw+LK4RNl2CU1hHuOKPVlg==
Expand Down Expand Up @@ -17816,10 +17781,10 @@ webpack-dev-middleware@^3.7.2:
range-parser "^1.2.1"
webpack-log "^2.0.0"

webpack-dev-server@^3.11.0:
version "3.11.0"
resolved "https://registry.yarnpkg.com/webpack-dev-server/-/webpack-dev-server-3.11.0.tgz#8f154a3bce1bcfd1cc618ef4e703278855e7ff8c"
integrity sha512-PUxZ+oSTxogFQgkTtFndEtJIPNmml7ExwufBZ9L2/Xyyd5PnOL5UreWe5ZT7IU25DSdykL9p1MLQzmLh2ljSeg==
webpack-dev-server@3.11.2, webpack-dev-server@^3.11.2:
version "3.11.2"
resolved "https://registry.yarnpkg.com/webpack-dev-server/-/webpack-dev-server-3.11.2.tgz#695ebced76a4929f0d5de7fd73fafe185fe33708"
integrity sha512-A80BkuHRQfCiNtGBS1EMf2ChTUs0x+B3wGDFmOeT4rmJOHhHTCH2naNxIHhmkr0/UillP4U3yeIyv1pNp+QDLQ==
dependencies:
ansi-html "0.0.7"
bonjour "^3.5.0"
Expand All @@ -17841,11 +17806,11 @@ webpack-dev-server@^3.11.0:
p-retry "^3.0.1"
portfinder "^1.0.26"
schema-utils "^1.0.0"
selfsigned "^1.10.7"
selfsigned "^1.10.8"
semver "^6.3.0"
serve-index "^1.9.1"
sockjs "0.3.20"
sockjs-client "1.4.0"
sockjs "^0.3.21"
sockjs-client "^1.5.0"
spdy "^4.0.2"
strip-ansi "^3.0.1"
supports-color "^6.1.0"
Expand All @@ -17869,44 +17834,6 @@ webpack-hot-middleware@^2.25.0:
html-entities "^1.2.0"
querystring "^0.2.0"
strip-ansi "^3.0.0"
webpack-dev-server@^3.11.2:
version "3.11.2"
resolved "https://registry.yarnpkg.com/webpack-dev-server/-/webpack-dev-server-3.11.2.tgz#695ebced76a4929f0d5de7fd73fafe185fe33708"
integrity sha512-A80BkuHRQfCiNtGBS1EMf2ChTUs0x+B3wGDFmOeT4rmJOHhHTCH2naNxIHhmkr0/UillP4U3yeIyv1pNp+QDLQ==
dependencies:
ansi-html "0.0.7"
bonjour "^3.5.0"
chokidar "^2.1.8"
compression "^1.7.4"
connect-history-api-fallback "^1.6.0"
debug "^4.1.1"
del "^4.1.1"
express "^4.17.1"
html-entities "^1.3.1"
http-proxy-middleware "0.19.1"
import-local "^2.0.0"
internal-ip "^4.3.0"
ip "^1.1.5"
is-absolute-url "^3.0.3"
killable "^1.0.1"
loglevel "^1.6.8"
opn "^5.5.0"
p-retry "^3.0.1"
portfinder "^1.0.26"
schema-utils "^1.0.0"
selfsigned "^1.10.8"
semver "^6.3.0"
serve-index "^1.9.1"
sockjs "^0.3.21"
sockjs-client "^1.5.0"
spdy "^4.0.2"
strip-ansi "^3.0.1"
supports-color "^6.1.0"
url "^0.11.0"
webpack-dev-middleware "^3.7.2"
webpack-log "^2.0.0"
ws "^6.2.1"
yargs "^13.3.2"

webpack-log@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -17968,13 +17895,6 @@ webpack@^4.44.2:
watchpack "^1.7.4"
webpack-sources "^1.4.1"

websocket-driver@0.6.5:
version "0.6.5"
resolved "https://registry.yarnpkg.com/websocket-driver/-/websocket-driver-0.6.5.tgz#5cb2556ceb85f4373c6d8238aa691c8454e13a36"
integrity sha1-XLJVbOuF9Dc8bYI4qmkchFThOjY=
dependencies:
websocket-extensions ">=0.1.1"

websocket-driver@>=0.5.1:
version "0.7.3"
resolved "https://registry.yarnpkg.com/websocket-driver/-/websocket-driver-0.7.3.tgz#a2d4e0d4f4f116f1e6297eba58b05d430100e9f9"
Expand Down

0 comments on commit 9498c6f

Please sign in to comment.