Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
205 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"presets": ["es2015-loose", "stage-0"], | ||
"plugins": ["add-module-exports"], | ||
"env": { | ||
"development": { | ||
"sourceMaps": "inline" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
.nyc_output | ||
coverage | ||
dist | ||
node_modules | ||
!fixtures/node_modules | ||
!src/__tests__/fixtures/node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import test from 'ava'; | ||
import postcss from 'postcss'; | ||
import plugin from '..'; | ||
import {name} from '../../package.json'; | ||
|
||
const tests = [{ | ||
message: 'should enable all modules from css', | ||
fixture: '/* test comment */@use postcss-discard-comments;/* test comment */', | ||
expected: '', | ||
options: {modules: '*'} | ||
}, { | ||
message: 'should enable postcss-discard-comments from css', | ||
fixture: '/* test comment */@use postcss-discard-comments;/* test comment */', | ||
expected: '', | ||
options: {modules: ['postcss-discard-comments']} | ||
}, { | ||
message: 'should enable postcss-discard-comments from css, with options', | ||
fixture: '/*! license comment */@use postcss-discard-comments(removeAll: true);', | ||
expected: '', | ||
options: {modules: ['postcss-discard-comments']} | ||
}, { | ||
message: 'should enable postcss-discard-font-face from css, with array as options', | ||
fixture: '@use postcss-discard-font-face(["svg", "woff"]); @font-face { font-family: A; src: url("a.svg") format("svg"), url("a.ttf") format("truetype")}', | ||
expected: '@font-face { font-family: A; src: url("a.svg") format("svg")}', | ||
options: {modules: ['postcss-discard-font-face']} | ||
}, { | ||
message: 'should enable autoprefixer from css', | ||
fixture: '@use autoprefixer (remove: false; browsers: "> 1%, firefox 32");main{-webkit-border-radius:10px;border-radius:10px;display:flex;}', | ||
expected: 'main{-webkit-border-radius:10px;border-radius:10px;display:-webkit-box;display:flex;}', | ||
options: {modules: ['autoprefixer']} | ||
}, { | ||
message: 'should enable autoprefixer from css, with nested options', | ||
fixture: '@use autoprefixer { remove: false; browsers: > 0%, firefox 32 };main{-webkit-border-radius:10px;border-radius:10px;display:flex;}', | ||
expected: 'main{-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;display:-webkit-box;display:-webkit-flex;display:-moz-box;display:-ms-flexbox;display:flex;}', | ||
options: {modules: ['autoprefixer']} | ||
}, { | ||
message: 'should enable autoprefixer from css, with nested stringy options', | ||
fixture: '@use autoprefixer { remove: false; browsers: "> 0%, firefox 32" };main{-webkit-border-radius:10px;border-radius:10px;display:flex;}', | ||
expected: 'main{-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;display:-webkit-box;display:-webkit-flex;display:-moz-box;display:-ms-flexbox;display:flex;}', | ||
options: {modules: ['autoprefixer']} | ||
}, { | ||
message: 'should enable autoprefixer from css, with deeply nested options', | ||
fixture: '@use autoprefixer { remove: false; browsers: "> 0%, firefox 32"; foo: { bar: true } /* ignores comments */ };main{-webkit-border-radius:10px;border-radius:10px;display:flex;}', | ||
expected: 'main{-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px;display:-webkit-box;display:-webkit-flex;display:-moz-box;display:-ms-flexbox;display:flex;}', | ||
options: {modules: ['autoprefixer']} | ||
}, { | ||
message: 'should enable postcss-cssnext', | ||
fixture: '@use postcss-cssnext;:root{--a: red}h1{color: var(--a)}', | ||
expected: 'h1{color: red}', | ||
options: {modules: ['postcss-cssnext']} | ||
}]; | ||
|
||
function process (css, options) { | ||
return postcss(plugin(options)).process(css).css; | ||
} | ||
|
||
tests.forEach(({message, fixture, expected, options}) => { | ||
test(message, t => { | ||
t.deepEqual(process(fixture, options), expected); | ||
}); | ||
}); | ||
|
||
test('multiple runs', t => { | ||
const processor = postcss(plugin({modules: ['postcss-discard-comments']})); | ||
|
||
return processor.process('@use postcss-discard-comments;/*test*/').then(({css}) => { | ||
t.deepEqual(css, ''); | ||
}).then(processor.process('/*test*/').then(({css}) => { | ||
t.deepEqual(css, '/*test*/'); | ||
})); | ||
}); | ||
|
||
function shouldThrow (t, css, opts = {}) { | ||
t.throws(() => process(css, opts)); | ||
} | ||
|
||
test('should not support arrows', shouldThrow, '@use postcss-discard-comments(foo => bar)', {modules: ['postcss-discard-comments']}); | ||
test('should not support function syntax', shouldThrow, '@use postcss-discard-comments(function () { alert(1); })', {modules: ['postcss-discard-comments']}); | ||
test('should not support unclosed keys', shouldThrow, '@use postcss-discard-comments(removeAll:)', {modules: ['postcss-discard-comments']}); | ||
test('should not support unclosed arrays', shouldThrow, '@use postcss-discard-font-face(["svg")', {modules: ['postcss-discard-font-face']}); | ||
test('should not support plugins that are not whitelisted', shouldThrow, '@use postcss-discard-comments;'); | ||
test('should not support null', shouldThrow, '@use postcss-discard-comments;', {modules: null}); | ||
test('should not support false', shouldThrow, '@use postcss-discard-comments;', {modules: false}); | ||
test('should not support strings that are not "*"', shouldThrow, '@use postcss-discard-comments;', {modules: 'all'}); | ||
|
||
test('should use the postcss plugin api', t => { | ||
t.truthy(plugin().postcssVersion, 'should be able to access version'); | ||
t.deepEqual(plugin().postcssPlugin, name, 'should be able to access name'); | ||
}); | ||
|
||
test('should use plugins relative to CSS file when using resolveFromFile', t => { | ||
const inputFile = path.join(__dirname, 'fixtures', 'test.css'); | ||
const outputFile = path.join(__dirname, 'fixtures', 'test.out.css'); | ||
const inputCss = fs.readFileSync(inputFile); | ||
return postcss(plugin({modules: '*', resolveFromFile: true})).process(inputCss, { | ||
from: inputFile, | ||
to: outputFile | ||
}).then(({css}) => { | ||
t.deepEqual(css, '.foo {color: red;}\n', 'should remove background decls'); | ||
}); | ||
}); | ||
|
||
test('should give meaningful error when module is not found', t => { | ||
const inputFile = path.join(__dirname, 'fixtures', 'error.css'); | ||
const outputFile = path.join(__dirname, 'fixtures', 'error.out.css'); | ||
const inputCss = fs.readFileSync(inputFile); | ||
return postcss(plugin({modules: '*', resolveFromFile: true})).process(inputCss, { | ||
from: inputFile, | ||
to: outputFile | ||
}).catch(err => { | ||
t.deepEqual(err.message, `Cannot find module 'postcss-fourohfour'`); | ||
}); | ||
}); | ||
|
||
test('should not resolve plugins relative to CSS file by default', t => { | ||
const inputFile = path.join(__dirname, 'fixtures', 'test.css'); | ||
const outputFile = path.join(__dirname, 'fixtures', 'test.out.css'); | ||
const inputCss = fs.readFileSync(inputFile); | ||
return postcss(plugin({modules: '*'})).process(inputCss, { | ||
from: inputFile, | ||
to: outputFile | ||
}).catch(err => { | ||
t.deepEqual(err.message, `Cannot find module 'postcss-nobg'`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.