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

ESM: Start migration: use ESM in node #28797

Closed
wants to merge 54 commits into from
Closed
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
d07d1dc
use ESM more
ndelangen Aug 3, 2024
30e5e18
add logging
ndelangen Aug 3, 2024
10eaae4
fix import path
ndelangen Aug 3, 2024
b492e05
fix paths
ndelangen Aug 3, 2024
38c1327
migrate to fs-extra in proper ESM form
ndelangen Aug 3, 2024
811d931
lint
ndelangen Aug 3, 2024
4785866
fixes
ndelangen Aug 3, 2024
4f1f0e0
make compile work
ndelangen Aug 3, 2024
c56f587
fixing tests
ndelangen Aug 3, 2024
a55f577
fix missing cjs
ndelangen Aug 3, 2024
b5a7ef5
fixes
ndelangen Aug 3, 2024
ca08104
Discard changes to code/.storybook/main.ts
ndelangen Aug 3, 2024
f0c912d
Discard changes to code/.storybook/preview.ts
ndelangen Aug 3, 2024
2db3945
cleanup
ndelangen Aug 3, 2024
52329e8
make builders use ESM if possible
ndelangen Aug 3, 2024
d070d77
avoid instanceOf checks
ndelangen Aug 3, 2024
e45518f
cleanup
ndelangen Aug 3, 2024
b5d678e
vite builder ESM only
ndelangen Aug 3, 2024
b0a1fc4
fix nextjs by bundling better
ndelangen Aug 3, 2024
27b0b02
fix sveltekit
ndelangen Aug 3, 2024
557986e
async loading of vite builder to get ESM
ndelangen Aug 3, 2024
a39535c
fix sveltekit
ndelangen Aug 4, 2024
9791786
Merge branch 'norbert/use-node-20' into norbert/esm-v2
ndelangen Aug 7, 2024
2c11c19
Merge branch 'norbert/use-node-20' into norbert/esm-v2
ndelangen Aug 8, 2024
d1e29dd
fixes
ndelangen Aug 8, 2024
0f6ac5e
upgrade from canary
ndelangen Aug 8, 2024
004e77a
remove added fs-extra uses
ndelangen Aug 8, 2024
38acaa0
Merge branch 'norbert/use-node-20' into norbert/esm-v2
ndelangen Aug 8, 2024
80b3e55
Merge branch 'next' into norbert/esm-v2
ndelangen Aug 8, 2024
1a503fe
Merge branch 'next' into norbert/esm-v2
ndelangen Aug 9, 2024
77f58d1
fixes
ndelangen Aug 10, 2024
c9f91db
fixes
ndelangen Aug 10, 2024
c1df1b7
Merge branch 'next' into norbert/esm-v2
ndelangen Aug 10, 2024
2023a83
fixes
ndelangen Aug 10, 2024
b40a2e6
fixes
ndelangen Aug 10, 2024
dd3eaa8
cleanup
ndelangen Aug 10, 2024
4bb30ee
cleanup
ndelangen Aug 10, 2024
16960c0
cleanup
ndelangen Aug 10, 2024
17bd702
make `builder-vite` type=module
ndelangen Aug 11, 2024
c28548e
Merge branch 'next' into norbert/esm-v2
ndelangen Aug 28, 2024
cdd6df0
fix entries types
ndelangen Aug 28, 2024
d5a5707
remove unused ts-expect-error
ndelangen Aug 28, 2024
7088f4f
fix
ndelangen Aug 28, 2024
ba80b2a
fix linting
ndelangen Aug 28, 2024
1c99dca
fix linting
ndelangen Aug 28, 2024
f7f3712
fix fs import
ndelangen Aug 28, 2024
13ad6ce
add more mocks
ndelangen Aug 28, 2024
2c38805
no platform node for nextjs-vite framework
ndelangen Aug 28, 2024
3ba8334
cleanup
ndelangen Aug 28, 2024
e221ee7
Merge branch 'next' into norbert/esm-v2
ndelangen Aug 29, 2024
d8f7f04
Merge branch 'norbert/esm-v2' of https://github.com/storybookjs/story…
ndelangen Aug 29, 2024
76d9997
Merge branch 'next' into norbert/esm-v2
ndelangen Aug 29, 2024
7ca4859
fix
ndelangen Aug 29, 2024
48954f0
bundle in ESM of fs-extra
ndelangen Aug 29, 2024
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
6 changes: 6 additions & 0 deletions code/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ module.exports = {
rules: {
'import/no-unresolved': 'off', // covered by typescript
'eslint-comments/disable-enable-pair': ['error', { allowWholeFile: true }],
'import/no-unresolved': 'off',
ndelangen marked this conversation as resolved.
Show resolved Hide resolved
'eslint-comments/no-unused-disable': 'error',
'react-hooks/rules-of-hooks': 'off',
'import/extensions': 'off', // for mjs, we sometimes need extensions
'jsx-a11y/control-has-associated-label': 'off',
'import/no-extraneous-dependencies': [
'error',
{ devDependencies: true, peerDependencies: true },
],

'@typescript-eslint/dot-notation': [
'error',
{
Expand Down
2 changes: 2 additions & 0 deletions code/addons/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,12 @@
"dependencies": {
"@babel/core": "^7.24.4",
"@mdx-js/react": "^3.0.0",
"@ndelangen/fs-extra-unified": "^1.0.4",
"@storybook/blocks": "workspace:*",
"@storybook/csf-plugin": "workspace:*",
"@storybook/global": "^5.0.0",
"@storybook/react-dom-shim": "workspace:*",
"@types/fs-extra": "^11.0.4",
"@types/react": "^16.8.0 || ^17.0.0 || ^18.0.0",
"fs-extra": "^11.1.0",
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
Expand Down
1 change: 1 addition & 0 deletions code/addons/essentials/src/actions/manager.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// @ts-expect-error (no types)
export * from '@storybook/addon-actions/manager';
1 change: 0 additions & 1 deletion code/addons/essentials/src/backgrounds/preview.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// @ts-expect-error (no types needed for this)
export * from '@storybook/addon-backgrounds/preview';
1 change: 0 additions & 1 deletion code/addons/essentials/src/highlight/preview.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// @ts-expect-error (no types needed for this)
export * from '@storybook/addon-highlight/preview';
1 change: 0 additions & 1 deletion code/addons/essentials/src/outline/preview.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// @ts-expect-error (no types needed for this)
export * from '@storybook/addon-outline/preview';
1 change: 0 additions & 1 deletion code/addons/essentials/src/viewport/preview.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
// @ts-expect-error (no types needed for this)
export * from '@storybook/addon-viewport/preview';
2 changes: 2 additions & 0 deletions code/addons/links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
"ts-dedent": "^2.0.0"
},
"devDependencies": {
"@ndelangen/fs-extra-unified": "^1.0.4",
"@types/fs-extra": "^11.0.4",
"fs-extra": "^11.1.0",
"typescript": "^5.3.2"
},
Expand Down
2 changes: 1 addition & 1 deletion code/addons/links/scripts/fix-preview-api-reference.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readFile, writeFile } from 'fs-extra';
import { readFile, writeFile } from 'node:fs/promises';

/* I wish this wasn't needed..
* There seems to be some bug in tsup / the unlaying lib that does DTS bundling
Expand Down
11 changes: 7 additions & 4 deletions code/builders/builder-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
"exports": {
".": {
"types": "./dist/index.d.ts",
"node": "./dist/index.js",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
"import": "./dist/index.mjs"
},
"./input/iframe.html": "./input/iframe.html",
"./package.json": "./package.json"
},
"main": "dist/index.js",
"main": "dist/index.mjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
Expand All @@ -43,8 +41,10 @@
"prep": "jiti ../../../scripts/prepare/bundle.ts"
},
"dependencies": {
"@ndelangen/fs-extra-unified": "^1.0.4",
"@storybook/csf-plugin": "workspace:*",
"@types/find-cache-dir": "^3.2.1",
"@types/fs-extra": "^11.0.4",
"browser-assert": "^1.2.1",
"es-module-lexer": "^1.5.0",
"express": "^4.19.2",
Expand Down Expand Up @@ -86,6 +86,9 @@
"entries": [
"./src/index.ts"
],
"formats": [
"esm"
],
"platform": "node"
},
"gitHead": "e6a7fd8a655c69780bc20b9749c2699e44beae16"
Expand Down
12 changes: 6 additions & 6 deletions code/builders/builder-vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// noinspection JSUnusedGlobalSymbols

import * as fs from 'fs-extra';
import type { RequestHandler } from 'express';
import type { ViteDevServer } from 'vite';
import express from 'express';
import { join, parse } from 'path';
import { NoStatsForViteDevError } from 'storybook/internal/server-errors';
import type { Options } from 'storybook/internal/types';
import { corePath } from 'storybook/core-path';
import { transformIframeHtml } from './transform-iframe-html';
import { createViteServer } from './vite-server';
import { build as viteBuild } from './build';
import type { ViteBuilder } from './types';
import { readFile } from 'node:fs/promises';
import { copy } from '@ndelangen/fs-extra-unified';

export { withoutVitePlugins } from './utils/without-vite-plugins';
export { hasVitePlugins } from './utils/has-vite-plugins';
Expand All @@ -32,7 +32,7 @@ function iframeMiddleware(options: Options, server: ViteDevServer): RequestHandl
return;
}

const indexHtml = await fs.readFile(
const indexHtml = await readFile(
require.resolve('@storybook/builder-vite/input/iframe.html'),
'utf-8'
);
Expand All @@ -57,7 +57,7 @@ export const start: ViteBuilder['start'] = async ({
}) => {
server = await createViteServer(options as Options, devServer);

const previewResolvedDir = join(corePath, 'dist/preview');
const previewResolvedDir = join((await import('storybook/core-path')).corePath, 'dist/preview');
const previewDirOrigin = previewResolvedDir;

router.use(`/sb-preview`, express.static(previewDirOrigin, { immutable: true, maxAge: '5m' }));
Expand All @@ -79,11 +79,11 @@ export const start: ViteBuilder['start'] = async ({
export const build: ViteBuilder['build'] = async ({ options }) => {
const viteCompilation = viteBuild(options as Options);

const previewResolvedDir = join(corePath, 'dist/preview');
const previewResolvedDir = join((await import('storybook/core-path')).corePath, 'dist/preview');
const previewDirOrigin = previewResolvedDir;
const previewDirTarget = join(options.outputDir || '', `sb-preview`);

const previewFiles = fs.copy(previewDirOrigin, previewDirTarget, {
const previewFiles = copy(previewDirOrigin, previewDirTarget, {
filter: (src) => {
const { ext } = parse(src);
if (ext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { join } from 'node:path';
import findCacheDirectory from 'find-cache-dir';
import { init, parse } from 'es-module-lexer';
import MagicString from 'magic-string';
import { ensureFile, writeFile } from 'fs-extra';
import { ensureFile } from '@ndelangen/fs-extra-unified';
import type { Alias, Plugin } from 'vite';
import { writeFile } from 'node:fs/promises';

const escapeKeys = (key: string) => key.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
const defaultImportRegExp = 'import ([^*{}]+) from';
Expand Down
2 changes: 2 additions & 0 deletions code/builders/builder-webpack5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
"prep": "jiti ../../../scripts/prepare/bundle.ts"
},
"dependencies": {
"@ndelangen/fs-extra-unified": "^1.0.4",
"@storybook/core-webpack": "workspace:*",
"@types/fs-extra": "^11.0.4",
"@types/node": "^22.0.0",
"@types/semver": "^7.3.4",
"browser-assert": "^1.2.1",
Expand Down
9 changes: 4 additions & 5 deletions code/builders/builder-webpack5/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import webpackDevMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import { logger } from 'storybook/internal/node-logger';
import type { Builder, Options } from 'storybook/internal/types';
import { corePath } from 'storybook/core-path';
import { checkWebpackVersion } from '@storybook/core-webpack';
import { join, parse } from 'path';
import express from 'express';
import fs from 'fs-extra';
import { copy } from '@ndelangen/fs-extra-unified';
import { PREVIEW_BUILDER_PROGRESS } from 'storybook/internal/core-events';
import {
WebpackCompilationError,
Expand Down Expand Up @@ -177,7 +176,7 @@ const starter: StarterFunction = async function* starterGeneratorFn({

compilation = webpackDevMiddleware(compiler, middlewareOptions);

const previewResolvedDir = join(corePath, 'dist/preview');
const previewResolvedDir = join((await import('storybook/core-path')).corePath, 'dist/preview');
const previewDirOrigin = previewResolvedDir;

router.use(`/sb-preview`, express.static(previewDirOrigin, { immutable: true, maxAge: '5m' }));
Expand Down Expand Up @@ -286,11 +285,11 @@ const builder: BuilderFunction = async function* builderGeneratorFn({ startTime,
});
});

const previewResolvedDir = join(corePath, 'dist/preview');
const previewResolvedDir = join((await import('storybook/core-path')).corePath, 'dist/preview');
const previewDirOrigin = previewResolvedDir;
const previewDirTarget = join(options.outputDir || '', `sb-preview`);

const previewFiles = fs.copy(previewDirOrigin, previewDirTarget, {
const previewFiles = copy(previewDirOrigin, previewDirTarget, {
filter: (src) => {
const { ext } = parse(src);
if (ext) {
Expand Down
2 changes: 2 additions & 0 deletions code/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@
"@emotion/styled": "^11.11.0",
"@emotion/use-insertion-effect-with-fallbacks": "^1.0.1",
"@fal-works/esbuild-plugin-global-externals": "^2.1.2",
"@ndelangen/fs-extra-unified": "^1.0.4",
"@ndelangen/get-tarball": "^3.0.7",
"@popperjs/core": "^2.6.0",
"@radix-ui/react-dialog": "^1.0.5",
Expand Down Expand Up @@ -396,6 +397,7 @@
"react-transition-group": "^4.4.5",
"require-from-string": "^2.0.2",
"resolve-from": "^5.0.0",
"resolve.exports": "^2.0.2",
"slash": "^5.0.0",
"store2": "^2.14.2",
"strip-json-comments": "^5.0.1",
Expand Down
2 changes: 1 addition & 1 deletion code/core/scripts/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defineEntry } from '../../../scripts/prepare/tools';
export const getEntries = (cwd: string) => {
const define = defineEntry(cwd);
return [
// empty, right now, TDB what to do with this
// empty, right now, TBD what to do with this
define('src/index.ts', ['node', 'browser'], true),

define('src/node-logger/index.ts', ['node'], true),
Expand Down
2 changes: 1 addition & 1 deletion code/core/scripts/helpers/dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readJson } from 'fs-extra';
import { readJson } from '@ndelangen/fs-extra-unified';
import { join } from 'node:path';

export async function flattenDependencies(
Expand Down
2 changes: 1 addition & 1 deletion code/core/scripts/helpers/generatePackageJsonFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { join, relative } from 'node:path';
import slash from 'slash';
import { sortPackageJson } from '../../../../scripts/prepare/tools';
import type { getEntries } from '../entries';
import { readJSON } from 'fs-extra';
import { readJSON } from '@ndelangen/fs-extra-unified';
import { writeFile } from 'node:fs/promises';

const cwd = process.cwd();
Expand Down
2 changes: 1 addition & 1 deletion code/core/scripts/helpers/generateTypesMapperFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { getEntries } from '../entries';
import { join, relative } from 'node:path';
import { dedent } from '../../../../scripts/prepare/tools';
import { writeFile } from 'node:fs/promises';
import { ensureFile } from 'fs-extra';
import { ensureFile } from '@ndelangen/fs-extra-unified';

const cwd = process.cwd();

Expand Down
9 changes: 5 additions & 4 deletions code/core/src/builder-manager/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dirname, join, parse } from 'node:path';
import fs from 'fs-extra';
import { copy, remove } from '@ndelangen/fs-extra-unified';
import express from 'express';

import { logger } from '@storybook/core/node-logger';
Expand All @@ -25,6 +25,7 @@ import { getData } from './utils/data';
import { safeResolve } from './utils/safeResolve';
import { readOrderedFiles } from './utils/files';
import { buildFrameworkGlobalsFromOptions } from './utils/framework';
import { writeFile } from 'node:fs/promises';

let compilation: Compilation;
let asyncIterator: ReturnType<StarterFunction> | ReturnType<BuilderFunction>;
Expand Down Expand Up @@ -149,7 +150,7 @@ const starter: StarterFunction = async function* starterGeneratorFn({
// make sure we clear output directory of addons dir before starting
// this could cause caching issues where addons are loaded when they shouldn't
const addonsDir = config.outdir;
await fs.remove(addonsDir);
await remove(addonsDir);

yield;

Expand Down Expand Up @@ -256,7 +257,7 @@ const builder: BuilderFunction = async function* builderGeneratorFn({ startTime,

yield;

const managerFiles = fs.copy(coreDirOrigin, coreDirTarget, {
const managerFiles = copy(coreDirOrigin, coreDirTarget, {
filter: (src) => {
const { ext } = parse(src);
if (ext) {
Expand Down Expand Up @@ -290,7 +291,7 @@ const builder: BuilderFunction = async function* builderGeneratorFn({ startTime,

await Promise.all([
//
fs.writeFile(join(options.outputDir, 'index.html'), html),
writeFile(join(options.outputDir, 'index.html'), html),
managerFiles,
]);

Expand Down
7 changes: 4 additions & 3 deletions code/core/src/builder-manager/utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { OutputFile } from 'esbuild';
import fs from 'fs-extra';
import { ensureFile } from '@ndelangen/fs-extra-unified';
import { join, normalize } from 'node:path';
import slash from 'slash';
import type { Compilation } from '../types';
import { writeFile } from 'node:fs/promises';

export async function readOrderedFiles(
addonsDir: string,
Expand All @@ -13,8 +14,8 @@ export async function readOrderedFiles(
// convert deeply nested paths to a single level, also remove special characters
const { location, url } = sanitizePath(file, addonsDir);

await fs.ensureFile(location);
await fs.writeFile(location, file.contents);
await ensureFile(location);
await writeFile(location, file.contents);
return url;
}) || []
);
Expand Down
7 changes: 4 additions & 3 deletions code/core/src/builder-manager/utils/managerEntries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from 'fs-extra';
import { ensureFile } from '@ndelangen/fs-extra-unified';
import { resolvePathInStorybookCache } from '@storybook/core/common';
import { writeFile } from 'node:fs/promises';
import { join, parse, relative, sep } from 'node:path';
import slash from 'slash';

Expand Down Expand Up @@ -50,8 +51,8 @@ export async function wrapManagerEntries(entrypoints: string[], uniqueId?: strin
sanitizeFinal(join(`${sanitizeBase(base)}-${i}`, `${sanitizeBase(name)}-bundle.js`))
);

await fs.ensureFile(location);
await fs.writeFile(location, `import '${slash(entry)}';`);
await ensureFile(location);
await writeFile(location, `import '${slash(entry)}';`);

return location;
})
Expand Down
4 changes: 2 additions & 2 deletions code/core/src/builder-manager/utils/template.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { dirname, join } from 'node:path';
import fs from 'fs-extra';

import { render } from 'ejs';

import type { DocsOptions, TagsOptions, Options, Ref } from '@storybook/core/types';
import { readFile } from 'node:fs/promises';

export const getTemplatePath = async (template: string) => {
return join(dirname(require.resolve('@storybook/core/package.json')), 'assets/server', template);
Expand All @@ -12,7 +12,7 @@ export const getTemplatePath = async (template: string) => {
export const readTemplate = async (template: string) => {
const path = await getTemplatePath(template);

return fs.readFile(path, 'utf8');
return readFile(path, 'utf8');
};

export async function getManagerMainTemplate() {
Expand Down
2 changes: 1 addition & 1 deletion code/core/src/cli/detect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ vi.mock('fs', () => ({
default: vi.fn(),
}));

vi.mock('fs-extra', () => ({
vi.mock('@ndelangen/fs-extra-unified', () => ({
pathExistsSync: vi.fn(() => true),
}));

Expand Down
3 changes: 2 additions & 1 deletion code/core/src/cli/eslintPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fse, { readFile, readJson, writeJson } from 'fs-extra';
import { readJson, writeJson } from '@ndelangen/fs-extra-unified';
import { dedent } from 'ts-dedent';
import detectIndent from 'detect-indent';
import prompts from 'prompts';
Expand All @@ -8,6 +8,7 @@ import { readConfig, writeConfig } from '@storybook/core/csf-tools';
import type { JsPackageManager } from '@storybook/core/common';
import { paddedLog } from '@storybook/core/common';
import fs from 'node:fs';
import { readFile } from 'node:fs/promises';

export const SUPPORTED_ESLINT_EXTENSIONS = ['js', 'cjs', 'json'];
const UNSUPPORTED_ESLINT_EXTENSIONS = ['yaml', 'yml'];
Expand Down
Loading