Skip to content

Commit 4179cee

Browse files
committed
add plugin support
1 parent b42fc52 commit 4179cee

9 files changed

Lines changed: 67 additions & 10 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"dripip": "^0.10.0",
4848
"execa": "^5.1.1",
4949
"husky": "^7.0.4",
50+
"postcss-calc": "^8.2.3",
5051
"postcss-scss": "^4.0.3",
5152
"rimraf": "^3.0.2",
5253
"tsdx": "^0.14.1",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* prettier-ignore */
2+
h1 {
3+
font-size: calc(16px * 2);
4+
height: calc(100px - 2em);
5+
width: calc(2 * var(--base-width));
6+
margin-bottom: calc(16px * 1.5);
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* prettier-ignore */
2+
h1 {
3+
font-size: 32px;
4+
height: calc(100px - 2em);
5+
width: calc(var(--base-width)*2);
6+
margin-bottom: 24px;
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Transform } from '../../src';
2+
import calc from 'postcss-calc';
3+
4+
export const transform: Transform = (fileInfo, api) => {
5+
const root = api.parse(fileInfo.source);
6+
return root.toString();
7+
};
8+
9+
export const plugins = [calc({})];

src/api.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import postcss, { Root, Parser } from 'postcss';
1+
import postcss, { Root, Parser, AcceptedPlugin } from 'postcss';
2+
23
export interface TransformAPI {
34
/**
45
* Parse a raw CSS string into an abstract syntax tree and return a PostCSS `Root` node.
@@ -8,11 +9,13 @@ export interface TransformAPI {
89

910
const createAPIParse = ({
1011
parser,
12+
plugins = [],
1113
}: {
1214
parser?: Parser;
15+
plugins?: AcceptedPlugin[];
1316
}): TransformAPI['parse'] => {
1417
const parse: TransformAPI['parse'] = source => {
15-
const result = postcss().process(source, {
18+
const result = postcss(plugins).process(source, {
1619
// Silence warning about sourcemaps. Not relevant to this use case.
1720
from: undefined,
1821
parser,
@@ -40,9 +43,10 @@ const createAPIParse = ({
4043

4144
export const createAPI = ({
4245
parser,
43-
}: { parser?: Parser } = {}): TransformAPI => {
46+
plugins,
47+
}: { parser?: Parser; plugins?: AcceptedPlugin[] } = {}): TransformAPI => {
4448
const api: TransformAPI = {
45-
parse: createAPIParse({ parser }),
49+
parse: createAPIParse({ parser, plugins }),
4650
};
4751

4852
return api;

src/perform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ interface ProcessTransformOptions {
1818
* Perform a transformation across a set of files.
1919
*/
2020
export const perform = async (options: ProcessTransformOptions) => {
21-
const { transform, parser } = await loadTransform(options.transform);
21+
const { transform, parser, plugins } = await loadTransform(options.transform);
2222
const files = getAllFilesToTransform(options.files);
23-
const api = createAPI({ parser });
23+
const api = createAPI({ parser, plugins });
2424

2525
files.map(file => {
2626
const fileInfo = getFileInfo(file);

src/transform.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bundleRequire } from 'bundle-require';
2-
import { Parser } from 'postcss';
2+
import { AcceptedPlugin, Parser } from 'postcss';
33
import { TransformAPI } from './api';
44
import { TransformFileInfo } from './files';
55

@@ -38,6 +38,7 @@ export const validateTransform = (transform: unknown): Transform => {
3838
interface LoadTransformResult {
3939
transform: Transform;
4040
parser?: Parser;
41+
plugins?: AcceptedPlugin[];
4142
}
4243

4344
/**
@@ -48,9 +49,11 @@ export const loadTransform = async (
4849
): Promise<LoadTransformResult> => {
4950
try {
5051
const { mod } = await bundleRequire({ filepath });
52+
5153
const transform = validateTransform(mod.transform || mod.default);
5254
const parser = mod.parser;
53-
return { transform, parser };
55+
const plugins = mod.plugins;
56+
return { transform, parser, plugins };
5457
} catch (err) {
5558
console.error(
5659
`An error occurred loading the transform file. Verify "${filepath}" exists.`

test/cli.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('cli', () => {
6767
});
6868

6969
afterAll(() => {
70-
fs.removeSync(cache);
70+
// fs.removeSync(cache);
7171
});
7272

7373
it.each(recipes)('should perform %s transform correctly', recipe => {

yarn.lock

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2429,6 +2429,11 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.3:
24292429
shebang-command "^2.0.0"
24302430
which "^2.0.1"
24312431

2432+
cssesc@^3.0.0:
2433+
version "3.0.0"
2434+
resolved "https://registry.yarnpkg.com/cssesc/-/cssesc-3.0.0.tgz#37741919903b868565e1c09ea747445cd18983ee"
2435+
integrity sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg==
2436+
24322437
cssom@^0.4.4:
24332438
version "0.4.4"
24342439
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.4.4.tgz#5a66cf93d2d0b661d80bf6a44fb65f5c2e4e0a10"
@@ -5116,6 +5121,14 @@ pkg-dir@^4.1.0, pkg-dir@^4.2.0:
51165121
dependencies:
51175122
find-up "^4.0.0"
51185123

5124+
postcss-calc@^8.2.3:
5125+
version "8.2.3"
5126+
resolved "https://registry.yarnpkg.com/postcss-calc/-/postcss-calc-8.2.3.tgz#53b95ce93de19213c2a5fdd71277a81690ef41d0"
5127+
integrity sha512-EGM2EBBWqP57N0E7N7WOLT116PJ39dwHVU01WO4XPPQLJfkL2xVgkMZ+TZvCfapj/uJH07UEfKHQNPHzSw/14Q==
5128+
dependencies:
5129+
postcss-selector-parser "^6.0.2"
5130+
postcss-value-parser "^4.0.2"
5131+
51195132
postcss-load-config@^3.0.1:
51205133
version "3.1.1"
51215134
resolved "https://registry.yarnpkg.com/postcss-load-config/-/postcss-load-config-3.1.1.tgz#2f53a17f2f543d9e63864460af42efdac0d41f87"
@@ -5129,6 +5142,19 @@ postcss-scss@^4.0.3:
51295142
resolved "https://registry.yarnpkg.com/postcss-scss/-/postcss-scss-4.0.3.tgz#36c23c19a804274e722e83a54d20b838ab4767ac"
51305143
integrity sha512-j4KxzWovfdHsyxwl1BxkUal/O4uirvHgdzMKS1aWJBAV0qh2qj5qAZqpeBfVUYGWv+4iK9Az7SPyZ4fyNju1uA==
51315144

5145+
postcss-selector-parser@^6.0.2:
5146+
version "6.0.9"
5147+
resolved "https://registry.yarnpkg.com/postcss-selector-parser/-/postcss-selector-parser-6.0.9.tgz#ee71c3b9ff63d9cd130838876c13a2ec1a992b2f"
5148+
integrity sha512-UO3SgnZOVTwu4kyLR22UQ1xZh086RyNZppb7lLAKBFK8a32ttG5i87Y/P3+2bRSjZNyJ1B7hfFNo273tKe9YxQ==
5149+
dependencies:
5150+
cssesc "^3.0.0"
5151+
util-deprecate "^1.0.2"
5152+
5153+
postcss-value-parser@^4.0.2:
5154+
version "4.2.0"
5155+
resolved "https://registry.yarnpkg.com/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz#723c09920836ba6d3e5af019f92bc0971c02e514"
5156+
integrity sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==
5157+
51325158
postcss@^8.4.5:
51335159
version "8.4.5"
51345160
resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.4.5.tgz#bae665764dfd4c6fcc24dc0fdf7e7aa00cc77f95"
@@ -6314,7 +6340,7 @@ uri-js@^4.2.2:
63146340
dependencies:
63156341
punycode "^2.1.0"
63166342

6317-
util-deprecate@^1.0.1:
6343+
util-deprecate@^1.0.1, util-deprecate@^1.0.2:
63186344
version "1.0.2"
63196345
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
63206346
integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=

0 commit comments

Comments
 (0)