Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make managerEntries be loaded as ESM, for improved tree-shaking #20070

Merged
merged 5 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion code/lib/builder-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const getConfig: ManagerBuilder['getConfig'] = async (options) => {
outdir: join(options.outputDir || './', 'sb-addons'),
format: 'esm',
write: false,
resolveExtensions: ['.ts', '.tsx', '.mjs', '.js', '.jsx'],
outExtension: { '.js': '.mjs' },
loader: {
'.js': 'jsx',
Expand All @@ -55,8 +56,9 @@ export const getConfig: ManagerBuilder['getConfig'] = async (options) => {
target: ['chrome100'],
platform: 'browser',
bundle: true,
minify: false,
minify: true,
sourcemap: true,
conditions: ['browser', 'module', 'default'],

jsxFactory: 'React.createElement',
jsxFragment: 'React.Fragment',
Expand Down
13 changes: 7 additions & 6 deletions code/lib/core-common/src/presets.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import './presets';

function wrapPreset(basePresets: any): { babel: Function; webpack: Function } {
Expand Down Expand Up @@ -409,15 +410,15 @@ describe('resolveAddonName', () => {
it('should resolve managerEntries', () => {
expect(resolveAddonName({}, '@storybook/addon-actions/register')).toEqual({
name: '@storybook/addon-actions/register',
managerEntries: ['@storybook/addon-actions/register'],
managerEntries: [path.normalize('@storybook/addon-actions/register')],
type: 'virtual',
});
});

it('should resolve managerEntries from new /manager path', () => {
expect(resolveAddonName({}, '@storybook/addon-actions/manager')).toEqual({
name: '@storybook/addon-actions/manager',
managerEntries: ['@storybook/addon-actions/manager'],
managerEntries: [path.normalize('@storybook/addon-actions/manager')],
type: 'virtual',
});
});
Expand Down Expand Up @@ -536,14 +537,14 @@ describe('loadPreset', () => {
name: '@storybook/addon-actions/register',
options: {},
preset: {
managerEntries: ['@storybook/addon-actions/register'],
managerEntries: [path.normalize('@storybook/addon-actions/register')],
},
},
{
name: 'addon-foo/register.js',
options: {},
preset: {
managerEntries: ['addon-foo/register.js'],
managerEntries: [path.normalize('addon-foo/register')],
},
},
{
Expand All @@ -565,14 +566,14 @@ describe('loadPreset', () => {
name: 'addon-baz/register.js',
options: {},
preset: {
managerEntries: ['addon-baz/register.js'],
managerEntries: [path.normalize('addon-baz/register')],
},
},
{
name: '@storybook/addon-notes/register-panel',
options: {},
preset: {
managerEntries: ['@storybook/addon-notes/register-panel'],
managerEntries: [path.normalize('@storybook/addon-notes/register-panel')],
},
},
{
Expand Down
24 changes: 19 additions & 5 deletions code/lib/core-common/src/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
PresetConfig,
Presets,
} from '@storybook/types';
import { join, parse } from 'path';
import { loadCustomPresets } from './utils/load-custom-presets';
import { safeResolve, safeResolveFrom } from './utils/safeResolve';
import { interopRequireDefault } from './utils/interpret-require';
Expand Down Expand Up @@ -66,14 +67,19 @@ export const resolveAddonName = (
const resolved = resolve(name);

if (resolved) {
if (name.match(/\/(manager|register(-panel)?)(\.(js|ts|tsx|jsx))?$/)) {
const { dir: fdir, name: fname } = parse(resolved);

if (name.match(/\/(manager|register(-panel)?)(\.(js|mjs|ts|tsx|jsx))?$/)) {
return {
type: 'virtual',
name,
managerEntries: [resolved],
// we remove the extension
// this is a bit of a hack to try en be able to find .mjs files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// this is a bit of a hack to try en be able to find .mjs files
// this is a bit of a hack to try to find .mjs files

// node can only ever resolve .js files; it does not look at the exports field in package.json
managerEntries: [join(fdir, fname)],
};
}
if (name.match(/\/(preset)(\.(js|ts|tsx|jsx))?$/)) {
if (name.match(/\/(preset)(\.(js|mjs|ts|tsx|jsx))?$/)) {
return {
type: 'presets',
name: resolved,
Expand Down Expand Up @@ -113,11 +119,19 @@ export const resolveAddonName = (
const managerEntries = [];

if (managerFile) {
managerEntries.push(managerFile);
// we remove the extension
// this is a bit of a hack to try en be able to find .mjs files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// this is a bit of a hack to try en be able to find .mjs files
// this is a bit of a hack to try to find .mjs files

// node can only ever resolve .js files; it does not look at the exports field in package.json
const { dir: fdir, name: fname } = parse(managerFile);
managerEntries.push(join(fdir, fname));
}
// register file is the old way of registering addons
if (!managerFile && registerFile && !presetFile) {
managerEntries.push(registerFile);
// we remove the extension
// this is a bit of a hack to try en be able to find .mjs files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// this is a bit of a hack to try en be able to find .mjs files
// this is a bit of a hack to try to find .mjs files

// node can only ever resolve .js files; it does not look at the exports field in package.json
const { dir: fdir, name: fname } = parse(registerFile);
managerEntries.push(join(fdir, fname));
}

return {
Expand Down