Skip to content

[feat] merge packaging exports #2327

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 12 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ test('fills in defaults', () => {
package: {
dir: 'package',
exports: {
behavior: 'replace',
include: ['**'],
exclude: ['_*', '**/_*']
},
Expand Down Expand Up @@ -140,6 +141,7 @@ test('fills in partial blanks', () => {
package: {
dir: 'package',
exports: {
behavior: 'replace',
include: ['**'],
exclude: ['_*', '**/_*']
},
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ const options = {
exports: {
type: 'branch',
children: {
behavior: expect_string('replace'),
include: expect_array_of_strings(['**']),
exclude: expect_array_of_strings(['_*', '**/_*'])
}
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/core/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async function testLoadDefaultConfig(path) {
package: {
dir: 'package',
exports: {
behavior: 'replace',
include: ['**'],
exclude: ['_*', '**/_*']
},
Expand Down
25 changes: 11 additions & 14 deletions packages/kit/src/packaging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ export async function make_package(config, cwd = process.cwd()) {
const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));

delete pkg.scripts;
pkg.type = 'module'; // type must be 'module'
pkg.type = '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';
/** @type {Record<string, string>} */
const generated = { './package.json': './package.json' };

for (const file of files) {
const ext = path.extname(file);
Expand Down Expand Up @@ -86,19 +82,20 @@ export async function make_package(config, cwd = process.cwd()) {

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

if (!user_defined_exports && exports_filter(file)) {
if (exports_filter(file)) {
const entry = `./${out_file.replace(/\\/g, '/')}`;
const key = entry.endsWith('/index.js') ? entry.slice(0, -'/index.js'.length) : entry;
pkg.exports[key] = entry;
generated[key] = entry;
}
}

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

if (!user_defined_exports && main) {
pkg.exports['.'] = main;
if (!pkg.exports) {
pkg.exports = generated;
} else if (config.kit.package.exports.behavior === 'replace') {
pkg.exports['./package.json'] = './package.json';
} else if (config.kit.package.exports.behavior === 'merge') {
pkg.exports = { ...generated, ...pkg.exports };
}

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

const whitelist = fs.readdirSync(cwd).filter((file) => {
Expand Down
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} TestProps */
/** @typedef {typeof __propDef.events} TestEvents */
/** @typedef {typeof __propDef.slots} TestSlots */
export default class Test extends SvelteComponentTyped<
{
foo: boolean;
},
{
[evt: string]: CustomEvent<any>;
},
{}
> {}
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: {
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,17 @@
{
"name": "exports-replace",
"version": "1.0.0",
"description": "exports.behavior settings set to merge",
"type": "module",
"exports": {
"./internal/constants.js": "./internal/constants.js",
"./internal/Test.svelte": "./internal/Test.svelte",
"./Test.svelte": "./Test.svelte",
".": {
"import": "./index.js"
},
"./constants": "./internal/constants.js",
"./Test": "./Test.svelte",
"./package.json": "./package.json"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"checkJs": true,
"baseUrl": ".",
"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,13 @@
{
"name": "exports-replace",
"version": "1.0.0",
"description": "exports.behavior settings set to merge",
"type": "module",
"exports": {
".": {
"import": "./index.js"
},
"./constants": "./internal/constants.js",
"./Test": "./Test.svelte"
}
}
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
package: {
exports: {
behavior: 'merge'
}
}
}
};

export default config;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "exports-replace",
"version": "1.0.0",
"description": "default exports settings",
"description": "exports.behavior settings set to replace",
"exports": {
".": {
"import": "./index.js"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "exports-replace",
"version": "1.0.0",
"description": "default exports settings",
"description": "exports.behavior settings set to replace",
"exports": {
".": {
"import": "./index.js"
Expand Down
6 changes: 5 additions & 1 deletion packages/kit/src/packaging/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ test('create package with emitTypes settings disabled', async () => {
await test_make_package('emitTypes-false');
});

test('create package with default exports settings (replace)', async () => {
test('create package with merge exports.behavior settings', async () => {
await test_make_package('exports-merge');
});

test('create package with replace exports.behavior settings', async () => {
await test_make_package('exports-replace');
});

Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface Config {
dir?: string;
emitTypes?: boolean;
exports?: {
behavior?: 'replace' | 'merge';
include?: string[];
exclude?: string[];
};
Expand Down