Skip to content

Commit

Permalink
[fix] exclude emitted declarations on packaging (#2247)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored Aug 22, 2021
1 parent ce19938 commit aaea5cf
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-spies-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Exclude emitted declarations on packaging
4 changes: 2 additions & 2 deletions packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"@rollup/plugin-replace": "^2.4.2",
"@types/amphtml-validator": "^1.0.1",
"@types/cookie": "^0.4.0",
"@types/globrex": "^0.1.1",
"@types/marked": "^2.0.2",
"@types/micromatch": "^4.0.2",
"@types/mime": "^2.0.3",
"@types/node": "^14.14.43",
"@types/rimraf": "^3.0.0",
Expand All @@ -22,10 +22,10 @@
"cookie": "^0.4.1",
"devalue": "^2.0.1",
"eslint": "^7.25.0",
"globrex": "^0.1.2",
"kleur": "^4.1.4",
"locate-character": "^2.0.5",
"marked": "^2.0.3",
"micromatch": "^4.0.4",
"mime": "^2.5.2",
"node-fetch": "^3.0.0-beta.9",
"port-authority": "^1.1.2",
Expand Down
38 changes: 17 additions & 21 deletions packages/kit/src/packaging/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import globrex from 'globrex';
import micromatch from 'micromatch';
import { createRequire } from 'module';
import { preprocess } from 'svelte/compiler';
import { mkdirp, rimraf, walk } from '../utils/filesystem.js';
Expand All @@ -21,8 +21,8 @@ export async function make_package(config, cwd = process.cwd()) {

const files_filter = create_filter(config.kit.package.files);
const exports_filter = create_filter({
...config.kit.package.exports,
exclude: [...config.kit.package.exports.exclude, '*.d.ts']
include: config.kit.package.exports.include,
exclude: [...config.kit.package.exports.exclude, '**/*.d.ts']
});

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

for (const file of files) {
if (!files_filter(file)) continue;
const ext = path.extname(file);
const svelte_ext = config.extensions.find((ext) => file.endsWith(ext)); // unlike `ext`, could be e.g. `.svelte.md`

if (!files_filter(file.replace(/\\/g, '/'))) {
const dts_file = (svelte_ext ? file : file.slice(0, -ext.length)) + '.d.ts';
const dts_path = path.join(cwd, config.kit.package.dir, dts_file);
if (fs.existsSync(dts_path)) fs.unlinkSync(dts_path);
continue;
}

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

const ext = path.extname(file);
const svelte_ext = config.extensions.find((ext) => file.endsWith(ext)); // unlike `ext`, could be e.g. `.svelte.md`

/** @type {string} */
let out_file;

Expand Down Expand Up @@ -163,21 +168,12 @@ function load_tsconfig(filename, ts) {
}

/**
* @param {{
* include: string[];
* exclude: string[];
* }} options
* @param {{ include: string[]; exclude: string[] }} options
* @returns {(str: string) => boolean}
*/
function create_filter(options) {
const include = options.include.map((str) => str && globrex(str));
const exclude = options.exclude.map((str) => str && globrex(str));

/** @param {string} str */
const filter = (str) =>
include.some((glob) => glob && glob.regex.test(str)) &&
!exclude.some((glob) => glob && glob.regex.test(str));

return filter;
return (str) =>
micromatch.isMatch(str, options.include) && !micromatch.isMatch(str, options.exclude);
}

/**
Expand All @@ -195,7 +191,7 @@ function write(file, contents) {
export async function emit_dts(config) {
const require = createRequire(import.meta.url);
const emit = await try_load_svelte2tsx();
emit({
await emit({
libRoot: config.kit.files.lib,
svelteShimsPath: require.resolve('svelte2tsx/svelte-shims.d.ts'),
declarationDir: config.kit.package.dir
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
@@ -1,11 +1,12 @@
{
"name": "javascript",
"name": "files-exclude",
"version": "1.0.0",
"description": "standard javascript package",
"description": "files.exclude settings configured",
"type": "module",
"exports": {
"./package.json": "./package.json",
"./internal": "./internal/index.js",
"./Test.svelte": "./Test.svelte",
".": "./index.js"
}
}
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} />
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const config = {
kit: {
package: {
files: {
exclude: ['**/exclude.js', '*.mjs']
exclude: ['**/*exclude.{js,svelte}', '**/*.mjs']
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/packaging/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test('create package with default exports settings (replace)', async () => {
await test_make_package('exports-replace');
});

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

Expand Down
22 changes: 14 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aaea5cf

Please sign in to comment.