From 14c4faae87305c9b965de4f468bb1e118f6b84cc Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Wed, 5 Feb 2020 11:36:29 -0500 Subject: [PATCH] feat: accept semver compatible postcss AST (#1049) --- package-lock.json | 19 +++++-------------- package.json | 3 ++- src/index.js | 24 ++++++++++++++---------- test/helpers/ast-loader.js | 27 +++++++++++++++++++++++++++ test/loader.test.js | 9 +++++---- 5 files changed, 53 insertions(+), 29 deletions(-) create mode 100644 test/helpers/ast-loader.js diff --git a/package-lock.json b/package-lock.json index 6921d2a5..ebd67ed7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -56,7 +56,6 @@ "json5": "^2.1.0", "lodash": "^4.17.13", "resolve": "^1.3.2", - "semver": "^5.4.1", "source-map": "^0.5.0" }, "dependencies": { @@ -818,8 +817,7 @@ "browserslist": "^4.6.0", "core-js-compat": "^3.6.0", "invariant": "^2.2.2", - "js-levenshtein": "^1.1.3", - "semver": "^5.5.0" + "js-levenshtein": "^1.1.3" } }, "@babel/runtime": { @@ -3744,7 +3742,6 @@ "requires": { "nice-try": "^1.0.4", "path-key": "^2.0.1", - "semver": "^5.5.0", "shebang-command": "^1.2.0", "which": "^1.2.9" } @@ -4252,7 +4249,6 @@ "requires": { "commander": "^2.19.0", "lru-cache": "^4.1.5", - "semver": "^5.6.0", "sigmund": "^1.0.1" }, "dependencies": { @@ -8699,8 +8695,7 @@ "integrity": "sha512-LS9X+dc8KLxXCb8dni79fLIIUA5VyZoyjSMCwTluaXA0o27cCK0bhXkpgw+sTXVpPy/lSO57ilRixqk0vDmtRA==", "dev": true, "requires": { - "pify": "^4.0.1", - "semver": "^5.6.0" + "pify": "^4.0.1" } }, "makeerror": { @@ -9015,7 +9010,6 @@ "minimist": "^1.2.0", "prop-ini": "^0.0.2", "readme-badger": "^0.3.0", - "semver": "^5.5.1", "smpltmpl": "^1.0.2", "split-lines": "^2.0.0", "strip-bom": "^3.0.0", @@ -9182,7 +9176,6 @@ "requires": { "growly": "^1.3.0", "is-wsl": "^1.1.0", - "semver": "^5.5.0", "shellwords": "^0.1.1", "which": "^1.3.0" } @@ -9212,7 +9205,6 @@ "requires": { "hosted-git-info": "^2.1.4", "resolve": "^1.10.0", - "semver": "2 || 3 || 4 || 5", "validate-npm-package-license": "^3.0.1" } }, @@ -11014,10 +11006,9 @@ } }, "semver": { - "version": "5.7.1", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", - "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", - "dev": true + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz", + "integrity": "sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==" }, "semver-compare": { "version": "1.0.0", diff --git a/package.json b/package.json index a9ed1cba..89701aa0 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,8 @@ "postcss-modules-scope": "^2.1.1", "postcss-modules-values": "^3.0.0", "postcss-value-parser": "^4.0.2", - "schema-utils": "^2.6.0" + "schema-utils": "^2.6.0", + "semver": "^6.3.0" }, "devDependencies": { "@babel/cli": "^7.7.7", diff --git a/src/index.js b/src/index.js index b4d7c311..6717d36c 100644 --- a/src/index.js +++ b/src/index.js @@ -2,24 +2,24 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -import validateOptions from 'schema-utils'; +import { getOptions, isUrlRequest } from 'loader-utils'; import postcss from 'postcss'; import postcssPkg from 'postcss/package.json'; +import validateOptions from 'schema-utils'; +import { satisfies } from 'semver'; -import { getOptions, isUrlRequest } from 'loader-utils'; - +import CssSyntaxError from './CssSyntaxError'; +import Warning from './Warning'; import schema from './options.json'; -import { importParser, icssParser, urlParser } from './plugins'; +import { icssParser, importParser, urlParser } from './plugins'; import { - normalizeSourceMap, - getModulesPlugins, + getExportCode, getFilter, getImportCode, getModuleCode, - getExportCode, + getModulesPlugins, + normalizeSourceMap, } from './utils'; -import Warning from './Warning'; -import CssSyntaxError from './CssSyntaxError'; export default function loader(content, map, meta) { const options = getOptions(this) || {}; @@ -63,7 +63,11 @@ export default function loader(content, map, meta) { if (meta) { const { ast } = meta; - if (ast && ast.type === 'postcss' && ast.version === postcssPkg.version) { + if ( + ast && + ast.type === 'postcss' && + satisfies(ast.version, `^${postcssPkg.version}`) + ) { // eslint-disable-next-line no-param-reassign content = ast.root; } diff --git a/test/helpers/ast-loader.js b/test/helpers/ast-loader.js new file mode 100644 index 00000000..392492d7 --- /dev/null +++ b/test/helpers/ast-loader.js @@ -0,0 +1,27 @@ +import postcss from 'postcss'; +import postcssPresetEnv from 'postcss-preset-env'; +import postcssPkg from 'postcss/package.json'; +import semver from 'semver'; + +const incomingVersion = semver.inc(postcssPkg.version, 'minor'); + +export default function astLoader(content) { + const callback = this.async(); + + const { spy = jest.fn() } = this.query; + + postcss([postcssPresetEnv({ stage: 0 })]) + .process(content) + .then(({ css, map, root, messages }) => { + const ast = { + type: 'postcss', + version: incomingVersion, + }; + + Object.defineProperty(ast, 'root', { + get: spy.mockReturnValue(root), + }); + + callback(null, css, map, { ast, messages }); + }); +} diff --git a/test/loader.test.js b/test/loader.test.js index 8763bd34..1c16d48d 100644 --- a/test/loader.test.js +++ b/test/loader.test.js @@ -1,7 +1,5 @@ import path from 'path'; -import postcssPresetEnv from 'postcss-preset-env'; - import { version } from 'webpack'; import { @@ -129,6 +127,7 @@ describe('loader', () => { }); it('should reuse `ast` from "postcss-loader"', async () => { + const spy = jest.fn(); const compiler = getCompiler( './postcss-present-env/source.js', {}, @@ -143,8 +142,8 @@ describe('loader', () => { options: { importLoaders: 1 }, }, { - loader: 'postcss-loader', - options: { plugins: () => [postcssPresetEnv({ stage: 0 })] }, + loader: require.resolve('./helpers/ast-loader'), + options: { spy }, }, ], }, @@ -159,6 +158,8 @@ describe('loader', () => { ); const stats = await compile(compiler); + expect(spy).toHaveBeenCalledTimes(1); + expect( getModuleSource('./postcss-present-env/source.css', stats) ).toMatchSnapshot('module');