Skip to content

[fix] preserve user defined config and files on svelte-kit package #1735

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

Merged
merged 20 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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/unlucky-planets-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Preserve README casing and package.json contents on svelte-kit package
2 changes: 1 addition & 1 deletion documentation/docs/12-packaging.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Running `svelte-kit package` will take the contents of `src/lib` and generate a

- All the files in `src/lib`, unless you [configure](#configuration-package) custom `include`/`exclude` options. Svelte components will be preprocessed, TypeScript files will be transpiled to JavaScript.
- Type definitions (`d.ts` files) which are generated for Svelte, JavaScript and TypeScript files. You need to install `typescript >= 4.0.0` and `svelte2tsx >= 0.4.1` for this. Type definitions are placed next to their implementation, hand-written `d.ts` files are copied over as is.
- A `package.json` that copies the `name`, `version`, `description`, `keywords`, `homepage`, `bugs`, `license`, `author`, `contributors`, `funding`, `repository`, `dependencies`, `private` and `publishConfig` fields from the root of the project, and adds a `"type": "module"` and an `"exports"` field
- A `package.json` copied from the project root without the `"scripts"` field and adds a `"type": "module"`. An `"exports"` field will also be added if it's not defined in the original file.

The `"exports"` field contains the package's entry points. By default, all files in `src/lib` will be treated as an entry point unless they start with (or live in a directory that starts with) an underscore, but you can [configure](#configuration-package) this behaviour. If you have a `src/lib/index.js` or `src/lib/index.svelte` file, it will be treated as the package root.

Expand Down
53 changes: 21 additions & 32 deletions packages/kit/src/core/make_package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,16 @@ export async function make_package(config, cwd = process.cwd()) {

const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));

const package_pkg = {
name: pkg.name,
version: pkg.version,
description: pkg.description,
keywords: pkg.keywords,
homepage: pkg.homepage,
bugs: pkg.bugs,
license: pkg.license,
author: pkg.author,
contributors: pkg.contributors,
funding: pkg.funding,
repository: pkg.repository,
dependencies: pkg.dependencies,
private: pkg.private,
publishConfig: pkg.publishConfig,
type: 'module',
/** @type {Record<string, string>} */
exports: {
delete pkg.scripts;
pkg.type = 'module'; // type must be 'module'

let user_defined_exports = true;
if (!('exports' in pkg)) {
user_defined_exports = false;
pkg.exports = {
'./package.json': './package.json'
}
};
};
}

for (const file of files) {
if (!files_filter(file)) continue;
Expand Down Expand Up @@ -89,27 +78,27 @@ export async function make_package(config, cwd = process.cwd()) {

write(path.join(cwd, config.kit.package.dir, out_file), out_contents);

if (exports_filter(file)) {
const entry = `./${out_file}`;
package_pkg.exports[entry] = entry;
if (!user_defined_exports && exports_filter(file)) {
const entry = `./${out_file.replace(/\\/g, '/')}`;
pkg.exports[entry] = entry;
}
}

const main = package_pkg.exports['./index.js'] || package_pkg.exports['./index.svelte'];
const main = pkg.exports['./index.js'] || pkg.exports['./index.svelte'];

if (main) {
package_pkg.exports['.'] = main;
if (!user_defined_exports && main) {
pkg.exports['.'] = main;
}

write(
path.join(cwd, config.kit.package.dir, 'package.json'),
JSON.stringify(package_pkg, null, ' ')
);
write(path.join(cwd, config.kit.package.dir, 'package.json'), JSON.stringify(pkg, null, ' '));

const project_readme = path.join(cwd, 'README.md');
const package_readme = path.join(cwd, config.kit.package.dir, 'README.md');
const readme_exists = fs.existsSync(project_readme);
if (!readme_exists) return;

if (fs.existsSync(project_readme) && !fs.existsSync(package_readme)) {
const preserved_filename = fs.realpathSync.native(project_readme).slice(-'README.md'.length);
const package_readme = path.join(cwd, config.kit.package.dir, preserved_filename);
if (!fs.existsSync(package_readme)) {
fs.copyFileSync(project_readme, package_readme);
}
}
Expand Down