Skip to content

Commit

Permalink
Merge pull request #20002 from storybookjs/tech/gen-exports-file-sing…
Browse files Browse the repository at this point in the history
…le-pass

Build: generate the `exports.ts` file in a single pass without `exec`
  • Loading branch information
shilman authored Nov 29, 2022
2 parents 4ff17a7 + 8e24756 commit b36691a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
30 changes: 17 additions & 13 deletions code/ui/manager/scripts/generate-exports-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,39 @@
import fs from 'fs-extra';
import path from 'path';
import { dedent } from 'ts-dedent';
import { exec } from '../../../../scripts/utils/exec';
import { ESLint } from '../../../../scripts/node_modules/eslint';
import { values } from '../src/globals/runtime';

const removeDefault = (input: string) => input !== 'default';

const location = path.join(__dirname, '..', 'src', 'globals', 'exports.ts');

const run = async () => {
const { values } = await import('../src/globals/runtime');
const data = Object.entries(values).reduce<Record<string, string[]>>((acc, [key, value]) => {
acc[key] = Object.keys(value).filter(removeDefault);
return acc;
}, {});

console.log('Generating...');
await fs.ensureFile(location);
await fs.writeFile(
location,
dedent`
// this file is generated by generate-exports-file.ts
// this is done to prevent runtime dependencies from making it's way into the build/start script of the manager
// the manager builder needs to know which dependencies are 'globalized' in the ui
const readyToWrite = fs.ensureFile(location);
const text = dedent`
// this file is generated by generate-exports-file.ts
// this is done to prevent runtime dependencies from making it's way into the build/start script of the manager
// the manager builder needs to know which dependencies are 'globalized' in the ui
export default ${JSON.stringify(data, null, 2)} as const;`
);
export default ${JSON.stringify(data, null, 2)} as const;`;

console.log('Linting...');
await exec(`yarn lint:js:cmd --fix ${location}`, {
cwd: path.join(__dirname, '..', '..', '..'),

const eslint = new ESLint({
cwd: path.join(__dirname, '..'),
fix: true,
});
const output = await eslint.lintText(text, { filePath: location });

await readyToWrite;
await fs.writeFile(location, output[0].output);

console.log('Done!');
};

Expand Down
1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"enzyme-adapter-react-16": "^1.15.5",
"esbuild": "^0.14.48",
"esbuild-plugin-alias": "^0.2.1",
"esbuild-register": "^3.4.1",
"eslint": "^8.27.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-react": "^7.31.10",
Expand Down
10 changes: 6 additions & 4 deletions scripts/prepare/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ const run = async ({ cwd, flags }: { cwd: string; flags: string[] }) => {
bundler: { entries = [], untypedEntries = [], platform, pre, post },
} = await fs.readJson(join(cwd, 'package.json'));

const tsnodePath = join(__dirname, '..', 'node_modules', '.bin', 'ts-node');

if (pre) {
await exec(`${tsnodePath} ${pre}`, { cwd });
await exec(`node -r ${__dirname}/../node_modules/esbuild-register/register.js ${pre}`, { cwd });
}

const reset = hasFlag(flags, 'reset');
Expand Down Expand Up @@ -125,7 +123,11 @@ const run = async ({ cwd, flags }: { cwd: string; flags: string[] }) => {
]);

if (post) {
await exec(`${tsnodePath} ${post}`, { cwd }, { debug: true });
await exec(
`node -r ${__dirname}/../node_modules/esbuild-register/register.js ${post}`,
{ cwd },
{ debug: true }
);
}
};

Expand Down
12 changes: 12 additions & 0 deletions scripts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3270,6 +3270,7 @@ __metadata:
enzyme-adapter-react-16: ^1.15.5
esbuild: ^0.14.48
esbuild-plugin-alias: ^0.2.1
esbuild-register: ^3.4.1
eslint: ^8.27.0
eslint-plugin-import: ^2.26.0
eslint-plugin-react: ^7.31.10
Expand Down Expand Up @@ -7688,6 +7689,17 @@ __metadata:
languageName: node
linkType: hard

"esbuild-register@npm:^3.4.1":
version: 3.4.1
resolution: "esbuild-register@npm:3.4.1"
dependencies:
debug: ^4.3.4
peerDependencies:
esbuild: ">=0.12 <1"
checksum: 6364c96f83fc4720d907d6342605f5891986c2a30fbc0a249047195ea5b02c0f7e4e698aa262875b37088f44ca741331d35c026d2606778d114fe082d560957d
languageName: node
linkType: hard

"esbuild-sunos-64@npm:0.14.54":
version: 0.14.54
resolution: "esbuild-sunos-64@npm:0.14.54"
Expand Down

0 comments on commit b36691a

Please sign in to comment.