Skip to content

Commit

Permalink
fix(v2): fix Webpack persistent caching (evict on swizzle/alias/confi…
Browse files Browse the repository at this point in the history
…g change) (#5047)

* webpack upgrade

* refactor docusaurus-utils hash fns

* Fix webpack cache eviction problems on config/aliases/swizzle changes

* Move/Rename InitPlugin type

* fix TS typos

* Add tests for webpack aliases

* fix windows tests
  • Loading branch information
slorber authored Jun 24, 2021
1 parent 9916a0b commit 99270db
Show file tree
Hide file tree
Showing 38 changed files with 274 additions and 169 deletions.
2 changes: 1 addition & 1 deletion packages/docusaurus-mdx-loader/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"stringify-object": "^3.3.0",
"unist-util-visit": "^2.0.2",
"url-loader": "^4.1.1",
"webpack": "^5.37.0"
"webpack": "^5.40.0"
},
"devDependencies": {
"@docusaurus/types": "2.0.0-beta.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"reading-time": "^1.3.0",
"remark-admonitions": "^1.2.1",
"tslib": "^2.2.0",
"webpack": "^5.37.0"
"webpack": "^5.40.0"
},
"peerDependencies": {
"react": "^16.8.4 || ^17.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"shelljs": "^0.8.4",
"tslib": "^2.2.0",
"utility-types": "^3.10.0",
"webpack": "^5.37.0"
"webpack": "^5.40.0"
},
"peerDependencies": {
"react": "^16.8.4 || ^17.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-pages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"remark-admonitions": "^1.2.1",
"slash": "^3.0.0",
"tslib": "^2.1.0",
"webpack": "^5.28.0"
"webpack": "^5.40.0"
},
"peerDependencies": {
"react": "^16.8.4 || ^17.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-ideal-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"react-waypoint": "^10.1.0",
"sharp": "^0.28.2",
"tslib": "^2.1.0",
"webpack": "^5.28.0"
"webpack": "^5.40.0"
},
"peerDependencies": {
"react": "^16.8.4 || ^17.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-pwa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"babel-loader": "^8.2.2",
"clsx": "^1.1.1",
"core-js": "^2.6.5",
"terser-webpack-plugin": "^5.1.1",
"webpack": "^5.28.0",
"terser-webpack-plugin": "^5.1.3",
"webpack": "^5.40.0",
"webpack-merge": "^5.7.3",
"workbox-build": "^6.1.1",
"workbox-precaching": "^6.1.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"commander": "^5.1.0",
"joi": "^17.4.0",
"querystring": "0.2.0",
"webpack": "^5.37.0",
"webpack-merge": "^5.7.3"
"webpack": "^5.40.0",
"webpack-merge": "^5.8.0"
}
}
10 changes: 9 additions & 1 deletion packages/docusaurus-types/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ export interface Plugin<Content> {
}): ThemeConfig;
}

export type InitializedPlugin = Plugin<unknown> & {
readonly options: PluginOptions;
readonly version: DocusaurusPluginVersionInformation;
};

export type LoadedPlugin = InitializedPlugin & {readonly content: unknown};

