Skip to content

Commit

Permalink
chore(revert) "refactor(compiler): add a Result union type for polymo…
Browse files Browse the repository at this point in the history
…rphic returns (#4281)" (#4415)

This reverts commit 3811a4c.

This commit is being reverted due to an issue with running Stencil
locally. Starting with the commit to be reverted, this is reproducible
by:
```
npm run clean && /
npm ci && /
npm run build /
npm pack
```
then
```
cd /tmp && /
npm init stencil component happy-monday && /
npm i <TARBALL>
```
  • Loading branch information
rwaskiewicz committed May 22, 2023
1 parent 2d2193b commit feaae32
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 309 deletions.
2 changes: 0 additions & 2 deletions scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { mockDoc } from './bundles/mock-doc';
import { screenshot } from './bundles/screenshot';
import { sysNode, sysNodeExternalBundles } from './bundles/sys-node';
import { testing } from './bundles/testing';
import { utils } from './bundles/utils';
import { createLicense } from './license';
import { release } from './release';
import { validateBuild } from './test/validate-build';
Expand Down Expand Up @@ -67,7 +66,6 @@ export async function createBuild(opts: BuildOptions): Promise<readonly RollupOp
screenshot(opts),
testing(opts),
sysNode(opts),
utils(opts),
]);

return bundles.flat();
Expand Down
6 changes: 1 addition & 5 deletions scripts/bundles/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ async function copyStencilInternalDts(opts: BuildOptions, outputInternalDir: str
// @stencil/core/internal/stencil-private.d.ts
const privateDtsSrcPath = join(declarationsInputDir, 'stencil-private.d.ts');
const privateDtsDestPath = join(outputInternalDir, 'stencil-private.d.ts');
let privateDts = cleanDts(await fs.readFile(privateDtsSrcPath, 'utf8'));

// the private `.d.ts` imports the `Result` type from the `@utils` module, so
// we need to rewrite the path so it imports from the right relative path
privateDts = privateDts.replace('@utils', './utils');
const privateDts = cleanDts(await fs.readFile(privateDtsSrcPath, 'utf8'));
await fs.writeFile(privateDtsDestPath, privateDts);

// @stencil/core/internal/stencil-public.compiler.d.ts
Expand Down
62 changes: 0 additions & 62 deletions scripts/bundles/utils.ts

This file was deleted.

13 changes: 7 additions & 6 deletions src/cli/find-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildError, isString, normalizePath, result } from '@utils';
import { buildError, isString, normalizePath } from '@utils';

import type { CompilerSystem, Diagnostic } from '../declarations';

Expand All @@ -17,14 +17,15 @@ export type FindConfigOptions = {
export type FindConfigResults = {
configPath: string;
rootDir: string;
diagnostics: Diagnostic[];
};

/**
* Attempt to find a Stencil configuration file on the file system
* @param opts the options needed to find the configuration file
* @returns the results of attempting to find a configuration file on disk
*/
export const findConfig = async (opts: FindConfigOptions): Promise<result.Result<FindConfigResults, Diagnostic[]>> => {
export const findConfig = async (opts: FindConfigOptions): Promise<FindConfigResults> => {
const sys = opts.sys;
const cwd = sys.getCurrentDirectory();
const rootDir = normalizePath(cwd);
Expand All @@ -48,16 +49,16 @@ export const findConfig = async (opts: FindConfigOptions): Promise<result.Result
const results: FindConfigResults = {
configPath,
rootDir: normalizePath(cwd),
diagnostics: [],
};

const stat = await sys.stat(configPath);
if (stat.error) {
const diagnostics: Diagnostic[] = [];
const diagnostic = buildError(diagnostics);
const diagnostic = buildError(results.diagnostics);
diagnostic.absFilePath = configPath;
diagnostic.header = `Invalid config path`;
diagnostic.messageText = `Config path "${configPath}" not found`;
return result.err(diagnostics);
return results;
}

if (stat.isFile) {
Expand All @@ -76,5 +77,5 @@ export const findConfig = async (opts: FindConfigOptions): Promise<result.Result
}
}

return result.ok(results);
return results;
};
9 changes: 4 additions & 5 deletions src/cli/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasError, isFunction, result, shouldIgnoreError } from '@utils';
import { hasError, isFunction, shouldIgnoreError } from '@utils';

