6.0.2 • Published 1 month ago

@stylable/cli v6.0.2

Weekly downloads
4,029
License
MIT
Repository
github
Last release
1 month ago

@stylable/cli

npm version

@stylable/cli is a low-level utility used for working with Stylable projects directly.

  • Build and transform stylesheets into JavaScript modules
  • Generate an entry index file to help consume a published project

Installation

Using NPM:

npm install @stylable/cli --save-dev

Using Yarn:

yarn add @stylable/cli -D

Usage

After installing @stylable/cli, the stc command will be available, running stc --help will provide a brief description for the options available.

stc accepts CLI arguments or a Stylable configuration file.

CLI Arguments

OptionAliasDescriptionDefault Value
--versionvshow CLI version numberboolean
--rootDirroot directory of projectcwd
--srcDirsource directory relative to root./
--outDirtarget directory relative to root./
--indexFilefilename of the generated indexfalse
--cjsoutput commonjs modules (.js)false
--esmoutput esm modules (.mjs)false
--cssoutput transpiled css files (.css)false
--stcssoutput stylable source files (.st.css)false
--dtsoutput definition files for the stylable source files (.st.css.d.ts)false
--dtsSourceMapoutput source-maps for the definitions of stylable source files (.st.css.d.ts.map)true if --dts is true, otherwise false
--watchwenable watch modefalse
--configcThe path to a config file specifying how to build and output Stylable stylesheetsThe directory containing the config file is assumed to be the "rootDir" for the project named "stylable.config.js"
--useNamespaceReferenceunsrmark output stylable source files with relative path for namespacing purposes (*)false
--customGeneratorpath of a custom index file generator-
--extextension of stylable css files.st.css
--cssInJsoutput transpiled css into the js modulefalse
--cssFilenamepattern of the generated css file[filename].css
--injectCSSRequesticradd a static import for the generated css in the js module outputfalse
--namespaceResolvernsrnode request to a module that exports a stylable resolveNamespace function@stylable/node
--requirerrequire hook to execute before running-
--optimizeoremoves: empty nodes, stylable directives, commentsfalse
--minifymminify generated cssfalse
--logverbose logfalse
--diagnosticsprint verbose diagnosticstrue
--diagnosticsModedetermine the diagnostics mode. if strict process will exit on any exception, loose will attempt to finish the process regardless of exceptionsfalse
--helphShow helpboolean

* - For the useNamespaceReference flag to function properly, the source folder must be published in addition to the output target code

Configuration file

The stc configuration should be located in the stylable.config.js file under the property name stcConfig. The CLI provides a helper method and type definitions to provide a better configuration experience.

const { stcConfig } = require('@stylable/cli');

// This can be an object or a method that returns an object.
exports.stcConfig = stcConfig({
    options: {
        // BuildOptions
    }
});

Build options

export interface BuildOptions {
    /** specify where to find source files */
    srcDir: string;
    /** specify where to build the target files */
    outDir: string;
    /** should the build need to output manifest file */
    manifest?: string;
    /** generates Stylable index file for the given name, the index file will reference Stylable sources from the `srcDir` unless the `outputSources` option is `true` in which case it will reference the `outDir` */
    indexFile?: string;
    /** custom cli index generator class */
    IndexGenerator?: typeof IndexGenerator;
    /** output commonjs module (.js) */
    cjs?: boolean;
    /** output esm module (.mjs) */
    esm?: boolean;
    /** template of the css file emitted when using outputCSS */
    outputCSSNameTemplate?: string;
    /** should include the css in the generated JS module */
    includeCSSInJS?: boolean;
    /** should output build css for each source file */
    outputCSS?: boolean;
    /** should output source .st.css file to dist */
    outputSources?: boolean;
    /** should add namespace reference to the .st.css copy  */
    useNamespaceReference?: boolean;
    /** should inject css import in the JS module for the generated css from outputCSS */
    injectCSSRequest?: boolean;
    /** should apply css optimizations */
    optimize?: boolean;
    /** should minify css */
    minify?: boolean;
    /** should generate .d.ts definitions for every stylesheet */
    dts?: boolean;
    /** should generate .d.ts.map files for every .d.ts mapping back to the source .st.css.
     * It will use the origin file path unless `outputSources` is true, then it will use the outputted file path as the source-map source.
     */
    dtsSourceMap?: boolean;
    /** should emit diagnostics */
    diagnostics?: boolean;
    /** determine the diagnostics mode. if strict process will exit on any exception, loose will attempt to finish the process regardless of exceptions */
    diagnosticsMode?: DiagnosticsMode;
}

