Skip to content

Commit

Permalink
fix: Copy objects passed as configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseppstein committed Mar 20, 2018
1 parent 0a17f60 commit 1d40f17
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
52 changes: 36 additions & 16 deletions packages/css-blocks/src/normalizeOptions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { Preprocessors } from "./BlockParser";
import {
Options,
ResolvedConfiguration,
} from "./options";

import { OutputMode } from "./OutputMode";

OutputMode,
} from "./OutputMode";
import {
filesystemImporter,
Importer,
ImporterData,
} from "./importing";
import {
Configuration,
ConfigurationObjectKeys,
ConfigurationSimpleKeys,
Options,
ResolvedConfiguration,
} from "./options";

import { Preprocessors } from "./BlockParser";

const CONFIG_OBJECT_KEYS: Array<ConfigurationObjectKeys> = [
"importerData",
"preprocessors",
];
const CONFIG_SIMPLE_KEYS: Array<ConfigurationSimpleKeys> = [
"outputMode",
"importer",
"rootDir",
"disablePreprocessChaining",
"maxConcurrentCompiles",
];
const DEFAULTS: ResolvedConfiguration = {
outputMode: OutputMode.BEM,
importer: filesystemImporter,
Expand All @@ -28,18 +41,25 @@ const DEFAULTS: ResolvedConfiguration = {
* passed.
*/
class OptionsReader implements ResolvedConfiguration {
private _opts: ResolvedConfiguration;
private _opts: Configuration;

constructor(options: Options = {}, defaults: Options = {}) {
constructor(options?: Options, defaults?: Options) {
this._opts = {...DEFAULTS};
for (let k of Object.keys(defaults)) {
if (defaults[k] !== undefined) {
this._opts[k] = defaults[k];
this.setAll(defaults);
this.setAll(options);
}
private setAll(opts: Options | undefined) {
if (opts === undefined) return;
for (let k of CONFIG_SIMPLE_KEYS) {
let v = opts[k];
if (v !== undefined) {
this._opts[k] = v;
}
}
for (let k of Object.keys(options)) {
if (options[k] !== undefined) {
this._opts[k] = options[k];
for (let k of CONFIG_OBJECT_KEYS) {
let v = opts[k];
if (v !== undefined) {
this._opts[k] = {...v};
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/css-blocks/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,11 @@ export type Options = Partial<Readonly<Configuration>>;
* values will have already been provided.
*/
export type ResolvedConfiguration = Readonly<Configuration>;

export type ConfigurationObjectKeys = "importerData"
| "preprocessors";
export type ConfigurationSimpleKeys = "outputMode"
| "importer"
| "rootDir"
| "disablePreprocessChaining"
| "maxConcurrentCompiles";
2 changes: 1 addition & 1 deletion packages/webpack-plugin/src/Plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
export interface CssBlocksWebpackOptions {
/// The name of the instance of the plugin. Defaults to outputCssFile.
name?: string;
/// The analzyer that decides what templates are analyzed and what blocks will be compiled.
/// The analyzer that decides what templates are analyzed and what blocks will be compiled.
analyzer: MultiTemplateAnalyzer;
/// The output css file for all compiled CSS Blocks. Defaults to "css-blocks.css"
outputCssFile?: string;
Expand Down

0 comments on commit 1d40f17

Please sign in to comment.