export type PluginModule = {
<T, X>(context: LoadContext, options: T): Plugin<X>;
validateOptions?<T>(data: OptionValidationContext<T>): T;
Expand Down Expand Up @@ -337,7 +344,8 @@ export interface RouteConfig {
priority?: number;
}

export interface ThemeAlias {
// Aliases used for Webpack resolution (when using docusaurus swizzle)
export interface ThemeAliases {
[alias: string]: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,28 @@
* LICENSE file in the root directory of this source tree.
*/

import {docuHash} from '../docuHash';
import {simpleHash, docuHash} from '../hashUtils';

describe('hashUtils', () => {
test('simpleHash', () => {
const asserts: Record<string, string> = {
'': 'd41',
'/foo-bar': '096',
'/foo/bar': '1df',
'/endi/lie': '9fa',
'/endi-lie': 'fd3',
'/yangshun/tay': '48d',
'/yangshun-tay': 'f3b',
'/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar':
'd46',
'/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/test1-test2':
'787',
};
Object.keys(asserts).forEach((str) => {
expect(simpleHash(str, 3)).toBe(asserts[str]);
});
});
});

describe('docuHash', () => {
test('docuHash works', () => {
Expand Down
21 changes: 1 addition & 20 deletions packages/docusaurus-utils/src/__tests__/pathUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,9 @@
* LICENSE file in the root directory of this source tree.
*/

import {simpleHash, isNameTooLong, shortName} from '../pathUtils';
import {isNameTooLong, shortName} from '../pathUtils';

describe('pathUtils', () => {
test('simpleHash', () => {
const asserts: Record<string, string> = {
'': 'd41',
'/foo-bar': '096',
'/foo/bar': '1df',
'/endi/lie': '9fa',
'/endi-lie': 'fd3',
'/yangshun/tay': '48d',
'/yangshun-tay': 'f3b',
'/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar':
'd46',
'/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/foo/bar/test1-test2':
'787',
};
Object.keys(asserts).forEach((file) => {
expect(simpleHash(file, 3)).toBe(asserts[file]);
});
});

test('isNameTooLong', () => {
const asserts: Record<string, boolean> = {
'': false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
* LICENSE file in the root directory of this source tree.
*/

import {createHash} from 'crypto';
import {kebabCase} from 'lodash';
import {shortName, isNameTooLong} from './pathUtils';

import {shortName, isNameTooLong, simpleHash} from './pathUtils';
export function md5Hash(str: string): string {
return createHash('md5').update(str).digest('hex');
}

export function simpleHash(str: string, length: number): string {
return md5Hash(str).substr(0, length);
}

// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files
/**
* Given an input string, convert to kebab-case and append a hash.
* Avoid str collision.
Expand Down
6 changes: 2 additions & 4 deletions packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import {
import resolvePathnameUnsafe from 'resolve-pathname';

import {posixPath as posixPathImport} from './posixPath';
import {simpleHash} from './pathUtils';
import {docuHash} from './docuHash';
import {simpleHash, docuHash} from './hashUtils';

export const posixPath = posixPathImport;

Expand All @@ -32,8 +31,7 @@ export * from './codeTranslationsUtils';
export * from './markdownParser';
export * from './markdownLinks';
export * from './escapePath';
export * from './docuHash';
export {simpleHash} from './pathUtils';
export {md5Hash, simpleHash, docuHash} from './hashUtils';

const fileHash = new Map();
export async function generate(
Expand Down
6 changes: 0 additions & 6 deletions packages/docusaurus-utils/src/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

// Based on https://github.com/gatsbyjs/gatsby/pull/21518/files

import {createHash} from 'crypto';

// MacOS (APFS) and Windows (NTFS) filename length limit = 255 chars, Others = 255 bytes
const MAX_PATH_SEGMENT_CHARS = 255;
const MAX_PATH_SEGMENT_BYTES = 255;
Expand Down Expand Up @@ -42,7 +40,3 @@ export const shortName = (str: string): string => {
)
.toString();
};

export function simpleHash(str: string, length: number): string {
return createHash('md5').update(str).digest('hex').substr(0, length);
}
12 changes: 6 additions & 6 deletions packages/docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@
"chokidar": "^3.5.1",
"clean-css": "^5.1.2",
"commander": "^5.1.0",
"copy-webpack-plugin": "^8.1.1",
"copy-webpack-plugin": "^9.0.0",
"core-js": "^3.9.1",
"css-loader": "^5.1.1",
"css-minimizer-webpack-plugin": "^2.0.0",
"css-minimizer-webpack-plugin": "^3.0.1",
"cssnano": "^5.0.4",
"del": "^6.0.0",
"detect-port": "^1.3.0",
Expand All @@ -79,7 +79,7 @@
"globby": "^11.0.2",
"html-minifier-terser": "^5.1.1",
"html-tags": "^3.1.0",
"html-webpack-plugin": "^5.3.1",
"html-webpack-plugin": "^5.3.2",
"import-fresh": "^3.3.0",
"is-root": "^2.1.0",
"leven": "^3.1.0",
Expand All @@ -105,15 +105,15 @@
"shelljs": "^0.8.4",
"std-env": "^2.2.1",
"strip-ansi": "^6.0.0",
"terser-webpack-plugin": "^5.1.2",
"terser-webpack-plugin": "^5.1.3",
"tslib": "^2.2.0",
"update-notifier": "^5.1.0",
"url-loader": "^4.1.1",
"wait-on": "^5.3.0",
"webpack": "^5.37.0",
"webpack": "^5.40.0",
"webpack-bundle-analyzer": "^4.4.2",
"webpack-dev-server": "^3.11.2",
"webpack-merge": "^5.7.3",
"webpack-merge": "^5.8.0",
"webpackbar": "^5.0.0-3"
},
"peerDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/docusaurus/src/commands/writeTranslations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {ConfigOptions} from '@docusaurus/types';
import {ConfigOptions, InitializedPlugin} from '@docusaurus/types';
import {loadContext, loadPluginConfigs} from '../server';
import initPlugins, {InitPlugin} from '../server/plugins/init';
import initPlugins from '../server/plugins/init';

import {
writePluginTranslations,
Expand All @@ -25,7 +25,7 @@ async function writePluginTranslationFiles({
options,
}: {
siteDir: string;
plugin: InitPlugin;
plugin: InitializedPlugin;
locale: string;
options: WriteTranslationsOptions;
}) {
Expand Down
8 changes: 6 additions & 2 deletions packages/docusaurus/src/server/html-tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
*/

import htmlTagObjectToString from './htmlTags';
import {InjectedHtmlTags, HtmlTagObject, HtmlTags} from '@docusaurus/types';
import {LoadedPlugin} from '../plugins';
import {
InjectedHtmlTags,
HtmlTagObject,
HtmlTags,
LoadedPlugin,
} from '@docusaurus/types';

function toString(val: string | HtmlTagObject): string {
return typeof val === 'string' ? val : htmlTagObjectToString(val);
Expand Down
15 changes: 0 additions & 15 deletions packages/docusaurus/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ import {
DEFAULT_BUILD_DIR_NAME,
DEFAULT_CONFIG_FILE_NAME,
GENERATED_FILES_DIR_NAME,
THEME_PATH,
} from '../constants';
import loadClientModules from './client-modules';
import loadConfig from './config';
import {loadPlugins} from './plugins';
import loadPresets from './presets';
import loadRoutes from './routes';
import loadThemeAlias from './themes';
import {
DocusaurusConfig,
DocusaurusSiteMetadata,
Expand Down Expand Up @@ -178,14 +176,6 @@ export async function load(
`export default ${JSON.stringify(siteConfig, null, 2)};`,
);

// Themes.
const fallbackTheme = path.resolve(__dirname, '../client/theme-fallback');
const pluginThemes: string[] = plugins
.map((plugin) => plugin.getThemePath && plugin.getThemePath())
.filter((x): x is string => Boolean(x));
const userTheme = path.resolve(siteDir, THEME_PATH);
const alias = loadThemeAlias([fallbackTheme, ...pluginThemes], [userTheme]);

// Make a fake plugin to:
// - Resolve aliased theme components
// - Inject scripts/stylesheets
Expand All @@ -202,11 +192,6 @@ export async function load(
getClientModules() {
return siteConfigClientModules;
},
configureWebpack: () => ({
resolve: {
alias,
},
}),
injectHtmlTags: () => {
const stylesheetsTags = stylesheets.map((source) =>
typeof source === 'string'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

import {InitializedPlugin} from '@docusaurus/types';
import {ensureUniquePluginInstanceIds} from '../pluginIds';

import {InitPlugin} from '../init';

function createTestPlugin(name: string, id?: string): InitPlugin {
function createTestPlugin(name: string, id?: string): InitializedPlugin {
// @ts-expect-error: good enough for tests
return {
name,
Expand Down
8 changes: 4 additions & 4 deletions packages/docusaurus/src/server/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import {
AllContent,
TranslationFiles,
ThemeConfig,
LoadedPlugin,
InitializedPlugin,
} from '@docusaurus/types';
import initPlugins, {InitPlugin} from './init';
import initPlugins from './init';
import chalk from 'chalk';
import {DEFAULT_PLUGIN_ID} from '../../constants';
import {chain} from 'lodash';
Expand Down Expand Up @@ -53,8 +55,6 @@ export function sortConfig(routeConfigs: RouteConfig[]): void {
});
}

export type LoadedPlugin = InitPlugin & {content: unknown};

export async function loadPlugins({
pluginConfigs,
context,
Expand All @@ -68,7 +68,7 @@ export async function loadPlugins({
themeConfigTranslated: ThemeConfig;
}> {
// 1. Plugin Lifecycle - Initialization/Constructor.
const plugins: InitPlugin[] = initPlugins({
const plugins: InitializedPlugin[] = initPlugins({
pluginConfigs,
context,
});
Expand Down
Loading

0 comments on commit 99270db

Please sign in to comment.