Skip to content

Add support for preprocessing with node-sass #187

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"babel-plugin-syntax-jsx": "^6.18.0",
"babel-types": "^6.26.0",
"generic-names": "^1.0.3",
"node-sass": "^4.9.0",
"postcss": "^6.0.22",
"postcss-modules": "^1.1.0",
"postcss-modules-extract-imports": "^1.1.0",
Expand Down
28 changes: 25 additions & 3 deletions src/requireCssModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {
readFileSync
} from 'fs';
import sass from 'node-sass';
import postcss from 'postcss';
import genericNames from 'generic-names';
import ExtractImports from 'postcss-modules-extract-imports';
Expand All @@ -21,13 +22,19 @@ import type {

type FiletypeOptionsType = {|
+syntax: string,
+plugins?: $ReadOnlyArray<string | $ReadOnlyArray<[string, mixed]>>
+plugins?: $ReadOnlyArray<string | $ReadOnlyArray<[string, mixed]>>,
+importer?: string | $ReadOnlyArray<string>
|};

type FiletypesConfigurationType = {
[key: string]: FiletypeOptionsType
};

type SassOptionsType = {|
file: string,
importer?: Array<[mixed]>
|};

const getFiletypeOptions = (cssSourceFilePath: string, filetypes: FiletypesConfigurationType): ?FiletypeOptionsType => {
const extension = cssSourceFilePath.substr(cssSourceFilePath.lastIndexOf('.'));
const filetype = filetypes ? filetypes[extension] : null;
Expand Down Expand Up @@ -70,12 +77,27 @@ const getTokens = (runner, cssSourceFilePath: string, filetypeOptions: ?Filetype
from: cssSourceFilePath
};

let fileContents = readFileSync(cssSourceFilePath, 'utf-8');

if (filetypeOptions) {
options.syntax = getSyntax(filetypeOptions);
if (filetypeOptions.syntax === 'node-sass') {
const sassOptions: SassOptionsType = {file: cssSourceFilePath};

if (filetypeOptions.importer) {
sassOptions.importer = [].concat(filetypeOptions.importer).map((importerName) => {
// eslint-disable-next-line import/no-dynamic-require, global-require
return require(importerName);
});
}

fileContents = sass.renderSync(sassOptions).css.toString();
} else {
options.syntax = getSyntax(filetypeOptions);
}
}

const lazyResult = runner
.process(readFileSync(cssSourceFilePath, 'utf-8'), options);
.process(fileContents, options);

lazyResult
.warnings()
Expand Down
10 changes: 10 additions & 0 deletions src/schemas/optionsSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
},
"syntax": {
"type": "string"
},
"importer": {
"oneOf": [
{
"type": "string"
},
{
"type": "array"
}
]
}
},
"type": "object"
Expand Down