Skip to content

Commit

Permalink
feat(cli): remove default cjs format (#2441)
Browse files Browse the repository at this point in the history
  • Loading branch information
tzachbon authored Apr 24, 2022
1 parent 57fd30d commit 852eb33
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
10 changes: 9 additions & 1 deletion packages/cli/src/build-stylable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { projectsConfig } from './config/projects-config';
import {
createBuildIdentifier,
createDefaultOptions,
hasStylableCSSOutput,
NAMESPACE_RESOLVER_MODULE_REQUEST,
} from './config/resolve-options';
import { DiagnosticsManager } from './diagnostics-manager';
import { createDefaultLogger } from './logger';
import { createDefaultLogger, levels } from './logger';
import type { BuildContext, BuildOptions } from './types';
import { WatchHandler } from './watch-handler';

Expand Down Expand Up @@ -81,6 +82,13 @@ export async function buildStylable(

log('[Project]', projectRoot, buildOptions);

if (!hasStylableCSSOutput(buildOptions)) {
log(
`No target output declared for "${identifier}", please provide one or more of the following target options: "cjs", "esm", "css", "stcss" or "indexFile"`,
levels.info
);
}

const stylable = new Stylable({
fileSystem,
requireModule,
Expand Down
12 changes: 11 additions & 1 deletion packages/cli/src/config/resolve-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export function createDefaultOptions(): BuildOptions {
return {
outDir: '.',
srcDir: '.',
cjs: true,
cjs: false,
esm: false,
dts: false,
injectCSSRequest: false,
Expand Down Expand Up @@ -262,3 +262,13 @@ export function createBuildIdentifier(
? projectRoot.replace(rootDir, '')
: projectRoot;
}

export function hasStylableCSSOutput(options: BuildOptions): boolean {
return (
options.cjs ||
options.esm ||
options.outputCSS ||
options.outputSources ||
Boolean(options.indexFile)
);
}
30 changes: 27 additions & 3 deletions packages/cli/test/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('Stylable Cli', function () {
'style.st.css': `.root{color:red}`,
});

runCliSync(['--rootDir', tempDir.path, '--nsr', testNsrPath]);
runCliSync(['--rootDir', tempDir.path, '--nsr', testNsrPath, '--cjs']);

const dirContent = loadDirSync(tempDir.path);
expect(
Expand All @@ -46,7 +46,15 @@ describe('Stylable Cli', function () {
'style.st.css': `.root{color:red}`,
});

runCliSync(['--rootDir', tempDir.path, '--nsr', testNsrPath, '--outDir', './dist']);
runCliSync([
'--rootDir',
tempDir.path,
'--nsr',
testNsrPath,
'--outDir',
'./dist',
'--cjs',
]);

const dirContent = loadDirSync(tempDir.path);
expect(Object.keys(dirContent)).to.eql([
Expand Down Expand Up @@ -110,7 +118,7 @@ describe('Stylable Cli', function () {
});

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

const dirContent = loadDirSync(tempDir.path);

Expand Down Expand Up @@ -356,6 +364,22 @@ describe('Stylable Cli', function () {
expect(stdout, 'stdout').to.match(/unknown var "xxx"/);
});

it('should report when there are no css output formats', () => {
populateDirectorySync(tempDir.path, {
'package.json': `{"name": "test", "version": "0.0.0"}`,
'style.st.css': `.root{}`,
});

const { stdout, status } = runCliSync(['--rootDir', tempDir.path]);

expect(status).to.equal(0);
expect(stdout, 'stdout').to.match(
new RegExp(
`No target output declared for "(.*?)", please provide one or more of the following target options: "cjs", "esm", "css", "stcss" or "indexFile"`
)
);
});

it('(diagnosticsMode) should not exit with error when using strict mode with only info diagnostics', () => {
populateDirectorySync(tempDir.path, {
'package.json': `{"name": "test", "version": "0.0.0"}`,
Expand Down
18 changes: 12 additions & 6 deletions packages/cli/test/config-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ describe('Stylable CLI config file options', function () {
'package.json': `{"name": "test", "version": "0.0.0"}`,
'style.st.css': `.root{color:red}`,
'stylable.config.js': `
exports.stcConfig = () => ({ options: {
outDir: './out',
} })
exports.stcConfig = () => ({
options: {
cjs: true,
outDir: './out',
}
})
`,
});

Expand All @@ -109,9 +112,12 @@ describe('Stylable CLI config file options', function () {
'package.json': `{"name": "test", "version": "0.0.0"}`,
'style.st.css': `.root{color:red}`,
'stylable.config.js': `
exports.stcConfig = () => ({ options: {
outDir: './dist',
} })
exports.stcConfig = () => ({
options: {
cjs: true,
outDir: './dist',
}
})
`,
},
});
Expand Down

0 comments on commit 852eb33

Please sign in to comment.