Skip to content

Commit

Permalink
refactor: simplify createSpec.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns committed Mar 17, 2017
1 parent 7ccfa43 commit 45ff5ca
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions test/helpers/createSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,18 @@ const projectPath = path.resolve(__dirname, '..', '..');
const fixturesPath = path.resolve(projectPath, 'test', 'fixtures');
const lessFixturesPath = path.resolve(fixturesPath, 'less');
const cssFixturesPath = path.resolve(fixturesPath, 'css');
const matchWebpackImports = /(@import\s+(\([^)]+\))?\s*["'])~/g;
const lessBin = require.resolve('.bin/lessc');
const ignore = [
'import-non-less',
'error-import-not-existing',
'error-mixed-resolvers',
];
/**
* This object specifies the replacements for the ~-character per test.
*
* Since less doesn't understand the semantics of ~, we need to replace the
* import path with a relative path.
*
* The object keys are the test ids.
*/
const tildeReplacements = {
import: '../node_modules/',
'import-webpack': '../node_modules/',
'source-map': '../node_modules/',
};
const lessReplacements = [
[/~some\//g, '../node_modules/some/'],
];
const cssReplacements = [
[/\.\.\/node_modules\/some\//g, '~some/'],
];
// Maps test ids on cli arguments
const lessOptions = {
'source-map': [
Expand All @@ -46,17 +38,24 @@ const testIds = fs.readdirSync(lessFixturesPath)
path.basename(name, '.less'),
);

function replace(content, replacements) {
return replacements.reduce(
(intermediate, [pattern, replacement]) => intermediate.replace(pattern, replacement),
content,
);
}

testIds
.forEach((testId) => {
const lessFile = path.resolve(lessFixturesPath, `${testId}.less`);
const cssFile = path.resolve(cssFixturesPath, `${testId}.css`);
const tildeReplacement = tildeReplacements[testId];
const originalLessContent = fs.readFileSync(lessFile, 'utf8');
const replacedLessContent = replace(originalLessContent, lessReplacements);

// It's safer to change the file and write it back to disk instead of piping it to the Less process
// because Less tends to create broken paths in url() statements and source maps when the content is read from stdin
// See also https://github.com/less/less.js/issues/3038
fs.writeFileSync(lessFile, originalLessContent.replace(matchWebpackImports, `$1${tildeReplacement}`), 'utf8');
fs.writeFileSync(lessFile, replacedLessContent, 'utf8');

exec(
[lessBin, '--relative-urls', ...lessOptions[testId] || '', lessFile, cssFile].join(' '),
Expand All @@ -69,9 +68,7 @@ testIds
// We remove the source mapping url because the less-loader will do it also.
// See removeSourceMappingUrl.js for the reasoning behind this.
const cssContent = removeSourceMappingUrl(
fs.readFileSync(cssFile, 'utf8')
// Change back tilde replacements
.replace(new RegExp(`(@import\\s+["'])${tildeReplacement}`, 'g'), '$1~'),
replace(fs.readFileSync(cssFile, 'utf8'), cssReplacements),
);

fs.writeFileSync(lessFile, originalLessContent, 'utf8');
Expand Down

0 comments on commit 45ff5ca

Please # to comment.