Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 16, 2022
2 parents 1210a72 + 6f355ea commit e45159b
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 216 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-boxes-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[feat] `$env/static/*` are now virtual to prevent writing sensitive values to disk
1 change: 1 addition & 0 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@
"ten-mice-kneel",
"ten-plants-sleep",
"tender-actors-tease",
"tender-bottles-cheer",
"tender-buckets-turn",
"tender-buses-talk",
"tender-geckos-agree",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/tender-bottles-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Lazy load Svelte components to reenable no-ssr use cases
6 changes: 6 additions & 0 deletions documentation/docs/01-project-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ my-project/
│ └ hooks.js
├ static/
│ └ [your static assets]
├ tests/
│ └ [your tests]
├ package.json
├ svelte.config.js
├ tsconfig.json
Expand Down Expand Up @@ -48,6 +50,10 @@ You can use `.ts` files instead of `.js` files, if using TypeScript.

Any static assets that should be served as-is, like `robots.txt` or `favicon.png`, go in here.

#### tests

If you chose to add tests to your project during `npm create svelte@latest`, they will live in this directory.

#### package.json

Your `package.json` file must include `@sveltejs/kit`, `svelte` and `vite` as `devDependencies`.
Expand Down
34 changes: 22 additions & 12 deletions packages/create-svelte/test/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,25 @@ import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { create } from '../index.js';
import { fileURLToPath } from 'url';
import glob from 'tiny-glob';
// use a directory outside of packages to ensure it isn't added to the pnpm workspace
const test_workspace_dir = fileURLToPath(
new URL('../../../.test-tmp/create-svelte/', import.meta.url)
);
const overrides = {};
['kit', 'adapter-auto', 'adapter-cloudflare', 'adapter-netlify', 'adapter-vercel'].forEach(
(pkg) => {
overrides[`@sveltejs/${pkg}`] = `${path.resolve(
test_workspace_dir,
'..',
'..',
'packages',
pkg
)}`; //'workspace:*';
}
);

const existing_workspace_overrides = JSON.parse(
fs.readFileSync(fileURLToPath(new URL('../../../package.json', import.meta.url)), 'utf-8')
).pnpm?.overrides;

const overrides = { ...existing_workspace_overrides };

(
await glob(fileURLToPath(new URL('../../../packages', import.meta.url)) + '/*/package.json')
).forEach((pkgPath) => {
const name = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')).name;
overrides[name] = path.dirname(path.resolve(pkgPath));
});

