@sveltejs/adapter-netlify 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +50 -2
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,8 +1,16 @@
1
- import { appendFileSync, existsSync, readFileSync, writeFileSync } from 'fs';
1
+ import {
2
+ appendFileSync,
3
+ existsSync,
4
+ readFileSync,
5
+ writeFileSync,
6
+ unlinkSync,
7
+ createReadStream
8
+ } from 'fs';
2
9
  import { dirname, join, resolve, posix } from 'path';
3
10
  import { fileURLToPath } from 'url';
4
11
  import esbuild from 'esbuild';
5
12
  import toml from '@iarna/toml';
13
+ import { createInterface } from 'readline';
6
14
 
7
15
  /**
8
16
  * @typedef {{
@@ -39,15 +47,55 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
39
47
  name: '@sveltejs/adapter-netlify',
40
48
 
41
49
  async adapt(builder) {
50
+ if (!builder.routes) {
51
+ throw new Error(
52
+ '@sveltejs/adapter-netlify >=2.x (possibly installed through @sveltejs/adapter-auto) requires @sveltejs/kit version 1.5 or higher. ' +
53
+ 'Either downgrade the adapter or upgrade @sveltejs/kit'
54
+ );
55
+ }
56
+
42
57
  const netlify_config = get_netlify_config();
43
58
 
44
59
  // "build" is the default publish directory when Netlify detects SvelteKit
45
60
  const publish = get_publish_directory(netlify_config, builder) || 'build';
46
61
 
62
+ const redirects_file_path = join(publish, '_redirects');
63
+
64
+ // If redirects file exists - empty any netlify generated files in functions-internal
65
+ // Without removing other files that may have been auto generated by integrations
66
+ if (existsSync(redirects_file_path)) {
67
+ // Read each line of the file
68
+ const fileStream = createReadStream(redirects_file_path);
69
+ const rl = createInterface({
70
+ input: fileStream,
71
+ crlfDelay: Infinity
72
+ });
73
+
74
+ // Create an array of lines
75
+ const lines = [];
76
+ for await (const line of rl) {
77
+ lines.push(line);
78
+ }
79
+
80
+ const functions_internal = join('.netlify', 'functions-internal');
81
+
82
+ // Loop through redirects, and delete corresponding functions-internal files
83
+ lines.forEach((line) => {
84
+ if (line) {
85
+ // example line /.netlify/functions/{function_name} 200
86
+ const path = line.split(' ')[1];
87
+ const function_name = path.split('/').pop();
88
+ const mjsFile = join(functions_internal, `${function_name}.mjs`);
89
+ const jsonFile = join(functions_internal, `${function_name}.json`);
90
+ if (existsSync(mjsFile)) unlinkSync(mjsFile);
91
+ if (existsSync(jsonFile)) unlinkSync(jsonFile);
92
+ }
93
+ });
94
+ }
95
+
47
96
  // empty out existing build directories
48
97
  builder.rimraf(publish);
49
98
  builder.rimraf('.netlify/edge-functions');
50
- builder.rimraf('.netlify/functions-internal');
51
99
  builder.rimraf('.netlify/server');
52
100
  builder.rimraf('.netlify/package.json');
53
101
  builder.rimraf('.netlify/serverless.js');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltejs/adapter-netlify",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/sveltejs/kit",
@@ -38,7 +38,7 @@
38
38
  "rollup": "^3.7.0",
39
39
  "typescript": "^4.9.4",
40
40
  "uvu": "^0.5.6",
41
- "@sveltejs/kit": "^1.5.0"
41
+ "@sveltejs/kit": "^1.5.1"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@sveltejs/kit": "^1.5.0"