Skip to content

Commit f14f7b1

Browse files
committed
chore: migration doc
1 parent 0e047ac commit f14f7b1

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

MIGRATE.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Migration Guide
2+
3+
## Running Netlify functions locally
4+
5+
The functionality to have functions run locally has been completely integrated into the Netlify CLI and offers even more functionality like local debugging.
6+
7+
If you had `netlify-lambda` in you npm scripts you can simply migrate by changing to the Netlify CLIs `dev` command.
8+
9+
For example in a theoretical Gatsby project you can migrate with the following changes:
10+
11+
```diff
12+
{
13+
"scripts": {
14+
- "start:app": "npm run develop",
15+
- "start:lambda": "netlify-lambda serve src/lambda",
16+
- "start": "concurrently \"yarn start:lambda\" \"yarn develop\"",
17+
- "develop": "gatsby develop",
18+
+ "start": "netlify dev",
19+
},
20+
"devDependencies": {
21+
- "netlify-lambda": "^1.4.3",
22+
+ "netlify-cli": "^10.14.0",
23+
}
24+
}
25+
```
26+
27+
## Using Typescript or non-standard JavaScript features
28+
29+
Netlify now also supports Typescript and non-standard JavaScript features.
30+
For Typescript there is no configuration needed and tit will work out of the box. The same is true if you use ESM modules in your functions. The bundling logic will automatically detect this and use `esbuild` to bundle the function.
31+
In any other case you can enable set the `node_bundler` to `esbuild` yourself for the functions in your `netlify.toml` file. https://docs.netlify.com/configure-builds/file-based-configuration/#functions
32+
33+
Should `esbuild` not work for you usecase then please report this to us or use webpack directly. You can check how this works in the next section.
34+
35+
## Webpack bundling
36+
37+
> TBD: Should we create an example repo that does this?
38+
39+
> TBD: If we update to webpack 5 this needs to be adapted. See notion doc for question.
40+
41+
You might want to give our automated bundling a try (see above).
42+
43+
If you still want to use webpack to bundle your functions, you can simply use webpack yourself and adjust the following config to your needs. This example is for webpack 4, which is the version that netlify-lambda also used.
44+
45+
> package.json
46+
47+
```json
48+
{
49+
"scripts":{
50+
"build":"webpack --config ./webpack.config.js"
51+
}
52+
"devDependencies": {
53+
"webpack": "^4.46.0",
54+
"webpack-cli": "^4.10.0",
55+
"babel-loader": "^8.2.5",
56+
"@babel/preset-env": "^7.18.9",
57+
}
58+
}
59+
```
60+
61+
> webpack.config.js
62+
63+
```js
64+
const webpack = require('webpack');
65+
66+
module.exports = {
67+
mode: 'production',
68+
resolve: {
69+
extensions: ['.wasm', '.mjs', '.js', '.json', '.ts'],
70+
mainFields: ['module', 'main'],
71+
},
72+
module: {
73+
rules: [
74+
{
75+
test: /\.(m?js|ts)?$/,
76+
exclude: new RegExp(
77+
`(node_modules|bower_components|\\.(test|spec)\\.?)`,
78+
),
79+
use: {
80+
loader: require.resolve('babel-loader'),
81+
options: {
82+
cacheDirectory: true,
83+
presets: [
84+
[
85+
require.resolve('@babel/preset-env'),
86+
{ targets: { node: '16.6.0' } },
87+
],
88+
],
89+
},
90+
},
91+
},
92+
],
93+
},
94+
context: './src/functions',
95+
entry: {},
96+
target: 'node',
97+
plugins: [new webpack.IgnorePlugin(/vertx/)],
98+
output: {
99+
path: './netlify/functions',
100+
filename: '[name].js',
101+
libraryTarget: 'commonjs',
102+
},
103+
optimization: {
104+
nodeEnv: process.env.NODE_ENV || 'production',
105+
},
106+
bail: true,
107+
devtool: false,
108+
stats: {
109+
colors: true,
110+
},
111+
};
112+
```
113+
114+
## Install function dependencies
115+
116+
> TBD: where we go with this. See notion doc, should we add functionality like this to the netlify CLI? Or should we leave this to the user? In builds in buildbot we do install these dependencies. So I guess to be consistent `netlify dev` should do it to?
117+
118+
```diff
119+
{
120+
"scripts": {
121+
- "postinstall": "netlify-lambda install",
122+
+ "postinstall": "npm --prefix ./functions/my-function i && npm --prefix ./functions/other-function i",
123+
},
124+
"devDependencies": {
125+
- "netlify-lambda": "^1.4.3",
126+
}
127+
}
128+
```

0 commit comments

Comments
 (0)