From 18c01b889c13138f7a27a687024aae9cb9fc72cd Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Sat, 18 Feb 2023 10:19:38 +0100 Subject: [PATCH] fix: dont emit false positive export condition warning fixes #9097 --- .changeset/friendly-pandas-sort.md | 5 ++ packages/package/src/validate.js | 61 +++++++++---- .../package/test/fixtures/assets/package.json | 11 ++- .../fixtures/emitTypes-false/package.json | 10 ++- .../test/fixtures/javascript/package.json | 11 ++- .../test/fixtures/resolve-alias/package.json | 11 ++- .../test/fixtures/svelte-kit/package.json | 11 ++- .../test/fixtures/typescript/package.json | 11 ++- packages/package/test/index.js | 86 +++++++++++++++++++ packages/package/test/watch/package.json | 11 ++- 10 files changed, 203 insertions(+), 25 deletions(-) create mode 100644 .changeset/friendly-pandas-sort.md diff --git a/.changeset/friendly-pandas-sort.md b/.changeset/friendly-pandas-sort.md new file mode 100644 index 000000000000..fac35085c9b8 --- /dev/null +++ b/.changeset/friendly-pandas-sort.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/package': patch +--- + +fix: dont emit false positive export condition warning diff --git a/packages/package/src/validate.js b/packages/package/src/validate.js index 83fe0c25ffbb..c0276aef7d90 100644 --- a/packages/package/src/validate.js +++ b/packages/package/src/validate.js @@ -1,10 +1,46 @@ -import { existsSync, readFileSync } from 'node:fs'; +import { readFileSync } from 'node:fs'; +import { join } from 'node:path'; import colors from 'kleur'; /** * @param {import("./types").Options} options */ export function create_validator(options) { + const { analyse_code, validate } = _create_validator(options); + + return { + /** + * Checks a file content for problematic imports and things like `import.meta` + * @param {string} name + * @param {string} content + */ + analyse_code(name, content) { + analyse_code(name, content); + }, + validate() { + /** @type {Record} */ + const pkg = JSON.parse(readFileSync(join(options.cwd, 'package.json'), 'utf-8')); + const warnings = validate(pkg); + // Just warnings, not errors, because + // - would be annoying in watch mode (would have to restart the server) + // - maybe there's a custom post-build script that fixes some of these + if (warnings.length) { + console.log( + colors + .bold() + .yellow('@sveltejs/package found the following issues while packaging your library:') + ); + for (const warning of warnings) { + console.log(colors.yellow(`${warning}\n`)); + } + } + } + }; +} +/** + * @param {import("./types").Options} options + */ +export function _create_validator(options) { /** @type {Set} */ const imports = new Set(); let uses_import_meta = false; @@ -32,9 +68,10 @@ export function create_validator(options) { } } - function validate() { - /** @type {Record} */ - const pkg = JSON.parse(readFileSync('package.json', 'utf-8')); + /** + * @param {Record} pkg + */ + function validate(pkg) { /** @type {string[]} */ const warnings = []; @@ -109,19 +146,7 @@ export function create_validator(options) { ); } - // Just warnings, not errors, because - // - would be annoying in watch mode (would have to restart the server) - // - maybe there's a custom post-build script that fixes some of these - if (warnings.length) { - console.log( - colors - .bold() - .yellow('@sveltejs/package found the following issues while packaging your library:') - ); - for (const warning of warnings) { - console.log(colors.yellow(`${warning}\n`)); - } - } + return warnings; } return { @@ -146,7 +171,7 @@ function traverse_exports(exports_map) { */ function traverse(exports_map, is_first_level) { for (const key of Object.keys(exports_map ?? {})) { - if (is_first_level) { + if (!is_first_level) { conditions.add(key); } diff --git a/packages/package/test/fixtures/assets/package.json b/packages/package/test/fixtures/assets/package.json index f257933677f6..8379863a85f9 100644 --- a/packages/package/test/fixtures/assets/package.json +++ b/packages/package/test/fixtures/assets/package.json @@ -2,5 +2,14 @@ "name": "assets", "private": true, "version": "1.0.0", - "description": "no tampering assets" + "description": "no tampering assets", + "peerDependencies": { + "svelte": "^3.55.0" + }, + "exports": { + ".": { + "types": "./dist/index.d.ts", + "svelte": "./dist/index.js" + } + } } diff --git a/packages/package/test/fixtures/emitTypes-false/package.json b/packages/package/test/fixtures/emitTypes-false/package.json index ec97559d2c4f..382c65e0aae7 100644 --- a/packages/package/test/fixtures/emitTypes-false/package.json +++ b/packages/package/test/fixtures/emitTypes-false/package.json @@ -3,5 +3,13 @@ "private": true, "version": "1.0.0", "description": "emitTypes settings disabled", - "type": "module" + "type": "module", + "peerDependencies": { + "svelte": "^3.55.0" + }, + "exports": { + ".": { + "svelte": "./dist/index.js" + } + } } diff --git a/packages/package/test/fixtures/javascript/package.json b/packages/package/test/fixtures/javascript/package.json index 77440f772c72..37f782bec1dc 100644 --- a/packages/package/test/fixtures/javascript/package.json +++ b/packages/package/test/fixtures/javascript/package.json @@ -2,5 +2,14 @@ "name": "javascript", "private": true, "version": "1.0.0", - "description": "standard javascript package" + "description": "standard javascript package", + "peerDependencies": { + "svelte": "^3.55.0" + }, + "exports": { + ".": { + "types": "./dist/index.d.ts", + "svelte": "./dist/index.js" + } + } } diff --git a/packages/package/test/fixtures/resolve-alias/package.json b/packages/package/test/fixtures/resolve-alias/package.json index 19ecfdbec27f..461bde2f3437 100644 --- a/packages/package/test/fixtures/resolve-alias/package.json +++ b/packages/package/test/fixtures/resolve-alias/package.json @@ -3,5 +3,14 @@ "private": true, "version": "1.0.0", "description": "package using $lib alias", - "type": "module" + "type": "module", + "peerDependencies": { + "svelte": "^3.55.0" + }, + "exports": { + ".": { + "types": "./dist/index.d.ts", + "svelte": "./dist/index.js" + } + } } diff --git a/packages/package/test/fixtures/svelte-kit/package.json b/packages/package/test/fixtures/svelte-kit/package.json index 318400b23321..202bf7e46b21 100644 --- a/packages/package/test/fixtures/svelte-kit/package.json +++ b/packages/package/test/fixtures/svelte-kit/package.json @@ -3,5 +3,14 @@ "private": true, "version": "1.0.0", "type": "module", - "description": "SvelteKit library project" + "description": "SvelteKit library project", + "peerDependencies": { + "svelte": "^3.55.0" + }, + "exports": { + ".": { + "types": "./dist/index.d.ts", + "svelte": "./dist/index.js" + } + } } diff --git a/packages/package/test/fixtures/typescript/package.json b/packages/package/test/fixtures/typescript/package.json index 7e166c721c41..eef96883502c 100644 --- a/packages/package/test/fixtures/typescript/package.json +++ b/packages/package/test/fixtures/typescript/package.json @@ -3,5 +3,14 @@ "private": true, "version": "1.0.0", "description": "standard typescript package", - "type": "module" + "type": "module", + "peerDependencies": { + "svelte": "^3.55.0" + }, + "exports": { + ".": { + "types": "./dist/index.d.ts", + "svelte": "./dist/index.js" + } + } } diff --git a/packages/package/test/index.js b/packages/package/test/index.js index a0ab8b4b379c..caa98282b7bd 100644 --- a/packages/package/test/index.js +++ b/packages/package/test/index.js @@ -9,6 +9,7 @@ import * as assert from 'uvu/assert'; import { build, watch } from '../src/index.js'; import { load_config } from '../src/config.js'; import { rimraf, walk } from '../src/filesystem.js'; +import { _create_validator } from '../src/validate.js'; const __filename = fileURLToPath(import.meta.url); const __dirname = join(__filename, '..'); @@ -214,4 +215,89 @@ if (!process.env.CI) { }); } +/** + * @param {string[]} actual + * @param {string[]} expected + */ +function has_warnings(actual, expected) { + assert.equal(actual.length, expected.length); + assert.equal( + actual.filter((warning) => expected.some((str) => warning.startsWith(str))).length, + expected.length + ); +} + +test('validates package (1)', () => { + const { analyse_code, validate } = _create_validator({ + config: {}, + cwd: '', + input: '', + output: '', + types: true + }); + analyse_code('src/lib/index.js', 'export const a = 1;import.meta.env;'); + analyse_code('src/lib/C.svelte', ''); + const warnings = validate({}); + + has_warnings(warnings, [ + 'No `exports` field found in `package.json`, please provide one.', + 'Avoid usage of `import.meta.env` in your code', + 'You are using Svelte components or Svelte-specific imports in your code, but you have not declared a dependency on `svelte` in your `package.json`. ' + ]); +}); + +test('validates package (2)', () => { + const { analyse_code, validate } = _create_validator({ + config: {}, + cwd: '', + input: '', + output: '', + types: true + }); + analyse_code('src/lib/C.svelte', ''); + const warnings = validate({ + exports: { '.': './dist/C.svelte' }, + peerDependencies: { svelte: '^3.55.0' } + }); + + has_warnings(warnings, [ + 'You are using Svelte files, but did not declare a `svelte` condition in one of your `exports` in your `package.json`. ' + ]); +}); + +test('validates package (all ok 1)', () => { + const { analyse_code, validate } = _create_validator({ + config: {}, + cwd: '', + input: '', + output: '', + types: true + }); + analyse_code('src/lib/C.svelte', ''); + const warnings = validate({ + exports: { '.': { svelte: './dist/C.svelte' } }, + peerDependencies: { svelte: '^3.55.0' } + }); + + assert.equal(warnings.length, 0); +}); + +test('validates package (all ok 2)', () => { + const { analyse_code, validate } = _create_validator({ + config: {}, + cwd: '', + input: '', + output: '', + types: true + }); + analyse_code('src/lib/C.svelte', ''); + const warnings = validate({ + exports: { '.': { svelte: './dist/C.svelte' } }, + peerDependencies: { svelte: '^3.55.0' }, + svelte: './dist/C.svelte' + }); + + assert.equal(warnings.length, 0); +}); + test.run(); diff --git a/packages/package/test/watch/package.json b/packages/package/test/watch/package.json index 854873b8484d..390dd4f4acc4 100644 --- a/packages/package/test/watch/package.json +++ b/packages/package/test/watch/package.json @@ -1,4 +1,13 @@ { "name": "watch", - "type": "module" + "type": "module", + "peerDependencies": { + "svelte": "^3.55.0" + }, + "exports": { + ".": { + "types": "./dist/index.d.ts", + "svelte": "./dist/index.js" + } + } }