Skip to content

Commit

Permalink
Chnages from review
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Aug 18, 2021
1 parent bb5939d commit a8863d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion examples/hn.svelte.dev/netlify.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[build]
command = "npm run build"
publish = ".svelte-kit/netlify/build/"
publish = "build"

[build.environment]
NPM_FLAGS="--prefix=/dev/null"
Expand Down
23 changes: 20 additions & 3 deletions packages/adapter-netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ export default {
};
```

Then, make sure you have a [netlify.toml](https://docs.netlify.com/configure-builds/file-based-configuration) file in the project root. This will determine where to write static assets and functions to based on the `build.publish` settings, as per this sample configuration:
Then, make sure you have a [netlify.toml](https://docs.netlify.com/configure-builds/file-based-configuration) file in the project root. This will determine where to write static assets based on the `build.publish` settings, as per this sample configuration:

```toml
[build]
command = "npm run build"
publish = "build/publish/"
publish = "build"
```

If the `netlify.toml` file or the `build.publish` value is missing, a default value of `"build"` will be used. Note that if you have set the publish directory in the Netlify UI to something else then you will need to set it in `netlify.toml` too, or use the default value of `"build"`.

## Netlify alternatives to SvelteKit functionality

You may build your app using functionality provided directly by SvelteKit without relying on any Netlify functionality. Using the SvelteKit versions of these features will allow them to be used in dev mode, tested with integration tests, and to work with other adapters should you ever decide to switch away from Netlify. However, in some scenarios you may find it beneficial to use the Netlify versions of these features. One example would be if you're migrating an app that's already hosted on Netlify to SvelteKit.
Expand All @@ -52,6 +54,20 @@ During compilation a required "catch all" redirect rule is automatically appende
2. Netlify's build bot parses your HTML files at deploy time, which means your form must be [prerendered](https://kit.svelte.dev/docs#ssr-and-javascript-prerender) as HTML. You can either add `export const prerender = true` to your `contact.svelte` to prerender just that page or set the `kit.prerender.force: true` option to prerender all pages.
3. If your Netlify form has a [custom success message](https://docs.netlify.com/forms/setup/#success-messages) like `<form netlify ... action="/success">` then ensure the corresponding `/routes/success.svelte` exists and is prerendered.

### Using Netlify Functions

[Netlify Functions](https://docs.netlify.com/functions/overview/) can be used alongside your SvelteKit routes. If you would like to add them to your site, you should create a directory for them and add the configuration to your `netlify.toml` file. For example:

```toml
[build]
command = "npm run build"
publish = "build"

[functions]
directory = "functions"
node_bundler = "esbuild"
```

## Advanced Configuration

### esbuild
Expand All @@ -76,7 +92,8 @@ The default options for this version are as follows:
```js
{
entryPoints: ['.svelte-kit/netlify/entry.js'],
outfile: `pathToFunctionsFolder/render/index.js`,
// This is Netlify's internal functions directory, not the one for user functions.
outfile: '.netlify/functions-internal/__render.js',
bundle: true,
inject: ['pathTo/shims.js'],
platform: 'node'
Expand Down
34 changes: 21 additions & 13 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { appendFileSync, existsSync, readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { join, resolve } from 'path';
import { fileURLToPath } from 'url';
import esbuild from 'esbuild';
import toml from '@iarna/toml';
Expand All @@ -19,7 +19,9 @@ export default function (options) {
name: '@sveltejs/adapter-netlify',

async adapt({ utils }) {
const { publish } = validate_config().build;
const publish = get_publish_directory(utils) || 'build';

utils.log.minor(`Publishing to "${publish}"`);

utils.rimraf(publish);

Expand All @@ -46,7 +48,7 @@ export default function (options) {

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

utils.log.info('Prerendering static pages...');
utils.log.minor('Prerendering static pages...');
await utils.prerender({
dest: publish
});
Expand All @@ -65,9 +67,12 @@ export default function (options) {

return adapter;
}

function validate_config() {
/**
* @param {import('@sveltejs/kit').AdapterUtils} utils
**/
function get_publish_directory(utils) {
if (existsSync('netlify.toml')) {
/** @type {{ build?: { publish?: string }} & toml.JsonMap } */
let netlify_config;

try {
Expand All @@ -78,22 +83,25 @@ function validate_config() {
}

if (!netlify_config.build || !netlify_config.build.publish) {
throw new Error(
'You must specify build.publish in netlify.toml. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration'
);
// This is the default publish directory when Netlify detects SvelteKit
utils.log.warn('No publish directory specified in netlify.toml, using default');
return;
}

if (netlify_config.redirects) {
throw new Error(
"Redirects are not supported in netlify.toml. Use _redirects instead. For more details consult the readme's troubleshooting section."
);
}

return netlify_config;
if (resolve(netlify_config.build.publish) === process.cwd()) {
throw new Error(
'The publish directory cannot be set to the site root. Please change it to "build" in netlify.toml.'
);
}
return netlify_config.build.publish;
}

// 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'
utils.log.warn(
'No netlify.toml found. Using default publish directory. Consult https://github.com/sveltejs/kit/tree/master/packages/adapter-netlify#configuration for more details '
);
}

0 comments on commit a8863d0

Please sign in to comment.