@sveltejs/kit 1.0.0-next.318 → 1.0.0-next.320

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,6 +21,19 @@ function coalesce_to_error(err) {
21
21
  * @returns {import('types').NormalizedLoadOutput}
22
22
  */
23
23
  function normalize(loaded) {
24
+ // TODO remove for 1.0
25
+ // @ts-expect-error
26
+ if (loaded.fallthrough) {
27
+ throw new Error(
28
+ 'fallthrough is no longer supported. Use matchers instead: https://kit.svelte.dev/docs/routing#advanced-routing-matching'
29
+ );
30
+ }
31
+
32
+ // TODO remove for 1.0
33
+ if ('maxage' in loaded) {
34
+ throw new Error('maxage should be replaced with cache: { maxage }');
35
+ }
36
+
24
37
  const has_error_status =
25
38
  loaded.status && loaded.status >= 400 && loaded.status <= 599 && !loaded.redirect;
26
39
  if (loaded.error || has_error_status) {
@@ -812,9 +825,9 @@ function create_client({ target, session, base, trailing_slash }) {
812
825
  }
813
826
 
814
827
  const leaf = filtered[filtered.length - 1];
815
- const maxage = leaf.loaded && leaf.loaded.maxage;
828
+ const load_cache = leaf?.loaded?.cache;
816
829
 
817
- if (maxage) {
830
+ if (load_cache) {
818
831
  const key = url.pathname + url.search; // omit hash
819
832
  let ready = false;
820
833
 
@@ -827,7 +840,7 @@ function create_client({ target, session, base, trailing_slash }) {
827
840
  clearTimeout(timeout);
828
841
  };
829
842
 
830
- const timeout = setTimeout(clear, maxage * 1000);
843
+ const timeout = setTimeout(clear, load_cache.maxage * 1000);
831
844
 
832
845
  const unsubscribe = stores.session.subscribe(() => {
833
846
  if (ready) clear();
@@ -1056,14 +1069,6 @@ function create_client({ target, session, base, trailing_slash }) {
1056
1069
  }
1057
1070
 
1058
1071
  if (node.loaded) {
1059
- // TODO remove for 1.0
1060
- // @ts-expect-error
1061
- if (node.loaded.fallthrough) {
1062
- throw new Error(
1063
- 'fallthrough is no longer supported. Use matchers instead: https://kit.svelte.dev/docs/routing#advanced-routing-matching'
1064
- );
1065
- }
1066
-
1067
1072
  if (node.loaded.error) {
1068
1073
  status = node.loaded.status;
1069
1074
  error = node.loaded.error;
@@ -1147,7 +1147,8 @@ async function render_response({
1147
1147
  let rendered;
1148
1148
 
1149
1149
  let is_private = false;
1150
- let maxage;
1150
+ /** @type {import('types').NormalizedLoadOutputCache | undefined} */
1151
+ let cache;
1151
1152
 
1152
1153
  if (error) {
1153
1154
  error.stack = options.get_stack(error);
@@ -1163,9 +1164,8 @@ async function render_response({
1163
1164
  if (fetched && page_config.hydrate) serialized_data.push(...fetched);
1164
1165
  if (props) shadow_props = props;
1165
1166
 
1166
- if (uses_credentials) is_private = true;
1167
-
1168
- maxage = loaded.maxage;
1167
+ cache = loaded?.cache;
1168
+ is_private = cache?.private ?? uses_credentials;
1169
1169
  });
1170
1170
 
1171
1171
  const session = writable($session);
@@ -1179,7 +1179,7 @@ async function render_response({
1179
1179
  session: {
1180
1180
  ...session,
1181
1181
  subscribe: (fn) => {
1182
- is_private = true;
1182
+ is_private = cache?.private ?? true;
1183
1183
  return session.subscribe(fn);
1184
1184
  }
1185
1185
  },
@@ -1366,8 +1366,8 @@ async function render_response({
1366
1366
  http_equiv.push(csp_headers);
1367
1367
  }
1368
1368
 
1369
- if (maxage) {
1370
- http_equiv.push(`<meta http-equiv="cache-control" content="max-age=${maxage}">`);
1369
+ if (cache) {
1370
+ http_equiv.push(`<meta http-equiv="cache-control" content="max-age=${cache.maxage}">`);
1371
1371
  }
1372
1372
 
1373
1373
  if (http_equiv.length > 0) {
@@ -1388,8 +1388,8 @@ async function render_response({
1388
1388
  etag: `"${hash(html)}"`
1389
1389
  });
1390
1390
 
1391
- if (maxage) {
1392
- headers.set('cache-control', `${is_private ? 'private' : 'public'}, max-age=${maxage}`);
1391
+ if (cache) {
1392
+ headers.set('cache-control', `${is_private ? 'private' : 'public'}, max-age=${cache.maxage}`);
1393
1393
  }
1394
1394
 
1395
1395
  if (!options.floc) {
@@ -1915,6 +1915,19 @@ var splitCookiesString_1 = setCookie.exports.splitCookiesString = splitCookiesSt
1915
1915
  * @returns {import('types').NormalizedLoadOutput}
1916
1916
  */
1917
1917
  function normalize(loaded) {
1918
+ // TODO remove for 1.0
1919
+ // @ts-expect-error
1920
+ if (loaded.fallthrough) {
1921
+ throw new Error(
1922
+ 'fallthrough is no longer supported. Use matchers instead: https://kit.svelte.dev/docs/routing#advanced-routing-matching'
1923
+ );
1924
+ }
1925
+
1926
+ // TODO remove for 1.0
1927
+ if ('maxage' in loaded) {
1928
+ throw new Error('maxage should be replaced with cache: { maxage }');
1929
+ }
1930
+
1918
1931
  const has_error_status =
1919
1932
  loaded.status && loaded.status >= 400 && loaded.status <= 599 && !loaded.redirect;
1920
1933
  if (loaded.error || has_error_status) {
@@ -2411,14 +2424,6 @@ async function load_node({
2411
2424
  // TODO do we still want to enforce this now that there's no fallthrough?
2412
2425
  throw new Error(`load function must return a value${options.dev ? ` (${node.entry})` : ''}`);
2413
2426
  }
2414
-
2415
- // TODO remove for 1.0
2416
- // @ts-expect-error
2417
- if (loaded.fallthrough) {
2418
- throw new Error(
2419
- 'fallthrough is no longer supported. Use matchers instead: https://kit.svelte.dev/docs/routing#advanced-routing-matching'
2420
- );
2421
- }
2422
2427
  } else if (shadow.body) {
2423
2428
  loaded = {
2424
2429
  props: shadow.body
@@ -1,8 +1,10 @@
1
1
  import * as fs from 'fs';
2
2
  import * as path from 'path';
3
- import { createRequire } from 'module';
3
+ import { join, relative, dirname } from 'path';
4
4
  import { $ } from '../cli.js';
5
- import { r as rimraf, m as mkdirp, w as walk$1 } from './filesystem.js';
5
+ import chokidar from 'chokidar';
6
+ import { w as walk$1, m as mkdirp, p as posixify, r as rimraf, c as copy } from './filesystem.js';
7
+ import { createRequire } from 'module';
6
8
  import 'sade';
7
9
  import 'child_process';
8
10
  import 'net';
@@ -15248,178 +15250,6 @@ async function preprocess(source, preprocessor, options) {
15248
15250
  return result.to_processed();
15249
15251
  }
15250
15252
 
15251
- const essential_files = ['README', 'LICENSE', 'CHANGELOG', '.gitignore', '.npmignore'];
15252
-
15253
- /**
15254
- * @param {import('types').ValidatedConfig} config
15255
- * @param {string} cwd
15256
- */
15257
- async function make_package(config, cwd = process.cwd()) {
15258
- if (!fs.existsSync(config.kit.files.lib)) {
15259
- throw new Error(`${config.kit.files.lib} does not exist`);
15260
- }
15261
-
15262
- const package_dir = path.isAbsolute(config.kit.package.dir)
15263
- ? config.kit.package.dir
15264
- : path.join(cwd, config.kit.package.dir);
15265
- rimraf(package_dir);
15266
- mkdirp(package_dir); // TODO https://github.com/sveltejs/kit/issues/2333
15267
-
15268
- if (config.kit.package.emitTypes) {
15269
- // Generate type definitions first so hand-written types can overwrite generated ones
15270
- await emit_dts(config);
15271
- // Resolve aliases, TS leaves them as-is
15272
- const files = walk$1(package_dir);
15273
- for (const file of files) {
15274
- const filename = path.join(package_dir, file);
15275
- const source = fs.readFileSync(filename, 'utf8');
15276
- fs.writeFileSync(filename, resolve_$lib_alias(file, source, config));
15277
- }
15278
- }
15279
-
15280
- const files = walk$1(config.kit.files.lib);
15281
-
15282
- const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));
15283
-
15284
- delete pkg.scripts;
15285
- pkg.type = 'module';
15286
-
15287
- /** @type {Record<string, string>} */
15288
- const generated = { './package.json': './package.json' };
15289
-
15290
- /** @type {Record<string, string>} */
15291
- const clashes = {};
15292
- let contains_svelte_files = false;
15293
-
15294
- for (const file of files) {
15295
- const ext = path.extname(file);
15296
- const normalized = file.replace(/\\/g, '/');
15297
- const svelte_ext = config.extensions.find((ext) => file.endsWith(ext)); // unlike `ext`, could be e.g. `.svelte.md`
15298
-
15299
- if (!config.kit.package.files(normalized)) {
15300
- const base = svelte_ext ? file : file.slice(0, -ext.length);
15301
- for (const e of ['.d.ts', '.d.mts', '.d.cts']) {
15302
- const dts_path = path.join(package_dir, base + e);
15303
- if (fs.existsSync(dts_path)) {
15304
- fs.unlinkSync(dts_path);
15305
-
15306
- const dir = path.dirname(dts_path);
15307
- if (fs.readdirSync(dir).length === 0) {
15308
- fs.rmdirSync(dir);
15309
- }
15310
- }
15311
- }
15312
- continue;
15313
- }
15314
-
15315
- const filename = path.join(config.kit.files.lib, file);
15316
- const source = fs.readFileSync(filename);
15317
-
15318
- /** @type {string} */
15319
- let out_file;
15320
-
15321
- /** @type {string | Buffer} */
15322
- let out_contents;
15323
-
15324
- if (svelte_ext) {
15325
- // it's a Svelte component
15326
- contains_svelte_files = true;
15327
- out_file = file.slice(0, -svelte_ext.length) + '.svelte';
15328
- out_contents = source.toString('utf-8');
15329
- out_contents = config.preprocess
15330
- ? strip_lang_tags((await preprocess(out_contents, config.preprocess, { filename })).code)
15331
- : out_contents;
15332
- out_contents = resolve_$lib_alias(out_file, out_contents, config);
15333
- } else if (ext === '.ts' && file.endsWith('.d.ts')) {
15334
- // TypeScript's declaration emit won't copy over the d.ts files, so we do it here
15335
- out_file = file;
15336
- out_contents = source.toString('utf-8');
15337
- out_contents = resolve_$lib_alias(out_file, out_contents, config);
15338
- if (fs.existsSync(path.join(package_dir, out_file))) {
15339
- console.warn(
15340
- 'Found already existing file from d.ts generation for ' +
15341
- out_file +
15342
- '. This file will be overwritten.'
15343
- );
15344
- }
15345
- } else if (ext === '.ts') {
15346
- out_file = file.slice(0, -'.ts'.length) + '.js';
15347
- out_contents = await transpile_ts(filename, source.toString('utf-8'));
15348
- out_contents = resolve_$lib_alias(out_file, out_contents, config);
15349
- } else {
15350
- out_file = file;
15351
- out_contents = source;
15352
- }
15353
-
15354
- write(path.join(package_dir, out_file), out_contents);
15355
-
15356
- if (config.kit.package.exports(normalized)) {
15357
- const original = `$lib/${normalized}`;
15358
- const entry = `./${out_file.replace(/\\/g, '/')}`;
15359
- const key = entry.replace(/\/index\.js$|(\/[^/]+)\.js$/, '$1');
15360
-
15361
- if (clashes[key]) {
15362
- throw new Error(
15363
- `Duplicate "${key}" export. Please remove or rename either ${clashes[key]} or ${original}`
15364
- );
15365
- }
15366
-
15367
- generated[key] = entry;
15368
- clashes[key] = original;
15369
- }
15370
- }
15371
-
15372
- pkg.exports = { ...generated, ...pkg.exports };
15373
-
15374
- if (!pkg.svelte && contains_svelte_files) {
15375
- // Several heuristics in Kit/vite-plugin-svelte to tell Vite to mark Svelte packages
15376
- // rely on the "svelte" property. Vite/Rollup/Webpack plugin can all deal with it.
15377
- // See https://github.com/sveltejs/kit/issues/1959 for more info and related threads.
15378
- if (pkg.exports['.']) {
15379
- const svelte_export =
15380
- typeof pkg.exports['.'] === 'string'
15381
- ? pkg.exports['.']
15382
- : pkg.exports['.'].import || pkg.exports['.'].default;
15383
- if (svelte_export) {
15384
- pkg.svelte = svelte_export;
15385
- } else {
15386
- console.warn(
15387
- 'Cannot generate a "svelte" entry point because ' +
15388
- 'the "." entry in "exports" is not a string. ' +
15389
- 'If you set it by hand, please also set one of the options as a "svelte" entry point\n'
15390
- );
15391
- }
15392
- } else {
15393
- console.warn(
15394
- 'Cannot generate a "svelte" entry point because ' +
15395
- 'the "." entry in "exports" is missing. ' +
15396
- 'Please specify one or set a "svelte" entry point yourself\n'
15397
- );
15398
- }
15399
- }
15400
-
15401
- write(path.join(package_dir, 'package.json'), JSON.stringify(pkg, null, 2));
15402
-
15403
- const whitelist = fs.readdirSync(cwd).filter((file) => {
15404
- const lowercased = file.toLowerCase();
15405
- return essential_files.some((name) => lowercased.startsWith(name.toLowerCase()));
15406
- });
15407
- for (const pathname of whitelist) {
15408
- const full_path = path.join(cwd, pathname);
15409
- if (fs.lstatSync(full_path).isDirectory()) continue; // just to be sure
15410
-
15411
- const package_path = path.join(package_dir, pathname);
15412
- if (!fs.existsSync(package_path)) fs.copyFileSync(full_path, package_path);
15413
- }
15414
-
15415
- const from = path.relative(cwd, config.kit.files.lib);
15416
- const to = path.relative(cwd, config.kit.package.dir);
15417
- console.log($.bold().green(`${from} -> ${to}`));
15418
- console.log(`Successfully built '${pkg.name}' package. To publish it to npm:`);
15419
- console.log($.bold().cyan(` cd ${to}`));
15420
- console.log($.bold().cyan(' npm publish\n'));
15421
- }
15422
-
15423
15253
  /**
15424
15254
  * Resolves the `$lib` alias.
15425
15255
  *
@@ -15433,7 +15263,7 @@ async function make_package(config, cwd = process.cwd()) {
15433
15263
  * @param {import('types').ValidatedConfig} config
15434
15264
  * @returns {string}
15435
15265
  */
15436
- function resolve_$lib_alias(file, content, config) {
15266
+ function resolve_lib_alias(file, content, config) {
15437
15267
  /**
15438
15268
  * @param {string} match
15439
15269
  * @param {string} _
@@ -15446,7 +15276,7 @@ function resolve_$lib_alias(file, content, config) {
15446
15276
 
15447
15277
  const full_path = path.join(config.kit.files.lib, file);
15448
15278
  const full_import_path = path.join(config.kit.files.lib, import_path.slice('$lib/'.length));
15449
- let resolved = path.relative(path.dirname(full_path), full_import_path).replace(/\\/g, '/');
15279
+ let resolved = posixify(path.relative(path.dirname(full_path), full_import_path));
15450
15280
  resolved = resolved.startsWith('.') ? resolved : './' + resolved;
15451
15281
  return match.replace(import_path, resolved);
15452
15282
  };
@@ -15462,27 +15292,170 @@ function resolve_$lib_alias(file, content, config) {
15462
15292
  * @param {string} content
15463
15293
  */
15464
15294
  function strip_lang_tags(content) {
15465
- strip_lang_tag('script');
15466
- strip_lang_tag('style');
15467
- return content;
15295
+ return content
15296
+ .replace(/(<!--[^]*?-->)|(<script[^>]*?)\s(?:type|lang)=(["']).*?\3/g, '$1$2')
15297
+ .replace(/(<!--[^]*?-->)|(<style[^>]*?)\s(?:type|lang)=(["']).*?\3/g, '$1$2');
15298
+ }
15468
15299
 
15469
- /**
15470
- * @param {string} tagname
15471
- */
15472
- function strip_lang_tag(tagname) {
15473
- const regexp = new RegExp(
15474
- `/<!--[^]*?-->|<${tagname}(\\s[^]*?)?(?:>([^]*?)<\\/${tagname}>|\\/>)`,
15475
- 'g'
15300
+ /**
15301
+ * @param {string} file
15302
+ * @param {Parameters<typeof fs.writeFileSync>[1]} contents
15303
+ */
15304
+ function write(file, contents) {
15305
+ mkdirp(path.dirname(file));
15306
+ fs.writeFileSync(file, contents);
15307
+ }
15308
+
15309
+ /**
15310
+ * @param {import('types').ValidatedConfig} config
15311
+ * @returns {import('./types').File[]}
15312
+ */
15313
+ function scan(config) {
15314
+ return walk$1(config.kit.files.lib).map((file) => analyze(config, file));
15315
+ }
15316
+
15317
+ /**
15318
+ * @param {import('types').ValidatedConfig} config
15319
+ * @param {string} file
15320
+ * @returns {import('./types').File}
15321
+ */
15322
+ function analyze(config, file) {
15323
+ const name = posixify(file);
15324
+
15325
+ const svelte_extension = config.extensions.find((ext) => name.endsWith(ext));
15326
+
15327
+ const base = svelte_extension ? name : name.slice(0, -path.extname(name).length);
15328
+
15329
+ const dest = svelte_extension
15330
+ ? name.slice(0, -svelte_extension.length) + '.svelte'
15331
+ : name.endsWith('.d.ts')
15332
+ ? name
15333
+ : name.endsWith('.ts')
15334
+ ? name.slice(0, -3) + '.js'
15335
+ : name;
15336
+
15337
+ return {
15338
+ name,
15339
+ dest,
15340
+ base,
15341
+ is_included: config.kit.package.files(name),
15342
+ is_exported: config.kit.package.exports(name),
15343
+ is_svelte: !!svelte_extension
15344
+ };
15345
+ }
15346
+
15347
+ /**
15348
+ * @param {string} cwd
15349
+ * @param {import('./types').File[]} files
15350
+ */
15351
+ function generate_pkg(cwd, files) {
15352
+ const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf8'));
15353
+
15354
+ delete pkg.scripts;
15355
+ pkg.type = 'module';
15356
+
15357
+ pkg.exports = {
15358
+ './package.json': './package.json',
15359
+ ...pkg.exports
15360
+ };
15361
+
15362
+ /** @type {Record<string, string>} */
15363
+ const clashes = {};
15364
+
15365
+ for (const file of files) {
15366
+ if (file.is_included && file.is_exported) {
15367
+ const original = `$lib/${file.name}`;
15368
+ const entry = `./${file.dest}`;
15369
+ const key = entry.replace(/\/index\.js$|(\/[^/]+)\.js$/, '$1');
15370
+
15371
+ if (clashes[key]) {
15372
+ throw new Error(
15373
+ `Duplicate "${key}" export. Please remove or rename either ${clashes[key]} or ${original}`
15374
+ );
15375
+ }
15376
+
15377
+ if (!pkg.exports[key]) {
15378
+ pkg.exports[key] = entry;
15379
+ }
15380
+
15381
+ clashes[key] = original;
15382
+ }
15383
+ }
15384
+
15385
+ return pkg;
15386
+ }
15387
+
15388
+ /**
15389
+ * @param {import('types').ValidatedConfig} config
15390
+ * @param {string} cwd
15391
+ * @param {import('./types').File[]} files
15392
+ */
15393
+ async function emit_dts(config, cwd, files) {
15394
+ const tmp = `${config.kit.outDir}/package/types`;
15395
+ rimraf(tmp);
15396
+ mkdirp(tmp);
15397
+
15398
+ const require = createRequire(import.meta.url);
15399
+ const emit = await try_load_svelte2tsx();
15400
+ await emit({
15401
+ libRoot: config.kit.files.lib,
15402
+ svelteShimsPath: require.resolve('svelte2tsx/svelte-shims.d.ts'),
15403
+ declarationDir: path.relative(cwd, tmp)
15404
+ });
15405
+
15406
+ const handwritten = new Set();
15407
+ const excluded = new Set();
15408
+
15409
+ // remove excluded files, and files that conflict with hand-written .d.ts
15410
+ for (const file of files) {
15411
+ if (file.name.endsWith('.d.ts')) {
15412
+ handwritten.add(file.name);
15413
+ }
15414
+
15415
+ if (!file.is_included) {
15416
+ excluded.add(file.base + '.d.ts');
15417
+ excluded.add(file.base + '.d.mts');
15418
+ excluded.add(file.base + '.d.cts');
15419
+ }
15420
+ }
15421
+
15422
+ // resolve $lib alias (TODO others), copy into package dir
15423
+ for (const file of walk$1(tmp)) {
15424
+ const normalized = posixify(file);
15425
+
15426
+ if (handwritten.has(normalized)) {
15427
+ console.warn(`Using $lib/${normalized} instead of generated .d.ts file`);
15428
+ }
15429
+
15430
+ // don't overwrite hand-written .d.ts files
15431
+ if (excluded.has(normalized)) continue;
15432
+
15433
+ const source = fs.readFileSync(path.join(tmp, normalized), 'utf8');
15434
+ write(
15435
+ path.join(config.kit.package.dir, normalized),
15436
+ resolve_lib_alias(normalized, source, config)
15437
+ );
15438
+ }
15439
+ }
15440
+
15441
+ async function try_load_svelte2tsx() {
15442
+ const svelte2tsx = await load();
15443
+ const emit_dts = svelte2tsx.emitDts;
15444
+ if (!emit_dts) {
15445
+ throw new Error(
15446
+ 'You need to install svelte2tsx >=0.4.1 if you want to generate type definitions'
15476
15447
  );
15477
- content = content.replace(regexp, (tag, attributes) => {
15478
- if (!attributes) return tag;
15479
- const idx = tag.indexOf(attributes);
15480
- return (
15481
- tag.substring(0, idx) +
15482
- attributes.replace(/\s(type|lang)=(["']).*?\2/, ' ') +
15483
- tag.substring(idx + attributes.length)
15448
+ }
15449
+ return emit_dts;
15450
+
15451
+ async function load() {
15452
+ try {
15453
+ return await import('svelte2tsx');
15454
+ } catch (e) {
15455
+ throw new Error(
15456
+ 'You need svelte2tsx and typescript if you want to generate type definitions. Install it through your package manager, or disable generation which is highly discouraged. See https://kit.svelte.dev/docs/packaging'
15484
15457
  );
15485
- });
15458
+ }
15486
15459
  }
15487
15460
  }
15488
15461
 
@@ -15513,14 +15486,32 @@ async function try_load_ts() {
15513
15486
  * @param {import('typescript')} ts
15514
15487
  */
15515
15488
  function load_tsconfig(filename, ts) {
15516
- const filedir = path.dirname(filename);
15517
- const tsconfig_filename = ts.findConfigFile(filedir, ts.sys.fileExists);
15489
+ let config_filename;
15490
+
15491
+ // ts.findConfigFile is broken (it will favour a distant tsconfig
15492
+ // over a near jsconfig, and then only when you call it twice)
15493
+ // so we implement it ourselves
15494
+ let dir = filename;
15495
+ while (dir !== (dir = path.dirname(dir))) {
15496
+ const tsconfig = path.join(dir, 'tsconfig.json');
15497
+ const jsconfig = path.join(dir, 'jsconfig.json');
15498
+
15499
+ if (fs.existsSync(tsconfig)) {
15500
+ config_filename = tsconfig;
15501
+ break;
15502
+ }
15518
15503
 
15519
- if (!tsconfig_filename) {
15504
+ if (fs.existsSync(jsconfig)) {
15505
+ config_filename = jsconfig;
15506
+ break;
15507
+ }
15508
+ }
15509
+
15510
+ if (!config_filename) {
15520
15511
  throw new Error('Failed to locate tsconfig or jsconfig');
15521
15512
  }
15522
15513
 
15523
- const { error, config } = ts.readConfigFile(tsconfig_filename, ts.sys.readFile);
15514
+ const { error, config } = ts.readConfigFile(config_filename, ts.sys.readFile);
15524
15515
 
15525
15516
  if (error) {
15526
15517
  throw new Error('Malformed tsconfig\n' + JSON.stringify(error, null, 2));
@@ -15532,54 +15523,215 @@ function load_tsconfig(filename, ts) {
15532
15523
  const { options } = ts.parseJsonConfigFileContent(
15533
15524
  config,
15534
15525
  ts.sys,
15535
- path.dirname(tsconfig_filename),
15526
+ path.dirname(config_filename),
15536
15527
  { sourceMap: false },
15537
- tsconfig_filename
15528
+ config_filename
15538
15529
  );
15539
15530
  return options;
15540
15531
  }
15541
15532
 
15533
+ const essential_files = ['README', 'LICENSE', 'CHANGELOG', '.gitignore', '.npmignore'];
15534
+
15542
15535
  /**
15543
- * @param {string} file
15544
- * @param {Parameters<typeof fs.writeFileSync>[1]} contents
15536
+ * @param {import('types').ValidatedConfig} config
15537
+ * @param {string} cwd
15545
15538
  */
15546
- function write(file, contents) {
15547
- mkdirp(path.dirname(file));
15548
- fs.writeFileSync(file, contents);
15539
+ async function build(config, cwd = process.cwd()) {
15540
+ const { lib } = config.kit.files;
15541
+ const { dir } = config.kit.package;
15542
+
15543
+ if (!fs.existsSync(lib)) {
15544
+ throw new Error(`${lib} does not exist`);
15545
+ }
15546
+
15547
+ rimraf(dir);
15548
+ mkdirp(dir);
15549
+
15550
+ const files = scan(config);
15551
+
15552
+ if (config.kit.package.emitTypes) {
15553
+ await emit_dts(config, cwd, files);
15554
+ }
15555
+
15556
+ const pkg = generate_pkg(cwd, files);
15557
+
15558
+ if (!pkg.svelte && files.some((file) => file.is_svelte)) {
15559
+ // Several heuristics in Kit/vite-plugin-svelte to tell Vite to mark Svelte packages
15560
+ // rely on the "svelte" property. Vite/Rollup/Webpack plugin can all deal with it.
15561
+ // See https://github.com/sveltejs/kit/issues/1959 for more info and related threads.
15562
+ if (pkg.exports['.']) {
15563
+ const svelte_export =
15564
+ typeof pkg.exports['.'] === 'string'
15565
+ ? pkg.exports['.']
15566
+ : pkg.exports['.'].import || pkg.exports['.'].default;
15567
+ if (svelte_export) {
15568
+ pkg.svelte = svelte_export;
15569
+ } else {
15570
+ console.warn(
15571
+ 'Cannot generate a "svelte" entry point because the "." entry in "exports" is not a string. If you set it by hand, please also set one of the options as a "svelte" entry point\n'
15572
+ );
15573
+ }
15574
+ } else {
15575
+ console.warn(
15576
+ 'Cannot generate a "svelte" entry point because the "." entry in "exports" is missing. Please specify one or set a "svelte" entry point yourself\n'
15577
+ );
15578
+ }
15579
+ }
15580
+
15581
+ write(join(dir, 'package.json'), JSON.stringify(pkg, null, 2));
15582
+
15583
+ for (const file of files) {
15584
+ await process_file(config, file);
15585
+ }
15586
+
15587
+ const whitelist = fs.readdirSync(cwd).filter((file) => {
15588
+ const lowercased = file.toLowerCase();
15589
+ return essential_files.some((name) => lowercased.startsWith(name.toLowerCase()));
15590
+ });
15591
+
15592
+ for (const pathname of whitelist) {
15593
+ const full_path = join(cwd, pathname);
15594
+ if (fs.lstatSync(full_path).isDirectory()) continue; // just to be sure
15595
+
15596
+ const package_path = join(dir, pathname);
15597
+ if (!fs.existsSync(package_path)) fs.copyFileSync(full_path, package_path);
15598
+ }
15599
+
15600
+ const from = relative(cwd, lib);
15601
+ const to = relative(cwd, dir);
15602
+ console.log($.bold().green(`${from} -> ${to}`));
15603
+ console.log(`Successfully built '${pkg.name}' package. To publish it to npm:`);
15604
+ console.log($.bold().cyan(` cd ${to}`));
15605
+ console.log($.bold().cyan(' npm publish\n'));
15549
15606
  }
15550
15607
 
15551
15608
  /**
15552
15609
  * @param {import('types').ValidatedConfig} config
15553
15610
  */
15554
- async function emit_dts(config) {
15555
- const require = createRequire(import.meta.url);
15556
- const emit = await try_load_svelte2tsx();
15557
- await emit({
15558
- libRoot: config.kit.files.lib,
15559
- svelteShimsPath: require.resolve('svelte2tsx/svelte-shims.d.ts'),
15560
- declarationDir: config.kit.package.dir
15611
+ async function watch(config, cwd = process.cwd()) {
15612
+ await build(config, cwd);
15613
+
15614
+ const message = `\nWatching ${relative(cwd, config.kit.files.lib)} for changes...\n`;
15615
+
15616
+ console.log(message);
15617
+
15618
+ const { lib } = config.kit.files;
15619
+ const { dir } = config.kit.package;
15620
+
15621
+ /** @type {Array<{ file: import('./types').File, type: string }>} */
15622
+ const pending = [];
15623
+
15624
+ /** @type {Array<(value?: any) => void>} */
15625
+ const fulfillers = [];
15626
+
15627
+ /** @type {NodeJS.Timeout} */
15628
+ let timeout;
15629
+
15630
+ const watcher = chokidar.watch(lib, { ignoreInitial: true });
15631
+
15632
+ watcher.on('all', async (type, path) => {
15633
+ const file = analyze(config, relative(lib, path));
15634
+ if (!file.is_included) return;
15635
+
15636
+ pending.push({ file, type });
15637
+
15638
+ clearTimeout(timeout);
15639
+ timeout = setTimeout(async () => {
15640
+ const files = scan(config);
15641
+
15642
+ let should_update_pkg = false;
15643
+
15644
+ const events = pending.slice();
15645
+ pending.length = 0;
15646
+
15647
+ for (const { file, type } of events) {
15648
+ if ((type === 'unlink' || type === 'add') && file.is_exported) {
15649
+ should_update_pkg = true;
15650
+ }
15651
+
15652
+ if (type === 'unlink') {
15653
+ for (const candidate of [
15654
+ file.name,
15655
+ `${file.base}.d.ts`,
15656
+ `${file.base}.d.mts`,
15657
+ `${file.base}.d.cts`
15658
+ ]) {
15659
+ const resolved = join(dir, candidate);
15660
+
15661
+ if (fs.existsSync(resolved)) {
15662
+ fs.unlinkSync(resolved);
15663
+
15664
+ const parent = dirname(resolved);
15665
+ if (parent !== dir && fs.readdirSync(parent).length === 0) {
15666
+ fs.rmdirSync(parent);
15667
+ }
15668
+ }
15669
+ }
15670
+ console.log(`Removed ${file.dest}`);
15671
+ }
15672
+
15673
+ if (type === 'add' || type === 'change') {
15674
+ await process_file(config, file);
15675
+ console.log(`Processing ${file.name}`);
15676
+ }
15677
+ }
15678
+
15679
+ if (should_update_pkg) {
15680
+ const pkg = generate_pkg(cwd, files);
15681
+ write(join(dir, 'package.json'), JSON.stringify(pkg, null, 2));
15682
+ console.log('Updated package.json');
15683
+ }
15684
+
15685
+ if (config.kit.package.emitTypes) {
15686
+ await emit_dts(config, cwd, scan(config));
15687
+ console.log('Updated .d.ts files');
15688
+ }
15689
+
15690
+ console.log(message);
15691
+
15692
+ fulfillers.forEach((fn) => fn());
15693
+ }, 100);
15561
15694
  });
15695
+
15696
+ return {
15697
+ watcher,
15698
+ settled: () =>
15699
+ new Promise((fulfil, reject) => {
15700
+ fulfillers.push(fulfil);
15701
+ setTimeout(() => reject(new Error('Timed out')), 1000);
15702
+ })
15703
+ };
15562
15704
  }
15563
15705
 
15564
- async function try_load_svelte2tsx() {
15565
- const svelte2tsx = await load();
15566
- const emit_dts = svelte2tsx.emitDts;
15567
- if (!emit_dts) {
15568
- throw new Error(
15569
- 'You need to install svelte2tsx >=0.4.1 if you want to generate type definitions'
15570
- );
15571
- }
15572
- return emit_dts;
15706
+ /**
15707
+ * @param {import('types').ValidatedConfig} config
15708
+ * @param {import('./types').File} file
15709
+ */
15710
+ async function process_file(config, file) {
15711
+ if (!file.is_included) return;
15573
15712
 
15574
- async function load() {
15575
- try {
15576
- return await import('svelte2tsx');
15577
- } catch (e) {
15578
- throw new Error(
15579
- 'You need svelte2tsx and typescript if you want to generate type definitions. Install it through your package manager, or disable generation which is highly discouraged. See https://kit.svelte.dev/docs/packaging'
15580
- );
15713
+ const filename = join(config.kit.files.lib, file.name);
15714
+ const dest = join(config.kit.package.dir, file.dest);
15715
+
15716
+ if (file.is_svelte || file.name.endsWith('.ts')) {
15717
+ let contents = fs.readFileSync(filename, 'utf-8');
15718
+
15719
+ if (file.is_svelte) {
15720
+ if (config.preprocess) {
15721
+ const preprocessed = (await preprocess(contents, config.preprocess, { filename })).code;
15722
+ contents = strip_lang_tags(preprocessed);
15723
+ }
15724
+ }
15725
+
15726
+ if (file.name.endsWith('.ts') && !file.name.endsWith('.d.ts')) {
15727
+ contents = await transpile_ts(filename, contents);
15581
15728
  }
15729
+
15730
+ contents = resolve_lib_alias(file.name, contents, config);
15731
+ write(dest, contents);
15732
+ } else {
15733
+ copy(filename, dest);
15582
15734
  }
15583
15735
  }
15584
15736
 
15585
- export { emit_dts, make_package };
15737
+ export { build, watch };
package/dist/cli.js CHANGED
@@ -870,7 +870,7 @@ async function launch(port, https, base) {
870
870
  exec(`${cmd} ${https ? 'https' : 'http'}://localhost:${port}${base}`);
871
871
  }
872
872
 
873
- const prog = sade('svelte-kit').version('1.0.0-next.318');
873
+ const prog = sade('svelte-kit').version('1.0.0-next.320');
874
874
 
875
875
  prog
876
876
  .command('dev')
@@ -981,13 +981,13 @@ prog
981
981
  prog
982
982
  .command('package')
983
983
  .describe('Create a package')
984
- .action(async () => {
984
+ .option('-w, --watch', 'Rerun when files change', false)
985
+ .action(async ({ watch }) => {
985
986
  try {
986
987
  const config = await load_config();
988
+ const packaging = await import('./chunks/index6.js');
987
989
 
988
- const { make_package } = await import('./chunks/index6.js');
989
-
990
- await make_package(config);
990
+ await (watch ? packaging.watch(config) : packaging.build(config));
991
991
  } catch (error) {
992
992
  handle_error(error);
993
993
  }
@@ -1049,7 +1049,7 @@ async function check_port(port) {
1049
1049
  function welcome({ port, host, https, open, base, loose, allow, cwd }) {
1050
1050
  if (open) launch(port, https, base);
1051
1051
 
1052
- console.log($.bold().cyan(`\n SvelteKit v${'1.0.0-next.318'}\n`));
1052
+ console.log($.bold().cyan(`\n SvelteKit v${'1.0.0-next.320'}\n`));
1053
1053
 
1054
1054
  const protocol = https ? 'https:' : 'http:';
1055
1055
  const exposed = typeof host !== 'undefined' && host !== 'localhost' && host !== '127.0.0.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/kit",
3
- "version": "1.0.0-next.318",
3
+ "version": "1.0.0-next.320",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -11,6 +11,7 @@
11
11
  "type": "module",
12
12
  "dependencies": {
13
13
  "@sveltejs/vite-plugin-svelte": "^1.0.0-next.32",
14
+ "chokidar": "^3.5.3",
14
15
  "sade": "^1.7.4",
15
16
  "vite": "^2.9.0"
16
17
  },
package/types/index.d.ts CHANGED
@@ -207,10 +207,15 @@ export interface LoadOutput<Props extends Record<string, any> = Record<string, a
207
207
  redirect?: string;
208
208
  props?: Props;
209
209
  stuff?: Partial<App.Stuff>;
210
- maxage?: number;
210
+ cache?: LoadOutputCache;
211
211
  dependencies?: string[];
212
212
  }
213
213
 
214
+ export interface LoadOutputCache {
215
+ maxage: number;
216
+ private?: boolean;
217
+ }
218
+
214
219
  export interface Navigation {
215
220
  from: URL;
216
221
  to: URL;
@@ -117,10 +117,15 @@ export type NormalizedLoadOutput = {
117
117
  redirect?: string;
118
118
  props?: Record<string, any> | Promise<Record<string, any>>;
119
119
  stuff?: Record<string, any>;
120
- maxage?: number;
120
+ cache?: NormalizedLoadOutputCache;
121
121
  dependencies?: string[];
122
122
  };
123
123
 
124
+ export interface NormalizedLoadOutputCache {
125
+ maxage: number;
126
+ private?: boolean;
127
+ }
128
+
124
129
  export interface PageData {
125
130
  type: 'page';
126
131
  id: string;