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

fix(cli): resolveNamespace cli arg should override stylable.config #2923

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/cli/src/build-stylable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
createBuildIdentifier,
createDefaultOptions,
hasStylableCSSOutput,
NAMESPACE_RESOLVER_MODULE_REQUEST,
} from './config/resolve-options';
import { DiagnosticsManager } from './diagnostics-manager';
import { createDefaultLogger, levels } from './logger';
Expand Down Expand Up @@ -51,7 +50,7 @@ export async function buildStylable(
}),
outputFiles = new Map(),
requireModule = require,
resolveNamespace = requireModule(NAMESPACE_RESOLVER_MODULE_REQUEST).resolveNamespace,
resolveNamespace,
configFilePath,
watchOptions = {},
}: BuildStylableContext = {}
Expand Down Expand Up @@ -92,10 +91,13 @@ export async function buildStylable(
fileSystem,
requireModule,
projectRoot,
resolveNamespace,
resolverCache,
fileProcessorCache,
...config?.defaultConfig,
resolveNamespace:
resolveNamespace ||
config?.defaultConfig?.resolveNamespace ||
requireModule('@stylable/node').resolveNamespace,
});

const { service, generatedFiles } = await build(buildOptions, {
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ async function main() {
config,
} = argv;
const rootDir = resolve(argv.rootDir);
const { resolveNamespace } = require(require.resolve(namespaceResolver, {
paths: [rootDir],
}));
const explicitResolveNs =
namespaceResolver &&
require(require.resolve(namespaceResolver, {
paths: [rootDir],
}));

//
const log = createLogger(
Expand All @@ -42,7 +44,7 @@ async function main() {
overrideBuildOptions,
defaultOptions,
fs,
resolveNamespace,
resolveNamespace: explicitResolveNs?.resolveNamespace,
watch,
log,
configFilePath: config,
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/config/resolve-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import type { CliArguments, BuildOptions, PartialBuildOptions } from '../types';

const { join } = nodeFs;

export const NAMESPACE_RESOLVER_MODULE_REQUEST = '@stylable/node';

export function getCliArguments(): Arguments<CliArguments> {
const defaults = createDefaultOptions();
return yargs
Expand Down Expand Up @@ -85,10 +83,12 @@ export function getCliArguments(): Arguments<CliArguments> {
alias: 'unsr',
})
.option('namespaceResolver', {
type: 'string',
description:
'node request to a module that exports a stylable resolveNamespace function',
alias: 'nsr',
default: NAMESPACE_RESOLVER_MODULE_REQUEST,
defaultDescription:
'default to @stylable/node, if stylable.config resolveNamespace is undefined',
})
.option('injectCSSRequest', {
type: 'boolean',
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export interface CliArguments {
bundle: string | undefined;
dtsSourceMap: boolean | undefined;
useNamespaceReference: boolean | undefined;
namespaceResolver: string;
namespaceResolver?: string | undefined;
injectCSSRequest: boolean | undefined;
cssFilename: string | undefined;
cssInJs: boolean | undefined;
Expand Down
70 changes: 60 additions & 10 deletions packages/cli/test/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createTempDirectory,
ITempDirectory,
} from '@stylable/e2e-test-kit';
import { nodeFs } from '@file-services/node';
import { STImport, STVar } from '@stylable/core/dist/features';
import { diagnosticBankReportToStrings } from '@stylable/core-test-kit';

Expand Down Expand Up @@ -137,23 +138,72 @@ describe('Stylable Cli', function () {
]);
});

it('single file build with default ns-resolver', () => {
it('should resolve resolveNamespace with defaults: cli-arg -> stylable.config -> default', () => {
populateDirectorySync(tempDir.path, {
'package.json': `{"name": "test", "version": "0.0.0"}`,
'style.st.css': `.root{color:red}`,
});

const nsr = require.resolve('@stylable/node');
runCliSync(['--rootDir', tempDir.path, '--nsr', nsr, '--cjs']);
{
// default
runCliSync(['--rootDir', tempDir.path, '--cjs']);

const dirContent = loadDirSync(tempDir.path);
expect(
evalStylableModule<{ namespace: string }>(
loadDirSync(tempDir.path)['style.st.css.js'],
'style.st.css.js'
).namespace,
'default'
).equal(resolveNamespace('style', join(tempDir.path, 'style.st.css')));
}
{
// stylable.config
nodeFs.writeFileSync(
join(tempDir.path, 'stylable.config.js'),
`
let c = 0;
module.exports = {
defaultConfig(fs) {
return {
resolveNamespace: () => 'config-ns-' + c++
};
},
};
`
);

expect(
evalStylableModule<{ namespace: string }>(
dirContent['style.st.css.js'],
'style.st.css.js'
).namespace
).equal(resolveNamespace('style', join(tempDir.path, 'style.st.css')));
runCliSync(['--rootDir', tempDir.path, '--cjs']);

expect(
evalStylableModule<{ namespace: string }>(
loadDirSync(tempDir.path)['style.st.css.js'],
'style.st.css.js'
).namespace,
'stylable.config'
).equal('config-ns-0');
}
{
// cli argument
nodeFs.writeFileSync(
join(tempDir.path, 'custom-ns-resolver.js'),
`
let c = 0;
module.exports.resolveNamespace = function resolveNamespace() {
return 'custom-ns-' + c++;
}
`
);

runCliSync(['--rootDir', tempDir.path, '--nsr', './custom-ns-resolver.js', '--cjs']);

expect(
evalStylableModule<{ namespace: string }>(
loadDirSync(tempDir.path)['style.st.css.js'],
'style.st.css.js'
).namespace,
'cli argument'
).equal('custom-ns-0');
}
});

it('build .st.css source files with namespace reference', () => {
Expand Down