mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 15:47:23 +02:00
perf(v2): smaller bundlesize by embedding metadata to content (#2088)
* wip embed metadata to content * embed metadata in blog as well * refactor * update test * yarn lock * avoid overwriting file everytime we run new nodejs process * nits
This commit is contained in:
parent
32c9d07b90
commit
7f8aca2ddc
16 changed files with 236 additions and 200 deletions
|
@ -5,25 +5,45 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import {readFile} from 'fs-extra';
|
||||
import {getOptions} from 'loader-utils';
|
||||
import {loader} from 'webpack';
|
||||
import linkify from './linkify';
|
||||
import {docuHash, aliasedSitePath} from '@docusaurus/utils';
|
||||
|
||||
export = function(fileString: string) {
|
||||
const callback = this.async();
|
||||
const {docsDir, siteDir, versionedDir, sourceToPermalink} = getOptions(this);
|
||||
return (
|
||||
callback &&
|
||||
callback(
|
||||
null,
|
||||
linkify(
|
||||
fileString,
|
||||
this.resourcePath,
|
||||
docsDir,
|
||||
siteDir,
|
||||
sourceToPermalink,
|
||||
versionedDir,
|
||||
),
|
||||
)
|
||||
const {
|
||||
dataDir,
|
||||
docsDir,
|
||||
siteDir,
|
||||
versionedDir,
|
||||
sourceToPermalink,
|
||||
} = getOptions(this);
|
||||
|
||||
// Replace all markdown linking to correct url
|
||||
const linkifiedStr = linkify(
|
||||
fileString,
|
||||
this.resourcePath,
|
||||
docsDir,
|
||||
siteDir,
|
||||
sourceToPermalink,
|
||||
versionedDir,
|
||||
);
|
||||
|
||||
// Read metadata & then embed it to this markdown content
|
||||
// Note that metadataPath must be the same/ in-sync as the path from createData
|
||||
const aliasedSource = aliasedSitePath(this.resourcePath, siteDir);
|
||||
const metadataPath = path.join(dataDir, `${docuHash(aliasedSource)}.json`);
|
||||
|
||||
// Add metadataPath as dependency of this loader result so that we can recompile if metadata is changed
|
||||
this.addDependency(metadataPath);
|
||||
|
||||
readFile(metadataPath, 'utf8', function(err, metadata) {
|
||||
if (err) return callback && callback(err);
|
||||
|
||||
const metadataStr = `export const metadata = ${metadata}`;
|
||||
callback && callback(null, linkifiedStr + '\n' + metadataStr);
|
||||
});
|
||||
} as loader.Loader;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue