mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 00:27:21 +02:00
feat(v2): implement ComponentCreator (#1366)
* v2(feat): convert blog to view-driven content queries * feat(v2): port blog to use ContentRenderer * misc(v2): fix test and change ContentRenderer url * avoid chunkName collision * avoid chunkname collision more * fix(v2): fix content-renderer ssr problem (#1367) * wip * avoid chunk names collision * ContentRenderer is a wrapper for Loadable * convert docs and pages * nits and rename * rename routeModules -> modules * remove lodash from component creator * resolve chunk not being picked up correctly * add comment for explanation * small refactoring
This commit is contained in:
parent
0ac2441d23
commit
96cb4672d5
14 changed files with 220 additions and 80 deletions
|
@ -33,7 +33,7 @@ describe('load utils', () => {
|
|||
|
||||
test('genComponentName', () => {
|
||||
const asserts = {
|
||||
'/': 'Index',
|
||||
'/': 'index',
|
||||
'/foo-bar': 'FooBar096',
|
||||
'/foo/bar': 'FooBar1Df',
|
||||
'/blog/2017/12/14/introducing-docusaurus':
|
||||
|
@ -51,7 +51,7 @@ describe('load utils', () => {
|
|||
test('docuHash', () => {
|
||||
const asserts = {
|
||||
'': '-d41',
|
||||
'/': 'Index',
|
||||
'/': 'index',
|
||||
'/foo-bar': 'foo-bar-096',
|
||||
'/foo/bar': 'foo-bar-1df',
|
||||
'/endi/lie': 'endi-lie-9fa',
|
||||
|
@ -81,7 +81,7 @@ describe('load utils', () => {
|
|||
});
|
||||
|
||||
test('genChunkName', () => {
|
||||
const asserts = {
|
||||
let asserts = {
|
||||
'/docs/adding-blog': 'docs-adding-blog-062',
|
||||
'/docs/versioning': 'docs-versioning-8a8',
|
||||
'/': 'index',
|
||||
|
@ -94,6 +94,20 @@ describe('load utils', () => {
|
|||
Object.keys(asserts).forEach(str => {
|
||||
expect(genChunkName(str)).toBe(asserts[str]);
|
||||
});
|
||||
|
||||
// Don't allow different chunk name for same path.
|
||||
expect(genChunkName('path/is/similar', 'oldPrefix')).toEqual(
|
||||
genChunkName('path/is/similar', 'newPrefix'),
|
||||
);
|
||||
|
||||
// Even with same preferred name, still different chunk name for different path
|
||||
asserts = {
|
||||
'/blog/1': 'blog-85-f-089',
|
||||
'/blog/2': 'blog-353-489',
|
||||
};
|
||||
Object.keys(asserts).forEach(str => {
|
||||
expect(genChunkName(str, undefined, 'blog')).toBe(asserts[str]);
|
||||
});
|
||||
});
|
||||
|
||||
test('idx', () => {
|
||||
|
|
|
@ -47,7 +47,7 @@ function encodePath(userpath) {
|
|||
*/
|
||||
function docuHash(str) {
|
||||
if (str === '/') {
|
||||
return 'Index';
|
||||
return 'index';
|
||||
}
|
||||
const shortHash = createHash('md5')
|
||||
.update(str)
|
||||
|
@ -63,7 +63,7 @@ function docuHash(str) {
|
|||
*/
|
||||
function genComponentName(pagePath) {
|
||||
if (pagePath === '/') {
|
||||
return 'Index';
|
||||
return 'index';
|
||||
}
|
||||
const pageHash = docuHash(pagePath);
|
||||
const pascalCase = _.flow(
|
||||
|
@ -88,9 +88,23 @@ function posixPath(str) {
|
|||
return str.replace(/\\/g, '/');
|
||||
}
|
||||
|
||||
function genChunkName(str, prefix) {
|
||||
const name = str === '/' ? 'index' : docuHash(str);
|
||||
return prefix ? `${prefix}---${name}` : name;
|
||||
const chunkNameCache = new Map();
|
||||
function genChunkName(modulePath, prefix, preferredName) {
|
||||
let chunkName = chunkNameCache.get(modulePath);
|
||||
if (!chunkName) {
|
||||
let str = modulePath;
|
||||
if (preferredName) {
|
||||
const shortHash = createHash('md5')
|
||||
.update(modulePath)
|
||||
.digest('hex')
|
||||
.substr(0, 3);
|
||||
str = `${preferredName}${shortHash}`;
|
||||
}
|
||||
const name = str === '/' ? 'index' : docuHash(str);
|
||||
chunkName = prefix ? `${prefix}---${name}` : name;
|
||||
chunkNameCache.set(modulePath, chunkName);
|
||||
}
|
||||
return chunkName;
|
||||
}
|
||||
|
||||
function idx(target, keyPaths) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue