-
-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: shorthands for themes/plugins/presets configuration (#5930)
Co-authored-by: Josh-Cena <sidachen2003@gmail.com> Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
- Loading branch information
1 parent
bfd7fd9
commit b366ba5
Showing
11 changed files
with
275 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/docusaurus/src/server/__tests__/moduleShorthand.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import {getNamePatterns, resolveModuleName} from '../moduleShorthand'; | ||
|
||
describe('getNamePatterns', () => { | ||
test('should resolve plain names', () => { | ||
expect(getNamePatterns('awesome', 'plugin')).toEqual([ | ||
'awesome', | ||
'@docusaurus/plugin-awesome', | ||
'docusaurus-plugin-awesome', | ||
]); | ||
|
||
expect(getNamePatterns('awesome', 'theme')).toEqual([ | ||
'awesome', | ||
'@docusaurus/theme-awesome', | ||
'docusaurus-theme-awesome', | ||
]); | ||
}); | ||
|
||
test('should expand bare scopes', () => { | ||
expect(getNamePatterns('@joshcena', 'plugin')).toEqual([ | ||
'@joshcena/docusaurus-plugin', | ||
]); | ||
|
||
expect(getNamePatterns('@joshcena', 'theme')).toEqual([ | ||
'@joshcena/docusaurus-theme', | ||
]); | ||
}); | ||
|
||
test('should expand scoped names', () => { | ||
expect(getNamePatterns('@joshcena/awesome', 'plugin')).toEqual([ | ||
'@joshcena/awesome', | ||
'@joshcena/docusaurus-plugin-awesome', | ||
]); | ||
|
||
expect(getNamePatterns('@joshcena/awesome', 'theme')).toEqual([ | ||
'@joshcena/awesome', | ||
'@joshcena/docusaurus-theme-awesome', | ||
]); | ||
}); | ||
|
||
test('should expand deep scoped paths', () => { | ||
expect(getNamePatterns('@joshcena/awesome/web', 'plugin')).toEqual([ | ||
'@joshcena/awesome/web', | ||
'@joshcena/docusaurus-plugin-awesome/web', | ||
]); | ||
|
||
expect(getNamePatterns('@joshcena/awesome/web', 'theme')).toEqual([ | ||
'@joshcena/awesome/web', | ||
'@joshcena/docusaurus-theme-awesome/web', | ||
]); | ||
}); | ||
}); | ||
|
||
describe('resolveModuleName', () => { | ||
test('should resolve longhand', () => { | ||
expect( | ||
resolveModuleName('@docusaurus/plugin-content-docs', require, 'plugin'), | ||
).toBeDefined(); | ||
}); | ||
|
||
test('should resolve shorthand', () => { | ||
expect(resolveModuleName('content-docs', require, 'plugin')).toBeDefined(); | ||
}); | ||
|
||
test('should throw good error message for longhand', () => { | ||
expect(() => | ||
resolveModuleName('@docusaurus/plugin-content-doc', require, 'plugin'), | ||
).toThrowErrorMatchingInlineSnapshot(` | ||
"Docusaurus was unable to resolve the \\"@docusaurus/plugin-content-doc\\" plugin. Make sure one of the following packages are installed: | ||
- @docusaurus/plugin-content-doc | ||
- @docusaurus/docusaurus-plugin-plugin-content-doc" | ||
`); | ||
}); | ||
|
||
test('should throw good error message for shorthand', () => { | ||
expect(() => resolveModuleName('content-doc', require, 'plugin')) | ||
.toThrowErrorMatchingInlineSnapshot(` | ||
"Docusaurus was unable to resolve the \\"content-doc\\" plugin. Make sure one of the following packages are installed: | ||
- content-doc | ||
- @docusaurus/plugin-content-doc | ||
- docusaurus-plugin-content-doc" | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
export function getNamePatterns( | ||
moduleName: string, | ||
moduleType: 'preset' | 'theme' | 'plugin', | ||
): string[] { | ||
if (moduleName.startsWith('@')) { | ||
// Pure scope: `@scope` => `@scope/docusaurus-plugin` | ||
if (!moduleName.includes('/')) { | ||
return [`${moduleName}/docusaurus-${moduleType}`]; | ||
} | ||
const [scope, packageName] = moduleName.split(/\/(.*)/); | ||
return [ | ||
`${scope}/${packageName}`, | ||
`${scope}/docusaurus-${moduleType}-${packageName}`, | ||
]; | ||
} | ||
return [ | ||
moduleName, | ||
`@docusaurus/${moduleType}-${moduleName}`, | ||
`docusaurus-${moduleType}-${moduleName}`, | ||
]; | ||
} | ||
|
||
export function resolveModuleName( | ||
moduleName: string, | ||
moduleRequire: NodeRequire, | ||
moduleType: 'preset' | 'theme' | 'plugin', | ||
): string { | ||
const modulePatterns = getNamePatterns(moduleName, moduleType); | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const module of modulePatterns) { | ||
try { | ||
moduleRequire.resolve(module); | ||
return module; | ||
} catch (e) {} | ||
} | ||
throw new Error(`Docusaurus was unable to resolve the "${moduleName}" ${moduleType}. Make sure one of the following packages are installed: | ||
${modulePatterns.map((module) => `- ${module}`).join('\n')}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.