test.before(() => {
try {
// prepare test pnpm workspace
Expand Down Expand Up @@ -69,6 +72,13 @@ for (const template of fs.readdirSync('templates')) {
if (pkg.dependencies?.[key]) {
pkg.dependencies[key] = value;
}
if (!pkg.pnpm) {
pkg.pnpm = {};
}
if (!pkg.pnpm.overrides) {
pkg.pnpm.overrides = {};
}
pkg.pnpm.overrides = { ...pkg.pnpm.overrides, ...overrides };
});
pkg.private = true;
fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(pkg, null, '\t') + '\n');
Expand Down
6 changes: 6 additions & 0 deletions packages/kit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @sveltejs/kit

## 1.0.0-next.412

### Patch Changes

- Lazy load Svelte components to reenable no-ssr use cases ([#5930](https://github.com/sveltejs/kit/pull/5930))

## 1.0.0-next.411

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sveltejs/kit",
"version": "1.0.0-next.411",
"version": "1.0.0-next.412",
"repository": {
"type": "git",
"url": "https://github.com/sveltejs/kit",
Expand Down
2 changes: 2 additions & 0 deletions packages/kit/src/core/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// in `vite dev` and `vite preview`, we use a fake asset path so that we can
// serve local assets while verifying that requests are correctly prefixed
export const SVELTE_KIT_ASSETS = '/_svelte_kit_assets';

export const GENERATED_COMMENT = '// this file is generated — do not edit it\n';
91 changes: 91 additions & 0 deletions packages/kit/src/core/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { GENERATED_COMMENT } from './constants.js';

/**
* @param {string} id
* @param {Record<string, string>} env
* @returns {string}
*/
export function create_module(id, env) {
/** @type {string[]} */
const declarations = [];

for (const key in env) {
if (!valid_identifier.test(key) || reserved.has(key)) {
continue;
}

const comment = `/** @type {import('${id}').${key}} */`;
const declaration = `export const ${key} = ${JSON.stringify(env[key])};`;

declarations.push(`${comment}\n${declaration}`);
}

return GENERATED_COMMENT + declarations.join('\n\n');
}

/**
* @param {string} id
* @param {Record<string, string>} env
* @returns {string}
*/
export function create_types(id, env) {
const declarations = Object.keys(env)
.filter((k) => valid_identifier.test(k))
.map((k) => `\texport const ${k}: string;`)
.join('\n');

return `declare module '${id}' {\n${declarations}\n}`;
}

export const reserved = new Set([
'do',
'if',
'in',
'for',
'let',
'new',
'try',
'var',
'case',
'else',
'enum',
'eval',
'null',
'this',
'true',
'void',
'with',
'await',
'break',
'catch',
'class',
'const',
'false',
'super',
'throw',
'while',
'yield',
'delete',
'export',
'import',
'public',
'return',
'static',
'switch',
'typeof',
'default',
'extends',
'finally',
'package',
'private',
'continue',
'debugger',
'function',
'arguments',
'interface',
'protected',
'implements',
'instanceof'
]);

export const valid_identifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
53 changes: 0 additions & 53 deletions packages/kit/src/core/sync/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,56 +42,3 @@ export function trim(str) {
const pattern = new RegExp(`^${indentation}`, 'gm');
return str.replace(pattern, '').trim();
}

export const reserved = new Set([
'do',
'if',
'in',
'for',
'let',
'new',
'try',
'var',
'case',
'else',
'enum',
'eval',
'null',
'this',
'true',
'void',
'with',
'await',
'break',
'catch',
'class',
'const',
'false',
'super',
'throw',
'while',
'yield',
'delete',
'export',
'import',
'public',
'return',
'static',
'switch',
'typeof',
'default',
'extends',
'finally',
'package',
'private',
'continue',
'debugger',
'function',
'arguments',
'interface',
'protected',
'implements',
'instanceof'
]);

export const valid_identifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
72 changes: 6 additions & 66 deletions packages/kit/src/core/sync/write_ambient.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path';
import colors from 'kleur';
import { get_env } from '../../vite/utils.js';
import { write_if_changed, reserved, valid_identifier } from './utils.js';
import { GENERATED_COMMENT } from '../constants.js';
import { create_types } from '../env.js';
import { write_if_changed } from './utils.js';

const autogen_comment = '// this file is generated — do not edit it\n';
const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';

/**
Expand All @@ -16,72 +16,12 @@ const types_reference = '/// <reference types="@sveltejs/kit" />\n\n';
export function write_ambient(config, mode) {
const env = get_env(mode, config.env.publicPrefix);

// TODO when testing src, `$app` points at `src/runtime/app`... will
// probably need to fiddle with aliases
write_if_changed(
path.join(config.outDir, 'runtime/env/static/public.js'),
create_env_module('$env/static/public', env.public)
);

write_if_changed(
path.join(config.outDir, 'runtime/env/static/private.js'),
create_env_module('$env/static/private', env.private)
);

write_if_changed(
path.join(config.outDir, 'ambient.d.ts'),
autogen_comment +
GENERATED_COMMENT +
types_reference +
create_env_types('$env/static/public', env.public) +
create_types('$env/static/public', env.public) +
'\n\n' +
create_env_types('$env/static/private', env.private)
create_types('$env/static/private', env.private)
);
}

/**
* @param {string} id
* @param {Record<string, string>} env
* @returns {string}
*/
function create_env_module(id, env) {
/** @type {string[]} */
const declarations = [];

for (const key in env) {
const warning = !valid_identifier.test(key)
? 'not a valid identifier'
: reserved.has(key)
? 'a reserved word'
: null;

if (warning) {
console.error(
colors
.bold()
.yellow(`Omitting environment variable "${key}" from ${id} as it is ${warning}`)
);
continue;
}

const comment = `/** @type {import('${id}').${key}} */`;
const declaration = `export const ${key} = ${JSON.stringify(env[key])};`;

declarations.push(`${comment}\n${declaration}`);
}

return autogen_comment + declarations.join('\n\n');
}

/**
* @param {string} id
* @param {Record<string, string>} env
* @returns {string}
*/
function create_env_types(id, env) {
const declarations = Object.keys(env)
.filter((k) => valid_identifier.test(k))
.map((k) => `\texport const ${k}: string;`)
.join('\n');

return `declare module '${id}' {\n${declarations}\n}`;
}
Loading

0 comments on commit e45159b

Please sign in to comment.