Skip to content

Commit

Permalink
fix: dont emit false positive export condition warning
Browse files Browse the repository at this point in the history
fixes #9097
  • Loading branch information
dummdidumm committed Feb 18, 2023
1 parent 29ee93c commit 18c01b8
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-pandas-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': patch
---

fix: dont emit false positive export condition warning
61 changes: 43 additions & 18 deletions packages/package/src/validate.js
Original file line number Diff line number Diff line change
@@ -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<string, any>} */
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<string>} */
const imports = new Set();
let uses_import_meta = false;
Expand Down Expand Up @@ -32,9 +68,10 @@ export function create_validator(options) {
}
}

function validate() {
/** @type {Record<string, any>} */
const pkg = JSON.parse(readFileSync('package.json', 'utf-8'));
/**
* @param {Record<string, any>} pkg
*/
function validate(pkg) {
/** @type {string[]} */
const warnings = [];

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}

Expand Down
11 changes: 10 additions & 1 deletion packages/package/test/fixtures/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
10 changes: 9 additions & 1 deletion packages/package/test/fixtures/emitTypes-false/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
11 changes: 10 additions & 1 deletion packages/package/test/fixtures/javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
11 changes: 10 additions & 1 deletion packages/package/test/fixtures/resolve-alias/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
11 changes: 10 additions & 1 deletion packages/package/test/fixtures/svelte-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
11 changes: 10 additions & 1 deletion packages/package/test/fixtures/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
86 changes: 86 additions & 0 deletions packages/package/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '..');
Expand Down Expand Up @@ -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();
11 changes: 10 additions & 1 deletion packages/package/test/watch/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}

0 comments on commit 18c01b8

Please sign in to comment.