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 30, 2018
1 parent a1f7456 commit fad5514
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 11 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-dynamic-import-node"
]
}
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
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@
"scripts": {
"build": "babel ./src -d dist",
"lint": "eslint ./src ./test",
"test": "ava",
"test": "ava ./test/*.js",
"cover": "nyc ava"
},
"ava": {
"require": [
"@babel/register"
]
},
"dependencies": {
"js-yaml": "^3.12.0",
"postcss": "^7.0.6",
Expand All @@ -45,7 +50,10 @@
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-object-rest-spread": "^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",
"babel-plugin-dynamic-import-node": "^2.2.0",
"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 @@ -32,12 +32,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' || ext === '.mjs') {
maps[name] = (await import(filename)).default;
} 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-es6.mjs
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!'
},
},
};
12 changes: 12 additions & 0 deletions test/fixture/javascript-es6/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
:root {
--javascript-es6-foo: foo value;
--javascript-es6-one-two-three: yeah!;
}

.foo {
content: var(--javascript-es6-foo);
}

.bar {
content: var(--javascript-es6-one-two-three);
}
7 changes: 7 additions & 0 deletions test/fixture/javascript-es6/input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.foo {
content: map(javascript-es6, foo);
}

.bar {
content: map(javascript-es6, one, two, three);
}
10 changes: 10 additions & 0 deletions test/fixture/javascript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
foo: 'foo value',
bar: 'bar value',
baz: ['one', 'two', 'three'],
one: {
two: {
three: 'yeah!',
},
},
};
12 changes: 12 additions & 0 deletions test/fixture/javascript/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
:root {
--javascript-foo: foo value;
--javascript-one-two-three: yeah!;
}

.foo {
content: var(--javascript-foo);
}

.bar {
content: var(--javascript-one-two-three);
}
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(javascript, foo);
}

.bar {
content: map(javascript, one, two, three);
}
26 changes: 25 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,8 @@ let opts = {
'breakpoints.yml',
'assets.yml',
'config.yml',
'javascript.js',
'javascript-es6.mjs',
],
};

Expand All @@ -30,6 +32,28 @@ 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('javascript:es6', async t => {
const input = read('javascript-es6/input.css');
const expected = read('javascript-es6/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 fad5514

Please sign in to comment.