Skip to content

Commit

Permalink
logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mrm007 committed Jun 20, 2023
1 parent 4919143 commit 3719fad
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"resolve-from": "^5.0.0",
"rollup": "^3.21.0",
"rollup": "^3.25.1",
"rollup-plugin-dts": "^5.3.0",
"rollup-plugin-node-externals": "^5.1.2",
"semver": "^7.5.1",
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import assert from 'assert';
import { AsyncLocalStorage } from 'async_hooks';
import fs from 'fs';
import path from 'path';

Expand Down Expand Up @@ -127,9 +129,21 @@ export const getConfig = (inlineConfig?: PartialConfig): EnhancedConfig => {
resolveFromRoot,
) as EnhancedConfig['appShell'];

return {
const enhancedConfig = {
...config,
appShell,
resolveFromRoot,
};

context.enterWith(enhancedConfig);

return enhancedConfig;
};

export const context = new AsyncLocalStorage<EnhancedConfig>();

export const getConfigFromContext = () => {
const config = context.getStore();
assert(config, 'config not set in context');
return config;
};
5 changes: 3 additions & 2 deletions packages/core/src/entries/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type PartialConfig, getConfig } from '../config';
import { generateDevFiles } from '../utils/dev-entry-points';

export const dev = async (inlineConfig?: PartialConfig) => {
const config = getConfig(inlineConfig);
await generateDevFiles(config);
getConfig(inlineConfig);

await generateDevFiles();
};
11 changes: 8 additions & 3 deletions packages/core/src/package-utils/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const createBundle = async (

const formats = ['cjs', 'esm'] as const;

const getRelativePath = (id: string) => path.relative(config.root, id);
const getSrcPath = (id: string) =>
path.relative(`${config.root}/${srcDir}`, id);

Expand All @@ -49,7 +50,7 @@ export const createBundle = async (

// internal package resolved by plugins/vite/internal-package-resolution.ts
if (srcPath.startsWith('../')) {
logger.debug(`Internal package ${id}`);
logger.debug(`Internal package: ${id}`);
return;
}

Expand All @@ -59,15 +60,15 @@ export const createBundle = async (
return normalizePath(`${stylesDir}/${srcPath}`);
}
if (isVocabFile(moduleInfo.id)) {
logger.debug(`Vocab file ${id}`);
logger.debug(`Vocab file: ${getRelativePath(id)}`);
return normalizePath(srcPath);
}
if (
typeof packageJson.sideEffects !== 'undefined' &&
moduleHasSideEffects(srcPath, packageJson.sideEffects) &&
!moduleInfo.isEntry
) {
logger.debug(`Has side-effects ${id}`);
logger.debug(`Has side-effects: ${getRelativePath(id)}`);
return normalizePath(`${sideEffectsDir}/${srcPath}`);
}
},
Expand Down Expand Up @@ -100,6 +101,10 @@ export const createBundle = async (
moduleSideEffects: 'no-external',
},
output: formats.map((format) => createOutputOptionsForFormat(format)),
onLog(level, log, defaultHandler) {
if (log.code === 'EMPTY_BUNDLE') return false;
defaultHandler(level, log);
},
},
},
});
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/package-utils/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const createDtsBundle = async (
compilerOptions: config.dtsOptions as any,
}),
],
onLog(level, log, defaultHandler) {
if (log.code === 'OPTIMIZE_CHUNK_STATUS') return false;
if (log.code === 'EMPTY_BUNDLE') return false;
defaultHandler(level, log);
},
});

await bundle.write({
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/plugins/rollup/externals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function externals(config: EnhancedConfig, format?: Format): Plugin {
(typeof resolved === 'boolean' && !resolved) ||
(typeof resolved === 'object' && resolved?.external)
) {
logDebugOnce(`External dependency ${id}`);
logDebugOnce(`External dependency: ${id}`);

const { packageId, isSubpath } = parseImportSpecifier(id);
const packageJson = packagesById.get(packageId);
Expand Down
22 changes: 8 additions & 14 deletions packages/core/src/utils/dev-entry-points.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import assert from 'assert';
import { AsyncLocalStorage } from 'async_hooks';
import path from 'path';

import dedent from 'dedent';

import type { EnhancedConfig } from '../config';
import { getConfigFromContext } from '../config';
import { logger } from '../entries/logger';
import type { Format, PackageEntryPoint } from '../types';

Expand All @@ -19,14 +17,11 @@ import { writeIfRequired } from './files';
import { promiseMap } from './promise-map';
import { resolveFrom } from './resolve-from';

const configContext = new AsyncLocalStorage<EnhancedConfig>();

const getHookLoader = async (entry: PackageEntryPoint, format: Format) => {
const stringifyRelative = (p: string) =>
JSON.stringify(path.relative(entry.outputDir, p));

const config = configContext.getStore();
assert(config, 'config not set in context');
const config = getConfigFromContext();

// ! don't change this ! unbuild searches for the string and inserts its own shims
const rekwire = 'req' + 'uire';
Expand Down Expand Up @@ -73,10 +68,10 @@ async function writeFile(

logger.debug(dedent`
[writeFile ${format}]
entryPath: ${entry.entryPath}
outputDir: ${entry.outputDir}
outputPath: ${outputPath}
relativePath: ${relativePath}
entryPath: ${entry.entryPath}
outputDir: ${entry.outputDir}
outputPath: ${outputPath}
relativePath: ${relativePath}
`);

const contents = await getContents(entry, { relativePath });
Expand Down Expand Up @@ -138,11 +133,10 @@ const getDtsContents: GetContents = async (entry, { relativePath }) => {
return contentLines.join('\n');
};

export const generateDevFiles = async (config: EnhancedConfig) => {
export const generateDevFiles = async () => {
const config = getConfigFromContext();
const packages = await getPackages(config);

configContext.enterWith(config);

for (const pkg of packages.values()) {
const entryPaths = await getPackageEntryPoints(pkg.root);

Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/utils/entry-points.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { fs, vol } from 'memfs';
import { beforeEach, describe, expect, test, vi } from 'vitest';

import { context } from '../config';

import {
cleanPackageEntryPoints,
createEntryPackageJsons,
Expand All @@ -25,6 +27,10 @@ describe('getPackageEntryPoints', () => {
},
packageRoot,
);

context.enterWith({
root: packageRoot,
} as any);
});

test('all entry points', async () => {
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/utils/entry-points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import dedent from 'dedent';
import glob from 'fast-glob';
import { findExportNames } from 'mlly';

import type { EnhancedConfig } from '../config';
import { getConfigFromContext, type EnhancedConfig } from '../config';
import { distDir } from '../constants';
import { logger } from '../entries/logger';
import type { Format, PackageEntryPoint } from '../types';
Expand All @@ -26,12 +26,14 @@ export interface Package {
export type Packages = Map<string, Package>;

export const getExports = async (filePath: string) => {
const config = getConfigFromContext();

const fileContents = await fs.promises.readFile(filePath, 'utf-8');
const exports = findExportNames(fileContents);
logger.debug(dedent`
[getExports]
filePath: ${filePath}
exports: ${exports}
filePath: ${path.relative(config.root, filePath)}
exports: ${exports}
`);
return exports;
};
Expand Down

0 comments on commit 3719fad

Please sign in to comment.