Skip to content

Commit

Permalink
[fix] preserve user defined config and files on svelte-kit package (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored Jul 10, 2021
1 parent 34d2049 commit 868f97a
Show file tree
Hide file tree
Showing 49 changed files with 487 additions and 49 deletions.
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. 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`, `files` 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
51 changes: 17 additions & 34 deletions packages/kit/src/core/make_package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,15 @@ 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,
files: pkg.files,
publishConfig: pkg.publishConfig,
type: 'module',
/** @type {Record<string, string>} */
exports: {
'./package.json': './package.json'
}
};
delete pkg.scripts;
pkg.type = 'module'; // type must be 'module'

const user_defined_exports = 'exports' in pkg;

// We still want to always predefine some exports
// like package.json that is used by other packages
if (!pkg.exports) pkg.exports = {};
pkg.exports['./package.json'] = './package.json';

for (const file of files) {
if (!files_filter(file)) continue;
Expand Down Expand Up @@ -94,22 +81,19 @@ 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 whitelist = fs.readdirSync(cwd).filter((file) => {
const lowercased = file.toLowerCase();
Expand All @@ -120,8 +104,7 @@ export async function make_package(config, cwd = process.cwd()) {
if (fs.lstatSync(full_path).isDirectory()) continue; // just to be sure

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { createEventDispatcher } from 'svelte';
/**
* @type {string}
*/
export const astring;
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/** @typedef {typeof __propDef.props} TestProps */
/** @typedef {typeof __propDef.events} TestEvents */
/** @typedef {typeof __propDef.slots} TestSlots */
export default class Test extends SvelteComponentTyped<
{
astring: string;
},
{
event: CustomEvent<any>;
} & {
[evt: string]: CustomEvent<any>;
},
{
default: {
astring: string;
};
}
> {
get astring(): string;
}
export type TestProps = typeof __propDef.props;
export type TestEvents = typeof __propDef.events;
export type TestSlots = typeof __propDef.slots;
import { SvelteComponentTyped } from 'svelte';
declare const __propDef: {
props: {
astring: string;
};
events: {
event: CustomEvent<any>;
} & {
[evt: string]: CustomEvent<any>;
};
slots: {
default: {
astring: string;
};
};
};
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "casing",
"version": "1.0.0",
"description": "package-casing-test",
"type": "module",
"exports": {
"./package.json": "./package.json",
"./index.js": "./index.js",
"./Test.svelte": "./Test.svelte",
".": "./index.js"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"checkJs": true,
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "casing",
"version": "1.0.0",
"description": "package-casing-test"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { createEventDispatcher } from 'svelte';
/**
* @type {string}
*/
export const astring;
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { createEventDispatcher } from 'svelte';
/**
* @type {string}
*/
export const astring;
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/** @typedef {typeof __propDef.props} TestProps */
/** @typedef {typeof __propDef.events} TestEvents */
/** @typedef {typeof __propDef.slots} TestSlots */
export default class Test extends SvelteComponentTyped<
{
astring: string;
},
{
event: CustomEvent<any>;
} & {
[evt: string]: CustomEvent<any>;
},
{
default: {
astring: string;
};
}
> {
get astring(): string;
}
export type TestProps = typeof __propDef.props;
export type TestEvents = typeof __propDef.events;
export type TestSlots = typeof __propDef.slots;
import { SvelteComponentTyped } from 'svelte';
declare const __propDef: {
props: {
astring: string;
};
events: {
event: CustomEvent<any>;
} & {
[evt: string]: CustomEvent<any>;
};
slots: {
default: {
astring: string;
};
};
};
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** @typedef {typeof __propDef.props} Test2Props */
/** @typedef {typeof __propDef.events} Test2Events */
/** @typedef {typeof __propDef.slots} Test2Slots */
export default class Test2 extends SvelteComponentTyped<
{
foo: boolean;
},
{
[evt: string]: CustomEvent<any>;
},
{}
> {}
export type Test2Props = typeof __propDef.props;
export type Test2Events = typeof __propDef.events;
export type Test2Slots = typeof __propDef.slots;
import { SvelteComponentTyped } from 'svelte';
declare const __propDef: {
props: {
foo: import('./foo').Foo;
};
events: {
[evt: string]: CustomEvent<any>;
};
slots: {};
};
export {};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Foo = boolean;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "exports",
"version": "1.0.0",
"description": "package-exports-test",
"exports": {
".": {
"import": "./index.js"
},
"./package.json": "./package.json"
},
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"checkJs": true,
"paths": {
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.svelte"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "exports",
"version": "1.0.0",
"description": "package-exports-test",
"exports": {
".": {
"import": "./index.js"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { createEventDispatcher } from 'svelte';
/**
* @type {string}
*/
export const astring;
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as Test } from './Test.svelte';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
/**
* @type {import('./foo').Foo}
*/
export let foo;
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Foo = boolean;
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
import { createEventDispatcher } from 'svelte';
/**
* @type {string}
*/
export const astring;
const dispatch = createEventDispatcher();
dispatch('event', true);
</script>

<slot {astring} />
Loading

0 comments on commit 868f97a

Please sign in to comment.