A specialized build tool for creating Svelte component libraries and packages
—
Validation system for package.json exports fields and Svelte-specific configuration compliance.
Creates a validator instance for analyzing and validating package structure.
/**
* Create validator for package analysis and validation
* @param options - Build options containing configuration
* @returns ValidatorResult with analysis and validation functions
*/
function create_validator(options: Options): ValidatorResult;
interface ValidatorResult {
/** Analyze processed code for validation */
analyse_code(name: string, code: string): void;
/** Run validation checks on the package */
validate(): void;
}Usage Examples:
import { create_validator } from "@sveltejs/package/src/validate.js";
import { load_config } from "@sveltejs/package/src/config.js";
// Create validator
const config = await load_config();
const { analyse_code, validate } = create_validator({
cwd: process.cwd(),
input: "src/lib",
output: "dist",
preserve_output: false,
types: true,
config
});
// Analyze processed code
analyse_code("components/Button.svelte", svelteCode);
analyse_code("utils/helpers.js", jsCode);
// Run validation
validate(); // Throws errors if validation failsThe validator checks package.json exports field compliance:
{
"exports": {
".": {
"svelte": "./dist/index.js",
"import": "./dist/index.js",
"default": "./dist/index.js"
}
}
}Validation Rules:
exports field must be present".") must be defined for packages with a svelte fieldWhen both svelte field and exports are present:
{
"svelte": "./dist/index.js",
"exports": {
".": {
"svelte": "./dist/index.js",
"import": "./dist/index.js"
}
}
}Validation Rules:
svelte field must match an export path in root exportsvelte field and exportssvelte when using Svelte componentsSupported export conditions:
svelte - Svelte component entry pointimport - ES module entry pointrequire - CommonJS entry pointdefault - Fallback entry pointtypes - TypeScript declaration entry pointThe validator analyzes processed code for:
$app/* imports and validates dependenciesimport.meta.env usage (warns about bundler compatibility)Specific Checks:
$app/environment imports should be avoided for non-SvelteKit compatibilityimport.meta.env usage is flagged as Vite-specific@sveltejs/kit as a dependencysvelte as a peer dependencyAvoid usage of `import.meta.env` in your code. It only works in apps bundled with Vite.
Consider using packages like `esm-env` instead which works with all bundlers or without bundling.Avoid usage of `$app/environment` in your code, if you want the library to work for people not using SvelteKit (only regular Svelte, for example).
Consider using packages like `esm-env` instead which provide cross-bundler-compatible environment variables.You are using SvelteKit-specific imports in your code, but you have not declared a dependency on `@sveltejs/kit` in your `package.json`.
Add it to your `dependencies` or `peerDependencies`.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`.
Add it to your `dependencies` or `peerDependencies`.No `exports` field found in `package.json`, please provide one.
See https://svelte.dev/docs/kit/packaging#anatomy-of-a-package-json-exports for more infoYou are using Svelte files, but did not declare a `svelte` condition in one of your `exports` in your `package.json`.
Add a `svelte` condition to your `exports` to ensure that your package is recognized as Svelte package by tooling.The `svelte` field in your `package.json` does not match any export in your root `exports`.
Please align them so that bundlers will resolve consistently to the same file.You have a `svelte` field in your `package.json`, but no root export in your `exports`.
Please align them so that bundlers will resolve consistently to the same file.The validator integrates with the build process:
In watch mode:
The validator uses regex patterns to match export paths:
// Converts export patterns to regex for validation
// "./dist/*.js" becomes /^\.\/dist\/.*\.js$/
function export_to_regex(exportPath: string): RegExp;Recursively traverses export conditions to extract:
Validation behavior changes based on configuration:
svelte export conditioninterface ValidatorResult {
analyse_code(name: string, code: string): void;
validate(): void;
}
interface Options {
cwd: string;
input: string;
output: string;
preserve_output: boolean;
types: boolean;
tsconfig?: string;
config: Config;
}Install with Tessl CLI
npx tessl i tessl/npm-sveltejs--package