diff --git a/package.json b/package.json index 7539a31..247f6e5 100644 --- a/package.json +++ b/package.json @@ -25,8 +25,7 @@ "license": "Apache-2.0", "repository": "https://github.com/google/postcss-rename", "dependencies": { - "postcss": "^7.0.18", - "postcss-selector-parser": "^6.0.2" + "postcss-selector-parser": "^6.0.4" }, "devDependencies": { "@babel/cli": "^7.6.0", @@ -40,9 +39,13 @@ "gts": "2.0.0", "jest": "^24.9.0", "jest-cli": "^24.9.0", + "postcss": "^8.2.6", "ts-jest": "^24.2.0", "typescript": "^3.8.0" }, + "peerDependencies": { + "postcss": "^8.2.6" + }, "engines": { "node": ">=8.3.0" } diff --git a/src/index.ts b/src/index.ts index ed80828..c7001ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,6 @@ * limitations under the License. */ -import * as postcss from 'postcss'; import selectorParser from 'postcss-selector-parser'; import {MinimalRenamer} from './minimal-renamer'; @@ -33,19 +32,19 @@ namespace plugin { } // eslint-disable-next-line no-redeclare -const plugin = postcss.plugin( - 'postcss-rename', - ({ - strategy = 'none', - by = 'whole', - prefix = '', - except = [], - ids = false, - outputMapCallback, - }: plugin.Options = {}) => { - const exceptSet = new Set(except); - return (root: postcss.Root): void => { - if (strategy === 'none' && !outputMapCallback && !prefix) return; +const plugin = ({ + strategy = 'none', + by = 'whole', + prefix = '', + except = [], + ids = false, + outputMapCallback, +}: plugin.Options = {}) => { + const exceptSet = new Set(except); + return { + postcssPlugin: 'postcss-rename', + prepare() { + if (strategy === 'none' && !outputMapCallback && !prefix) return {}; const outputMap: {[key: string]: string} | null = outputMapCallback ? {} @@ -97,11 +96,16 @@ const plugin = postcss.plugin( if (ids) selectors.walkIds(renameNode); }); - root.walkRules(ruleNode => selectorProcessor.process(ruleNode)); - - if (outputMapCallback) outputMapCallback(outputMap); - }; - } -); + return { + Rule(ruleNode) { + selectorProcessor.process(ruleNode); + }, + OnceExit() { + if (outputMapCallback) outputMapCallback(outputMap); + }, + }; + }, + }; +}; export = plugin; diff --git a/test/index.test.ts b/test/index.test.ts index f0b0394..581e925 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -16,17 +16,14 @@ */ import plugin = require('../src'); -import postcss from 'postcss'; +import postcss, {Result} from 'postcss'; import {toShortName} from '../src/minimal-renamer'; -async function run( - input: string, - options?: plugin.Options -): Promise { +async function run(input: string, options?: plugin.Options): Promise { return await postcss([plugin(options)]).process(input, {from: undefined}); } -function assertPostcss(result: postcss.Result, output: string): void { +function assertPostcss(result: Result, output: string): void { expect(result.css).toEqual(output); expect(result.warnings()).toHaveLength(0); } diff --git a/yarn.lock b/yarn.lock index bb94681..1bceae7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1686,6 +1686,11 @@ color-name@~1.1.4: resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== +colorette@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.1.tgz#4d0b921325c14faf92633086a536db6e89564b1b" + integrity sha512-puCDz0CzydiSYOrnXpz/PKd69zRrribezjtE9yd4zvytoRc8+RY/KJPvtPFKZS3E3wP6neGyMe0vOTlHO5L3Pw== + combined-stream@^1.0.6, combined-stream@~1.0.6: version "1.0.8" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" @@ -3810,6 +3815,11 @@ nan@^2.12.1: resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c" integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg== +nanoid@^3.1.20: + version "3.1.20" + resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.20.tgz#badc263c6b1dcf14b71efaa85f6ab4c1d6cfc788" + integrity sha512-a1cQNyczgKbLX9jwbS/+d7W8fX/RfgYR7lVWwWOGIPNgK2m0MWvrGF6/m4kk6U3QcFMnZf3RIhL0v2Jgh/0Uxw== + nanomatch@^1.2.9: version "1.2.13" resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119" @@ -4177,23 +4187,24 @@ posix-character-classes@^0.1.0: resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab" integrity sha1-AerA/jta9xoqbAL+q7jB/vfgDqs= -postcss-selector-parser@^6.0.2: - version "6.0.2" - resolved "https://registry.yarnpkg.com/postcss-selector-parser/-/postcss-selector-parser-6.0.2.tgz#934cf799d016c83411859e09dcecade01286ec5c" - integrity sha512-36P2QR59jDTOAiIkqEprfJDsoNrvwFei3eCqKd1Y0tUsBimsq39BLp7RD+JWny3WgB1zGhJX8XVePwm9k4wdBg== +postcss-selector-parser@^6.0.4: + version "6.0.4" + resolved "https://registry.yarnpkg.com/postcss-selector-parser/-/postcss-selector-parser-6.0.4.tgz#56075a1380a04604c38b063ea7767a129af5c2b3" + integrity sha512-gjMeXBempyInaBqpp8gODmwZ52WaYsVOsfr4L4lDQ7n3ncD6mEyySiDtgzCT+NYC0mmeOLvtsF8iaEf0YT6dBw== dependencies: cssesc "^3.0.0" indexes-of "^1.0.1" uniq "^1.0.1" + util-deprecate "^1.0.2" -postcss@^7.0.18: - version "7.0.27" - resolved "https://registry.yarnpkg.com/postcss/-/postcss-7.0.27.tgz#cc67cdc6b0daa375105b7c424a85567345fc54d9" - integrity sha512-WuQETPMcW9Uf1/22HWUWP9lgsIC+KEHg2kozMflKjbeUtw9ujvFX6QmIfozaErDkmLWS9WEnEdEe6Uo9/BNTdQ== +postcss@^8.2.6: + version "8.2.6" + resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.2.6.tgz#5d69a974543b45f87e464bc4c3e392a97d6be9fe" + integrity sha512-xpB8qYxgPuly166AGlpRjUdEYtmOWx2iCwGmrv4vqZL9YPVviDVPZPRXxnXr6xPZOdxQ9lp3ZBFCRgWJ7LE3Sg== dependencies: - chalk "^2.4.2" + colorette "^1.2.1" + nanoid "^3.1.20" source-map "^0.6.1" - supports-color "^6.1.0" prelude-ls@~1.1.2: version "1.1.2" @@ -5331,7 +5342,7 @@ use@^3.1.0: resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f" integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ== -util-deprecate@~1.0.1: +util-deprecate@^1.0.2, util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf" integrity sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=