Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Aug 16, 2019
1 parent b8a7ca7 commit 3545434
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
12 changes: 5 additions & 7 deletions src/normalizeOptions.js → src/getSassOptions.js
@@ -1,7 +1,6 @@
import os from 'os';
import path from 'path';

import utils from 'loader-utils';
import cloneDeep from 'clone-deep';

import proxyCustomImporters from './proxyCustomImporters';
Expand All @@ -13,12 +12,12 @@ import proxyCustomImporters from './proxyCustomImporters';
* That's why we must not modify the object directly.
*
* @param {LoaderContext} loaderContext
* @param {string} content
* @param {Function} webpackImporter
* @param {string} loaderOptions
* @param {object} content
* @returns {Object}
*/
function normalizeOptions(loaderContext, content, webpackImporter) {
const options = cloneDeep(utils.getOptions(loaderContext)) || {};
function getSassOptions(loaderContext, loaderOptions, content) {
const options = cloneDeep(loaderOptions);
const { resourcePath } = loaderContext;

// allow opt.functions to be configured WRT loaderContext
Expand Down Expand Up @@ -86,7 +85,6 @@ function normalizeOptions(loaderContext, content, webpackImporter) {
options.importer = options.importer
? proxyCustomImporters(options.importer, resourcePath)
: [];
options.importer.push(webpackImporter);

// `node-sass` uses `includePaths` to resolve `@import` paths. Append the currently processed file.
options.includePaths = options.includePaths || [];
Expand All @@ -95,4 +93,4 @@ function normalizeOptions(loaderContext, content, webpackImporter) {
return options;
}

export default normalizeOptions;
export default getSassOptions;
36 changes: 18 additions & 18 deletions src/index.js
Expand Up @@ -3,10 +3,11 @@ import path from 'path';
import async from 'neo-async';
import pify from 'pify';
import semver from 'semver';
import { getOptions } from 'loader-utils';

import formatSassError from './formatSassError';
import webpackImporter from './webpackImporter';
import normalizeOptions from './normalizeOptions';
import getSassOptions from './getSassOptions';

let nodeSassJobQueue = null;

Expand Down Expand Up @@ -34,17 +35,15 @@ function hasGetResolve(loaderContext) {
* @param {string} content
*/
function loader(content) {
const callback = this.async();
const isSync = typeof callback !== 'function';
const self = this;
const { resourcePath } = this;
const options = getOptions(this) || {};

function addNormalizedDependency(file) {
const callback = this.async();
const addNormalizedDependency = (file) => {
// node-sass returns POSIX paths
self.dependency(path.normalize(file));
}
this.dependency(path.normalize(file));
};

if (isSync) {
if (typeof callback !== 'function') {
throw new Error(
'Synchronous compilation is not supported anymore. See https://github.com/webpack-contrib/sass-loader/issues/333'
);
Expand All @@ -53,22 +52,22 @@ function loader(content) {
let resolve = pify(this.resolve);

// Supported since v4.36.0
if (hasGetResolve(self)) {
if (hasGetResolve(this)) {
resolve = this.getResolve({
mainFields: ['sass', 'style', 'main', '...'],
mainFiles: ['_index', 'index', '...'],
extensions: ['.scss', '.sass', '.css', '...'],
});
}

const options = normalizeOptions(
this,
content,
webpackImporter(resourcePath, resolve, addNormalizedDependency)
const sassOptions = getSassOptions(this, options, content);

sassOptions.importer.push(
webpackImporter(this.resourcePath, resolve, addNormalizedDependency)
);

// Skip empty files, otherwise it will stop webpack, see issue #21
if (options.data.trim() === '') {
if (sassOptions.data.trim() === '') {
callback(null, '');
return;
}
Expand All @@ -78,7 +77,7 @@ function loader(content) {
options.implementation || getDefaultSassImpl()
);

render(options, (error, result) => {
render(sassOptions, (error, result) => {
if (error) {
formatSassError(error, this.resourcePath);

Expand All @@ -101,7 +100,7 @@ function loader(content) {

// One of the sources is 'stdin' according to dart-sass/node-sass because we've used the data input.
// Now let's override that value with the correct relative path.
// Since we specified options.sourceMap = path.join(process.cwd(), "/sass.map"); in normalizeOptions,
// Since we specified options.sourceMap = path.join(process.cwd(), "/sass.map"); in getSassOptions,
// we know that this path is relative to process.cwd(). This is how node-sass works.
// eslint-disable-next-line no-param-reassign
const stdinIndex = result.map.sources.findIndex(
Expand All @@ -112,9 +111,10 @@ function loader(content) {
// eslint-disable-next-line no-param-reassign
result.map.sources[stdinIndex] = path.relative(
process.cwd(),
resourcePath
this.resourcePath
);
}

// node-sass returns POSIX paths, that's why we need to transform them back to native paths.
// This fixes an error on windows where the source-map module cannot resolve the source maps.
// @see https://github.com/webpack-contrib/sass-loader/issues/366#issuecomment-279460722
Expand Down

0 comments on commit 3545434

Please sign in to comment.