-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
feat: shorthands for themes/plugins/presets configuration #5930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f2ee7af
513b9fa
bf9d2af
adb095e
d3fa972
e24b980
2bc2947
83c49c3
6a8b6bf
aafc148
96080e8
75e9aa9
6a73cb4
6cf84fb
b8cf8e3
c664a1f
01788bf
424f9d8
64d976a
3f28a10
d7787d3
0c37a2a
c6170f1
eeff8e3
c59b20e
5781c02
0b6fb24
ddd3385
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
| `); | ||
| }); | ||
| }); | ||
| 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok for that. Throwing exceptions has a runtime cost, but it's probably not very important here. |
||
| 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')}`); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,7 @@ const fs = require('fs'); | |
| /** @type {import('@docusaurus/types').PluginConfig[]} */ | ||
| const dogfoodingPluginInstances = [ | ||
| [ | ||
| '@docusaurus/plugin-content-docs', | ||
| 'content-docs', // dogfood shorthand | ||
| /** @type {import('@docusaurus/plugin-content-docs').Options} */ | ||
| ({ | ||
| id: 'docs-tests', | ||
|
|
@@ -24,7 +24,7 @@ const dogfoodingPluginInstances = [ | |
| ], | ||
|
|
||
| [ | ||
| '@docusaurus/plugin-content-blog', | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. going to dogfood other forms of plugin names here, it's the purpose of dogfooding to have a variety of settings ;) |
||
| '@docusaurus/plugin-content-blog', // dogfood longhand | ||
| /** @type {import('@docusaurus/plugin-content-blog').Options} */ | ||
| ({ | ||
| id: 'blog-tests', | ||
|
|
@@ -46,7 +46,7 @@ const dogfoodingPluginInstances = [ | |
| ], | ||
|
|
||
| [ | ||
| '@docusaurus/plugin-content-pages', | ||
| require.resolve('@docusaurus/plugin-content-pages'), // dogfood longhand resolve | ||
| /** @type {import('@docusaurus/plugin-content-pages').Options} */ | ||
| ({ | ||
| id: 'pages-tests', | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: added this test, but this resolve name looks a bit weird.
I'll still merge, but if you feel we should do something about it 🤷♂️ feel free to open another PR