diff --git a/package.json b/package.json index 32f4660..b530807 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/requireCssModule.js b/src/requireCssModule.js index 283718e..c82b73f 100644 --- a/src/requireCssModule.js +++ b/src/requireCssModule.js @@ -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'; @@ -21,13 +22,19 @@ import type { type FiletypeOptionsType = {| +syntax: string, - +plugins?: $ReadOnlyArray> + +plugins?: $ReadOnlyArray>, + +importer?: string | $ReadOnlyArray |}; 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; @@ -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() diff --git a/src/schemas/optionsSchema.json b/src/schemas/optionsSchema.json index e14e525..5b03e4a 100644 --- a/src/schemas/optionsSchema.json +++ b/src/schemas/optionsSchema.json @@ -28,6 +28,16 @@ }, "syntax": { "type": "string" + }, + "importer": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "array" + } + ] } }, "type": "object"