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:
Endi 2019-12-06 12:34:21 +07:00 committed by GitHub
parent 32c9d07b90
commit 7f8aca2ddc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 236 additions and 200 deletions

View file

@ -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;