Skip to content

Commit

Permalink
[fix] copy essentials files from root on packaging (#1747)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored Jul 9, 2021
1 parent 29ba943 commit af1aa54
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
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. You can [disable generation](#configuration-package), but we strongly recommend against it.
- 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
17 changes: 13 additions & 4 deletions packages/kit/src/core/make_package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as path from 'path';
import { preprocess } from 'svelte/compiler';
import { mkdirp, rimraf } from '../filesystem/index.js';

const essential_files = ['README', 'LICENSE', 'CHANGELOG', '.gitignore', '.npmignore'];

/**
* @param {import('types/config').ValidatedConfig} config
* @param {string} cwd
Expand Down Expand Up @@ -41,6 +43,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 @@ -108,11 +111,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

0 comments on commit af1aa54

Please sign in to comment.