Skip to content

Commit

Permalink
fix(compat): fix window path resolution compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jgranstrom committed Jul 20, 2017
1 parent 2004cd5 commit 835f542
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 38 deletions.
20 changes: 1 addition & 19 deletions src/extract.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import Promise from 'bluebird';
import sass from 'node-sass';
import path from 'path';
import { normalizePath, makeAbsolute } from './util';
import { loadCompiledFiles, loadCompiledFilesSync } from './load';
import { processFiles } from './process';
import { makeImporter, makeSyncImporter } from './importer';

Promise.promisifyAll(sass);

const NORMALIZED_PATH_SEPARATOR = '/';
const PLATFORM_PATH_SEPARATOR = path.sep;

/**
* Normalize path across platforms
*/
export function normalizePath(path) {
return path.split(PLATFORM_PATH_SEPARATOR).join(NORMALIZED_PATH_SEPARATOR);
}

export function makeAbsolute(maybeRelativePath) {
if(path.posix.isAbsolute(maybeRelativePath)) {
return maybeRelativePath;
} else {
return path.posix.join(process.cwd(), maybeRelativePath);
}
}

/**
* Get rendered stats required for extraction
*/
Expand Down
10 changes: 3 additions & 7 deletions src/importer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import { normalizePath, makeAbsolute } from './util';

/**
* Search for the imported file in order of included paths
Expand All @@ -14,13 +15,8 @@ function findImportedPath(url, prev, includedFilesMap, includedPaths) {
}

for(let i = 0; i < candidateFromPaths.length; i++) {
let candidateFromPath = candidateFromPaths[i];

// Ensure we get absolute included path
if(!path.posix.isAbsolute(candidateFromPath)) {
candidateFromPath = path.posix.join(process.cwd(), candidateFromPath);
}

// Get normalize absolute candidate from path
let candidateFromPath = normalizePath(makeAbsolute(candidateFromPaths[i]));
let candidatePath = path.posix.join(candidateFromPath, url);

if(includedFilesMap[candidatePath]) {
Expand Down
23 changes: 23 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import path from 'path';

const NORMALIZED_PATH_SEPARATOR = '/';
const PLATFORM_PATH_SEPARATOR = path.sep;

/**
* Normalize path across platforms
*/
export function normalizePath(path) {
return path.split(PLATFORM_PATH_SEPARATOR).join(NORMALIZED_PATH_SEPARATOR);
}

/**
* Make a potentially relative path absolute relative to cwd
*/
export function makeAbsolute(maybeRelativePath) {
if(path.isAbsolute(maybeRelativePath)) {
return maybeRelativePath;
} else {
return path.posix.join(process.cwd(), maybeRelativePath);
}
}

4 changes: 2 additions & 2 deletions test/basic.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect } = require('chai');
const path = require('path');
const { render, renderSync } = require('../lib');
const { normalizePath } = require('../lib/extract');
const { normalizePath } = require('../lib/util');
const { EOL } = require('os');

const basicImplicitFile = path.join(__dirname, 'sass', 'basic-implicit.scss');
Expand Down Expand Up @@ -62,7 +62,7 @@ function verifyBasic(rendered, sourceFile, mapIncluded) {
expect(rendered.vars.global.$list.value[2].value.b).to.equal(0);
expect(rendered.vars.global.$list.value[2].value.a).to.equal(1);
expect(rendered.vars.global.$list.value[2].value.hex).to.equal('#000000');
expect(rendered.vars.global.$list.value[2].type).to.equal('SassColor');
expect(rendered.vars.global.$list.value[2].type).to.equal('SassColor');

expect(rendered.vars.global.$string.value).to.equal('string');
expect(rendered.vars.global.$string.type).to.equal('SassString');
Expand Down
2 changes: 1 addition & 1 deletion test/comments.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect } = require('chai');
const path = require('path');
const { render, renderSync } = require('../lib');
const { normalizePath } = require('../lib/extract');
const { normalizePath } = require('../lib/util');

const commentFile = path.join(__dirname, 'sass', 'comments.scss');

Expand Down
2 changes: 1 addition & 1 deletion test/functions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect } = require('chai');
const path = require('path');
const { render, renderSync } = require('../lib');
const { normalizePath } = require('../lib/extract');
const { normalizePath } = require('../lib/util');
const { types } = require('node-sass');

const functionsFile = path.join(__dirname, 'sass', 'functions.scss');
Expand Down
10 changes: 5 additions & 5 deletions test/include.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect } = require('chai');
const path = require('path');
const { render, renderSync } = require('../lib');
const { normalizePath } = require('../lib/extract');
const { normalizePath } = require('../lib/util');

const includeRootFile = path.join(__dirname, 'sass', 'include', 'root.scss');
const includeRoot2File = path.join(__dirname, 'sass', 'include', 'root2.scss');
Expand Down Expand Up @@ -45,10 +45,10 @@ function verifyFunctions(rendered, sourceFile, includedColor, separateColor, inc
describe('include', () => {
describe('sub only', () => {
describe('root1', () => {
describe('sync', () => {
it('should extract all variables', () => {
const rendered = renderSync({ file: includeRootFile, includePaths: [includeSubDir] });
verifyFunctions(rendered, includeRootFile, SUB_INCLUDED_COLOR, SUB_INCLUDED2_COLOR, includeSubFile, includeSubFile2);
describe('sync', () => {
it('should extract all variables', () => {
const rendered = renderSync({ file: includeRootFile, includePaths: [includeSubDir] });
verifyFunctions(rendered, includeRootFile, SUB_INCLUDED_COLOR, SUB_INCLUDED2_COLOR, includeSubFile, includeSubFile2);
});
});

Expand Down
2 changes: 1 addition & 1 deletion test/inline.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect } = require('chai');
const path = require('path');
const { render, renderSync } = require('../lib');
const { normalizePath } = require('../lib/extract');
const { normalizePath } = require('../lib/util');

const inlineData = `
$number1: 123px;
Expand Down
2 changes: 1 addition & 1 deletion test/nested.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect } = require('chai');
const path = require('path');
const { render, renderSync } = require('../lib');
const { normalizePath } = require('../lib/extract');
const { normalizePath } = require('../lib/util');

const nestedBasicFile = path.join(__dirname, 'sass', 'nested', 'nested-basic.scss');
const nestedSubFile = path.join(__dirname, 'sass', 'nested', 'sub', 'sub.scss');
Expand Down
2 changes: 1 addition & 1 deletion test/partial.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { expect } = require('chai');
const path = require('path');
const { render, renderSync } = require('../lib');
const { normalizePath } = require('../lib/extract');
const { normalizePath } = require('../lib/util');

const partialFile = path.join(__dirname, 'sass', 'partial.scss');
const somePartialFile = path.join(__dirname, 'sass', '_somepartial.scss');
Expand Down

0 comments on commit 835f542

Please sign in to comment.