Skip to content

Commit

Permalink
Pass adapters directly to config (#579)
Browse files Browse the repository at this point in the history
* pass adapters directly to config

* changeset

* update docs
  • Loading branch information
Rich Harris authored Mar 19, 2021
1 parent 04f17f5 commit 8805c6d
Show file tree
Hide file tree
Showing 33 changed files with 226 additions and 109 deletions.
11 changes: 11 additions & 0 deletions .changeset/tough-lamps-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'@sveltejs/adapter-begin': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-static': patch
'@sveltejs/adapter-vercel': patch
'create-svelte': patch
'@sveltejs/kit': patch
---

Pass adapters directly to svelte.config.cjs
25 changes: 13 additions & 12 deletions documentation/docs/10-adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ For example, if you want to run your app as a simple Node server, you would use

```js
// svelte.config.cjs
const node = require('@sveltejs/adapter-node');

module.exports = {
kit: {
adapter: '@sveltejs/adapter-node'
adapter: node()
}
};
```
Expand All @@ -19,26 +21,25 @@ With this, [svelte-kit build](#command-line-interface-svelte-kit-build) will gen

```diff
// svelte.config.cjs
const node = require('@sveltejs/adapter-node');

module.exports = {
kit: {
- adapter: '@sveltejs/adapter-node'
+ adapter: ['@sveltejs/adapter-node', {
+ out: 'my-output-directory'
+ }]
- adapter: node()
+ adapter: node({ out: 'my-output-directory' })
}
};
```

A variety of official adapters exist for serverless platforms...

* [`adapter-begin`](https://github.com/sveltejs/kit/tree/master/packages/adapter-begin) — for [begin.com](https://begin.com)
* [`adapter-netlify`](https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify) — for [netlify.com](https://netlify.com)
* [`adapter-vercel`](https://github.com/sveltejs/kit/tree/master/packages/adapter-vercel) — for [vercel.com](https://vercel.com)
- [`adapter-begin`](https://github.com/sveltejs/kit/tree/master/packages/adapter-begin) — for [begin.com](https://begin.com)
- [`adapter-netlify`](https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify) — for [netlify.com](https://netlify.com)
- [`adapter-vercel`](https://github.com/sveltejs/kit/tree/master/packages/adapter-vercel) — for [vercel.com](https://vercel.com)

...and others:

* [`adapter-node`](https://github.com/sveltejs/kit/tree/master/packages/adapter-node) — for creating self-contained Node apps
* [`adapter-static`](https://github.com/sveltejs/kit/tree/master/packages/adapter-static) — for prerendering your entire site as a collection of static files

- [`adapter-node`](https://github.com/sveltejs/kit/tree/master/packages/adapter-node) — for creating self-contained Node apps
- [`adapter-static`](https://github.com/sveltejs/kit/tree/master/packages/adapter-static) — for prerendering your entire site as a collection of static files

> The adapter API is still in flux and will likely change before 1.0.
> The adapter API is still in flux and will likely change before 1.0.
2 changes: 1 addition & 1 deletion documentation/docs/13-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module.exports = {

#### adapter

Determines how the output of `svelte-kit build` is converted for different platforms. Can be specified as a `string` or a `[string, object]` tuple if you need to pass options.
Determines how the output of `svelte-kit build` is converted for different platforms. See [Adapters](#adapters).

#### amp

Expand Down
4 changes: 3 additions & 1 deletion examples/hn.svelte.dev/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const netlify = require('@sveltejs/adapter-netlify');

module.exports = {
kit: {
adapter: '@sveltejs/adapter-netlify',
adapter: netlify(),
target: '#svelte'
}
};
4 changes: 3 additions & 1 deletion examples/realworld.svelte.dev/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const node = require('@sveltejs/adapter-node');

module.exports = {
kit: {
// By default, `npm run build` will create a standard Node app.
// You can create optimized builds for different platforms by
// specifying a different adapter
adapter: '@sveltejs/adapter-node',
adapter: node(),

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',
Expand Down
3 changes: 2 additions & 1 deletion examples/sandbox/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const node = require('@sveltejs/adapter-node');
const pkg = require('./package.json');

module.exports = {
kit: {
adapter: '@sveltejs/adapter-node',
adapter: node(),
target: '#svelte',
vite: {
build: {
Expand Down
4 changes: 3 additions & 1 deletion examples/svelte-kit-demo/svelte.config.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const node = require('@sveltejs/adapter-node');

module.exports = {
kit: {
adapter: '@sveltejs/adapter-node',
adapter: node(),
target: '#svelte'
}
};
4 changes: 3 additions & 1 deletion packages/adapter-begin/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
node_modules
/files
/files
/index.cjs
/index.cjs.map
20 changes: 12 additions & 8 deletions packages/adapter-begin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,29 @@ import { copy } from '@sveltejs/app-utils/files';
import { resolve, join } from 'path';
import parse from '@architect/parser';

function parse_arc(arcPath) {
if (!existsSync(arcPath)) {
throw new Error(`No ${arcPath} found. See the documentation.`);
/** @param {string} file */
function parse_arc(file) {
if (!existsSync(file)) {
throw new Error(`No ${file} found. See the documentation.`);
}

try {
const text = readFileSync(arcPath).toString();
const text = readFileSync(file).toString();
const arc = parse(text);

return {
static: arc.static[0][1]
};
} catch (e) {
throw new Error(
`Error parsing ${arcPath}. Please consult the documentation for correct syntax.`
);
throw new Error(`Error parsing ${file}. Please consult the documentation for correct syntax.`);
}
}

export default function () {
return {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-begin',

async adapt(builder) {
builder.log.minor('Parsing app.arc file');
const { static: static_mount_point } = parse_arc('app.arc');
Expand All @@ -49,4 +51,6 @@ export default function () {
});
}
};

return adapter;
}
13 changes: 10 additions & 3 deletions packages/adapter-begin/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@sveltejs/adapter-begin",
"version": "1.0.0-next.3",
"main": "index.js",
"main": "index.cjs",
"scripts": {
"lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format",
"dev": "rollup -cw",
"build": "rollup -c",
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"prepublishOnly": "npm run build"
},
"files": [
"files"
Expand All @@ -17,7 +18,13 @@
"@sveltejs/app-utils": "workspace:*"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"rollup": "^2.41.1",
"sirv": "^1.0.11"
"sirv": "^1.0.11",
"typescript": "^4.2.3"
},
"exports": {
"import": "./index.js",
"require": "./index.cjs"
}
}
34 changes: 24 additions & 10 deletions packages/adapter-begin/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
input: 'src/index.js',
output: {
file: 'files/index.js',
format: 'cjs',
sourcemap: true,
exports: 'named'
export default [
{
input: 'src/index.js',
output: {
file: 'files/index.js',
format: 'cjs',
sourcemap: true,
exports: 'named'
},
plugins: [nodeResolve(), commonjs()],
external: [...require('module').builtinModules, '@architect/shared/app.js']
},
plugins: [nodeResolve(), commonjs()],
external: [...require('module').builtinModules, '@architect/shared/app.js']
};

{
input: 'index.js',
output: {
file: 'index.cjs',
format: 'cjs',
sourcemap: true,
exports: 'named'
},
plugins: [nodeResolve(), commonjs()],
external: [...require('module').builtinModules, '@architect/parser']
}
];
13 changes: 13 additions & 0 deletions packages/adapter-begin/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"noImplicitAny": true,
"target": "es2020",
"module": "es2020",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
},
"include": ["./index.js", "src"]
}
18 changes: 10 additions & 8 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { existsSync, readFileSync, copyFileSync, writeFileSync, renameSync } from 'fs';
import { dirname, resolve } from 'path';
import toml from 'toml';
import { fileURLToPath } from 'url';
const { existsSync, readFileSync, copyFileSync, writeFileSync, renameSync } = require('fs');
const { resolve } = require('path');
const toml = require('toml');

const __dirname = dirname(fileURLToPath(import.meta.url));
module.exports = function () {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-netlify',

export default function () {
return {
async adapt(builder) {
let netlify_config;

Expand Down Expand Up @@ -60,4 +60,6 @@ export default function () {
});
}
};
}

return adapter;
};
5 changes: 4 additions & 1 deletion packages/adapter-netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@sveltejs/adapter-netlify",
"version": "1.0.0-next.3",
"main": "index.js",
"type": "module",
"files": [
"files"
],
Expand All @@ -13,5 +12,9 @@
},
"dependencies": {
"toml": "^3.0.0"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"typescript": "^4.2.3"
}
}
13 changes: 13 additions & 0 deletions packages/adapter-netlify/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noEmit": true,
"noImplicitAny": true,
"target": "es2020",
"module": "es2020",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true
},
"include": ["./index.js", "src"]
}
15 changes: 7 additions & 8 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { copyFileSync } from 'fs';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
const { copyFileSync } = require('fs');
const { join } = require('path');

/**
* @param {{
* out?: string;
* }} options
*/
export default function ({ out = 'build' } = {}) {
module.exports = function ({ out = 'build' } = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
async adapt(builder) {
const dir = dirname(fileURLToPath(import.meta.url));
name: '@sveltejs/adapter-node',

async adapt(builder) {
builder.log.minor('Copying assets');
const static_directory = join(out, 'assets');
builder.copy_client_files(static_directory);
Expand All @@ -21,7 +20,7 @@ export default function ({ out = 'build' } = {}) {
builder.log.minor('Copying server');
builder.copy_server_files(out);

copyFileSync(`${dir}/files/server.js`, `${out}/index.js`);
copyFileSync(`${__dirname}/files/server.js`, `${out}/index.js`);

builder.log.minor('Prerendering static pages');
await builder.prerender({
Expand All @@ -31,4 +30,4 @@ export default function ({ out = 'build' } = {}) {
};

return adapter;
}
};
1 change: 0 additions & 1 deletion packages/adapter-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "@sveltejs/adapter-node",
"version": "1.0.0-next.8",
"main": "index.js",
"type": "module",
"files": [
"files"
],
Expand Down
11 changes: 8 additions & 3 deletions packages/adapter-static/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export default function ({ pages = 'build', assets = 'build' } = {}) {
return {
module.exports = function ({ pages = 'build', assets = 'build' } = {}) {
/** @type {import('@sveltejs/kit').Adapter} */
const adapter = {
name: '@sveltejs/adapter-static',

async adapt(builder) {
builder.copy_static_files(assets);
builder.copy_client_files(assets);
Expand All @@ -10,4 +13,6 @@ export default function ({ pages = 'build', assets = 'build' } = {}) {
});
}
};
}

return adapter;
};
5 changes: 4 additions & 1 deletion packages/adapter-static/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "@sveltejs/adapter-static",
"version": "1.0.0-next.3",
"type": "module",
"scripts": {
"lint": "eslint --ignore-path .gitignore \"**/*.{ts,js,svelte}\" && npm run check-format",
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
},
"devDependencies": {
"@sveltejs/kit": "workspace:*",
"typescript": "^4.2.3"
}
}
Loading

0 comments on commit 8805c6d

Please sign in to comment.