mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-21 20:17:50 +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
|
@ -8,11 +8,14 @@
|
|||
const {parseQuery, getOptions} = require('loader-utils');
|
||||
import {loader} from 'webpack';
|
||||
import {truncate} from './blogUtils';
|
||||
import path from 'path';
|
||||
import {readFile} from 'fs-extra';
|
||||
import {aliasedSitePath, docuHash} from '@docusaurus/utils';
|
||||
|
||||
export = function(fileString: string) {
|
||||
const callback = this.async();
|
||||
|
||||
const {truncateMarker}: {truncateMarker: RegExp | string} = getOptions(this);
|
||||
const {truncateMarker, siteDir, dataDir} = getOptions(this);
|
||||
|
||||
let finalContent = fileString;
|
||||
|
||||
|
@ -21,5 +24,19 @@ export = function(fileString: string) {
|
|||
if (truncated) {
|
||||
finalContent = truncate(fileString, truncateMarker);
|
||||
}
|
||||
return callback && callback(null, finalContent);
|
||||
|
||||
// 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, finalContent + '\n' + metadataStr);
|
||||
});
|
||||
} as loader.Loader;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue