Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

support postcss 8 plugins, closes #5 #6

Merged
merged 10 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ os:
- osx

node_js:
- 14
- 12
- 10
- 8

jobs:
include:
- node_js: 8
os: windows
before_script: |
npm install postcss@7
nvs add 12
nvs use 12
npm run pretest:tape
nvs use 8
script: npm run test:tape:7 -- --ci true

install:
- git config --global core.autocrlf false
Expand Down
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,25 @@
"pretest:tape": "npm run build",
"test": "npm run test:js && npm run test:tape",
"test:js": "eslint src/{*,**/*}.js --cache --ignore-pattern .gitignore",
"test:tape": "node . --plugin test/plugin.js --config test",
"test:tape:ci": "node . --ci true --plugin test/plugin.js --config test"
"test:tape:7": "node . --plugin test/postcss7-plugin.js --config test",
"test:tape:8": "node . --plugin test/postcss8-plugin.js --config test",
"test:tape": "npm run test:tape:7 && npm run test:tape:8",
"test:tape:ci": "npm run test:tape:7 -- --ci true && npm run test:tape:8 -- --ci true"
},
"engines": {
"node": ">=8.0.0"
},
"peerDependencies": {
"postcss": "^7.0.0 || ^8.0.0"
},
"devDependencies": {
"@babel/core": "^7.7.2",
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
"@babel/preset-env": "^7.7.1",
"babel-eslint": "^10.0.3",
"eslint": "^6.6.0",
"postcss": "^7.0.21",
"rollup": "^1.26.3",
"postcss": "^8.0.5",
"rollup": "^2.27.1",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-terser": "^5.1.2"
},
Expand Down
42 changes: 34 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ import getErrorMessage from './lib/get-error-message';
import getOptions from './lib/get-options';
import path from 'path';

async function postcss8(plugins) {
const pkg = await import('postcss/package.json');
if (pkg.version[0] === '8') {
const m = await import('postcss');
return m.default(plugins);
} else {
throw new Error(`postcss@8 must be installed, found ${pkg.version}`);
}
}

function isPostcss8Plugin(plugin) {
return typeof plugin === 'function' && Object(plugin).postcss === true;
}

getOptions().then(
async options => {
let hadError = false;
Expand All @@ -13,12 +27,6 @@ getOptions().then(
for (const name in options.config) {
const test = options.config[name];

const testPlugin = typeof Object(test.plugin).process === 'function'
? test.plugin
: typeof test.plugin === 'function'
? { process: test.plugin }
: options.plugin;

const testBase = name.split(':')[0];
const testFull = name.split(':').join('.');

Expand All @@ -30,7 +38,19 @@ getOptions().then(
const processOptions = Object.assign({ from: sourcePath, to: resultPath }, test.processOptions);
const pluginOptions = test.options;

const pluginName = Object(testPlugin.postcss).postcssPlugin || 'postcss';
let rawPlugin = test.plugin || options.plugin;
if (rawPlugin.default) {
rawPlugin = rawPlugin.default;
}
const plugin = isPostcss8Plugin(rawPlugin)
? rawPlugin(pluginOptions)
: typeof Object(rawPlugin).process === 'function'
? rawPlugin
: typeof rawPlugin === 'function'
? { process: rawPlugin }
: Object(rawPlugin).postcssPlugin;

const pluginName = plugin.postcssPlugin || Object(rawPlugin.postcss).postcssPlugin || 'postcss';

log.wait(pluginName, test.message, options.ci);

Expand All @@ -42,7 +62,13 @@ getOptions().then(
const expectCSS = await safelyReadFile(expectPath);
const sourceCSS = await readOrWriteFile(sourcePath, expectCSS);

const result = await testPlugin.process(sourceCSS, processOptions, pluginOptions);
let result;
if (isPostcss8Plugin(rawPlugin)) {
const postcss = await postcss8([ plugin ]);
result = await postcss.process(sourceCSS, processOptions);
} else {
result = await plugin.process(sourceCSS, processOptions, pluginOptions);
}
const resultCSS = result.css;

if (options.fix) {
Expand Down
File renamed without changes.
14 changes: 14 additions & 0 deletions test/postcss8-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = function testPlugin(options) {
return {
postcssPlugin: 'test-plugin',
Root (root, { result }) {
if (Object(options).shouldFail) {
throw new Error('This should fail.');
} else if (Object(options).shouldWarn) {
result.warn('This should warn.');
}
}
};
};

module.exports.postcss = true;