@docusaurus/plugin-client-redirects 2.0.0-beta.15a2b59f9 → 2.0.0-beta.17

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 (36) hide show
  1. package/lib/collectRedirects.d.ts +1 -1
  2. package/lib/collectRedirects.js +30 -35
  3. package/lib/createRedirectPageContent.js +2 -4
  4. package/lib/extensionRedirects.d.ts +1 -1
  5. package/lib/extensionRedirects.js +9 -16
  6. package/lib/index.d.ts +3 -3
  7. package/lib/index.js +5 -5
  8. package/lib/normalizePluginOptions.d.ts +1 -1
  9. package/lib/normalizePluginOptions.js +2 -2
  10. package/lib/redirectValidation.d.ts +1 -1
  11. package/lib/types.d.ts +2 -14
  12. package/lib/writeRedirectFiles.d.ts +1 -1
  13. package/lib/writeRedirectFiles.js +15 -14
  14. package/package.json +16 -14
  15. package/src/collectRedirects.ts +36 -46
  16. package/src/createRedirectPageContent.ts +4 -4
  17. package/src/extensionRedirects.ts +9 -17
  18. package/src/index.ts +5 -4
  19. package/src/normalizePluginOptions.ts +6 -2
  20. package/src/plugin-client-redirects.d.ts +27 -0
  21. package/src/redirectValidation.ts +1 -1
  22. package/src/types.ts +2 -22
  23. package/src/writeRedirectFiles.ts +17 -14
  24. package/lib/.tsbuildinfo +0 -1
  25. package/src/__tests__/__snapshots__/collectRedirects.test.ts.snap +0 -27
  26. package/src/__tests__/__snapshots__/createRedirectPageContent.test.ts.snap +0 -29
  27. package/src/__tests__/__snapshots__/normalizePluginOptions.test.ts.snap +0 -35
  28. package/src/__tests__/__snapshots__/redirectValidation.test.ts.snap +0 -11
  29. package/src/__tests__/__snapshots__/writeRedirectFiles.test.ts.snap +0 -147
  30. package/src/__tests__/collectRedirects.test.ts +0 -340
  31. package/src/__tests__/createRedirectPageContent.test.ts +0 -26
  32. package/src/__tests__/extensionRedirects.test.ts +0 -143
  33. package/src/__tests__/normalizePluginOptions.test.ts +0 -76
  34. package/src/__tests__/redirectValidation.test.ts +0 -66
  35. package/src/__tests__/writeRedirectFiles.test.ts +0 -204
  36. package/tsconfig.json +0 -10
