Skip to content

Commit aaea5cf

Browse files
authored
[fix] exclude emitted declarations on packaging (#2247)
1 parent ce19938 commit aaea5cf

File tree

12 files changed

+107
-35
lines changed

12 files changed

+107
-35
lines changed

.changeset/lazy-spies-wash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
Exclude emitted declarations on packaging

packages/kit/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"@rollup/plugin-replace": "^2.4.2",
1313
"@types/amphtml-validator": "^1.0.1",
1414
"@types/cookie": "^0.4.0",
15-
"@types/globrex": "^0.1.1",
1615
"@types/marked": "^2.0.2",
16+
"@types/micromatch": "^4.0.2",
1717
"@types/mime": "^2.0.3",
1818
"@types/node": "^14.14.43",
1919
"@types/rimraf": "^3.0.0",
@@ -22,10 +22,10 @@
2222
"cookie": "^0.4.1",
2323
"devalue": "^2.0.1",
2424
"eslint": "^7.25.0",
25-
"globrex": "^0.1.2",
2625
"kleur": "^4.1.4",
2726
"locate-character": "^2.0.5",
2827
"marked": "^2.0.3",
28+
"micromatch": "^4.0.4",
2929
"mime": "^2.5.2",
3030
"node-fetch": "^3.0.0-beta.9",
3131
"port-authority": "^1.1.2",

packages/kit/src/packaging/index.js

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import globrex from 'globrex';
3+
import micromatch from 'micromatch';
44
import { createRequire } from 'module';
55
import { preprocess } from 'svelte/compiler';
66
import { mkdirp, rimraf, walk } from '../utils/filesystem.js';
@@ -21,8 +21,8 @@ export async function make_package(config, cwd = process.cwd()) {
2121

2222
const files_filter = create_filter(config.kit.package.files);
2323
const exports_filter = create_filter({
24-
...config.kit.package.exports,
25-
exclude: [...config.kit.package.exports.exclude, '*.d.ts']
24+
include: config.kit.package.exports.include,
25+
exclude: [...config.kit.package.exports.exclude, '**/*.d.ts']
2626
});
2727

2828
const files = walk(config.kit.files.lib);
@@ -40,14 +40,19 @@ export async function make_package(config, cwd = process.cwd()) {
4040
pkg.exports['./package.json'] = './package.json';
4141

4242
for (const file of files) {
43-
if (!files_filter(file)) continue;
43+
const ext = path.extname(file);
44+
const svelte_ext = config.extensions.find((ext) => file.endsWith(ext)); // unlike `ext`, could be e.g. `.svelte.md`
45+
46+
if (!files_filter(file.replace(/\\/g, '/'))) {
47+
const dts_file = (svelte_ext ? file : file.slice(0, -ext.length)) + '.d.ts';
48+
const dts_path = path.join(cwd, config.kit.package.dir, dts_file);
49+
if (fs.existsSync(dts_path)) fs.unlinkSync(dts_path);
50+
continue;
51+
}
4452

4553
const filename = path.join(config.kit.files.lib, file);
4654
const source = fs.readFileSync(filename, 'utf8');
4755

48-
const ext = path.extname(file);
49-
const svelte_ext = config.extensions.find((ext) => file.endsWith(ext)); // unlike `ext`, could be e.g. `.svelte.md`
50-
5156
/** @type {string} */
5257
let out_file;
5358

@@ -163,21 +168,12 @@ function load_tsconfig(filename, ts) {
163168
}
164169

165170
/**
166-
* @param {{
167-
* include: string[];
168-
* exclude: string[];
169-
* }} options
171+
* @param {{ include: string[]; exclude: string[] }} options
172+
* @returns {(str: string) => boolean}
170173
*/
171174
function create_filter(options) {
172-
const include = options.include.map((str) => str && globrex(str));
173-
const exclude = options.exclude.map((str) => str && globrex(str));
174-
175-
/** @param {string} str */
176-
const filter = (str) =>
177-
include.some((glob) => glob && glob.regex.test(str)) &&
178-
!exclude.some((glob) => glob && glob.regex.test(str));
179-
180-
return filter;
175+
return (str) =>
176+
micromatch.isMatch(str, options.include) && !micromatch.isMatch(str, options.exclude);
181177
}
182178

183179
/**
@@ -195,7 +191,7 @@ function write(file, contents) {
195191
export async function emit_dts(config) {
196192
const require = createRequire(import.meta.url);
197193
const emit = await try_load_svelte2tsx();
198-
emit({
194+
await emit({
199195
libRoot: config.kit.files.lib,
200196
svelteShimsPath: require.resolve('svelte2tsx/svelte-shims.d.ts'),
201197
declarationDir: config.kit.package.dir
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import { createEventDispatcher } from 'svelte';
3+
/**
4+
* @type {string}
5+
*/
6+
export const astring;
7+
8+
const dispatch = createEventDispatcher();
9+
dispatch('event', true);
10+
</script>
11+
12+
<slot {astring} />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/** @typedef {typeof __propDef.props} TestProps */
2+
/** @typedef {typeof __propDef.events} TestEvents */
3+
/** @typedef {typeof __propDef.slots} TestSlots */
4+
export default class Test extends SvelteComponentTyped<
5+
{
6+
astring: string;
7+
},
8+
{
9+
event: CustomEvent<any>;
10+
} & {
11+
[evt: string]: CustomEvent<any>;
12+
},
13+
{
14+
default: {
15+
astring: string;
16+
};
17+
}
18+
> {
19+
get astring(): string;
20+
}
21+
export type TestProps = typeof __propDef.props;
22+
export type TestEvents = typeof __propDef.events;
23+
export type TestSlots = typeof __propDef.slots;
24+
import { SvelteComponentTyped } from 'svelte';
25+
declare const __propDef: {
26+
props: {
27+
astring: string;
28+
};
29+
events: {
30+
event: CustomEvent<any>;
31+
} & {
32+
[evt: string]: CustomEvent<any>;
33+
};
34+
slots: {
35+
default: {
36+
astring: string;
37+
};
38+
};
39+
};
40+
export {};
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
2-
"name": "javascript",
2+
"name": "files-exclude",
33
"version": "1.0.0",
4-
"description": "standard javascript package",
4+
"description": "files.exclude settings configured",
55
"type": "module",
66
"exports": {
77
"./package.json": "./package.json",
88
"./internal": "./internal/index.js",
9+
"./Test.svelte": "./Test.svelte",
910
".": "./index.js"
1011
}
1112
}

packages/kit/src/packaging/test/fixtures/files-exclude/src/lib/Test.exclude.svelte

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<script>
2+
import { createEventDispatcher } from 'svelte';
3+
/**
4+
* @type {string}
5+
*/
6+
export const astring;
7+
8+
const dispatch = createEventDispatcher();
9+
dispatch('event', true);
10+
</script>
11+
12+
<slot {astring} />

packages/kit/src/packaging/test/fixtures/files-exclude/src/lib/exclude.js

Whitespace-only changes.

packages/kit/src/packaging/test/fixtures/files-exclude/svelte.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const config = {
33
kit: {
44
package: {
55
files: {
6-
exclude: ['**/exclude.js', '*.mjs']
6+
exclude: ['**/*exclude.{js,svelte}', '**/*.mjs']
77
}
88
}
99
}

packages/kit/src/packaging/test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ test('create package with default exports settings (replace)', async () => {
8383
await test_make_package('exports-replace');
8484
});
8585

86-
test.skip('create package with files.exclude settings', async () => {
86+
test('create package with files.exclude settings', async () => {
8787
await test_make_package('files-exclude');
8888
});
8989

pnpm-lock.yaml

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)