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

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