import { createLogger } from '../compiler/sys/logger/console-logger';
import type * as d from '../declarations';
Expand Down Expand Up @@ -47,8 +47,8 @@ export const run = async (init: d.CliInitOptions) => {
startupLog(logger, task);

const findConfigResults = await findConfig({ sys, configPath: flags.config });
if (findConfigResults.isErr) {
logger.printDiagnostics(findConfigResults.value);
if (hasError(findConfigResults.diagnostics)) {
logger.printDiagnostics(findConfigResults.diagnostics);
return sys.exit(1);
}

Expand All @@ -68,12 +68,11 @@ export const run = async (init: d.CliInitOptions) => {
return;
}

const foundConfig = result.unwrap(findConfigResults);
const validated = await coreCompiler.loadConfig({
config: {
flags,
},
configPath: foundConfig.configPath,
configPath: findConfigResults.configPath,
logger,
sys,
});
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/build/build-ctx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasError, hasWarning, result } from '@utils';
import { hasError, hasWarning } from '@utils';

import type * as d from '../../declarations';

Expand All @@ -18,7 +18,7 @@ export class BuildContext implements d.BuildCtx {
componentGraph = new Map<string, string[]>();
config: d.Config;
data: any = {};
buildStats?: result.Result<d.CompilerBuildStats, { diagnostics: d.Diagnostic[] }> = undefined;
buildStats?: d.CompilerBuildStats = undefined;
esmBrowserComponentBundle: d.BundleModule[];
esmComponentBundle: d.BundleModule[];
es5ComponentBundle: d.BundleModule[];
Expand Down
39 changes: 21 additions & 18 deletions src/compiler/build/build-stats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { byteSize, isOutputTargetStats, result, sortBy } from '@utils';
import { byteSize, isOutputTargetStats, sortBy } from '@utils';

import type * as d from '../../declarations';

Expand All @@ -11,15 +11,17 @@ import type * as d from '../../declarations';
export function generateBuildStats(
config: d.Config,
buildCtx: d.BuildCtx
): result.Result<d.CompilerBuildStats, { diagnostics: d.Diagnostic[] }> {
): d.CompilerBuildStats | { diagnostics: d.Diagnostic[] } {
// TODO(STENCIL-461): Investigate making this return only a single type
const buildResults = buildCtx.buildResults;

let jsonData: d.CompilerBuildStats | { diagnostics: d.Diagnostic[] };

try {
if (buildResults.hasError) {
return result.err({
jsonData = {
diagnostics: buildResults.diagnostics,
});
};
} else {
const stats: d.CompilerBuildStats = {
timestamp: buildResults.timestamp,
Expand Down Expand Up @@ -56,7 +58,8 @@ export function generateBuildStats(
rollupResults: buildCtx.rollupResults,
collections: getCollections(config, buildCtx),
};
return result.ok(stats);

jsonData = stats;
}
} catch (e: unknown) {
const diagnostic: d.Diagnostic = {
Expand All @@ -65,10 +68,12 @@ export function generateBuildStats(
messageText: `Generate Build Stats Error: ` + e,
type: `build`,
};
return result.err({
jsonData = {
diagnostics: [diagnostic],
});
};
}

return jsonData;
}

/**
Expand All @@ -79,21 +84,19 @@ export function generateBuildStats(
*/
export async function writeBuildStats(
config: d.Config,
data: result.Result<d.CompilerBuildStats, { diagnostics: d.Diagnostic[] }>
data: d.CompilerBuildStats | { diagnostics: d.Diagnostic[] }
): Promise<void> {
const statsTargets = config.outputTargets.filter(isOutputTargetStats);

await result.map(data, async (compilerBuildStats) => {
await Promise.all(
statsTargets.map(async (outputTarget) => {
const result = await config.sys.writeFile(outputTarget.file, JSON.stringify(compilerBuildStats, null, 2));
await Promise.all(
statsTargets.map(async (outputTarget) => {
const result = await config.sys.writeFile(outputTarget.file, JSON.stringify(data, null, 2));

if (result.error) {
config.logger.warn([`Stats failed to write file to ${outputTarget.file}`]);
}
})
);
});
if (result.error) {
config.logger.warn([`Stats failed to write file to ${outputTarget.file}`]);
}
})
);
}

function sanitizeBundlesForStats(bundleArray: ReadonlyArray<d.BundleModule>): ReadonlyArray<d.CompilerBuildStatBundle> {
Expand Down
17 changes: 8 additions & 9 deletions src/compiler/build/test/build-stats.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type * as d from '@stencil/core/declarations';
import { mockBuildCtx, mockCompilerCtx, mockConfig } from '@stencil/core/testing';
import { result } from '@utils';

import { generateBuildResults } from '../build-results';
import { generateBuildStats } from '../build-stats';
Expand All @@ -18,17 +17,17 @@ describe('generateBuildStats', () => {
it('should return a structured json object', async () => {
buildCtx.buildResults = generateBuildResults(config, compilerCtx, buildCtx);

const compilerBuildStats = result.unwrap(generateBuildStats(config, buildCtx));
const result = generateBuildStats(config, buildCtx) as d.CompilerBuildStats;

if (compilerBuildStats.hasOwnProperty('timestamp')) {
delete compilerBuildStats.timestamp;
if (result.hasOwnProperty('timestamp')) {
delete result.timestamp;
}

if (compilerBuildStats.hasOwnProperty('compiler') && compilerBuildStats.compiler.hasOwnProperty('version')) {
delete compilerBuildStats.compiler.version;
if (result.hasOwnProperty('compiler') && result.compiler.hasOwnProperty('version')) {
delete result.compiler.version;
}

expect(compilerBuildStats).toStrictEqual({
expect(result).toStrictEqual({
app: { bundles: 0, components: 0, entries: 0, fsNamespace: undefined, namespace: 'Testing', outputs: [] },
collections: [],
compiler: { name: 'in-memory' },
Expand Down Expand Up @@ -59,9 +58,9 @@ describe('generateBuildStats', () => {
lines: [],
};
buildCtx.buildResults.diagnostics = [diagnostic];
const diagnostics = result.unwrapErr(generateBuildStats(config, buildCtx));
const result = generateBuildStats(config, buildCtx);

expect(diagnostics).toStrictEqual({
expect(result).toStrictEqual({
diagnostics: [diagnostic],
});
});
Expand Down
4 changes: 1 addition & 3 deletions src/declarations/stencil-private.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { result } from '@utils';

import type { InMemoryFileSystem } from '../compiler/sys/in-memory-fs';
import type {
BuildEvents,
Expand Down Expand Up @@ -221,7 +219,7 @@ export interface UpdatedLazyBuildCtx {
export interface BuildCtx {
buildId: number;
buildResults: CompilerBuildResults;
buildStats?: result.Result<CompilerBuildStats, { diagnostics: Diagnostic[] }>;
buildStats?: CompilerBuildStats | { diagnostics: Diagnostic[] };
buildMessages: string[];
bundleBuildCount: number;
collections: Collection[];
Expand Down
1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export * from './message-utils';
export * from './normalize-path';
export * from './output-target';
export * from './query-nonce-meta-tag-content';
export * as result from './result';
export * from './sourcemaps';
export * from './url-paths';
export * from './util';
Expand Down
Loading

0 comments on commit feaae32

Please sign in to comment.