Skip to content

Commit

Permalink
js map support. Fixes pascalduez#114
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy Johnson committed Nov 27, 2018
1 parent b346c5e commit d6bd288
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 10 deletions.
5 changes: 4 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
}
]
],
"plugins": ["@babel/plugin-proposal-object-rest-spread"]
"plugins": [
"@babel/plugin-proposal-object-rest-spread",
"@babel/plugin-syntax-dynamic-import"
]
}
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
parser: babel-eslint
extends: standard
root: true
env:
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"test": "ava",
"cover": "nyc ava"
},
"ava": {
"require": [
"@babel/register"
]
},
"dependencies": {
"js-yaml": "^3.12.0",
"postcss": "^7.0.6",
Expand All @@ -44,8 +49,11 @@
"@babel/cli": "^7.1.5",
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"@babel/register": "^7.0.0",
"ava": "^1.0.0-rc.2",
"babel-eslint": "^10.0.1",
"coveralls": "^3.0.2",
"eslint": "^5.9.0",
"eslint-config-standard": "^12.0.0",
Expand Down
15 changes: 9 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ export default postcss.plugin('postcss-map', opts => {
return path.resolve(opts.basePath, map);
});

let promises = paths.map(async map => {
let name = path.basename(map, path.extname(map));
let data = await readFile(map, 'utf-8');
maps[name] = yaml.safeLoad(data, {
filename: map,
});
let promises = paths.map(async filename => {
let ext = path.extname(filename);
let name = path.basename(filename, ext);
if (ext === '.js') {
maps[name] = await import(filename);
} else {
let data = await readFile(filename, 'utf-8');
maps[name] = yaml.safeLoad(data, { filename });
}
});

return css => {
Expand Down
2 changes: 1 addition & 1 deletion test/control.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import fs from 'fs';
import path from 'path';
import postcss from 'postcss';
import plugin from '../dist';
import plugin from '../src';

const pluginName = require('../package.json').name;
let from;
Expand Down
10 changes: 10 additions & 0 deletions test/fixture/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
foo: 'foo value',
bar: 'bar value',
baz: ['one', 'two', 'three'],
one: {
two: {
three: 'yeah!',
},
},
};
9 changes: 9 additions & 0 deletions test/fixture/javascript/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.foo {
content: foo value;
}

.bar {
background-image: url(../assets/img/upper.png),
url(../assets/img/between.png),
url(../assets/img/under.png);
}
7 changes: 7 additions & 0 deletions test/fixture/javascript/input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.foo {
content: map(dummy, foo);
}

.bar {
content: map(one, two, three);
}
14 changes: 13 additions & 1 deletion test/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import fs from 'fs';
import path from 'path';
import postcss from 'postcss';
import plugin from '../dist';
import plugin from '../src';

const read = name =>
fs.readFileSync(path.join(__dirname, 'fixture', name), 'utf8');
Expand All @@ -16,6 +16,7 @@ let opts = {
'breakpoints.yml',
'assets.yml',
'config.yml',
'javascript.js',
],
};

Expand All @@ -30,6 +31,17 @@ test('value', async t => {
t.is(result.css, expected);
});

test('javascript', async t => {
const input = read('javascript/input.css');
const expected = read('javascript/expected.css');

const result = await postcss()
.use(plugin(opts))
.process(input, { from });

t.is(result.css, expected);
});

test('block', async t => {
const input = read('block/input.css');
const expected = read('block/expected.css');
Expand Down

0 comments on commit d6bd288

Please sign in to comment.