diff --git a/.travis.yml b/.travis.yml index 12ea3cd..3e1b0d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/package.json b/package.json index ee567a2..84f94d7 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/src/index.js b/src/index.js index 8d2a7cb..4938bb1 100644 --- a/src/index.js +++ b/src/index.js @@ -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; @@ -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('.'); @@ -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); @@ -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) { diff --git a/test/plugin.js b/test/postcss7-plugin.js similarity index 100% rename from test/plugin.js rename to test/postcss7-plugin.js diff --git a/test/postcss8-plugin.js b/test/postcss8-plugin.js new file mode 100644 index 0000000..b4e1bc1 --- /dev/null +++ b/test/postcss8-plugin.js @@ -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;