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

test(mock-compiler): make testing the stencil compiler and it's outputs easier #3127

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d67352e
test(mock-compiler): make testing the stencil compiler and it's outpu…
Oct 29, 2021
7ed4a77
Merge branch 'stencil-main' into test-mock-compiler
Oct 29, 2021
9c15500
run prettier / build
Oct 29, 2021
f2562c2
help stencil resolve declarations
Oct 29, 2021
52f645f
help stencil resolve modules
Oct 29, 2021
579818a
Merge branch 'main' into test-mock-compiler
johnjenkins Oct 29, 2021
b35361a
- Change compiler.build result to return buildCtx (instead of the Com…
Oct 30, 2021
8c1bdb9
Merge branch 'test-mock-compiler' of github.com:johnjenkins/stencil i…
Oct 30, 2021
d182d5c
run prettier
Oct 31, 2021
13bb8cc
fix dev server - 'buildFinish' event listener cast return as any - so…
Nov 3, 2021
c1e06f3
Merge branch 'main' into test-mock-compiler
johnjenkins Nov 3, 2021
40b4950
Merge branch 'main' into test-mock-compiler
johnjenkins Nov 8, 2021
b485e3d
Merge branch 'main' into test-mock-compiler
johnjenkins Nov 10, 2021
5b18ed4
Merge branch 'main' into test-mock-compiler
johnjenkins Nov 25, 2021
f8c3319
Merge branch 'main' into test-mock-compiler
johnjenkins Dec 2, 2021
cb3dc1c
Merge branch 'main' into test-mock-compiler
johnjenkins Dec 7, 2021
6ee6def
Merge branch 'stencil-main' into test-mock-compiler
Jun 23, 2022
c6a97fe
Merge branch 'stencil-main' into test-mock-compiler
Jun 28, 2022
12a0984
Merge branch 'main' into test-mock-compiler
johnjenkins Jul 1, 2022
1af6642
Merge branch 'stencil-main' into test-mock-compiler
Aug 30, 2022
4517df1
prettier / lint
Aug 30, 2022
3c12018
up timeout
Aug 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli/task-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const taskBuild = async (coreCompiler: CoreCompiler, config: d.ValidatedC
coreCompiler,
config,
results.hydrateAppFilePath,
results.componentGraph,
results.buildResults.componentGraph,
null
);
config.logger.printDiagnostics(prerenderDiagnostics);
Expand Down
6 changes: 3 additions & 3 deletions src/cli/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ export async function telemetryBuildFinishedAction(
sys: d.CompilerSystem,
config: d.ValidatedConfig,
coreCompiler: CoreCompiler,
result: d.CompilerBuildResults
result: d.BuildCtx
) {
const tracking = await shouldTrack(config, sys, config.flags.ci);

if (!tracking) {
return;
}

const component_count = Object.keys(result.componentGraph).length;
const component_count = Object.keys(result.buildResults.componentGraph).length;

const data = await prepareData(coreCompiler, config, sys, result.duration, component_count);
const data = await prepareData(coreCompiler, config, sys, result.buildResults.duration, component_count);

await sendMetric(sys, config, 'stencil_cli_command', data);

Expand Down
8 changes: 5 additions & 3 deletions src/cli/telemetry/test/telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ describe('telemetryBuildFinishedAction', () => {
);

const results = {
componentGraph: {},
duration: 100,
} as d.CompilerBuildResults;
buildResults: {
componentGraph: {},
duration: 100,
},
} as d.BuildCtx;

await telemetry.telemetryBuildFinishedAction(sys, config, coreCompiler, results);
expect(spyShouldTrack).toHaveBeenCalled();
Expand Down
16 changes: 8 additions & 8 deletions src/compiler/build/build-finish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { relative } from 'path';
* @param buildCtx the build context for the build being aborted
* @returns the build results
*/
export const buildFinish = async (buildCtx: d.BuildCtx): Promise<d.CompilerBuildResults> => {
export const buildFinish = async (buildCtx: d.BuildCtx): Promise<d.BuildCtx> => {
const results = await buildDone(buildCtx.config, buildCtx.compilerCtx, buildCtx, false);

const buildLog: d.BuildLog = {
Expand All @@ -28,9 +28,9 @@ export const buildFinish = async (buildCtx: d.BuildCtx): Promise<d.CompilerBuild
* Finish a build early due to failure. During the build process, a fatal error has occurred where the compiler cannot
* continue further
* @param buildCtx the build context for the build being aborted
* @returns the build results
* @returns the build context
*/
export const buildAbort = (buildCtx: d.BuildCtx): Promise<d.CompilerBuildResults> => {
export const buildAbort = (buildCtx: d.BuildCtx): Promise<d.BuildCtx> => {
return buildDone(buildCtx.config, buildCtx.compilerCtx, buildCtx, true);
};

Expand All @@ -40,18 +40,18 @@ export const buildAbort = (buildCtx: d.BuildCtx): Promise<d.CompilerBuildResults
* @param compilerCtx the compiler context associated with the build
* @param buildCtx the build context associated with the build to mark as done
* @param aborted true if the build ended early due to failure, false otherwise
* @returns the build results
* @returns the build context
*/
const buildDone = async (
config: d.Config,
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
aborted: boolean
): Promise<d.CompilerBuildResults> => {
): Promise<d.BuildCtx> => {
if (buildCtx.hasFinished && buildCtx.buildResults) {
// we've already marked this build as finished and
// already created the build results, just return these
return buildCtx.buildResults;
return buildCtx;
}

// create the build results data
Expand Down Expand Up @@ -107,7 +107,7 @@ const buildDone = async (
}

// emit a buildFinish event for anyone who cares
compilerCtx.events.emit('buildFinish', buildCtx.buildResults);
compilerCtx.events.emit('buildFinish', buildCtx);

// write all of our logs to disk if config'd to do so
// do this even if there are errors or not the active build
Expand All @@ -128,7 +128,7 @@ const buildDone = async (
}
}

return buildCtx.buildResults;
return buildCtx;
};

const logHmr = (logger: d.Logger, buildCtx: d.BuildCtx) => {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const build = async (
compilerCtx: d.CompilerCtx,
buildCtx: d.BuildCtx,
tsBuilder: ts.BuilderProgram
) => {
): Promise<d.BuildCtx> => {
try {
// reset process.cwd() for 3rd-party plugins
process.chdir(config.rootDir);
Expand Down
7 changes: 2 additions & 5 deletions src/compiler/build/full-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import ts from 'typescript';
* @param compilerCtx the current Stencil compiler context
* @returns the results of a full build of Stencil
*/
export const createFullBuild = async (
config: d.ValidatedConfig,
compilerCtx: d.CompilerCtx
): Promise<d.CompilerBuildResults> => {
return new Promise<d.CompilerBuildResults>((resolve) => {
export const createFullBuild = async (config: d.ValidatedConfig, compilerCtx: d.CompilerCtx): Promise<d.BuildCtx> => {
return new Promise<d.BuildCtx>((resolve) => {
let tsWatchProgram: ts.WatchOfConfigFile<ts.BuilderProgram> = null;

compilerCtx.events.on('fileUpdate', (p) => {
Expand Down
1 change: 1 addition & 0 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const createCompiler = async (userConfig: Config): Promise<Compiler> => {
createWatcher,
destroy,
sys,
compilerCtx,
};

config.logger.printDiagnostics(diagnostics);
Expand Down
135 changes: 65 additions & 70 deletions src/compiler/output-targets/test/output-targets-dist.spec.ts
Original file line number Diff line number Diff line change
@@ -1,94 +1,89 @@
// @ts-nocheck
import type * as d from '@stencil/core/declarations';
import { expectFilesDoNotExist, expectFilesExist } from '../../../testing/testing-utils';
import { Compiler, Config } from '@stencil/core/compiler';
import { mockConfig } from '@stencil/core/testing';
import { mockCreateCompiler, MockCompiler, mockCompilerRoot } from '../../../testing/mock-compiler';
import path from 'path';

describe.skip('outputTarget, dist', () => {
jest.setTimeout(20000);
let compiler: Compiler;
let config: Config;
const root = path.resolve('/');
describe('outputTarget, dist', () => {
jest.setTimeout(25000);
let compiler: MockCompiler;
let config: d.Config = {};

it('default dist files', async () => {
config = mockConfig({
config = {
buildAppCore: true,
buildEs5: true,
globalScript: path.join(root, 'User', 'testing', 'src', 'global.ts'),
globalScript: path.join(mockCompilerRoot, 'src', 'global.ts'),
namespace: 'TestApp',
outputTargets: [{ type: 'dist' }],
rootDir: path.join(root, 'User', 'testing', '/'),
});
sourceMap: true,
};

compiler = new Compiler(config);
compiler = await mockCreateCompiler(config);
config = compiler.config;

await compiler.fs.writeFiles({
[path.join(config.sys.getClientPath('polyfills/index.js'))]: `/* polyfills */`,
[path.join(root, 'User', 'testing', 'package.json')]: `{
"module": "dist/index.mjs",
"main": "dist/index.js",
await config.sys.writeFile(
config.packageJsonFilePath,
`{
"module": "dist/index.js",
"main": "dist/index.cjs.js",
"collection": "dist/collection/collection-manifest.json",
"types": "dist/types/components.d.ts"
}`,
[path.join(root, 'User', 'testing', 'src', 'index.html')]: `<cmp-a></cmp-a>`,
[path.join(root, 'User', 'testing', 'src', 'components', 'cmp-a.tsx')]: `
@Component({
tag: 'cmp-a',
styleUrls: {
ios: 'cmp-a.ios.css',
md: 'cmp-a.md.css'
}
}) export class CmpA {}`,
[path.join(root, 'User', 'testing', 'src', 'components', 'cmp-a.ios.css')]: `cmp-a { color: blue; }`,
[path.join(root, 'User', 'testing', 'src', 'components', 'cmp-a.md.css')]: `cmp-a { color: green; }`,
[path.join(
root,
'User',
'testing',
'src',
'global.ts'
)]: `export default function() { console.log('my global'); }`,
});
await compiler.fs.commit();
}`
);
await config.sys.writeFile(
path.join(config.srcDir, 'components', 'cmp-a.tsx'),
`
import { Component, h } from '@stencil/core';
@Component({
tag: 'cmp-a',
styleUrls: {
ios: 'cmp-a.ios.css',
md: 'cmp-a.md.css'
}
}) export class CmpA {}
`
);
await config.sys.writeFile(path.join(config.srcDir, 'components', 'cmp-a.ios.css'), `cmp-a { color: blue; }`);
await config.sys.writeFile(path.join(config.srcDir, 'components', 'cmp-a.md.css'), `cmp-a { color: green; }`);
await config.sys.writeFile(
path.join(config.srcDir, 'global.ts'),
`export default function() { console.log('my global'); }`
);

const r = await compiler.build();
expect(r.diagnostics).toHaveLength(0);

expectFilesExist(compiler.fs, [
path.join(root, 'User', 'testing', 'dist', 'index.js'),
path.join(root, 'User', 'testing', 'dist', 'index.mjs'),
path.join(root, 'User', 'testing', 'dist', 'index.js.map'),
expectFilesExist(compiler.compilerCtx.fs, [
path.join(mockCompilerRoot, 'dist', 'index.js'),

path.join(root, 'User', 'testing', 'dist', 'collection', 'collection-manifest.json'),
path.join(root, 'User', 'testing', 'dist', 'collection', 'components', 'cmp-a.js'),
path.join(root, 'User', 'testing', 'dist', 'collection', 'components', 'cmp-a.js.map'),
path.join(root, 'User', 'testing', 'dist', 'collection', 'components', 'cmp-a.ios.css'),
path.join(root, 'User', 'testing', 'dist', 'collection', 'components', 'cmp-a.md.css'),
path.join(root, 'User', 'testing', 'dist', 'collection', 'global.js'),
path.join(root, 'User', 'testing', 'dist', 'collection', 'global.js.map'),
path.join(mockCompilerRoot, 'dist', 'collection', 'collection-manifest.json'),
path.join(mockCompilerRoot, 'dist', 'collection', 'components', 'cmp-a.js'),
path.join(mockCompilerRoot, 'dist', 'collection', 'components', 'cmp-a.js.map'),
path.join(mockCompilerRoot, 'dist', 'collection', 'components', 'cmp-a.ios.css'),
path.join(mockCompilerRoot, 'dist', 'collection', 'components', 'cmp-a.md.css'),
path.join(mockCompilerRoot, 'dist', 'collection', 'global.js'),
path.join(mockCompilerRoot, 'dist', 'collection', 'global.js.map'),

path.join(root, 'User', 'testing', 'dist', 'esm', 'index.mjs'),
path.join(root, 'User', 'testing', 'dist', 'esm', 'index.js.map'),
path.join(root, 'User', 'testing', 'dist', 'esm', 'loader.mjs'),
path.join(root, 'User', 'testing', 'dist', 'esm-es5', 'index.mjs'),
path.join(root, 'User', 'testing', 'dist', 'esm-es5', 'index.js.map'),
path.join(root, 'User', 'testing', 'dist', 'esm-es5', 'loader.mjs'),
path.join(root, 'User', 'testing', 'dist', 'esm', 'polyfills', 'index.js'),
path.join(root, 'User', 'testing', 'dist', 'esm', 'polyfills', 'index.js.map'),

path.join(root, 'User', 'testing', 'dist', 'loader'),

path.join(root, 'User', 'testing', 'dist', 'types'),

path.join(root, 'User', 'testing', 'src', 'components.d.ts'),
path.join(mockCompilerRoot, 'dist', 'esm', 'index.js'),
path.join(mockCompilerRoot, 'dist', 'esm', 'index.js.map'),
path.join(mockCompilerRoot, 'dist', 'esm', 'loader.js'),
path.join(mockCompilerRoot, 'dist', 'esm-es5', 'index.js'),
path.join(mockCompilerRoot, 'dist', 'esm-es5', 'index.js.map'),
path.join(mockCompilerRoot, 'dist', 'esm-es5', 'loader.js'),
path.join(mockCompilerRoot, 'dist', 'esm', 'polyfills'),
path.join(mockCompilerRoot, 'dist', 'loader'),
path.join(mockCompilerRoot, 'dist', 'types'),
path.join(mockCompilerRoot, 'src', 'components.d.ts'),
]);

expectFilesDoNotExist(compiler.fs, [
path.join(root, 'User', 'testing', 'build'),
path.join(root, 'User', 'testing', 'esm'),
path.join(root, 'User', 'testing', 'es5'),
path.join(root, 'User', 'testing', 'www'),
path.join(root, 'User', 'testing', 'index.html'),
expectFilesDoNotExist(compiler.compilerCtx.fs, [
path.join(mockCompilerRoot, 'build'),
path.join(mockCompilerRoot, 'esm'),
path.join(mockCompilerRoot, 'es5'),
path.join(mockCompilerRoot, 'www'),
path.join(mockCompilerRoot, 'index.html'),
]);

compiler.destroy();
});
});