Generating an index file

This generates an index.st.css file that acts as an export entry from every stylesheet in the provided srcDir.

$ stc --srcDir="./src" --outDir="./dist" --indexFile="index.st.css"

The generated index file will include re-exports of all stylesheet roots found in the given srcDir path.

Generating a custom index file

Exporting a Generator named export class from a file will allow it to be used as a customGenerator.

Usually this generator will inherit from our base generator class and override the generateReExports and generateIndexSource methods.

This example demonstrates the default generator behavior (only exports stylesheet roots):

import { Generator as Base, ReExports } from '@stylable/cli';

export class Generator extends Base {
    public generateReExports(filePath): ReExports {
        return {
            root: this.filename2varname(filePath),
            parts: {},
            keyframes: {},
            stVars: {},
            vars: {},
        };
    }    
    protected generateIndexSource(indexFileTargetPath: string) {
        const source = super.generateIndexSource(indexFileTargetPath);
        return '@st-namespace "INDEX";\n' + source;
    }
}
  • See our named exports generator for a more complete example that re-exports every symbol from every stylesheet found in the generated index file.

Build source stylesheets to JavaScript modules

To transform your project stylesheets to target JavaScript modules containing the transformed source files, you must provide the indexFile parameter with an empty string.

$ stc --srcDir="./src" --outDir="./dist"

Multiple Projects

Projects allow sharing stc configurations and management of Stylable projects in one location. They provides a controllable and predictable build order with caching optimizations.

export interface MultipleProjectsConfig<PRESET extends string> {
    options?: PartialBuildOptions;
    presets?: Presets<PRESET>;
    projects: Projects<PRESET>;
    projectsOptions?: {
        resolveRequests?: ResolveRequests;
    };
}

Example for simple monorepo with Stylable packages

const { stcConfig } = require('@stylable/cli');

exports.stcConfig = stcConfig({ options: { srcDir: './src', outDir: './dist', outputSources: true, cjs: false, useNamespaceReference: true, }, projects: 'packages/*' });

### Options

