Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont emit false positive export condition warning #9109

Merged
merged 2 commits into from
Feb 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: don't 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"
}
}
}