Skip to content

Commit a7171d0

Browse files
committed
Migrate to ESM
1 parent 4099e2e commit a7171d0

37 files changed

Lines changed: 284 additions & 152 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ npm install postcss postcss-cli postcss-sorting --save-dev
8686
Create a `postcss.config.js` with PostCSS Sorting configuration:
8787

8888
```js
89-
module.exports = {
89+
export default {
9090
plugins: {
9191
'postcss-sorting': {
9292
order: [
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ export default [
1919
},
2020
},
2121
},
22+
{
23+
files: ['eslint.config.js', 'index.js'],
24+
rules: {
25+
'import/no-default-export': 'off',
26+
},
27+
},
2228
];

index.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
const validateOptions = require('./lib/validateOptions');
2-
const isString = require('./lib/isString');
3-
const getContainingNode = require('./lib/getContainingNode');
4-
const sortNode = require('./lib/order/sortNode');
5-
const sortNodeProperties = require('./lib/properties-order/sortNodeProperties');
1+
import { validateOptions } from './lib/validateOptions.js';
2+
import { isString } from './lib/isString.js';
3+
import { getContainingNode } from './lib/getContainingNode.js';
4+
import { sortNode } from './lib/order/sortNode.js';
5+
import { sortNodeProperties } from './lib/properties-order/sortNodeProperties.js';
66

7-
module.exports = function postcssSorting(opts) {
7+
function postcssSorting(opts) {
88
return {
99
postcssPlugin: 'postcss-sorting',
1010
Root(css) {
1111
plugin(css, opts);
1212
},
1313
};
14-
};
14+
}
15+
16+
postcssSorting.postcss = true;
1517

16-
module.exports.postcss = true;
18+
export default postcssSorting;
1719

1820
function plugin(css, opts) {
1921
const validatedOptions = validateOptions(opts);
@@ -56,3 +58,6 @@ function plugin(css, opts) {
5658
});
5759
}
5860
}
61+
62+
/** These are not a public API, and could be removed at any point */
63+
export { sortNode, sortNodeProperties }; // eslint-disable-line unicorn/prefer-export-from

jest-setup.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const fs = require('fs');
2-
const path = require('path');
3-
const plugin = require('.');
4-
const postcss = require('postcss');
5-
const cssInJS = require('postcss-styled-syntax'); // eslint-disable-line import/no-extraneous-dependencies
6-
const html = require('postcss-html'); // eslint-disable-line import/no-extraneous-dependencies
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import postcssSorting from './index.js';
4+
import postcss from 'postcss';
5+
import cssInJS from 'postcss-styled-syntax'; // eslint-disable-line import/no-extraneous-dependencies
6+
import html from 'postcss-html'; // eslint-disable-line import/no-extraneous-dependencies
77

88
global.groupTest = function groupTest(testGroups) {
99
testGroups.forEach((group) => {
@@ -15,7 +15,7 @@ global.groupTest = function groupTest(testGroups) {
1515
const testFn = item.only ? test.only : test;
1616

1717
testFn(message, () =>
18-
postcss(plugin(group.options))
18+
postcss(postcssSorting(group.options))
1919
.process(item.fixture, { from: undefined })
2020
.then((root) => {
2121
expect(root.css).toEqual(item.expected);
@@ -64,7 +64,7 @@ global.runTest = function runTest(input, opts, dirname) {
6464
syntax = html;
6565
}
6666

67-
return postcss([plugin(opts)])
67+
return postcss([postcssSorting(opts)])
6868
.process(inputCSS, {
6969
from: inputPath,
7070
syntax,

lib/__tests__/test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
const plugin = require('../..');
2-
const postcss = require('postcss');
1+
import postcssSorting from '../../index.js';
2+
import postcss from 'postcss';
3+
4+
const __dirname = import.meta.dirname;
35

46
test(`Should do nothing if config is undefined`, () => {
57
runTest('empty-lines-preserve', undefined, __dirname);
@@ -11,7 +13,7 @@ test(`Should throw an error if config has error`, () => {
1113
order: 'Justice Rains From Above',
1214
};
1315

14-
return expect(postcss([plugin(opts)]).process('', { from: undefined })).rejects.toThrow(
16+
return expect(postcss([postcssSorting(opts)]).process('', { from: undefined })).rejects.toThrow(
1517
'postcss-sorting: order: Should be an array',
1618
);
1719
});

lib/__tests__/validate-options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const validateOptions = require('../validateOptions');
1+
import { validateOptions } from '../validateOptions.js';
22

33
function testConfig(input) {
44
let testFn;

lib/getComments.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
1-
module.exports = {
2-
beforeNode,
3-
afterNode,
4-
beforeDeclaration,
5-
afterDeclaration,
6-
};
7-
81
// eslint-disable-next-line max-params
9-
function beforeNode(comments, previousNode, node, currentInitialIndex) {
2+
export function beforeNode(comments, previousNode, node, currentInitialIndex) {
103
if (!previousNode || previousNode.type !== 'comment') {
114
return comments;
125
}
@@ -29,7 +22,7 @@ function beforeNode(comments, previousNode, node, currentInitialIndex) {
2922
}
3023

3124
// eslint-disable-next-line max-params
32-
function afterNode(comments, nextNode, node, currentInitialIndex) {
25+
export function afterNode(comments, nextNode, node, currentInitialIndex) {
3326
if (!nextNode || nextNode.type !== 'comment') {
3427
return comments;
3528
}
@@ -47,7 +40,7 @@ function afterNode(comments, nextNode, node, currentInitialIndex) {
4740
}
4841

4942
// eslint-disable-next-line max-params
50-
function beforeDeclaration(comments, previousNode, nodeData, currentInitialIndex) {
43+
export function beforeDeclaration(comments, previousNode, nodeData, currentInitialIndex) {
5144
if (!previousNode || previousNode.type !== 'comment') {
5245
return comments;
5346
}
@@ -73,7 +66,7 @@ function beforeDeclaration(comments, previousNode, nodeData, currentInitialIndex
7366
}
7467

7568
// eslint-disable-next-line max-params
76-
function afterDeclaration(comments, nextNode, nodeData, currentInitialIndex) {
69+
export function afterDeclaration(comments, nextNode, nodeData, currentInitialIndex) {
7770
if (!nextNode || nextNode.type !== 'comment') {
7871
return comments;
7972
}

lib/getContainingNode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function getContainingNode(node) {
1+
export function getContainingNode(node) {
22
if (node.type === 'rule' || node.type === 'atrule') {
33
return node;
44
}
@@ -14,4 +14,4 @@ module.exports = function getContainingNode(node) {
1414
}
1515

1616
return node;
17-
};
17+
}

lib/isAllowedToProcess.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const atRuleExcludes = ['function', 'if', 'else', 'for', 'each', 'while'];
22

3-
module.exports = function isAllowedToProcess(node, options) {
3+
export function isAllowedToProcess(node, options) {
44
if (node.type === 'atrule' && atRuleExcludes.includes(node.name)) {
55
return false;
66
}
@@ -24,4 +24,4 @@ module.exports = function isAllowedToProcess(node, options) {
2424
}
2525

2626
return true;
27-
};
27+
}

lib/isCustomProperty.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module.exports = function isCustomProperty(property) {
1+
export function isCustomProperty(property) {
22
return property.slice(0, 2) === '--';
3-
};
3+
}

0 commit comments

Comments
 (0)