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

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 +2 -2
  2. package/lib/collectRedirects.js +40 -28
  3. package/lib/createRedirectPageContent.js +4 -6
  4. package/lib/extensionRedirects.d.ts +1 -1
  5. package/lib/extensionRedirects.js +16 -12
  6. package/lib/index.d.ts +3 -3
  7. package/lib/index.js +10 -9
  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 +2 -2
  13. package/lib/writeRedirectFiles.js +38 -12
  14. package/package.json +16 -13
  15. package/src/collectRedirects.ts +54 -37
  16. package/src/createRedirectPageContent.ts +4 -4
  17. package/src/extensionRedirects.ts +21 -15
  18. package/src/index.ts +13 -6
  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 +42 -12
  24. package/lib/.tsbuildinfo +0 -1970
  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 -71
  30. package/src/__tests__/collectRedirects.test.ts +0 -255
  31. package/src/__tests__/createRedirectPageContent.test.ts +0 -26
  32. package/src/__tests__/extensionRedirects.test.ts +0 -109
  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 -146
  36. package/tsconfig.json +0 -10
@@ -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,146 +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', 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
- );
59
-
60
- expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
61
- path.join(pluginContext.outDir, '/abc.html/index.html'),
62
- path.join(pluginContext.outDir, '/def/index.html'),
63
- path.join(pluginContext.outDir, '/xyz/index.html'),
64
- ]);
65
-
66
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
67
- 'fileContent',
68
- );
69
- });
70
-
71
- test('should create appropriate metadatas for root baseUrl', async () => {
72
- const pluginContext = {
73
- outDir: '/tmp/someFixedOutDir',
74
- baseUrl: '/',
75
- };
76
- const redirectFiles = toRedirectFilesMetadata(
77
- [{from: '/abc.html', to: '/abc'}],
78
- pluginContext,
79
- );
80
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
81
- 'fileContent baseUrl=/',
82
- );
83
- });
84
-
85
- test('should create appropriate metadatas for empty baseUrl', async () => {
86
- const pluginContext = {
87
- outDir: '/tmp/someFixedOutDir',
88
- baseUrl: '',
89
- };
90
- const redirectFiles = toRedirectFilesMetadata(
91
- [{from: '/abc.html', to: '/abc'}],
92
- pluginContext,
93
- );
94
- expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
95
- 'fileContent baseUrl=empty',
96
- );
97
- });
98
- });
99
-
100
- describe('writeRedirectFiles', () => {
101
- test('write the files', async () => {
102
- const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
103
-
104
- const filesMetadata = [
105
- {
106
- fileAbsolutePath: path.join(outDir, 'someFileName'),
107
- fileContent: 'content 1',
108
- },
109
- {
110
- fileAbsolutePath: path.join(outDir, '/some/nested/filename'),
111
- fileContent: 'content 2',
112
- },
113
- ];
114
-
115
- await writeRedirectFiles(filesMetadata);
116
-
117
- await expect(
118
- fs.readFile(filesMetadata[0].fileAbsolutePath, 'utf8'),
119
- ).resolves.toEqual('content 1');
120
-
121
- await expect(
122
- fs.readFile(filesMetadata[1].fileAbsolutePath, 'utf8'),
123
- ).resolves.toEqual('content 2');
124
- });
125
-
126
- test('avoid overwriting existing files', async () => {
127
- const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
128
-
129
- const filesMetadata = [
130
- {
131
- fileAbsolutePath: path.join(outDir, 'someFileName/index.html'),
132
- fileContent: 'content 1',
133
- },
134
- ];
135
-
136
- await fs.ensureDir(path.dirname(filesMetadata[0].fileAbsolutePath));
137
- await fs.writeFile(
138
- filesMetadata[0].fileAbsolutePath,
139
- 'file already exists!',
140
- );
141
-
142
- await expect(writeRedirectFiles(filesMetadata)).rejects.toThrowError(
143
- `Redirect file creation error for path=${filesMetadata[0].fileAbsolutePath}: Error: The redirect plugin is not supposed to override existing files`,
144
- );
145
- });
146
- });
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
- }