Skip to content
This repository was archived by the owner on Jul 9, 2021. It is now read-only.

Commit e0e030a

Browse files
committed
[changed] constructor signatures of Compiler, JSCompiler, SASSCompiler, JS and SASS to allow for additional options to be passed more easily; JSCompiler now supports the "library" and "libraryTarget" options from webpack
1 parent 96690ef commit e0e030a

9 files changed

Lines changed: 122 additions & 103 deletions

File tree

interfaces/webcompiler.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* @flow */
22

33
import type {ProgramData, ProgramDataCallback, StringOrErrorCallback, ResultOrErrorCallback, LiveReloadTrigger,
4-
ObjectOrErrorCallback, DevServerConfig, LintError, LintCallback, PostCSSWarning, NodeSassError} from '../src/typedef';
4+
ObjectOrErrorCallback, DevServerConfig, LintError, LintCallback, PostCSSWarning, NodeSassError,
5+
WatchCallback, JSCompilerConfig, SASSCompilerConfig} from '../src/typedef';
56

67
declare module 'webcompiler' {
78

@@ -28,7 +29,7 @@ declare module 'webcompiler' {
2829
declare function highlightArray(code: ?string): Array<string | Object>;
2930
declare function highlightJSX(code: ?string): string;
3031

31-
declare function watch(dir: string, type: string, callback: () => void): void;
32+
declare function watch(dir: string, type: string, callback: WatchCallback): void;
3233
declare function yaml(filename: string, callback: ObjectOrErrorCallback): void;
3334
declare function findBinary(name: string, callback: ResultOrErrorCallback): void;
3435
declare function livereload(): LiveReloadTrigger;
@@ -81,7 +82,7 @@ declare module 'webcompiler' {
8182
}
8283

8384
declare class JSCompiler {
84-
constructor(compress: ?boolean): void;
85+
constructor(options: ?JSCompilerConfig): void;
8586
be(inPath: string, outPath: string, callback: ?() => void): void;
8687
fe(inPath: string, outPath: string, callback: ?() => void): void;
8788
}
@@ -92,15 +93,15 @@ declare module 'webcompiler' {
9293
}
9394

9495
declare class SASSCompiler {
95-
constructor(compress: ?boolean, includePaths: ?string[], importOnceOptions: ?Object): void;
96+
constructor(options: ?SASSCompilerConfig): void;
9697
addPostcssPlugins(...plugins: any[]): SASSCompiler;
9798
postcss(path: string, data: ProgramData, callback: ProgramDataCallback): void;
9899
fe(inPath: string, outPath: string, callback: ?() => void): void;
99100
}
100101

101102
declare class JS {
102103
compiler: JSCompiler;
103-
constructor(compress: ?boolean, configFile: ?string): void;
104+
constructor(options: ?JSCompilerConfig, configFile: ?string): void;
104105
typecheck(callback: () => void): void;
105106
lint(paths: string[], callback: () => void): void;
106107
be(inPath: string, outPath: string, lintPaths: ?string[], callback: ?() => void): void;
@@ -109,7 +110,7 @@ declare module 'webcompiler' {
109110

110111
declare class SASS {
111112
compiler: SASSCompiler;
112-
constructor(compress: ?boolean, includePaths: ?string[], configFile: ?string, importOnceOptions: ?Object): void;
113+
constructor(options: ?SASSCompilerConfig, configFile: ?string): void;
113114
lint(paths: string[], callback: () => void): void;
114115
fe(inPath: string, outPath: string, lintPaths: ?string[], callback: ?() => void): void;
115116
}

src/Compiler.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
/* @flow */
22

3-
import type {ProgramData, ProgramDataCallback} from './typedef';
3+
import type {ProgramData, ProgramDataCallback, CompilerConfig} from './typedef';
44
import mkdirp from 'mkdirp';
55
import {dirname} from 'path';
66
import {writeFile, readFile} from 'fs';
77
import {gzip, gunzip} from 'zlib';
88
import {isProduction} from './util';
99
import {logError, logSequentialSuccessMessage} from './logger';
1010

11+
const defaultOptions = {
12+
compress: isProduction
13+
};
14+
1115
/**
1216
* The base compiler class
1317
*
1418
* @class Compiler
1519
* @abstract
16-
* @param {boolean} [compress=true] - if true `Compiler#save` will gzip compress the data in production mode
20+
* @protected
21+
* @param {CompilerConfig} [options={}] - configuration object
1722
*/
1823
export class Compiler {
1924

2025
/**
21-
* if true `Compiler#save` will gzip compress the data
26+
* configured options
2227
*
23-
* @member {boolean} compress
28+
* @member {CompilerConfig} options
2429
* @memberof Compiler
2530
* @private
2631
* @instance
2732
*/
28-
compress: boolean;
33+
options: Object;
2934

30-
/* eslint-disable require-jsdoc */
31-
constructor(compress: boolean = true) {
32-
/* eslint-enable require-jsdoc */
33-
this.compress = isProduction && compress;
35+
// eslint-disable-next-line require-jsdoc
36+
constructor(options: CompilerConfig = {}) {
37+
this.options = {...defaultOptions, ...options};
3438
}
3539

3640
/**
@@ -156,7 +160,7 @@ export class Compiler {
156160
readFile(`${path}.map`, 'utf8', (mapErr, mapData) => {
157161
const map = mapErr ? '' : mapData;
158162

159-
if (!this.compress) {
163+
if (!this.options.compress) {
160164
return callback({code: scriptData.toString('utf8'), map});
161165
}
162166

@@ -192,7 +196,7 @@ export class Compiler {
192196
map: oldData.map === data.map ? '' : data.map
193197
};
194198

195-
if (!this.compress) {
199+
if (!this.options.compress) {
196200
Compiler.writeAndCallDone(inPath, outPath, newData, callback);
197201

198202
return;

src/DevServer.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@ export class DevServer {
6464
/**
6565
* optional configuration
6666
*
67-
* @member {Object} options
67+
* @member {DevServerConfig} options
6868
* @memberof DevServer
6969
* @private
7070
* @instance
7171
*/
7272
options: Object;
7373

74-
/* eslint-disable require-jsdoc */
74+
// eslint-disable-next-line require-jsdoc
7575
constructor(script: string, options: DevServerConfig = {}) {
76-
/* eslint-enable require-jsdoc */
7776
this.script = script;
7877
this.options = {...defaultOptions, ...options};
7978
}
@@ -122,7 +121,7 @@ export class DevServer {
122121
return;
123122
}
124123

125-
const sass = new SASSCompiler(false),
124+
const sass = new SASSCompiler({compress: false}),
126125
lr = livereload(),
127126
compileSASS = sass.fe.bind(sass, style, join(contentBase, 'style.css'), () => {
128127
lr('/style.css');

src/JS.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow */
22

3+
import type {JSCompilerConfig} from './typedef';
34
import {JSCompiler} from './JSCompiler';
45
import type {NativeProcess} from './NativeProcess';
56
import {JSLint} from './JSLint';
@@ -13,9 +14,8 @@ import {findBinary} from './findBinary';
1314
* Wraps {@link JSCompiler} to add static analysis and linting. If you don't want that, use {@link JSCompiler} directly.
1415
*
1516
* @class JS
16-
* @param {boolean} [compress=true] - if true `Compiler#save` will gzip compress the data in
17-
* production mode
18-
* @param {string} [configFile="webcompiler/.eslintrc.yaml"] - path to the ESLint configuration file
17+
* @param {JSCompilerConfig} [options={}] - configuration object
18+
* @param {string} [configFile="webcompiler/.eslintrc.yaml"] - path to the ESLint configuration file
1919
* @example
2020
* import {JS} from 'webcompiler';
2121
* // or - import {JS} from 'webcompiler/lib/JS';
@@ -59,10 +59,9 @@ export class JS {
5959
*/
6060
linter: JSLint;
6161

62-
/* eslint-disable require-jsdoc */
63-
constructor(compress: boolean = true, configFile?: string) {
64-
/* eslint-enable require-jsdoc */
65-
this.compiler = new JSCompiler(compress);
62+
// eslint-disable-next-line require-jsdoc
63+
constructor(options?: JSCompilerConfig, configFile?: string) {
64+
this.compiler = new JSCompiler(options);
6665
this.linter = new JSLint(configFile);
6766
}
6867

src/JSCompiler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {yellow, red} = consoleStyles;
1919
*
2020
* @class JSCompiler
2121
* @extends Compiler
22-
* @param {boolean} [compress=true] - if true `Compiler#save` will gzip compress the data in production mode
22+
* @param {JSCompilerConfig} [options={}] - configuration object
2323
* @example
2424
* import {JSCompiler} from 'webcompiler';
2525
* // or - import {JSCompiler} from 'webcompiler/lib/JSCompiler';
@@ -178,7 +178,7 @@ export class JSCompiler extends Compiler {
178178
* @param {Function} [callback=function () {}] - a callback function
179179
*/
180180
fe(inPath: string, outPath: string, callback: () => void = noop) {
181-
const compiler = getCompiler(inPath, outPath);
181+
const compiler = getCompiler(inPath, outPath, this.options);
182182

183183
compiler.run((err, stats) => {
184184
if (err) {

src/SASS.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import type {ImportOnceOptions} from './typedef';
3+
import type {SASSCompilerConfig} from './typedef';
44
import {SASSCompiler} from './SASSCompiler';
55
import {SASSLint} from './SASSLint';
66
import noop from 'lodash/noop';
@@ -22,12 +22,8 @@ import {logLintingErrors} from './logger';
2222
* the module in JavaScript).
2323
*
2424
* @class SASS
25-
* @param {boolean} [compress=true] - if true `Compiler#save` will gzip compress
26-
* the data in production mode
27-
* @param {Array<string>} [includePaths=[]] - an array of additional include paths
28-
* @param {string} [configFile="webcompiler/.stylelintrc.yaml"] - path to the stylelint configuration file
29-
* @param {ImportOnceOptions} [importOnceOptions={}] - an object that lets you override default
30-
* importOnce resolver configuration
25+
* @param {SASSCompilerConfig} [options={}] - configuration object
26+
* @param {string} [configFile="webcompiler/.stylelintrc.yaml"] - path to the stylelint configuration file
3127
* @example
3228
* import {SASS} from 'webcompiler';
3329
* // or - import {SASS} from 'webcompiler/lib/SASS';
@@ -65,11 +61,9 @@ export class SASS {
6561
*/
6662
linter: SASSLint;
6763

68-
/* eslint-disable require-jsdoc */
69-
constructor(compress: boolean = true, includePaths: string[] = [], configFile?: string,
70-
importOnceOptions: ImportOnceOptions = {}) {
71-
/* eslint-enable require-jsdoc */
72-
this.compiler = new SASSCompiler(compress, includePaths, importOnceOptions);
64+
// eslint-disable-next-line require-jsdoc
65+
constructor(options?: SASSCompilerConfig, configFile?: string) {
66+
this.compiler = new SASSCompiler(options);
7367
this.linter = new SASSLint(configFile);
7468
}
7569

src/SASSCompiler.js

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @flow */
22

3-
import type {ProgramData, ProgramDataCallback, ImportOnceOptions} from './typedef';
3+
import type {ProgramData, ProgramDataCallback, SASSCompilerConfig} from './typedef';
44
import {Compiler} from './Compiler';
55
import {render} from 'node-sass';
66
import importer from 'node-sass-import-once';
@@ -16,7 +16,11 @@ const precision = 8,
1616
'node_modules/font-awesome/scss',
1717
'node_modules',
1818
'node_modules/bootswatch'
19-
];
19+
],
20+
defaultOptions = {
21+
includePaths: [],
22+
importOnce: {}
23+
};
2024

2125
/**
2226
* A SASS compiler
@@ -33,11 +37,7 @@ const precision = 8,
3337
*
3438
* @class SASSCompiler
3539
* @extends Compiler
36-
* @param {boolean} [compress=true] - if true `Compiler#save` will gzip compress the data in production
37-
* mode
38-
* @param {Array<string>} [includePaths=[]] - an array of additional include paths
39-
* @param {ImportOnceOptions} [importOnceOptions={}] - an object that lets you override default importOnce resolver
40-
* configuration
40+
* @param {SASSCompilerConfig} [options={}] - configuration object
4141
* @example
4242
* import {SASSCompiler} from 'webcompiler';
4343
* // or - import {SASSCompiler} from 'webcompiler/lib/SASSCompiler';
@@ -55,26 +55,6 @@ const precision = 8,
5555
*/
5656
export class SASSCompiler extends Compiler {
5757

58-
/**
59-
* an array of paths to search for an scss file in if it's not found in cwd
60-
*
61-
* @member {Array<string>} includePaths
62-
* @memberof SASSCompiler
63-
* @private
64-
* @instance
65-
*/
66-
includePaths: string[];
67-
68-
/**
69-
* importOnce resolver configuration
70-
*
71-
* @member {ImportOnceOptions} importOnce
72-
* @memberof SASSCompiler
73-
* @private
74-
* @instance
75-
*/
76-
importOnce: ImportOnceOptions;
77-
7858
/**
7959
* postcss plugins
8060
*
@@ -85,12 +65,15 @@ export class SASSCompiler extends Compiler {
8565
*/
8666
postcssPlugins: any[] = [autoprefixer];
8767

88-
/* eslint-disable require-jsdoc */
89-
constructor(compress: boolean = true, includePaths: string[] = [], importOnceOptions: ImportOnceOptions = {}) {
90-
/* eslint-enable require-jsdoc */
91-
super(compress);
92-
this.includePaths = defaultIncludePaths.concat(includePaths);
93-
this.importOnce = {...importOnceDefaults, ...importOnceOptions};
68+
// eslint-disable-next-line require-jsdoc
69+
constructor(options: SASSCompilerConfig = {}) {
70+
const {includePaths, importOnce, ...rest} = {...defaultOptions, ...options};
71+
72+
super({
73+
includePaths: defaultIncludePaths.concat(includePaths),
74+
importOnce: {...importOnceDefaults, ...importOnce},
75+
...rest
76+
});
9477
}
9578

9679
/**
@@ -144,13 +127,15 @@ export class SASSCompiler extends Compiler {
144127
* @param {Function} [callback=function () {}] - a callback function
145128
*/
146129
fe(inPath: string, outPath: string, callback: () => void = noop) {
130+
const {importOnce, includePaths} = this.options;
131+
147132
render({
148133
file: inPath,
149134
outFile: outPath,
150135
importer,
151136
precision,
152-
importOnce: this.importOnce,
153-
includePaths: this.includePaths,
137+
importOnce,
138+
includePaths,
154139
sourceMap: true,
155140
sourceMapContents: true,
156141
outputStyle: 'compressed'

0 commit comments

Comments
 (0)