Skip to content

Commit eef7687

Browse files
committed
Refactor createSpec to use importer feature instead of string replacement
String replacement was too error prone...
1 parent 1a28a63 commit eef7687

File tree

1 file changed

+14
-38
lines changed

1 file changed

+14
-38
lines changed

test/tools/createSpec.js

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@ var path = require('path');
66

77
var testFolder = path.resolve(__dirname, '../');
88
var error = 'error';
9-
var filesWithTildeImports = [
10-
'imports', 'import-other-style', 'import-css'
11-
];
129

1310
function createSpec(ext) {
1411
var basePath = path.join(testFolder, ext);
15-
var nodeModulesPath = path.join(testFolder, 'node_modules');
16-
var tildeReplacement = path.relative(basePath, nodeModulesPath) + path.sep;
17-
var sassError;
12+
var testModulePath = path.relative(basePath, path.join(testFolder, 'node_modules', 'test-module'));
1813

1914
fs.readdirSync(path.join(testFolder, ext))
2015
.filter(function (file) {
@@ -23,41 +18,22 @@ function createSpec(ext) {
2318
.map(function (file) {
2419
var fileName = path.join(basePath, file);
2520
var fileWithoutExt = file.slice(0, -ext.length - 1);
26-
var oldFileContent;
27-
var newFileContent;
2821
var css;
2922

30-
if (filesWithTildeImports.indexOf(fileWithoutExt) > -1) {
31-
// We need to replace all occurrences of '~' with relative paths
32-
// so node-sass finds the imported files without webpack's resolving algorithm
33-
oldFileContent = fs.readFileSync(fileName, 'utf8');
34-
newFileContent = oldFileContent.replace(/~/g, tildeReplacement);
35-
fs.writeFileSync(fileName, newFileContent, 'utf8');
36-
}
37-
38-
try {
39-
css = sass.renderSync({
40-
file: fileName,
41-
includePaths: [
42-
path.join(testFolder, ext, 'another'),
43-
path.join(testFolder, ext, 'from-include-path')
44-
]
45-
}).css;
46-
fs.writeFileSync(path.join(basePath, 'spec', fileWithoutExt + '.css'), css, 'utf8');
47-
} catch (err) {
48-
// Capture the sass error, but don't crash the script in order to roll-back all temporary file changes
49-
sassError = err;
50-
}
51-
52-
if (filesWithTildeImports.indexOf(fileWithoutExt) > -1) {
53-
fs.writeFileSync(fileName, oldFileContent, 'utf8');
54-
}
23+
css = sass.renderSync({
24+
file: fileName,
25+
importer: function (url) {
26+
return {
27+
file: url.replace(/^~test-module/, testModulePath)
28+
};
29+
},
30+
includePaths: [
31+
path.join(testFolder, ext, 'another'),
32+
path.join(testFolder, ext, 'from-include-path')
33+
]
34+
}).css;
35+
fs.writeFileSync(path.join(basePath, 'spec', fileWithoutExt + '.css'), css, 'utf8');
5536
});
56-
57-
if (sassError) {
58-
// Now we can throw the sass error
59-
throw sassError;
60-
}
6137
}
6238

6339
module.exports = createSpec;

0 commit comments

Comments
 (0)