Skip to content

Commit

Permalink
Support sass file includes
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
dsc8x committed Jul 8, 2018
1 parent e11d922 commit 692d568
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@epegzz/sass-vars-loader",
"version": "3.3.2",
"version": "3.4.0",
"author": "Daniel Schäfer <[email protected]>",
"description": "A SASS vars loader for Webpack. Load global SASS vars from JS/JSON files or from Webpack config.",
"keywords": [
Expand Down
13 changes: 9 additions & 4 deletions src/__snapshots__/sassVarsLoader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`With sass syntax Returns expected Sass contents 1`] = `
"$value1FromWebpack: foo
"
$value1FromWebpack: foo
$nested: (works: (veryWell: true, withoutProblems: indeed))
sassFileContents"
`;

exports[`With vars from JSON, JS and config Returns expected Sass contents 1`] = `
"$value1FromJson: foo;
"
$value1FromJson: foo;
$loadingOrderTest1: fromJSON;
$loadingOrderTest2: fromJS;
$value1FromJs: foo;
Expand All @@ -16,7 +18,8 @@ sassFileContents"
`;

exports[`With vars from files Returns expected Sass contents 1`] = `
"$value1FromJson: foo;
"
$value1FromJson: foo;
$loadingOrderTest1: fromJSON;
$loadingOrderTest2: fromJS;
$value2FromJson: foo;
Expand All @@ -26,12 +29,14 @@ sassFileContents"
`;

exports[`With vars from webpack config Returns expected Sass contents 1`] = `
"$value1FromWebpack: foo;
"
$value1FromWebpack: foo;
$nested: (works: (veryWell: true, withoutProblems: indeed));
sassFileContents"
`;

exports[`Without options Returns expected Sass contents 1`] = `
"
sassFileContents"
`;
11 changes: 6 additions & 5 deletions src/sassVarsLoader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import loaderUtils from 'loader-utils'
import getVarsFromJSONFiles from './utils/readVarsFromJSONFiles'
import getVarsFromJavascriptFiles from './utils/readVarsFromJavascriptFiles'
import readVarsFromJSONFiles from './utils/readVarsFromJSONFiles'
import readVarsFromJavascriptFiles from './utils/readVarsFromJavascriptFiles'
import readSassFiles from './utils/readSassFiles'
import watchFilesForChanges from './utils/watchFilesForChanges'
import convertJsToSass from './utils/convertJsToSass'

Expand All @@ -14,12 +15,12 @@ export default function(content) {
watchFilesForChanges(this, files)

const vars = {
...getVarsFromJSONFiles(files),
...getVarsFromJavascriptFiles(files),
...readVarsFromJSONFiles(files),
...readVarsFromJavascriptFiles(files),
...options.vars,
}

const sassVarsString = convertJsToSass(vars, syntax)

return `${sassVarsString}\n${content}`
return [readSassFiles(files), sassVarsString, content].join('\n')
}
10 changes: 10 additions & 0 deletions src/utils/readSassFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fs from 'fs'

export default function(files) {
return files.reduce((vars, filepath) => {
if (filepath.match(/\.s[ac]ss/)) {
return [vars, fs.readFileSync(filepath, 'utf8')].join('\n')
}
return vars
}, '')
}

0 comments on commit 692d568

Please sign in to comment.