From af1aa5404803c72601b04c2063931ef971c5c48d Mon Sep 17 00:00:00 2001 From: Ignatius Bagus Date: Sat, 10 Jul 2021 05:26:08 +0700 Subject: [PATCH] [fix] copy essentials files from root on packaging (#1747) --- .changeset/lazy-mice-smile.md | 5 +++++ documentation/docs/12-packaging.md | 2 +- packages/kit/src/core/make_package/index.js | 17 +++++++++++++---- 3 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 .changeset/lazy-mice-smile.md diff --git a/.changeset/lazy-mice-smile.md b/.changeset/lazy-mice-smile.md new file mode 100644 index 000000000000..0735bae540df --- /dev/null +++ b/.changeset/lazy-mice-smile.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +copy essential root files on `svelte-kit package` diff --git a/documentation/docs/12-packaging.md b/documentation/docs/12-packaging.md index 0cdd288d02ef..86acf5d64bb0 100644 --- a/documentation/docs/12-packaging.md +++ b/documentation/docs/12-packaging.md @@ -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. diff --git a/packages/kit/src/core/make_package/index.js b/packages/kit/src/core/make_package/index.js index 55f77ff7624d..0980533e8f7e 100644 --- a/packages/kit/src/core/make_package/index.js +++ b/packages/kit/src/core/make_package/index.js @@ -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 @@ -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} */ @@ -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); } }