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

feat: run stc from integrations #2342

Merged
merged 65 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
f77f535
feat: run stc inside intergrations
tzachbon Feb 20, 2022
9b54a6f
refactor: remove unused cache variable
tzachbon Feb 20, 2022
8f2a264
deps: update package json with stylable-cli
tzachbon Feb 21, 2022
62d2606
refactor(e2e-test-kit): file-system helpers
tzachbon Feb 21, 2022
63c9f24
test(webpack-plugin): basic stc config
tzachbon Feb 21, 2022
b97ed5b
feat(webpack): handle file deletion and creation; rename STCProjects …
tzachbon Feb 21, 2022
fb81a6b
chore: clean-ups
tzachbon Feb 21, 2022
83e790f
chore: loadStylableConfig return config extracted value
tzachbon Feb 21, 2022
85513c6
feat(cli): support "config" flag
tzachbon Feb 21, 2022
e11984a
Revert "chore: loadStylableConfig return config extracted value"
tzachbon Feb 21, 2022
4a1979d
feat(cli): wrap config resolution methods with 'resolveConfig'
tzachbon Feb 21, 2022
cde2dee
feat(webpack): support stcConfig from the webpack-config
tzachbon Feb 21, 2022
0a989d7
refactor(webpack): clean-ups and reordering
tzachbon Feb 21, 2022
719d83b
fix(webpack): type fix for the stcBuilder
tzachbon Feb 21, 2022
1521e5e
test(webpack): fix package name
tzachbon Feb 22, 2022
9d31580
feat(cli): build stylable watchMode flag and watch hooks
tzachbon Feb 23, 2022
5de4898
feat(cli): rewrite stc-builder with better API
tzachbon Feb 23, 2022
718102b
fix(rollup): rollup starting skeleton
tzachbon Mar 2, 2022
1316b06
Merge remote-tracking branch 'origin/master' into tzach/run-stc-insid…
tzachbon Mar 3, 2022
b0589ac
fix(rollup): handle watched directories
tzachbon Mar 3, 2022
e0bc9ba
fix: update stc-builder api
tzachbon Mar 3, 2022
62b25c8
fix(rollup): use console error since rollup error always throw when u…
tzachbon Mar 3, 2022
ec6ba1d
infra: add cli to rollup tsconfig references
tzachbon Mar 3, 2022
9780906
fix: update stc watch mode api
tzachbon Mar 3, 2022
9a583bd
fix(cli): remove watch hook
tzachbon Mar 3, 2022
1638728
fix(rollup): register projects sources as watcher dep before emitting…
tzachbon Mar 6, 2022
e6250f4
Merge remote-tracking branch 'origin/master' into tzach/run-stc-insid…
tzachbon Mar 6, 2022
db7a352
fix(rollup): emitting diagnostic only on app's stylable module
tzachbon Mar 6, 2022
6f9fbdc
refactor(rollup): remove unused method
tzachbon Mar 6, 2022
80c15a0
refactor(cli): raw diagnostic destructuring
tzachbon Mar 7, 2022
06b59b4
fix: add quots to the glob string inside the spawn
tzachbon Mar 9, 2022
3f9258b
feat(rollup): update rollup peer dependency to be 2.70.0
tzachbon Mar 10, 2022
8b4a80e
fix(rollup): update package lock
tzachbon Mar 10, 2022
834e1bf
fix(rollup): typo
tzachbon Mar 10, 2022
48ae607
fix rollup testkit
barak007 Mar 10, 2022
75a6f97
Merge remote-tracking branch 'origin/master' into tzach/run-stc-insid…
tzachbon Mar 13, 2022
8e39b07
docs: update plugins readme files
tzachbon Mar 13, 2022
7638b2d
refactor(cli): build single file getAllDiagnostics
tzachbon Mar 13, 2022
e050d7c
refactor: stc-builder cleanups
tzachbon Mar 13, 2022
0ad3a91
refactor: rename stc-builder apis and add documentation
tzachbon Mar 13, 2022
4b63596
test: fix dedup and sort diagnostics test in CLI
tzachbon Mar 13, 2022
e0a11a8
refactor: remove only
tzachbon Mar 13, 2022
acb307b
test(rollup-plugin): update testkit to use the correct context
tzachbon Mar 13, 2022
9d2f77c
refactor(cli): unified stc builder rebuild api
tzachbon Mar 14, 2022
4668783
test: use typed configuration in the test fixtures
tzachbon Mar 14, 2022
5d0a5f6
refactor: cleaner stc-builder api
tzachbon Mar 14, 2022
d05660a
fix(cli): typo
tzachbon Mar 14, 2022
17ccc5e
refactor(cli): build-stylable watch options API
tzachbon Mar 14, 2022
73ad356
chore: cleanups
tzachbon Mar 20, 2022
3a78ea2
Merge remote-tracking branch 'origin/master' into tzach/run-stc-insid…
tzachbon Mar 24, 2022
6484d7a
Merge remote-tracking branch 'origin/master' into tzach/run-stc-insid…
tzachbon Mar 24, 2022
ce16478
refactor(rollup-plugin): separate rollup test to 2 tests functions
tzachbon Mar 24, 2022
587f0ec
test(webpack): use promise write file
tzachbon Mar 24, 2022
c491591
test(webpack): separate project runner recompilations
tzachbon Mar 24, 2022
e191481
Merge remote-tracking branch 'origin/master' into tzach/run-stc-insid…
tzachbon Mar 30, 2022
d6d0f8c
test(e2e-test-kit): webpack project runner debounce on recompile
tzachbon Mar 30, 2022
daf1d1f
test(webpack): add wait-for for the stc in watch mode
tzachbon Mar 31, 2022
973efa0
Revert "test(e2e-test-kit): webpack project runner debounce on recomp…
tzachbon Mar 31, 2022
b870084
fix(cli): add the initialized files to the affected files
tzachbon Apr 3, 2022
6ce4d42
feat(webpack-plugin): report files that are not part of webpack entry…
tzachbon Apr 3, 2022
60d80f2
fix(cli): typos
tzachbon Apr 3, 2022
be23366
fix(cli): clone diagnostics messages in stc-builder
tzachbon Apr 3, 2022
e7e33f0
Merge branch 'master' into tzach/run-stc-inside-intergrations
tzachbon Apr 4, 2022
0d4192e
test(webpack-plugin): increase stc test timeout
tzachbon Apr 4, 2022
8fce154
test(webpack-plugin): assert output file content in stc watch
tzachbon Apr 4, 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
6 changes: 5 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions packages/build-tools/src/load-stylable-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import findConfig from 'find-config';

