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] copy essentials files from root on packaging #1747

Merged
merged 11 commits into from
Jul 9, 2021
5 changes: 5 additions & 0 deletions .changeset/lazy-mice-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

copy essential root files 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` that copies the `name`, `version`, `description`, `keywords`, `homepage`, `bugs`, `license`, `author`, `contributors`, `funding`, `repository`, `dependencies`, `private`, `files` and `publishConfig` fields from the root of the project, and adds a `"type": "module"` and an `"exports"` field

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
1 change: 1 addition & 0 deletions packages/kit/src/core/make_package/essential_files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ['README', 'LICENSE', 'CHANGELOG', '.gitignore', '.npmignore'];
ignatiusmb marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 12 additions & 4 deletions packages/kit/src/core/make_package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createRequire } from 'module';
import * as path from 'path';
import { preprocess } from 'svelte/compiler';
import { mkdirp, rimraf } from '../filesystem/index.js';
import essential_files from './essential_files.js';

/**
* @param {import('types/config').ValidatedConfig} config
Expand Down Expand Up @@ -39,6 +40,7 @@ export async function make_package(config, cwd = process.cwd()) {
repository: pkg.repository,
dependencies: pkg.dependencies,
private: pkg.private,
files: pkg.files,
publishConfig: pkg.publishConfig,
type: 'module',
/** @type {Record<string, string>} */
Expand Down Expand Up @@ -106,11 +108,17 @@ export async function make_package(config, cwd = process.cwd()) {
JSON.stringify(package_pkg, null, ' ')
);

const project_readme = path.join(cwd, 'README.md');
const package_readme = path.join(cwd, config.kit.package.dir, 'README.md');
const whitelist = fs.readdirSync(cwd).filter((file) => {
const lowercased = file.toLowerCase();
return essential_files.some((name) => lowercased.startsWith(name.toLowerCase()));
});
for (const pathname of whitelist) {
const full_path = path.join(cwd, pathname);
if (fs.lstatSync(full_path).isDirectory()) continue; // just to be sure

if (fs.existsSync(project_readme) && !fs.existsSync(package_readme)) {
fs.copyFileSync(project_readme, package_readme);
const package_path = path.join(cwd, config.kit.package.dir, pathname);
if (fs.existsSync(package_path)) continue;
fs.copyFileSync(full_path, package_path);
}
}

Expand Down