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: CSS variable injection #426

Merged
merged 6 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
58 changes: 58 additions & 0 deletions packages/lwc-compiler/src/compiler/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ const DEFAULT_OPTIONS = {
baseDir: "",
};

const DEFAULT_STYLESHEET_CONFIG = {
customProperties: {
allowDefinition: false,
resolveFromModule: undefined
}
};

const DEFAULT_OUTPUT_CONFIG = {
env: {
NODE_ENV: "development"
Expand All @@ -17,6 +24,15 @@ export type OutputProxyCompatConfig =
| { module: string }
| { independent: string };

export interface CustomPropertiesConfig {
allowDefinition?: boolean;
resolveFromModule?: string;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like the simplification

}

export interface StylesheetConfig {
customProperties?: CustomPropertiesConfig;
}

export interface OutputConfig {
env?: { [name: string]: string };
compat?: boolean;
Expand All @@ -37,11 +53,20 @@ export interface CompilerOptions {
* files. Only used when the component that is the compiler's entry point.
*/
baseDir?: string;
stylesheetConfig?: StylesheetConfig;
outputConfig?: OutputConfig;
}

export interface NormalizedCompilerOptions extends CompilerOptions {
outputConfig: NormalizedOutputConfig;
stylesheetConfig: NormalizedStylesheetConfig;
}

export interface NormalizedStylesheetConfig extends StylesheetConfig {
customProperties: {
allowDefinition: boolean;
resolveFromModule?: string;
};
}

export interface NormalizedOutputConfig extends OutputConfig {
Expand All @@ -55,6 +80,7 @@ export interface NormalizedOutputConfig extends OutputConfig {
export function validateNormalizedOptions(options: NormalizedCompilerOptions) {
validateOptions(options);
validateOutputConfig(options.outputConfig);
validateStylesheetConfig(options.stylesheetConfig);
}

export function validateOptions(options: CompilerOptions) {
Expand Down Expand Up @@ -87,11 +113,35 @@ export function validateOptions(options: CompilerOptions) {
}
}

if (!isUndefined(options.stylesheetConfig)) {
validateStylesheetConfig(options.stylesheetConfig);
}

if (!isUndefined(options.outputConfig)) {
validateOutputConfig(options.outputConfig);
}
}

function validateStylesheetConfig(config: StylesheetConfig) {
const { customProperties } = config;

if (!isUndefined(customProperties)) {
const { allowDefinition, resolveFromModule } = customProperties;

if (!isUndefined(allowDefinition) && !isBoolean(allowDefinition)) {
throw new TypeError(`Expected a boolean for stylesheetConfig.allowDefinition, received ${
allowDefinition
}`);
}

if (!isUndefined(resolveFromModule) && !isString(resolveFromModule)) {
throw new TypeError(`Expected a string for stylesheetConfig.resolveFromModule, received ${
resolveFromModule
}`);
}
}
}

function validateOutputConfig(config: OutputConfig) {
if (!isUndefined(config.minify) && !isBoolean(config.minify)) {
throw new TypeError(
Expand All @@ -118,9 +168,17 @@ export function normalizeOptions(
...options.outputConfig
};

const stylesheetConfig: NormalizedStylesheetConfig = {
customProperties: {
...DEFAULT_STYLESHEET_CONFIG.customProperties,
...(options.stylesheetConfig && options.stylesheetConfig.customProperties),
}
};

return {
...DEFAULT_OPTIONS,
...options,
stylesheetConfig,
outputConfig
};
}
Loading