Skip to content

Commit

Permalink
#166, but javascript (#180)
Browse files Browse the repository at this point in the history
* start work on #166

* remove typescript

* remove ts-node in favour of esm

* Build kit package with sucrase

* Convert tests to use sucrase

* get build working again

* fix gitignore files

* add back some missing files

* tidy up

* bit more tidying

* restrict uvu pattern

* copy client files, not everything

* remove generated file

* implement adapter-static using builder API

* ignore generated file

* get svelte-kit-demo example working

* update lockfile

* add back svelte eslint config peer dependencies

* move render logic into kit

* update test

* update adapter-netlify

* fixes

* simplify adapter-netlify

* use netlify in hn example

* add use strict pragmas

* remove unused dependency

* update lockfile

* tidy up

* update snowpack config

* add changeset

Co-authored-by: Ben McCann <[email protected]>
  • Loading branch information
Rich-Harris and benmccann authored Nov 26, 2020
1 parent 104d962 commit b475ed4
Show file tree
Hide file tree
Showing 51 changed files with 621 additions and 596 deletions.
12 changes: 12 additions & 0 deletions .changeset/gold-readers-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'hn.svelte.dev': patch
'svelte-kit-demo': patch
'@sveltejs/adapter-netlify': patch
'@sveltejs/adapter-node': patch
'@sveltejs/adapter-static': patch
'@sveltejs/app-utils': patch
'@sveltejs/kit': patch
'@sveltejs/snowpack-config': patch
---

Overhaul adapter API - fixes #166
4 changes: 4 additions & 0 deletions examples/hn.svelte.dev/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[build]
command = "npm run build"
publish = "build/"
functions = "functions/"
2 changes: 1 addition & 1 deletion examples/hn.svelte.dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"build": "svelte build"
},
"devDependencies": {
"@sveltejs/adapter-node": "workspace:*",
"@sveltejs/adapter-netlify": "workspace:*",
"@sveltejs/kit": "workspace:*",
"@sveltejs/snowpack-config": "workspace:*",
"svelte": "^3.29.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/hn.svelte.dev/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module.exports = {
// 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: '@sveltejs/adapter-netlify'
};
Empty file.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^9.0.0",
"@sveltejs/eslint-config": "github:sveltejs/eslint-config#v5.6.0",
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
"eslint": "^7.11.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-svelte3": "^2.7.3",
"prettier": "2.1.2",
"rollup": "^2.32.0"
"rollup": "^2.32.0",
"typescript": "^4.1.2"
}
}
4 changes: 1 addition & 3 deletions packages/adapter-netlify/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
.DS_Store
node_modules
/index.js
/render.js
node_modules
45 changes: 45 additions & 0 deletions packages/adapter-netlify/files/render.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const url = require('url');
const app = require('./app.js');

exports.handler = async (event) => {
const {
path,
httpMethod,
headers,
queryStringParameters
// body, // TODO pass this to renderer
// isBase64Encoded // TODO is this useful?
} = event;

const query = new url.URLSearchParams();
for (const k in queryStringParameters) {
const value = queryStringParameters[k];
value.split(', ').forEach((v) => {
query.append(k, v);
});
}

const rendered = await app.render({
host: null, // TODO
method: httpMethod,
headers,
path,
query
});

if (rendered) {
return {
isBase64Encoded: false,
statusCode: rendered.status,
headers: rendered.headers,
body: rendered.body
};
}

return {
statusCode: 404,
body: 'Not found'
};
};
48 changes: 48 additions & 0 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

const fs = require('fs');
const path = require('path');
const toml = require('toml');

module.exports = async function adapter(builder) {
let netlify_config;

if (fs.existsSync('netlify.toml')) {
try {
netlify_config = toml.parse(fs.readFileSync('netlify.toml', 'utf-8'));
} catch (err) {
err.message = `Error parsing netlify.toml: ${err.message}`;
throw err;
}
} else {
// TODO offer to create one?
throw new Error(
'Missing a netlify.toml file. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration'
);
}

if (!netlify_config.build || !netlify_config.build.publish || !netlify_config.build.functions) {
throw new Error(
'You must specify build.publish and build.functions in netlify.toml. Consult https://github.com/sveltejs/adapter-netlify#configuration'
);
}

const publish = path.resolve(netlify_config.build.publish);
const functions = path.resolve(netlify_config.build.functions);

builder.copy_static_files(publish);
builder.copy_client_files(`${publish}/_app`);
builder.copy_server_files(`${functions}/render`);

// copy the renderer
fs.copyFileSync(path.resolve(__dirname, 'files/render.js'), `${functions}/render/index.js`);

// create _redirects
fs.writeFileSync(`${publish}/_redirects`, '/* /.netlify/functions/render 200');

// prerender
builder.log.info('Prerendering static pages...');
await builder.prerender({
dest: publish
});
};
15 changes: 2 additions & 13 deletions packages/adapter-netlify/package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
{
"name": "@sveltejs/adapter-netlify",
"version": "0.0.11",
"devDependencies": {
"@sveltejs/app-utils": "workspace:*",
"@types/aws-lambda": "^8.10.64",
"devalue": "^2.0.1",
"rollup": "^2.32.0"
},
"main": "index.js",
"files": [
"render.js"
"files"
],
"scripts": {
"dev": "rollup -cw",
"build": "rollup -c",
"lint": "eslint --ignore-pattern node_modules/ --ignore-pattern render.js \"**/*.{ts,js,svelte}\" && npm run check-format",
"format": "prettier --write . --config ../../.prettierrc --ignore-path .gitignore",
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore",
"prepublishOnly": "npm run build"
"check-format": "prettier --check . --config ../../.prettierrc --ignore-path .gitignore"
},
"dependencies": {
"kleur": "^4.1.3",
"tiny-glob": "^0.2.8",
"toml": "^3.0.0"
}
}
23 changes: 0 additions & 23 deletions packages/adapter-netlify/rollup.config.js

This file was deleted.

91 changes: 0 additions & 91 deletions packages/adapter-netlify/src/index.js

This file was deleted.

66 changes: 0 additions & 66 deletions packages/adapter-netlify/src/render.js

This file was deleted.

5 changes: 1 addition & 4 deletions packages/adapter-node/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
.DS_Store
node_modules
index.js
index.d.ts
server.js
server.d.ts
/files
17 changes: 17 additions & 0 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const fs = require('fs');

module.exports = async function adapter(builder) {
const out = 'build'; // TODO implement adapter options

builder.copy_server_files(out);
builder.copy_client_files(`${out}/assets/_app`);

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

builder.log.info('Prerendering static pages...');
await builder.prerender({
dest: `${out}/prerendered`
});
};
Loading

0 comments on commit b475ed4

Please sign in to comment.