Skip to content
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

[chore] ambient modules and imports #2266

Merged
merged 15 commits into from
Aug 24, 2021
2 changes: 1 addition & 1 deletion packages/adapter-begin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default function () {
const adapter = {
name: '@sveltejs/adapter-begin',

async adapt({ utils }) {
async adapt() {
console.log('@sveltejs/adapter-begin can now be found at architect/sveltekit-adapter.');
}
};
Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-cloudflare-workers/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js';
import { getAssetFromKV, NotFoundError } from '@cloudflare/kv-asset-handler';

init();

Expand Down
11 changes: 7 additions & 4 deletions packages/adapter-cloudflare-workers/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
type BuildOptions = import('esbuild').BuildOptions;
declare function plugin(options?: {
esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}): import('@sveltejs/kit').Adapter;
import { Adapter } from '@sveltejs/kit';
import { BuildOptions } from 'esbuild';

interface AdapterOptions {
esbuild?: (options: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}

declare function plugin(options?: AdapterOptions): Adapter;
export = plugin;
20 changes: 7 additions & 13 deletions packages/adapter-cloudflare-workers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ import { fileURLToPath } from 'url';
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/

/**
* @param {{
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
* }} [options]
**/
/** @type {import('.')} */
export default function (options) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
return {
name: '@sveltejs/adapter-cloudflare-workers',

async adapt({ utils }) {
const { site } = validate_config(utils);

Expand All @@ -39,18 +35,18 @@ export default function (options) {
utils.copy(`${files}/entry.js`, '.svelte-kit/cloudflare-workers/entry.js');

/** @type {BuildOptions} */
const defaultOptions = {
const default_options = {
entryPoints: ['.svelte-kit/cloudflare-workers/entry.js'],
outfile: `${entrypoint}/index.js`,
bundle: true,
target: 'es2020',
platform: 'browser'
};

const buildOptions =
options && options.esbuild ? await options.esbuild(defaultOptions) : defaultOptions;
const build_options =
options && options.esbuild ? await options.esbuild(default_options) : default_options;

await esbuild.build(buildOptions);
await esbuild.build(build_options);

fs.writeFileSync(`${entrypoint}/package.json`, JSON.stringify({ main: 'index.js' }));

Expand All @@ -64,8 +60,6 @@ export default function (options) {
utils.copy_client_files(bucket);
}
};

return adapter;
}

function validate_config(utils) {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-netlify/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js';

init();

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-netlify/files/shims.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';
11 changes: 7 additions & 4 deletions packages/adapter-netlify/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
type BuildOptions = import('esbuild').BuildOptions;
declare function plugin(options?: {
esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}): import('@sveltejs/kit').Adapter;
import { Adapter } from '@sveltejs/kit';
import { BuildOptions } from 'esbuild';

interface AdapterOptions {
esbuild?: (options: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}

declare function plugin(options?: AdapterOptions): Adapter;
export = plugin;
19 changes: 6 additions & 13 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ import toml from '@iarna/toml';
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/

/**
* @param {{
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
* }} [options]
**/
/** @type {import('.')} */
export default function (options) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
return {
name: '@sveltejs/adapter-netlify',

async adapt({ utils }) {
Expand All @@ -32,7 +27,7 @@ export default function (options) {
utils.copy(join(files, 'entry.js'), '.svelte-kit/netlify/entry.js');

/** @type {BuildOptions} */
const defaultOptions = {
const default_options = {
entryPoints: ['.svelte-kit/netlify/entry.js'],
// Any functions in ".netlify/functions-internal" are bundled in addition to user-defined Netlify functions.
// See https://github.com/netlify/build/pull/3213 for more details
Expand All @@ -42,10 +37,10 @@ export default function (options) {
platform: 'node'
};

const buildOptions =
options && options.esbuild ? await options.esbuild(defaultOptions) : defaultOptions;
const build_options =
options && options.esbuild ? await options.esbuild(default_options) : default_options;

await esbuild.build(buildOptions);
await esbuild.build(build_options);

writeFileSync(join('.netlify', 'package.json'), JSON.stringify({ type: 'commonjs' }));

Expand All @@ -65,8 +60,6 @@ export default function (options) {
appendFileSync(redirectPath, '\n\n/* /.netlify/functions/__render 200');
}
};

return adapter;
}
/**
* @param {import('@sveltejs/kit').AdapterUtils} utils
Expand Down
12 changes: 8 additions & 4 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
type BuildOptions = import('esbuild').BuildOptions;
declare function plugin(options?: {
import { Adapter } from '@sveltejs/kit';
import { BuildOptions } from 'esbuild';

interface AdapterOptions {
out?: string;
precompress?: boolean;
env?: {
path?: string;
host?: string;
port?: string;
};
esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}): import('@sveltejs/kit').Adapter;
esbuild?: (options: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}

declare function plugin(options?: AdapterOptions): Adapter;
export = plugin;
22 changes: 3 additions & 19 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,14 @@ const pipe = promisify(pipeline);
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/

/**
* @param {{
* out?: string;
* precompress?: boolean;
* env?: {
* path?: string;
* host?: string;
* port?: string;
* };
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
* }} options
*/
/** @type {import('.')} */
export default function ({
out = 'build',
precompress,
env: { path: path_env = 'SOCKET_PATH', host: host_env = 'HOST', port: port_env = 'PORT' } = {},
esbuild: esbuildConfig
} = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
return {
name: '@sveltejs/adapter-node',

async adapt({ utils, config }) {
Expand Down Expand Up @@ -93,8 +81,6 @@ export default function ({
}
}
};

return adapter;
}

/**
Expand Down Expand Up @@ -127,9 +113,7 @@ async function compress_file(file, format = 'gz') {
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: statSync(file).size
}
})
: zlib.createGzip({
level: zlib.constants.Z_BEST_COMPRESSION
});
: zlib.createGzip({ level: zlib.constants.Z_BEST_COMPRESSION });

const source = createReadStream(file);
const destination = createWriteStream(`${file}.${format}`);
Expand Down
6 changes: 4 additions & 2 deletions packages/adapter-node/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { path, host, port } from './env.js'; // eslint-disable-line import/no-unresolved
// @ts-ignore
ignatiusmb marked this conversation as resolved.
Show resolved Hide resolved
import { init, render } from '../output/server/app.js';
// @ts-ignore
import { path, host, port } from './env.js';
import { createServer } from './server';

init();
Expand Down
11 changes: 7 additions & 4 deletions packages/adapter-node/src/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRawBody } from '@sveltejs/kit/node'; // eslint-disable-line import/no-unresolved
import { getRawBody } from '@sveltejs/kit/node';
import compression from 'compression';
import fs from 'fs';
import { dirname, join } from 'path';
Expand All @@ -16,6 +16,8 @@ const paths = {
prerendered: join(__dirname, '/prerendered')
};

// TODO: type render function from @sveltejs/kit/adapter
// @ts-ignore
export function createServer({ render }) {
const prerendered_handler = fs.existsSync(paths.prerendered)
? sirv(paths.prerendered, {
Expand All @@ -28,9 +30,9 @@ export function createServer({ render }) {

const assets_handler = fs.existsSync(paths.assets)
? sirv(paths.assets, {
setHeaders: (res, pathname, stats) => {
// eslint-disable-next-line no-undef
if (pathname.startsWith(APP_DIR)) {
setHeaders: (res, pathname) => {
// @ts-ignore - dynamically replaced with define
ignatiusmb marked this conversation as resolved.
Show resolved Hide resolved
if (pathname.startsWith(/* eslint-disable-line no-undef */ APP_DIR)) {
res.setHeader('cache-control', 'public, max-age=31536000, immutable');
}
},
Expand All @@ -40,6 +42,7 @@ export function createServer({ render }) {
: noop_handler;

const server = polka().use(
// @ts-ignore - compression return doesn't play well with polka.use
compression({ threshold: 0 }),
assets_handler,
prerendered_handler,
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/src/shims.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRequire } from 'module';
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';

// esbuild automatically renames "require"
// So we still have to use Object.defineProperty here
Expand Down
7 changes: 5 additions & 2 deletions packages/adapter-static/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
declare function plugin(options?: {
import { Adapter } from '@sveltejs/kit';

interface AdapterOptions {
pages?: string;
assets?: string;
fallback?: string;
}): import('@sveltejs/kit').Adapter;
}

declare function plugin(options?: AdapterOptions): Adapter;
export = plugin;
4 changes: 2 additions & 2 deletions packages/adapter-vercel/files/entry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getRawBody } from '@sveltejs/kit/node'; // eslint-disable-line import/no-unresolved
import { getRawBody } from '@sveltejs/kit/node';

// TODO hardcoding the relative location makes this brittle
import { init, render } from '../output/server/app.js'; // eslint-disable-line import/no-unresolved
import { init, render } from '../output/server/app.js';

init();

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-vercel/files/shims.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch'; // eslint-disable-line import/no-unresolved
export { fetch, Response, Request, Headers } from '@sveltejs/kit/install-fetch';
11 changes: 7 additions & 4 deletions packages/adapter-vercel/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
type BuildOptions = import('esbuild').BuildOptions;
declare function plugin(options?: {
esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}): import('@sveltejs/kit').Adapter;
import { Adapter } from '@sveltejs/kit';
import { BuildOptions } from 'esbuild';

interface AdapterOptions {
esbuild?: (options: BuildOptions) => Promise<BuildOptions> | BuildOptions;
}

declare function plugin(options?: AdapterOptions): Adapter;
export = plugin;
19 changes: 6 additions & 13 deletions packages/adapter-vercel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@ import esbuild from 'esbuild';
* @typedef {import('esbuild').BuildOptions} BuildOptions
*/

/**
* @param {{
* esbuild?: (defaultOptions: BuildOptions) => Promise<BuildOptions> | BuildOptions;
* }} [options]
**/
/** @type {import('.')} **/
export default function (options) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
return {
name: '@sveltejs/adapter-vercel',

async adapt({ utils }) {
Expand All @@ -37,18 +32,18 @@ export default function (options) {
utils.copy(join(files, 'entry.js'), '.svelte-kit/vercel/entry.js');

/** @type {BuildOptions} */
const defaultOptions = {
const default_options = {
entryPoints: ['.svelte-kit/vercel/entry.js'],
outfile: join(dirs.lambda, 'index.js'),
bundle: true,
inject: [join(files, 'shims.js')],
platform: 'node'
};

const buildOptions =
options && options.esbuild ? await options.esbuild(defaultOptions) : defaultOptions;
const build_options =
options && options.esbuild ? await options.esbuild(default_options) : default_options;

await esbuild.build(buildOptions);
await esbuild.build(build_options);

writeFileSync(join(dirs.lambda, 'package.json'), JSON.stringify({ type: 'commonjs' }));

Expand All @@ -65,6 +60,4 @@ export default function (options) {
utils.copy(join(files, 'routes.json'), join(dir, 'config/routes.json'));
}
};

return adapter;
}
5 changes: 1 addition & 4 deletions packages/kit/src/core/node/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* @param {import('http').IncomingMessage} req
* @returns {Promise<import('types/helper').RawBody>}
*/
/** @type {import('@sveltejs/kit/node').GetRawBody} */
export function getRawBody(req) {
return new Promise((fulfil, reject) => {
const h = req.headers;
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/runtime/client/start.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-ignore - value will be replaced on build step
import Root from 'ROOT'; // eslint-disable-line import/no-unresolved
import Root from 'ROOT';
ignatiusmb marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore - value will be replaced on build step
import { routes, fallback } from 'MANIFEST'; // eslint-disable-line import/no-unresolved
import { routes, fallback } from 'MANIFEST';
import { Router } from './router.js';
import { Renderer } from './renderer.js';
import { init } from './singletons.js';
Expand Down
6 changes: 1 addition & 5 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { lowercase_keys } from './utils.js';
import { coalesce_to_error } from '../utils.js';
import { hash } from '../hash.js';

/**
* @param {import('types/app').IncomingRequest} incoming
* @param {import('types/internal').SSRRenderOptions} options
* @param {import('types/internal').SSRRenderState} [state]
*/
/** @type {import('@sveltejs/kit/ssr').Respond} */
export async function respond(incoming, options, state = {}) {
if (incoming.path !== '/' && options.trailing_slash !== 'ignore') {
const has_trailing_slash = incoming.path.endsWith('/');
Expand Down
Loading