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

[feat] use Netlify internal functions directory for generated functions #2113

Merged
merged 8 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brave-clouds-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/adapter-netlify': patch
---

Deploy generated Netlify entrypoint to the internal functions directory. This allows it to co-exist with other Netlify functions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ yarn.lock
.svelte-kit
.cloudflare
.pnpm-debug.log
.netlify
3 changes: 2 additions & 1 deletion examples/hn.svelte.dev/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
node_modules
/.svelte
/build
/functions
/functions
/.netlify/functions*
benmccann marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 3 additions & 2 deletions examples/hn.svelte.dev/.netlify/netlify-plugin-pnpm/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export default {
module.exports = {
benmccann marked this conversation as resolved.
Show resolved Hide resolved
onPreBuild: async ({ utils: { build, run } }) => {
try {
await run.command("npm install -g pnpm")
await run.command("pnpm install")
await run.command("pnpm -w install")
await run.command("pnpm -w build")
} catch (error) {
return build.failBuild(error)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"commonjs"}
1 change: 0 additions & 1 deletion examples/hn.svelte.dev/netlify.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[build]
command = "npm run build"
publish = ".svelte-kit/netlify/build/"
functions = ".svelte-kit/netlify/functions/"

[build.environment]
NPM_FLAGS="--prefix=/dev/null"
Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@ 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` and `build.functions` 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 and functions to based on the `build.publish` settings, as per this sample configuration:

```toml
[build]
command = "npm run build"
publish = "build/publish/"
functions = "build/functions/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if we can just remove this. Where will it put functions then? We put this here on purpose so that all output would show up under build and be automatically gitignored without the user having to edit their .gitignore at all. It would be nice if that continued to work for people who are not using the Netlify CLI

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or at least if we're going to remove this line, should we also remove the publish line so that ends up under .netlify/publish or something? It seems weird to treat these inconsistently.

Perhaps what we could do is maintain the old behavior and use the directories in these files if they are present and if they are not then we use the directory you're specifying as a default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functions will be in .netlify/functions-internal. The functions directory isn't needed anymore, because we're using the internal functions directory instead, which is hard-coded. The idea is that the functions value is for user functions, while .netlify/functions-internal is for auto-generated functions. If we remove the publish dir it will default to static because that's the value specified for SvelteKit. Should that be changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they user isn't using the Netlify CLI then presumably this won't be an issue anyway, as it wouldn't be using the Netlify preset when building locally as the auto-detection won't trigger it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I suppose the user could have manually specified the preset.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks for clarifying and being patient as I come up-to-speed on Netlify stuff. I'm on board with your plan. Lets always output to .netlify/functions-internal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so skip the CLI detection entirely?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the benefit of CLI detection? If we're not using the Netlify CLI, I'm assuming that any code .netlify/functions-internal will still be used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I'll update the PR.

Copy link
Contributor Author

@ascorbic ascorbic Aug 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I've updated the PR as follows:

  • Always write out the generated functions to .netlify/functions-internal
  • Read "publish" from netlify.toml, but if it's missing then default to "build"
  • Add more warnings and checks, such as ensuring that publish isn't set to the site root (that would delete the site!)
  • Updated the README

One thing I'd like to suggest for a follow-up PR is that cwd is passed to adapter functions here. That would allow us to ensure that we resolve all the paths correctly inside adapters. I may be misunderstanding it as I've not tested it, but as it stands I'm not sure if these will all work if cwd isn't process.cwd() This would likely apply to most adapters, as they all seem to assume paths are relative to process.cwd().

```

## Netlify alternatives to SvelteKit functionality
Expand Down
17 changes: 9 additions & 8 deletions packages/adapter-netlify/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ export default function (options) {
name: '@sveltejs/adapter-netlify',

async adapt({ utils }) {
const { publish, functions } = validate_config().build;
const { publish } = validate_config().build;

utils.rimraf(publish);
utils.rimraf(functions);

const files = fileURLToPath(new URL('./files', import.meta.url));

Expand All @@ -32,7 +31,7 @@ export default function (options) {
/** @type {BuildOptions} */
const defaultOptions = {
entryPoints: ['.svelte-kit/netlify/entry.js'],
outfile: join(functions, 'render/index.js'),
outfile: '.netlify/functions-internal/__render.js',
benmccann marked this conversation as resolved.
Show resolved Hide resolved
bundle: true,
inject: [join(files, 'shims.js')],
platform: 'node'
Expand All @@ -43,7 +42,7 @@ export default function (options) {

await esbuild.build(buildOptions);

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

utils.log.info('Prerendering static pages...');
await utils.prerender({
Expand All @@ -55,8 +54,10 @@ export default function (options) {
utils.copy_client_files(publish);

utils.log.minor('Writing redirects...');
utils.copy('_redirects', `${publish}/_redirects`);
appendFileSync(`${publish}/_redirects`, '\n\n/* /.netlify/functions/render 200');

const redirectPath = join(publish, '_redirects');
utils.copy('_redirects', redirectPath);
appendFileSync(redirectPath, '\n\n/* /.netlify/functions/__render 200');
benmccann marked this conversation as resolved.
Show resolved Hide resolved
}
};

Expand All @@ -74,9 +75,9 @@ function validate_config() {
throw err;
}

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

Expand Down