Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate webpack file-loader to asset modules #4708

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/docusaurus-init/templates/bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
"@mdx-js/react": "^1.6.21",
"@svgr/webpack": "^5.5.0",
"clsx": "^1.1.1",
"file-loader": "^6.2.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"url-loader": "^4.1.1"
"react-dom": "^17.0.1"
},
"browserslist": {
"production": [
Expand Down
4 changes: 1 addition & 3 deletions packages/docusaurus-init/templates/classic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
"@mdx-js/react": "^1.6.21",
"@svgr/webpack": "^5.5.0",
"clsx": "^1.1.1",
"file-loader": "^6.2.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"url-loader": "^4.1.1"
"react-dom": "^17.0.1"
},
"browserslist": {
"production": [
Expand Down
4 changes: 1 addition & 3 deletions packages/docusaurus-init/templates/facebook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
"@mdx-js/react": "^1.6.21",
"@svgr/webpack": "^5.5.0",
"clsx": "^1.1.1",
"file-loader": "^6.2.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"url-loader": "^4.1.1"
"react-dom": "^17.0.1"
},
"devDependencies": {
"@babel/eslint-parser": "^7.13.10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,15 @@ const {
toMessageRelativeFilePath,
} = require('@docusaurus/utils');

const {
loaders: {inlineMarkdownImageFileLoader},
} = getFileLoaderUtils();
const {assetQuery} = getFileLoaderUtils();

const createJSX = (node, pathUrl) => {
const jsxNode = node;
jsxNode.type = 'jsx';
jsxNode.value = `<img ${node.alt ? `alt={"${escapeHtml(node.alt)}"} ` : ''}${
node.url
? `src={require("${inlineMarkdownImageFileLoader}${escapePath(
pathUrl,
)}").default}`
? // see https://github.com/facebook/docusaurus/pull/4708#discussion_r624515715
`src={new URL("${escapePath(pathUrl)}?${assetQuery}", import.meta.url)}`
: ''
}${node.title ? ` title="${escapeHtml(node.title)}"` : ''} />`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ const escapeHtml = require('escape-html');
const {toValue} = require('../utils');
const {getFileLoaderUtils} = require('@docusaurus/core/lib/webpack/utils');

const {
loaders: {inlineMarkdownLinkFileLoader},
} = getFileLoaderUtils();
const {assetQuery} = getFileLoaderUtils();

async function ensureAssetFileExist(fileSystemAssetPath, sourceFilePath) {
const assetExists = await fs.pathExists(fileSystemAssetPath);
Expand All @@ -47,9 +45,9 @@ function toAssetRequireNode({node, filePath, requireAssetPath}) {
? relativeRequireAssetPath
: `./${relativeRequireAssetPath}`;

const href = `require('${inlineMarkdownLinkFileLoader}${escapePath(
const href = `new URL('${escapePath(
relativeRequireAssetPath,
)}').default`;
)}?${assetQuery}', import.meta.url).toString()`;
const children = (node.children || []).map((n) => toValue(n)).join('');
const title = node.title ? `title="${escapeHtml(node.title)}"` : '';

Expand Down
14 changes: 10 additions & 4 deletions packages/docusaurus-plugin-ideal-image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ export default function (
module: {
rules: [
{
test: /\.(png|jpe?g|gif)$/i,
test: /\.(png|jpe?g)$/i,
resourceQuery: {
not: [/asset/],
},
type: 'javascript/auto',
generator: {
emit: !isServer,
},
use: [
require.resolve('@docusaurus/lqip-loader'),
{
Expand All @@ -40,9 +47,8 @@ export default function (
emitFile: !isServer, // don't emit for server-side rendering
disable: !isProd,
adapter: require('@docusaurus/responsive-loader/sharp'),
name: isProd
? 'assets/ideal-img/[name].[hash:hex:7].[width].[ext]'
: 'assets/ideal-img/[name].[width].[ext]',
name:
'assets/ideal-img/[name]-[contenthash:8].[width].[ext]',
...options,
},
},
Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
applyConfigurePostCss,
applyConfigureWebpack,
compile,
getFileLoaderUtils,
} from '../webpack/utils';
import CleanWebpackPlugin from '../webpack/plugins/CleanWebpackPlugin';
import {loadI18n} from '../server/i18n';
Expand Down Expand Up @@ -197,6 +198,11 @@ async function buildLocale({
}
});

// Add the very high-priority rules triggered by using a resourceQuery like ?asset
const {prependAssetQueryRules} = getFileLoaderUtils();
clientConfig = prependAssetQueryRules(clientConfig);
serverConfig = prependAssetQueryRules(serverConfig);

// Make sure generated client-manifest is cleaned first so we don't reuse
// the one from previous builds.
if (await fs.pathExists(clientManifestPath)) {
Expand Down
5 changes: 5 additions & 0 deletions packages/docusaurus/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
applyConfigureWebpack,
applyConfigurePostCss,
getHttpsConfig,
getFileLoaderUtils,
} from '../webpack/utils';
import {getCLIOptionHost, getCLIOptionPort} from './commandUtils';
import {getTranslationsLocaleDirPath} from '../server/translations/translations';
Expand Down Expand Up @@ -157,6 +158,10 @@ export default async function start(
}
});

// Add the very high-priority rules triggered by using a resourceQuery like ?asset
const {prependAssetQueryRules} = getFileLoaderUtils();
config = prependAssetQueryRules(config);

// https://webpack.js.org/configuration/dev-server
const devServerConfig: WebpackDevServer.Configuration = {
...{
Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus/src/webpack/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export function createBaseConfig(
chunkFilename: isProd
? 'assets/js/[name].[contenthash:8].js'
: '[name].js',
assetModuleFilename: 'assets/[name]-[hash][ext]',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assetModuleFilename: 'assets/[name]-[hash][ext]',
assetModuleFilename: 'assets/[name]-[contenthash][ext]',

Usually contenthash gives better caching results.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, prefer to use contenthash

publicPath: baseUrl,
},
// Don't throw warning when asset created is over 250kb
Expand Down Expand Up @@ -191,7 +192,7 @@ export function createBaseConfig(
fileLoaderUtils.rules.fonts(),
fileLoaderUtils.rules.media(),
fileLoaderUtils.rules.svg(),
fileLoaderUtils.rules.otherAssets(),
fileLoaderUtils.rules.files(),
{
test: /\.(j|t)sx?$/,
exclude: excludeJS,
Expand Down
Loading