@@ -1,204 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- import fs from 'fs-extra';
8
- import path from 'path';
9
-
10
- import writeRedirectFiles, {
11
- toRedirectFilesMetadata,
12
- createToUrl,
13
- } from '../writeRedirectFiles';
14
-
15
- // Test to fix toUrl bugs we had:
16
- // - https://github.com/facebook/docusaurus/issues/3886
17
- // - https://github.com/facebook/docusaurus/issues/3925
18
- describe('createToUrl', () => {
19
- test('should create appropriate redirect urls', async () => {
20
- expect(createToUrl('/', '/docs/something/else')).toEqual(
21
- '/docs/something/else',
22
- );
23
- expect(createToUrl('/', '/docs/something/else/')).toEqual(
24
- '/docs/something/else/',
25
- );
26
- expect(createToUrl('/', 'docs/something/else')).toEqual(
27
- '/docs/something/else',
28
- );
29
- });
30
-
31
- test('should create appropriate redirect urls with baseUrl', async () => {
32
- expect(createToUrl('/baseUrl/', '/docs/something/else')).toEqual(
33
- '/baseUrl/docs/something/else',
34
- );
35
- expect(createToUrl('/baseUrl/', '/docs/something/else/')).toEqual(
36
- '/baseUrl/docs/something/else/',
37
- );
38
- expect(createToUrl('/baseUrl/', 'docs/something/else')).toEqual(
39
- '/baseUrl/docs/something/else',
40
- );
41
- });
42
- });
43
-
44
- describe('toRedirectFilesMetadata', () => {
45
- test('should create appropriate metadatas trailingSlash=undefined', async () => {
46
- const pluginContext = {
47
- outDir: '/tmp/someFixedOutDir',
48
- baseUrl: 'https://docusaurus.io',
49
- };
50
-
51
- const redirectFiles = toRedirectFilesMetadata(
52
- [
53
- {from: '/abc.html', to: '/abc'},
54
- {from: '/def', to: '/def.html'},
55
- {from: '/xyz', to: '/'},
56
- ],
57
- pluginContext,
58
- undefined,
59
- );
60
-
61
- expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
62
- path.join(pluginContext.outDir, '/abc.html/index.html'),
63
- path.join(pluginContext.outDir, '/def/index.html'),
64
- path.join(pluginContext.outDir, '/xyz/index.html'),
65
- ]);
66
-
67
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
68
- 'fileContent',
69
- );
70
- });
71
-
72
- test('should create appropriate metadatas trailingSlash=true', async () => {
73
- const pluginContext = {
74
- outDir: '/tmp/someFixedOutDir',
75
- baseUrl: 'https://docusaurus.io',
76
- };
77
-
78
- const redirectFiles = toRedirectFilesMetadata(
79
- [
80
- {from: '/abc.html', to: '/abc'},
81
- {from: '/def', to: '/def.html'},
82
- {from: '/xyz', to: '/'},
83
- ],
84
- pluginContext,
85
- true,
86
- );
87
-
88
- expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
89
- path.join(pluginContext.outDir, '/abc.html/index.html'),
90
- path.join(pluginContext.outDir, '/def/index.html'),
91
- path.join(pluginContext.outDir, '/xyz/index.html'),
92
- ]);
93
-
94
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
95
- 'fileContent',
96
- );
97
- });
98
-
99
- test('should create appropriate metadatas trailingSlash=false', async () => {
100
- const pluginContext = {
101
- outDir: '/tmp/someFixedOutDir',
102
- baseUrl: 'https://docusaurus.io',
103
- };
104
-
105
- const redirectFiles = toRedirectFilesMetadata(
106
- [
107
- {from: '/abc.html', to: '/abc'},
108
- {from: '/def', to: '/def.html'},
109
- {from: '/xyz', to: '/'},
110
- ],
111
- pluginContext,
112
- false,
113
- );
114
-
115
- expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
116
- // path.join(pluginContext.outDir, '/abc.html/index.html'), // Can't be used because /abc.html already exists, and file/folder can't share same name on Unix!
117
- path.join(pluginContext.outDir, '/abc.html.html'), // Weird but on purpose!
118
- path.join(pluginContext.outDir, '/def/index.html'),
119
- path.join(pluginContext.outDir, '/xyz/index.html'),
120
- ]);
121
-
122
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
123
- 'fileContent',
124
- );
125
- });
126
-
127
- test('should create appropriate metadatas for root baseUrl', async () => {
128
- const pluginContext = {
129
- outDir: '/tmp/someFixedOutDir',
130
- baseUrl: '/',
131
- };
132
- const redirectFiles = toRedirectFilesMetadata(
133
- [{from: '/abc.html', to: '/abc'}],
134
- pluginContext,
135
- undefined,
136
- );
137
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
138
- 'fileContent baseUrl=/',
139
- );
140
- });
141
-
142
- test('should create appropriate metadatas for empty baseUrl', async () => {
143
- const pluginContext = {
144
- outDir: '/tmp/someFixedOutDir',
145
- baseUrl: '',
146
- };
147
- const redirectFiles = toRedirectFilesMetadata(
148
- [{from: '/abc.html', to: '/abc'}],
149
- pluginContext,
150
- undefined,
151
- );
152
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
153
- 'fileContent baseUrl=empty',
154
- );
155
- });
156
- });
157
-
158
- describe('writeRedirectFiles', () => {
159
- test('write the files', async () => {
160
- const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
161
-
162
- const filesMetadata = [
163
- {
164
- fileAbsolutePath: path.join(outDir, 'someFileName'),
165
- fileContent: 'content 1',
166
- },
167
- {
168
- fileAbsolutePath: path.join(outDir, '/some/nested/filename'),
169
- fileContent: 'content 2',
170
- },
171
- ];
172
-
173
- await writeRedirectFiles(filesMetadata);
174
-
175
- await expect(
176
- fs.readFile(filesMetadata[0].fileAbsolutePath, 'utf8'),
177
- ).resolves.toEqual('content 1');
178
-
179
- await expect(
180
- fs.readFile(filesMetadata[1].fileAbsolutePath, 'utf8'),
181
- ).resolves.toEqual('content 2');
182
- });
183
-
184
- test('avoid overwriting existing files', async () => {
185
- const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
186
-
187
- const filesMetadata = [
188
- {
189
- fileAbsolutePath: path.join(outDir, 'someFileName/index.html'),
190
- fileContent: 'content 1',
191
- },
192
- ];
193
-
194
- await fs.ensureDir(path.dirname(filesMetadata[0].fileAbsolutePath));
195
- await fs.writeFile(
196
- filesMetadata[0].fileAbsolutePath,
197
- 'file already exists!',
198
- );
199
-
200
- await expect(writeRedirectFiles(filesMetadata)).rejects.toThrowError(
201
- `Redirect file creation error for "${filesMetadata[0].fileAbsolutePath}" path: Error: The redirect plugin is not supposed to override existing files.`,
202
- );
203
- });
204
- });
package/tsconfig.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "incremental": true,
5
- "tsBuildInfoFile": "./lib/.tsbuildinfo",
6
- "rootDir": "src",
7
- "outDir": "lib",
8
- "typeRoots": ["./types", "./node_modules/@types"]
9
- }
10
- }