export function loadStylableConfig<T>(context: string, extract: (config: any) => T): T | undefined {
export function loadStylableConfig<T>(
context: string,
extract: (config: any) => T
): { path: string; config: T } | undefined {
const path = findConfig('stylable.config.js', { cwd: context });
let config;
if (path) {
Expand All @@ -16,7 +19,10 @@ export function loadStylableConfig<T>(context: string, extract: (config: any) =>
`Stylable configuration loaded from ${path} but no exported configuration found`
);
}
return extract(config);
return {
path,
config: extract(config),
};
}
return undefined;
}
1 change: 1 addition & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ After installing `@stylable/cli`, the `stc` command will be available, running `
| `--dts` | | output definition files for the stylable source files (`.st.css.d.ts`) | `false` |
| `--dtsSourceMap` | | output source-maps for the definitions of stylable source files (`.st.css.d.ts.map`) | `true` if `--dts` is true, otherwise `false` |
| `--watch` | `w` | enable watch mode | `false` |
| `--config` | `c` | The path to a config file specifying how to build and output Stylable stylesheets | The directory containing the config file is assumed to be the "rootDir" for the project named "stylable.config.js" |
| `--useNamespaceReference` | `unsr` | mark output stylable source files with relative path for namespacing purposes (\*) | `false` |
| `--customGenerator` | | path of a custom index file generator | - |
| `--ext` | | extension of stylable css files | `.st.css` |
Expand Down
15 changes: 8 additions & 7 deletions packages/cli/src/build-single-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,14 @@ export function getAllDiagnostics(res: StylableResults): Diagnostic[] {
? res.meta.diagnostics.reports.concat(res.meta.transformDiagnostics.reports)
: res.meta.diagnostics.reports;

return diagnostics.map((diagnostic) => {
const err = diagnostic.node.error(diagnostic.message, diagnostic.options);

return {
type: diagnostic.type,
message: `${diagnostic.message}\n${err.showSourceCode(true)}`,
offset: diagnostic.node.source?.start?.offset,
return diagnostics.map(({ message, node, options, type }) => {
const err = node.error(message, options);
const diagnostic: Diagnostic = {
type,
message: `${message}\n${err.showSourceCode(true)}`,
...(node.source?.start && {}),
};

return diagnostic;
});
}
31 changes: 21 additions & 10 deletions packages/cli/src/build-stylable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface BuildStylableContext
outputFiles?: Map<string, Set<string>>;
defaultOptions?: BuildOptions;
overrideBuildOptions?: Partial<BuildOptions>;
configFilePath?: string;
watchMode?: boolean;
}

export async function buildStylable(
Expand All @@ -30,23 +32,32 @@ export async function buildStylable(
overrideBuildOptions = {},
fs: fileSystem = fs,
log = createDefaultLogger(),
watch = false,
watch: runWatch = false,
watchMode: watch = runWatch,
tzachbon marked this conversation as resolved.
Show resolved Hide resolved
resolverCache = new Map(),
fileProcessorCache = {},
diagnosticsManager = new DiagnosticsManager({
log,
onFatalDiagnostics() {
if (!watch) {
process.exitCode = 1;
}
hooks: {
postReport(_diagnostics, hasFatalDiagnostic) {
if (hasFatalDiagnostic && !watch) {
process.exitCode = 1;
}
},
},
}),
outputFiles = new Map(),
requireModule = require,
resolveNamespace = requireModule(NAMESPACE_RESOLVER_MODULE_REQUEST).resolveNamespace,
configFilePath,
}: BuildStylableContext = {}
) {
const projects = await projectsConfig(rootDir, overrideBuildOptions, defaultOptions);
const projects = await projectsConfig(
rootDir,
overrideBuildOptions,
defaultOptions,
configFilePath
);
const watchHandler = new WatchHandler(fileSystem, {
log,
resolverCache,
Expand Down Expand Up @@ -77,7 +88,7 @@ export async function buildStylable(
fileProcessorCache,
});

const { service } = await build(buildOptions, {
const { service, generatedFiles } = await build(buildOptions, {
watch,
stylable,
log,
Expand All @@ -89,15 +100,15 @@ export async function buildStylable(
diagnosticsManager,
});

watchHandler.register({ service, identifier, stylable });
watchHandler.register({ service, identifier, stylable, generatedFiles });
}
}

diagnosticsManager.report();

if (watch) {
if (runWatch) {
watchHandler.start();
}

return { watchHandler };
return { watchHandler, outputFiles, projects, diagnosticsManager };
}
2 changes: 1 addition & 1 deletion packages/cli/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export async function build(
log(mode, buildMessages.BUILD_SKIPPED(isMultiPackagesProject ? identifier : undefined));
}

return { service };
return { service, generatedFiles: buildGeneratedFiles };

function buildFiles(filesToBuild: Set<string>, generated: Set<string>) {
for (const filePath of filesToBuild) {
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function main() {
log: shouldLog,
namespaceResolver,
preserveWatchOutput,
config,
} = argv;
const { resolveNamespace } = require(namespaceResolver);
const rootDir = resolve(argv.rootDir);
Expand Down Expand Up @@ -44,6 +45,7 @@ async function main() {
resolveNamespace,
watch,
log,
configFilePath: config,
});

process.on('SIGTERM', () => {
Expand Down
56 changes: 35 additions & 21 deletions packages/cli/src/config/projects-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
RawProjectEntity,
ResolveProjectsContext,
ResolveRequests,
STCConfig,
STCProjects,
} from '../types';
import { processProjects } from './process-projects';
import { createDefaultOptions, mergeBuildOptions, validateOptions } from './resolve-options';
Expand All @@ -17,28 +17,30 @@ import { resolveNpmRequests } from './resolve-requests';
export async function projectsConfig(
rootDir: string,
overrideBuildOptions: Partial<BuildOptions>,
defaultOptions: BuildOptions = createDefaultOptions()
): Promise<STCConfig> {
const configFile = resolveConfigFile(rootDir);
defaultOptions: BuildOptions = createDefaultOptions(),
configFilePath?: string
): Promise<STCProjects> {
const { config } = resolveConfig(rootDir, configFilePath) || {};

const topLevelOptions = mergeBuildOptions(
defaultOptions,
configFile?.options,
config?.options,
overrideBuildOptions
);

validateOptions(topLevelOptions);

let projects: STCConfig;
let projects: STCProjects;

if (isMultipleConfigProject(configFile)) {
const { entities } = processProjects(configFile, {
if (isMultipleConfigProject(config)) {
const { entities } = processProjects(config, {
defaultOptions: topLevelOptions,
});

projects = await resolveProjectsRequests({
rootDir,
entities,
resolveRequests: configFile.projectsOptions?.resolveRequests ?? resolveNpmRequests,
resolveRequests: config.projectsOptions?.resolveRequests ?? resolveNpmRequests,
});
} else {
projects = [
Expand All @@ -52,17 +54,29 @@ export async function projectsConfig(
return projects;
}

export function resolveConfigFile(context: string) {
return loadStylableConfig(context, (config) =>
tryRun(
() =>
isSTCConfig(config)
? typeof config.stcConfig === 'function'
? config.stcConfig()
: config.stcConfig
: undefined,
'Failed to evaluate "stcConfig"'
)
export function resolveConfig(context: string, request?: string) {
return request ? requireConfigFile(request, context) : resolveConfigFile(context);
}

function requireConfigFile(request: string, context: string) {
const path = require.resolve(request, { paths: [context] });
const config = resolveConfigValue(require(path));
return config ? { config, path } : undefined;
}

function resolveConfigFile(context: string) {
return loadStylableConfig(context, (config) => resolveConfigValue(config));
}

function resolveConfigValue(config: any) {
return tryRun(
() =>
isSTCConfig(config)
? typeof config.stcConfig === 'function'
? config.stcConfig()
: config.stcConfig
: undefined,
'Failed to evaluate "stcConfig"'
);
}

Expand All @@ -86,7 +100,7 @@ async function resolveProjectsRequests({
rootDir: string;
entities: Array<RawProjectEntity>;
resolveRequests: ResolveRequests;
}): Promise<STCConfig> {
}): Promise<STCProjects> {
const context: ResolveProjectsContext = { rootDir };

return resolveRequests(entities, context);
Expand Down
8 changes: 8 additions & 0 deletions packages/cli/src/config/resolve-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ export function getCliArguments(): Arguments<CliArguments> {
defaultDescription: defaults.diagnosticsMode,
choices: ['strict', 'loose'],
})
.option('config', {
alias: 'c',
type: 'string',
description:
'The path to a config file specifying how to build and output Stylable stylesheets',
defaultDescription:
'The directory containing the config file is assumed to be the "rootDir" for the project named "stylable.config.js"',
})
.option('watch', {
alias: 'w',
type: 'boolean',
Expand Down
11 changes: 7 additions & 4 deletions packages/cli/src/diagnostics-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ type DiagnosticsStore = Map<string, Map<string, ProcessDiagnostics>>;

interface DiagnosticsManagerOptions {
log?: Log;
onFatalDiagnostics?: () => void;
hooks?: {
preReport?(diagnosticsMessages: DiagnosticMessages): void;
postReport?(diagnosticsMessages: DiagnosticMessages, hasFatalDiagnostic: boolean): void;
};
}

export class DiagnosticsManager {
Expand Down Expand Up @@ -94,16 +97,16 @@ export class DiagnosticsManager {
}
}

this.options.hooks?.preReport?.(diagnosticMessages);

if (diagnosticMessages.size) {
const hasFatalDiangostics = reportDiagnostics(
this.log,
diagnosticMessages,
diagnosticMode
);

if (hasFatalDiangostics) {
this.options.onFatalDiagnostics?.();
}
this.options.hooks?.postReport?.(diagnosticMessages, hasFatalDiangostics);
}

return Boolean(diagnosticMessages.size);
Expand Down
7 changes: 6 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ export {
BuildOptions,
Configuration,
ConfigurationProvider,
STCConfig,
STCProjects,
ResolveRequests,
typedConfiguration,
} from './types';
export { resolveConfig } from './config/projects-config';
export type { WatchHandler } from './watch-handler';
export { DiagnosticsManager } from './diagnostics-manager';
export {
DirectoryProcessService,
DirectoryProcessServiceOptions,
createWatchEvent,
} from './directory-process-service/directory-process-service';
export { STCBuilder } from './stc-builder';
export { BuildStylableContext, buildStylable } from './build-stylable';
export type { CodeMod } from './code-mods/types';
2 changes: 2 additions & 0 deletions packages/cli/src/report-diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { levels, Log } from './logger';
export interface Diagnostic {
message: string;
type: DiagnosticType;
line?: number;
column?: number;
offset?: number;
}

Expand Down