Skip to content

Commit

Permalink
Alias 'storycap' to '@remix/storycap' for Storybook v7+
Browse files Browse the repository at this point in the history
Storybook v7+ uses Webpack 5, which follows a different module
resolution strategy than Webpack 4. This is mostly irrelevant for this
fork of reg-viz/storycap, but it proves to be problematic in test.

The package generated from this fork goes by the name '@remix/storycap'
instead of 'storycap', and we made edits to the root `package.json` file
to reflect the name difference. Among other things, the test harness for
'storycap' copies the root package.json and the package's compiled JS
into the `node_modules` subdirectories under `examples/*`, simulating
the installation of 'storycap' via npm/yarn. Each 'storybook' version
(v5, v6, and v7) has at least one set of examples so that 'storycap' can
be tested against each one. For 'storybook' v5 and v6, the tests pass
successfully. However, for 'storybook' v7, the use of Webpack 5 and its
module resolution strategy causes module lookup issues for 'storycap';
the 'storycap' directory can be found under `node_modules`, but the
custom package name ('@remix/storycap') causes the module lookup to
ultimately fail.

For consistency, this commit simulates the installation of a
'@remix/storycap' package instead of a 'storycap' package, since this
is what we'd like to test anyways. Now, the name of the directory under
`node_modules` matches the declared package name in `package.json`. We
don't want to tinker with the test setup too much further, so instead of
replacing all references to 'storycap' with '@remix/storycap', we
instead update the Storybook Webpack config to alias 'storycap' to
'@remix/storycap', which works in all places but one.

This solution isn't ideal, but it prevents us from drifting further
from upstream, which is nice. At current date, there's only a 2 line
diff that forces us to keep this fork: exposing `setViewport` so that
the viewport can be set dynamically before the test is run. In the
future, we would love to get this change merged into upstream. In the
meantime, we try to keep the set of changes to this fork as minimal as
possible to make the eventual cleanup straightforward.
  • Loading branch information
throwandgo committed Oct 4, 2023
1 parent 270565d commit ecf696b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
12 changes: 12 additions & 0 deletions examples/v6-managed-react/.storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
module.exports = {
stories: ['../src/**/*.stories.@(js|jsx|mdx)'],
addons: ['@storybook/addon-actions', '@storybook/addon-links', 'storycap'],
webpackFinal: async (config) => {
return {
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
storycap: '@remix/storycap',
}
}
};
},
};
6 changes: 5 additions & 1 deletion examples/v6-managed-react/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { withScreenshot } from 'storycap';
// Annoyingly, this file is occasionally run without webpack (e.g. when
// calling `react-scripts test --setupFiles ./setupFile.js`), which
// prevents us from relying on our Webpack config to alias the
// 'storycap' module to our custom '@remix/storycap' module.
import { withScreenshot } from '@remix/storycap';

export const decorators = [
withScreenshot,
Expand Down
14 changes: 13 additions & 1 deletion examples/v7-managed-react/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ module.exports = {
name: '@storybook/react-webpack5',
options: {}
},
webpackFinal: async (config) => {
return {
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
storycap: '@remix/storycap',
}
}
};
},
docs: {
autodocs: true
}
};
};
6 changes: 5 additions & 1 deletion examples/v7-managed-react/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { withScreenshot } from 'storycap';
// Annoyingly, this file is occasionally run without webpack (e.g. when
// calling `react-scripts test --setupFiles ./setupFile.js`), which
// prevents us from relying on our Webpack config to alias the
// 'storycap' module to our custom '@remix/storycap' module.
import { withScreenshot } from '@remix/storycap';

export const decorators = [
withScreenshot,
Expand Down
14 changes: 13 additions & 1 deletion examples/v7-react18/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@ module.exports = {
name: '@storybook/react-webpack5',
options: {}
},
webpackFinal: async (config) => {
return {
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
storycap: '@remix/storycap',
}
}
};
},
docs: {
autodocs: true
}
};
};
6 changes: 5 additions & 1 deletion examples/v7-react18/.storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { withScreenshot } from 'storycap';
// Annoyingly, this file is occasionally run without webpack (e.g. when
// calling `react-scripts test --setupFiles ./setupFile.js`), which
// prevents us from relying on our Webpack config to alias the
// 'storycap' module to our custom '@remix/storycap' module.
import { withScreenshot } from '@remix/storycap';

export const decorators = [
withScreenshot,
Expand Down
12 changes: 6 additions & 6 deletions scripts/e2e-prestorybook.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ async function main() {
}
const prjDir = path.resolve(__dirname, '../packages/storycap');
const cwd = process.cwd();
const dist = path.resolve(cwd, target, 'node_modules/storycap');
const dist = path.resolve(cwd, target, 'node_modules/@remix/storycap');
if (prjDir === dist) {
console.error(`target dir shold not be "${prjDir}".`);
console.error(`target dir should not be "${prjDir}".`);
return 1;
}
rimraf.sync(dist);
mkdirp.sync(dist);
copyDir.sync(`${path.join(prjDir, 'lib')}`, path.join(dist, 'lib'), {});
await cpy(['package.json', 'register.js'], dist, { cwd: prjDir });
rimraf.sync(path.resolve(dist, '../.bin/storycap'));
mkdirp(path.resolve(dist, '../.bin'));
fs.symlinkSync(path.resolve(prjDir, 'lib/node/cli.js'), path.resolve(dist, '../.bin/storycap'));
fs.chmodSync(path.resolve(dist, '../.bin/storycap'), 0o775);
rimraf.sync(path.resolve(dist, '../../.bin/storycap'));
mkdirp(path.resolve(dist, '../../.bin'));
fs.symlinkSync(path.resolve(prjDir, 'lib/node/cli.js'), path.resolve(dist, '../../.bin/storycap'));
fs.chmodSync(path.resolve(dist, '../../.bin/storycap'), 0o775);
return 0;
}

Expand Down

0 comments on commit ecf696b

Please sign in to comment.