Similar to a [single project](#configuration-file), `options` is the top-level `BuildOptions` and is the default options for each project.

### Projects

**Projects** is a generic term that refers to a set of path requests that define single or multiple `BuildOptions`.\
This set of requests is being processed and then evaluated as a map of `projectRoot` (directory path) to a set of `BuildOptions`.

By default, the request is a path to a package, and in order to make the correct topological sort, the dependency needs to be specified in each package `package.json`

As mentioned above, the value of a request can be resolved to a single or multiple `BuildOptions`.

```jsonc
{
    //...
    projects: {
        "packages/*": { 
            // ...BuildOptions
        },
        "other-package/*": [
            { /* #1 ...BuildOptions */ }, 
            { /* #2 ...BuildOptions */ }, 
        ]
    }
    //...
}

The full types specification for defining Projects

export type Projects =
    | Array<string | [string, ProjectEntryValues]>
    | Record<string, ProjectEntryValues>;

export type ProjectEntryValues = | ProjectEntryValue | Array<ProjectEntryValue>;

export type ProjectEntryValue = | PRESET | PartialBuildOptions | { preset?: PRESET; presets?: Array; options: PartialBuildOptions; };

### Presets

To reuse `BuildOptions`, define them using a name under the `presets` property and use them as the project entry value.

```js
exports.stcConfig = {
    //...
    presets: {
        firstPreset: {/* ...BuildOptions */},
        secondPreset: {/* ...BuildOptions */},
    },
    projects: {
        'packages/*': ['firstPreset', 'secondPreset']
    }

};

Projects Options

These options control the projects resolution process.

resolveRequests Function (Advanced usage)

Default: resolveNpmRequests

This method is used to resolve the Projects requests (e.g. 'packages/*' in the example) to the actual projectRoots (absolute path to the relevant projects).

The order of the resolved entities will be the order of the builds.

Usage stc-format

After installing @stylable/cli, the stc-format command will be available, running stc-format --help will provide a brief description for the options available.

OptionAliasDescriptionValue TypeDefault Value
--targetTfile or directory to formatstringcurrent working directory
--endWithNewlinenEnd output with newlinebooleanfalse
--indentEmptyLinesEKeep indentation on empty linesbooleanfalse
--indentSizesIndentation sizenumber4
--indentWithTabstIndent with tabs, overrides -s and -cbooleanfalse
--maxPerserveNewlinesmMaximum number of line-breaks to be preserved in one chunknumber1
--newlineBetweenRulesNAdd a newline between CSS rulesbooleantrue
--perserveNewlinespPreserve existing line-breaksbooleantrue
--selectorSeparatorNewlineLAdd a newline between multiple selectorsbooleantrue
--debugDEnable explicit debug log (overrides --silent)booleanfalse
--silentSWill not print any messages to the logbooleanfalse
--requirerrequire hooksarray[]
--helphShow helpboolean
--versionvShow version numberboolean

Experimental formatter

A new experimental formatter is available using the --experimental argument.

Currently not all configuration is accepted by the new formatter, the supported formatting options arguments are --endWithNewline, --indentSize, and a new --wrapLineLength.

Formatting the source directory

$ stc-format --target ./src

Usage stc-codemod

After installing @stylable/cli, the stc-codemod command will be available, running stc-codemod --help will provide a brief description for the options available.

Usage with npx

It is possible to run the codemod cli with npx with the following command

npx -p @stylable/cli stc-codemod --help
OptionAliasDescriptionValue TypeDefault Value
--rootDirdRoot directory of a projectstringcurrent working directory
--modsmArray of builtin codemods to executearray[]
--externaleArray of external codemod requests to executearray[]
--requirerrequire hooksarray[]
--helphShow helpboolean

builtin codemods

  • st-import-to-at-import - Convert :import to @st-import syntax.

    Note that this codemod does not preserve comments inside the :import

  • st-global-custom-property-to-at-property - Convert deprecated @st-global-custom-property *; to @property st-global(*); syntax.

  • namespace-to-st-namespace - Converts @namespace that would have been used as Stylable namespace configuration to @st-namespace.

Provide an external codemod

Codemods are transformation operations for code based on AST.

The contract for external codemods is that any requested module (cjs) will provide a module.export.codemods which is an iterable with the following signature:

interface CodeModResponse {
    changed: boolean;
}

type Codemods = Iterable<{ id: string, apply: CodeMod }>;

type CodeMod = (context: CodeModContext) => CodeModResponse;

interface CodeModContext {
    ast: Root;
    diagnostics: Diagnostics;
    postcss: Postcss;
}

Codemod ids should be namespaced to avoid collision.

Basic codemod example

module.exports.codemods = [
    {
        id: 'banner', 
        apply({ ast, postcss }) { 
            ast.prepend(postcss.comment({text: 'Hello Codemod'}));
            
            return {
                changed: true
            }
        }
    }
]

License

Copyright (c) 2017 Wix.com Ltd. All Rights Reserved. Use of this source code is governed by a MIT license.

6.0.2

1 month ago

6.0.1

2 months ago

6.0.0

2 months ago

5.19.0

3 months ago

5.18.1

3 months ago

6.0.0-rc.3

4 months ago

5.18.0

4 months ago

5.15.0

10 months ago

5.16.1

8 months ago

5.16.0

8 months ago

5.17.0

6 months ago

6.0.0-rc.1

7 months ago

6.0.0-rc.2

7 months ago

5.14.0

11 months ago

5.15.2

8 months ago

5.15.1

10 months ago

5.11.1

12 months ago

5.12.0

11 months ago

5.13.0

11 months ago

5.11.0

1 year ago

5.9.0

1 year ago

5.10.0

1 year ago

5.8.0

1 year ago

5.7.1

1 year ago

5.7.0

1 year ago

5.4.1-rc.1

1 year ago

5.5.0

1 year ago

5.6.1

1 year ago

5.6.0

1 year ago

5.3.0-rc.1

2 years ago

5.3.0

2 years ago

5.4.0

1 year ago

5.3.1-rc.1

2 years ago

5.2.1

2 years ago

4.14.1

2 years ago

4.14.2

2 years ago

5.0.0-rc.3

2 years ago

5.0.0-rc.4

2 years ago

5.0.0-rc.5

2 years ago

4.14.0

2 years ago

5.0.1

2 years ago

4.13.3

2 years ago

5.0.0

2 years ago

5.1.0

2 years ago

5.2.0

2 years ago

4.15.0

2 years ago

4.15.1

2 years ago

5.0.0-rc.1

2 years ago

5.0.0-rc.2

2 years ago

4.13.2

2 years ago

4.13.1

2 years ago

4.13.0

2 years ago

4.12.0

2 years ago

4.11.0

2 years ago

4.10.4

2 years ago

4.10.1

2 years ago

4.10.2

2 years ago

4.10.3

2 years ago

4.10.0

2 years ago

4.9.4

2 years ago

4.9.3

2 years ago

4.9.5

2 years ago

4.9.0

2 years ago

4.9.2

2 years ago

4.9.1

2 years ago

3.13.2

2 years ago

4.8.1

3 years ago

4.8.3

3 years ago

4.8.2

3 years ago

4.8.0

3 years ago

4.7.4

3 years ago

4.7.3

3 years ago

4.7.2

3 years ago

4.7.1

3 years ago

3.13.0

3 years ago

4.7.0

3 years ago

4.6.0

3 years ago

3.12.6

3 years ago

4.5.1

3 years ago

4.5.0

3 years ago

4.4.0

3 years ago

3.12.5

3 years ago

4.3.3

3 years ago

4.3.2

3 years ago

4.3.1

3 years ago

4.3.0

3 years ago

4.2.5

3 years ago

4.2.4

3 years ago

4.2.3

3 years ago

4.2.2

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

4.1.0

3 years ago

4.0.6

3 years ago

3.12.4

3 years ago

4.0.4

3 years ago

3.12.3

3 years ago

4.1.0-alpha.3

3 years ago

4.1.0-alpha.1

3 years ago

4.1.0-alpha.0

3 years ago

4.1.0-alpha.2

3 years ago

4.0.2

3 years ago

3.12.2

3 years ago

4.0.1

3 years ago

3.12.1

3 years ago

3.12.0

3 years ago

4.0.0

3 years ago

3.11.14

3 years ago

4.0.0-alpha.10

3 years ago

4.0.0-alpha.9

3 years ago

4.0.0-alpha.7

3 years ago

3.11.13

3 years ago

4.0.0-alpha.6

3 years ago

4.0.0-alpha.5

3 years ago

3.11.10

3 years ago

4.0.0-alpha.4

3 years ago

3.11.9

3 years ago

4.0.0-alpha.3

3 years ago

4.0.0-alpha.1

3 years ago

4.0.0-alpha.2

3 years ago

3.11.8

3 years ago

3.11.6

3 years ago

3.11.7

3 years ago

3.11.5

4 years ago

3.11.2

4 years ago

3.11.0

4 years ago

3.11.1

4 years ago

3.10.1

4 years ago

3.10.0

4 years ago

3.9.1

4 years ago

3.9.0

4 years ago

3.8.4

4 years ago

3.8.1

4 years ago

3.8.0

4 years ago

3.7.2

4 years ago

3.7.1

4 years ago

3.7.0

4 years ago

3.6.3

4 years ago

3.6.2

4 years ago

3.6.1

4 years ago

3.6.0

4 years ago

3.5.2

4 years ago

3.5.1

4 years ago

3.5.0

4 years ago

3.4.5

4 years ago

3.4.4

4 years ago

3.4.3

4 years ago

3.4.2

4 years ago

3.4.1

4 years ago

3.4.0

4 years ago

3.3.2-rc.1

4 years ago

3.3.0

4 years ago

1.4.0

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.4

4 years ago

3.1.3

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.5.3

4 years ago

1.3.1

4 years ago

2.5.2

4 years ago

2.5.1

5 years ago

2.5.0

5 years ago

1.3.0

5 years ago

2.5.0-alpha.1

5 years ago

2.5.0-alpha.0

5 years ago

2.4.1

5 years ago

2.4.1-alpha.1

5 years ago

2.4.1-alpha.0

5 years ago

2.4.0

5 years ago

2.3.3-alpha.1

5 years ago

2.3.3-alpha.0

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

1.2.11

5 years ago

2.3.1-alpha.0

5 years ago

2.3.0

5 years ago

2.2.8-alpha.1

5 years ago

2.2.8-alpha.0

5 years ago

2.2.7

5 years ago

2.2.6

5 years ago

2.2.5

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.12

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

1.2.10

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

1.2.9

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.2-alpha.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.2

6 years ago

1.0.0

6 years ago