Skip to content

Commit

Permalink
Make loader ready for webpack 2
Browse files Browse the repository at this point in the history
- Do not read options from a custom key on the webpack config
- Refactor tests to use the new module rules syntax
  • Loading branch information
jhnns committed Dec 27, 2016
1 parent 58789df commit 74d007b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 46 deletions.
20 changes: 1 addition & 19 deletions lib/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const os = require("os");
const utils = require("loader-utils");
const assign = require("object-assign");
const path = require("path");
const proxyCustomImporters = require("./proxyCustomImporters");

Expand All @@ -15,7 +14,7 @@ const proxyCustomImporters = require("./proxyCustomImporters");
* @returns {Object}
*/
function normalizeOptions(loaderContext, content, webpackImporter) {
const options = getLoaderOptions(loaderContext);
const options = utils.parseQuery(loaderContext.query);
const resourcePath = loaderContext.resourcePath;

options.data = options.data ? (options.data + os.EOL + content) : content;
Expand Down Expand Up @@ -63,21 +62,4 @@ function normalizeOptions(loaderContext, content, webpackImporter) {
return options;
}

/**
* Check the loader query and webpack config for loader options. If an option is defined in both places,
* the loader query takes precedence.
*
* @param {LoaderContext} loaderContext
* @returns {Object}
*/
function getLoaderOptions(loaderContext) {
const query = utils.parseQuery(loaderContext.query);
const configKey = query.config || "sassLoader";
const config = loaderContext.options[configKey] || {};

delete query.config;

return assign({}, config, query);
}

module.exports = normalizeOptions;
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"async": "^2.0.1",
"loader-utils": "^0.2.15",
"lodash.tail": "^4.1.1",
"object-assign": "^4.1.0",
"pify": "^2.3.0"
},
"devDependencies": {
Expand All @@ -48,7 +47,7 @@
"raw-loader": "^0.5.1",
"should": "^11.1.0",
"style-loader": "^0.13.1",
"webpack": "^1.13.1",
"webpack": "^2.2.0-rc.2",
"webpack-dev-server": "^1.7.0",
"webpack-merge": "^2.0.0"
}
Expand Down
41 changes: 16 additions & 25 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use strict";

Object.assign = Object.assign || require("object-assign");
require("should");

const path = require("path");
Expand All @@ -20,15 +19,23 @@ const pathToErrorFile = path.resolve(__dirname, "./scss/error.scss");
const pathToErrorImport = path.resolve(__dirname, "./scss/error-import.scss");

syntaxStyles.forEach(ext => {
function execTest(testId, config) {
function execTest(testId, options) {
return new Promise((resolve, reject) => {
const sassFile = pathToSassFile(ext, testId);
const baseConfig = merge({
entry: sassFile,
entry: path.join(__dirname, ext, testId + "." + ext),
output: {
filename: "bundle." + ext + ".js"
},
module: {
rules: [{
test: new RegExp(`\\.${ ext }$`),
use: [
{ loader: "raw-loader" },
{ loader: pathToSassLoader, options }
]
}]
}
}, config || {});
});

runWebpack(baseConfig, (err) => err ? reject(err) : resolve());
}).then(() => {
Expand All @@ -55,32 +62,24 @@ syntaxStyles.forEach(ext => {
it("should resolve imports from other language style correctly", () => execTest("import-other-style"));
// Test for includePath imports
it("should resolve imports from another directory declared by includePaths correctly", () => execTest("import-include-paths", {
sassLoader: {
includePaths: [path.join(__dirname, ext, "from-include-path")]
}
includePaths: [path.join(__dirname, ext, "from-include-path")]
}));
it("should not resolve CSS imports", () => execTest("import-css"));
it("should compile bootstrap-sass without errors", () => execTest("bootstrap-sass"));
});
describe("custom importers", () => {
it("should use custom importer", () => execTest("custom-importer", {
sassLoader: {
importer: customImporter
}
importer: customImporter
}));
});
describe("custom functions", () => {
it("should expose custom functions", () => execTest("custom-functions", {
sassLoader: {
functions: customFunctions
}
functions: customFunctions
}));
});
describe("prepending data", () => {
it("should extend the data-option if present", () => execTest("prepending-data", {
sassLoader: {
data: "$prepended-data: hotpink;"
}
data: "$prepended-data: hotpink;"
}));
});
});
Expand Down Expand Up @@ -167,11 +166,3 @@ function runWebpack(baseConfig, done) {
done(err || null);
});
}

function pathToSassFile(ext, id) {
return [
"raw-loader",
pathToSassLoader,
path.join(__dirname, ext, id + "." + ext)
].join("!");
}

0 comments on commit 74d007b

Please sign in to comment.