From 51339b1957c37f972325d01c5ca016972db0b87e Mon Sep 17 00:00:00 2001 From: Espen Hovlandsdal Date: Thu, 1 Apr 2021 23:10:37 -0700 Subject: [PATCH] fix(verify): allow query parameters on imports (raw css etc) --- src/dependencies/find.js | 18 ++++++--- .../verify/css-raw-import-missing/LICENSE | 1 + .../verify/css-raw-import-missing/README.md | 1 + .../verify/css-raw-import-missing/lib/one.js | 34 +++++++++++++++++ .../css-raw-import-missing/lib/styles/One.css | 3 ++ .../verify/css-raw-import-missing/lib/two.js | 3 ++ .../css-raw-import-missing/package.json | 19 ++++++++++ .../verify/css-raw-import-missing/sanity.json | 13 +++++++ .../verify/css-raw-import-missing/src/one.js | 13 +++++++ .../css-raw-import-missing/src/styles/One.css | 3 ++ .../verify/css-raw-import-missing/src/two.js | 3 ++ test/fixtures/verify/css-raw-import/LICENSE | 1 + test/fixtures/verify/css-raw-import/README.md | 1 + .../fixtures/verify/css-raw-import/lib/one.js | 34 +++++++++++++++++ .../verify/css-raw-import/lib/styles/One.css | 3 ++ .../fixtures/verify/css-raw-import/lib/two.js | 3 ++ .../verify/css-raw-import/package.json | 19 ++++++++++ .../verify/css-raw-import/sanity.json | 13 +++++++ .../fixtures/verify/css-raw-import/src/one.js | 13 +++++++ .../verify/css-raw-import/src/styles/One.css | 3 ++ .../fixtures/verify/css-raw-import/src/two.js | 3 ++ test/verify.test.js | 38 +++++++++++++------ 22 files changed, 225 insertions(+), 17 deletions(-) create mode 100644 test/fixtures/verify/css-raw-import-missing/LICENSE create mode 100644 test/fixtures/verify/css-raw-import-missing/README.md create mode 100644 test/fixtures/verify/css-raw-import-missing/lib/one.js create mode 100644 test/fixtures/verify/css-raw-import-missing/lib/styles/One.css create mode 100644 test/fixtures/verify/css-raw-import-missing/lib/two.js create mode 100644 test/fixtures/verify/css-raw-import-missing/package.json create mode 100644 test/fixtures/verify/css-raw-import-missing/sanity.json create mode 100644 test/fixtures/verify/css-raw-import-missing/src/one.js create mode 100644 test/fixtures/verify/css-raw-import-missing/src/styles/One.css create mode 100644 test/fixtures/verify/css-raw-import-missing/src/two.js create mode 100644 test/fixtures/verify/css-raw-import/LICENSE create mode 100644 test/fixtures/verify/css-raw-import/README.md create mode 100644 test/fixtures/verify/css-raw-import/lib/one.js create mode 100644 test/fixtures/verify/css-raw-import/lib/styles/One.css create mode 100644 test/fixtures/verify/css-raw-import/lib/two.js create mode 100644 test/fixtures/verify/css-raw-import/package.json create mode 100644 test/fixtures/verify/css-raw-import/sanity.json create mode 100644 test/fixtures/verify/css-raw-import/src/one.js create mode 100644 test/fixtures/verify/css-raw-import/src/styles/One.css create mode 100644 test/fixtures/verify/css-raw-import/src/two.js diff --git a/src/dependencies/find.js b/src/dependencies/find.js index aa433197..774b749f 100644 --- a/src/dependencies/find.js +++ b/src/dependencies/find.js @@ -146,25 +146,31 @@ function findDependencies(entryPath, seen = new Set()) { } function resolveDependency(fromDir, toPath, entryPath) { + const [querylessPath] = toPath.split('?', 1) + let depPath try { - depPath = require.resolve(path.resolve(fromDir, toPath)) + depPath = require.resolve(path.resolve(fromDir, querylessPath)) } catch (err) { - throw new Error(`Unable to resolve "${toPath}" from ${entryPath}`) + throw new Error(`Unable to resolve "${querylessPath}" from ${entryPath}`) } let actualPath try { actualPath = discoverPathSync(depPath) } catch (err) { - const paths = (err.suggestions || []).map((suggested) => getDidYouMeanPath(toPath, suggested)) + const paths = (err.suggestions || []).map((suggested) => + getDidYouMeanPath(querylessPath, suggested) + ) const didYouMean = paths ? `Did you mean:\n${paths.join('\n- ')}` : '' - throw new Error(`Unable to resolve "${toPath}" from ${entryPath}. ${didYouMean}`) + throw new Error(`Unable to resolve "${querylessPath}" from ${entryPath}. ${didYouMean}`) } if (actualPath !== depPath) { - const didYouMean = getDidYouMeanPath(toPath, actualPath) - throw new Error(`Unable to resolve "${toPath} from ${entryPath}. Did you mean "${didYouMean}"?`) + const didYouMean = getDidYouMeanPath(querylessPath, actualPath) + throw new Error( + `Unable to resolve "${querylessPath} from ${entryPath}. Did you mean "${didYouMean}"?` + ) } return actualPath diff --git a/test/fixtures/verify/css-raw-import-missing/LICENSE b/test/fixtures/verify/css-raw-import-missing/LICENSE new file mode 100644 index 00000000..615204bd --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/LICENSE @@ -0,0 +1 @@ +some license diff --git a/test/fixtures/verify/css-raw-import-missing/README.md b/test/fixtures/verify/css-raw-import-missing/README.md new file mode 100644 index 00000000..1f2d5e61 --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/README.md @@ -0,0 +1 @@ +# some cool diff --git a/test/fixtures/verify/css-raw-import-missing/lib/one.js b/test/fixtures/verify/css-raw-import-missing/lib/one.js new file mode 100644 index 00000000..668db535 --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/lib/one.js @@ -0,0 +1,34 @@ +'use strict' + +Object.defineProperty(exports, '__esModule', { + value: true, +}) +exports.default = void 0 + +var _react = _interopRequireDefault(require('react')) + +var _two = _interopRequireDefault(require('./two')) + +var _one = _interopRequireDefault(require('./styles/one.css?raw')) + +function _interopRequireDefault(obj) { + return obj && obj.__esModule ? obj : {default: obj} +} + +class One extends _react.default.PureComponent { + componentDidMount() { + ;(0, _two.default)() + } + + render() { + return /*#__PURE__*/ _react.default.createElement( + 'button', + { + className: _one.default.button, + }, + 'Click me' + ) + } +} + +exports.default = One diff --git a/test/fixtures/verify/css-raw-import-missing/lib/styles/One.css b/test/fixtures/verify/css-raw-import-missing/lib/styles/One.css new file mode 100644 index 00000000..5d486eba --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/lib/styles/One.css @@ -0,0 +1,3 @@ +.button { + background: #bf1942; +} diff --git a/test/fixtures/verify/css-raw-import-missing/lib/two.js b/test/fixtures/verify/css-raw-import-missing/lib/two.js new file mode 100644 index 00000000..411187ba --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/lib/two.js @@ -0,0 +1,3 @@ +export default function two() { + // do something important +} diff --git a/test/fixtures/verify/css-raw-import-missing/package.json b/test/fixtures/verify/css-raw-import-missing/package.json new file mode 100644 index 00000000..15051f1c --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/package.json @@ -0,0 +1,19 @@ +{ + "name": "sanity-plugin-css-part", + "version": "1.0.0", + "private": true, + "description": "Just a fixture", + "main": "./lib/one.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "sanity", + "sanity-plugin" + ], + "peerDependencies": { + "react": "*" + }, + "author": "Some person", + "license": "MIT" +} diff --git a/test/fixtures/verify/css-raw-import-missing/sanity.json b/test/fixtures/verify/css-raw-import-missing/sanity.json new file mode 100644 index 00000000..0c006ad1 --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/sanity.json @@ -0,0 +1,13 @@ +{ + "paths": { + "source": "./src", + "compiled": "./lib" + }, + + "parts": [ + { + "name": "part:css-part/some/thing", + "path": "one.js" + } + ] +} diff --git a/test/fixtures/verify/css-raw-import-missing/src/one.js b/test/fixtures/verify/css-raw-import-missing/src/one.js new file mode 100644 index 00000000..b217064a --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/src/one.js @@ -0,0 +1,13 @@ +import React from 'react' +import two from './two' +import styles from './styles/one.css?raw' + +export default class One extends React.PureComponent { + componentDidMount() { + two() + } + + render() { + return + } +} diff --git a/test/fixtures/verify/css-raw-import-missing/src/styles/One.css b/test/fixtures/verify/css-raw-import-missing/src/styles/One.css new file mode 100644 index 00000000..5d486eba --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/src/styles/One.css @@ -0,0 +1,3 @@ +.button { + background: #bf1942; +} diff --git a/test/fixtures/verify/css-raw-import-missing/src/two.js b/test/fixtures/verify/css-raw-import-missing/src/two.js new file mode 100644 index 00000000..411187ba --- /dev/null +++ b/test/fixtures/verify/css-raw-import-missing/src/two.js @@ -0,0 +1,3 @@ +export default function two() { + // do something important +} diff --git a/test/fixtures/verify/css-raw-import/LICENSE b/test/fixtures/verify/css-raw-import/LICENSE new file mode 100644 index 00000000..615204bd --- /dev/null +++ b/test/fixtures/verify/css-raw-import/LICENSE @@ -0,0 +1 @@ +some license diff --git a/test/fixtures/verify/css-raw-import/README.md b/test/fixtures/verify/css-raw-import/README.md new file mode 100644 index 00000000..1f2d5e61 --- /dev/null +++ b/test/fixtures/verify/css-raw-import/README.md @@ -0,0 +1 @@ +# some cool diff --git a/test/fixtures/verify/css-raw-import/lib/one.js b/test/fixtures/verify/css-raw-import/lib/one.js new file mode 100644 index 00000000..d446d063 --- /dev/null +++ b/test/fixtures/verify/css-raw-import/lib/one.js @@ -0,0 +1,34 @@ +'use strict' + +Object.defineProperty(exports, '__esModule', { + value: true, +}) +exports.default = void 0 + +var _react = _interopRequireDefault(require('react')) + +var _two = _interopRequireDefault(require('./two')) + +var _one = _interopRequireDefault(require('./styles/One.css?raw')) + +function _interopRequireDefault(obj) { + return obj && obj.__esModule ? obj : {default: obj} +} + +class One extends _react.default.PureComponent { + componentDidMount() { + ;(0, _two.default)() + } + + render() { + return /*#__PURE__*/ _react.default.createElement( + 'button', + { + className: _one.default.button, + }, + 'Click me' + ) + } +} + +exports.default = One diff --git a/test/fixtures/verify/css-raw-import/lib/styles/One.css b/test/fixtures/verify/css-raw-import/lib/styles/One.css new file mode 100644 index 00000000..5d486eba --- /dev/null +++ b/test/fixtures/verify/css-raw-import/lib/styles/One.css @@ -0,0 +1,3 @@ +.button { + background: #bf1942; +} diff --git a/test/fixtures/verify/css-raw-import/lib/two.js b/test/fixtures/verify/css-raw-import/lib/two.js new file mode 100644 index 00000000..411187ba --- /dev/null +++ b/test/fixtures/verify/css-raw-import/lib/two.js @@ -0,0 +1,3 @@ +export default function two() { + // do something important +} diff --git a/test/fixtures/verify/css-raw-import/package.json b/test/fixtures/verify/css-raw-import/package.json new file mode 100644 index 00000000..15051f1c --- /dev/null +++ b/test/fixtures/verify/css-raw-import/package.json @@ -0,0 +1,19 @@ +{ + "name": "sanity-plugin-css-part", + "version": "1.0.0", + "private": true, + "description": "Just a fixture", + "main": "./lib/one.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "sanity", + "sanity-plugin" + ], + "peerDependencies": { + "react": "*" + }, + "author": "Some person", + "license": "MIT" +} diff --git a/test/fixtures/verify/css-raw-import/sanity.json b/test/fixtures/verify/css-raw-import/sanity.json new file mode 100644 index 00000000..0c006ad1 --- /dev/null +++ b/test/fixtures/verify/css-raw-import/sanity.json @@ -0,0 +1,13 @@ +{ + "paths": { + "source": "./src", + "compiled": "./lib" + }, + + "parts": [ + { + "name": "part:css-part/some/thing", + "path": "one.js" + } + ] +} diff --git a/test/fixtures/verify/css-raw-import/src/one.js b/test/fixtures/verify/css-raw-import/src/one.js new file mode 100644 index 00000000..8600dc15 --- /dev/null +++ b/test/fixtures/verify/css-raw-import/src/one.js @@ -0,0 +1,13 @@ +import React from 'react' +import two from './two' +import styles from './styles/One.css?raw' + +export default class One extends React.PureComponent { + componentDidMount() { + two() + } + + render() { + return + } +} diff --git a/test/fixtures/verify/css-raw-import/src/styles/One.css b/test/fixtures/verify/css-raw-import/src/styles/One.css new file mode 100644 index 00000000..5d486eba --- /dev/null +++ b/test/fixtures/verify/css-raw-import/src/styles/One.css @@ -0,0 +1,3 @@ +.button { + background: #bf1942; +} diff --git a/test/fixtures/verify/css-raw-import/src/two.js b/test/fixtures/verify/css-raw-import/src/two.js new file mode 100644 index 00000000..411187ba --- /dev/null +++ b/test/fixtures/verify/css-raw-import/src/two.js @@ -0,0 +1,3 @@ +export default function two() { + // do something important +} diff --git a/test/verify.test.js b/test/verify.test.js index 0897e1bf..5b9cf960 100644 --- a/test/verify.test.js +++ b/test/verify.test.js @@ -25,7 +25,7 @@ tap.test('throws on missing license key', options, async (t) => { const fixtureDir = path.join(baseFixturesDir, 'missing-license-key') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, 'missing "license" key') - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1, 'should have exit code 1') }) @@ -33,7 +33,7 @@ tap.test('throws on missing license file', options, async (t) => { const fixtureDir = path.join(baseFixturesDir, 'missing-license') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, 'does not contain a LICENSE-file') - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1, 'should have exit code 1') }) @@ -41,7 +41,7 @@ tap.test('throws on non-spdx license', options, async (t) => { const fixtureDir = path.join(baseFixturesDir, 'non-spdx-license') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, 'SPDX license ID') - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1, 'should have exit code 1') }) @@ -49,7 +49,7 @@ tap.test('throws on referenced files being ignored by npm (package.json)', optio const fixtureDir = path.join(baseFixturesDir, 'npm-ref-ignored-file') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, 'ignored from being published: "types.d.ts"') - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1, 'should have exit code 1') }) @@ -57,7 +57,7 @@ tap.test('throws on referenced files being ignored by npm (sanity.json)', option const fixtureDir = path.join(baseFixturesDir, 'part-ref-ignored-file') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, 'ignored from being published: "lib/ignored.js"') - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1, 'should have exit code 1') }) @@ -65,7 +65,7 @@ tap.test('throws on invalid dist config', options, async (t) => { const fixtureDir = path.join(baseFixturesDir, 'invalid-dist-config') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, 'must be an object, got:\n[]') - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1, 'should have exit code 1') }) @@ -73,7 +73,7 @@ tap.test('throws on invalid dist config (syntax)', options, async (t) => { const fixtureDir = path.join(baseFixturesDir, 'invalid-dist-config-syntax') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, 'Unexpected end of JSON input') - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1, 'should have exit code 1') }) @@ -81,7 +81,7 @@ tap.test('warns on "useless" files', options, async (t) => { const fixtureDir = path.join(baseFixturesDir, 'useless-files') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, '".eslintignore", ".prettierrc"') - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 0) }) @@ -105,7 +105,7 @@ tap.test('verifies plugin with CSS-part referenced (missing)', options, async (t const fixtureDir = path.join(baseFixturesDir, 'css-part-missing') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, `references file ("styles/one.css") that does not exist in compiled`) - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1) }) @@ -113,6 +113,22 @@ tap.test('verifies plugin with import referencing missing CSS file', options, as const fixtureDir = path.join(baseFixturesDir, 'css-import') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, /unable to resolve.*?one\.css.*?did you mean.*?One.css/i) + t.equal(stdout, '', 'should have empty stdout') + t.equal(exitCode, 1) +}) + +tap.test('verifies plugin with import referencing missing ?raw CSS file', options, async (t) => { + const fixtureDir = path.join(baseFixturesDir, 'css-raw-import-missing') + const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) + t.includes(stderr, /unable to resolve.*?one\.css.*?did you mean.*?One.css/i) + t.equal(stdout, '', 'should have empty stdout') + t.equal(exitCode, 1) +}) + +tap.test('verifies plugin with import referencing valid ?raw CSS file', options, async (t) => { + const fixtureDir = path.join(baseFixturesDir, 'css-raw-import') + const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) + t.equal(stderr, '', 'should have empty stderr') t.includes(stdout, 'good to publish', 'should have success in stdout') t.equal(exitCode, 1) }) @@ -121,7 +137,7 @@ tap.test('verifies plugin with css modules referencing missing CSS file', option const fixtureDir = path.join(baseFixturesDir, 'css-bad-composes') const {stdout, stderr, exitCode} = await execa(sanipack, ['verify', fixtureDir], {reject: false}) t.includes(stderr, /unable to resolve.*?baseButton\.css/i) - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1) }) @@ -131,7 +147,7 @@ tap.test('verifies plugin with css importing incorrectly cased CSS file', option reject: false, }) t.includes(stderr, /unable to resolve.*?Button\.css.*?did you mean.*?button.css/i) - t.includes(stdout, 'good to publish', 'should have success in stdout') + t.equal(stdout, '', 'should have empty stdout') t.equal(exitCode, 1) })