-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
v2: Allow custom extensions to be passed into the Webpack file loader #4445
Comments
My workaround for this issue was to do the following: import useBaseUrl from "@docusaurus/useBaseUrl";
export const InsomniaButton = () => {
const fileUrl = "/downloads/insomnia.yml";
const url = `${location.protocol}//${location.host}${useBaseUrl(fileUrl)}`;
const encodedUrl = encodeURIComponent(url);
// ... There might be some other Docusaurus hook that allows generating the full url without relying on location, but I wasn't able to find it... But this approach is just string juggling which means we don't get the safety of using the file loader and requires using |
That's not entirely true, you can add whatever you want to the webpack config with Something like this might work: module.exports = {
configureWebpack: function () {
return {
module: {
rules: [
{
test: /\.yaml$/,
use: [
{
loader: 'file-loader',
options: {name: 'assets/files/[name]-[hash].[ext]'},
},
],
},
],
},
};
},
}; Note we have multiple output folders for static assets currently. If we enabled to configure a list of extensions, do we want all these files to be in build/files? |
@slorber Thanks for that, I looked around and I couldn't find the Webpack escape hatch in the docs 😅. I think it's still worth while for something that is a bit more systematic for this, maybe I overestimate how often docs are generated with downloaded files? Otherwise I'm perfectly happy for this issue to be just closed, your answer is a good solution. |
Thanks for the feedback
This is the first time anyone request that, so I'll just close for now, and see if this is requested again in the future |
Hello! I tried to place
Where should I put the configureWebpack? |
@khusamov
If you want to modify module.exports = {
plugins: [
() => ({
configureWebpack() {
return {...};
},
}),
],
}; |
I needed to import a file from the filesystem into the documentation to be included raw, to do so I added the following Docusaurus plugin to the list of plugins in const webpackAssetSourcePlugin = () => ({
configureWebpack() {
return {
module: {
rules: [
{
resourceQuery: /raw/,
type: "asset/source",
},
],
},
};
},
}), The plugin uses Webpack 5's import Mermaid from '@theme/Mermaid';
import chart from '../path/to/file.mermaid?raw';
# Blah
Blah blah blah
<Mermaid chart={chart}/> Thanks for all the guidance above ❤️ |
@slorber (and anyone else interested!) - I would love something to be easier to configure here. Frankly as a relative n00b coming to webpack5 I barely understand what you guys say in terms of solutions here :) I come from Hugo where the idea of a Literally all I want to do is load content from .yml files in the site (its actually a collection of gihub workflows) and render various parts. Even a more laborious method (yaml-js + fs) isn't working because also webpack doesnt seem to like the fs package There are many I am sure that have a local datasource that is a bunch of YAML or JSON files and to be honest its a whole world of pain for noobs like me to get something working The only little success I have had is with something like:
But that is difficult to work with in a component way as I need to pass it in from props But to return to the issue - surely command data exchange formats such as YAML should be in the OOTB list? |
@erzz there's not a single way to import a yaml file:
We can't satisfy both users at the same time, and in any case you can configure this yourself with the I'm open to supporting Yaml OOTB better. But if we supported Yaml out of the box, but not in the way you though we should (because there's not a single use-case), it could even make it harder for you to revert our OOTB yaml support and add your own support, and vice versa. I hope you understand my concerns about doing so and that enabling you to bring your own support is the safest option for us, even if it's less convenient for a few users. |
Thanks @slorber - totally understand that it becomes complex in keeping everyone happy with OOTB So can we we instead have some n00b documentation for webpack configuration (and I get that its likely described very well @ webpack :) ) - but in the context of docusaurus and the default configuration (where all config is in |
@erzz I don't want to actually document any Webpack thing because it's not sure we'll stick with Webpack forever. If you want to use them, you should feel the pain and understand that you are using something that is clearly "unofficial", and may disappear later in any upcoming Docusaurus release. If we document it, we encourage you to use things that will go away some day. By not documenting it on purpose, we actually reduce the long-term overall pain of our whole user base. |
Haha! Fair enough! |
Is it possible to configure it for .pdf files? |
Afaik we already support file loader for PDF files. otherAssets: () => ({
use: [loaders.file({folder: 'files'})],
test: /\.(?:pdf|docx?|xlsx?|zip|rar)$/i,
}), If you think we don't, please provide a repro. |
The following is my problem, I just want to use docusaurus v3.1.1 and a pdf viewer. Log after
pdfviewer.js: import React, { useState } from 'react';
import { Document, Page, pdfjs } from 'react-pdf';
import 'react-pdf/dist/esm/Page/AnnotationLayer.css';
// Set worker src.
pdfjs.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
const PdfViewer = ({ file, width, fromPage, toPage }) => {
const [numPages, setNumPages] = useState(null);
function onDocumentLoadSuccess({ numPages: loadedNumPages }) {
setNumPages(loadedNumPages);
}
return (
<div>
<Document
file={file}
onLoadSuccess={onDocumentLoadSuccess}
>
{numPages && Array.from(
new Array(numPages),
(_, index) => index + 1
)
.filter(pageNumber => (!fromPage || pageNumber >= fromPage) && (!toPage || pageNumber <= toPage))
.map(pageNumber => (
<Page
key={`page_${pageNumber}`}
pageNumber={pageNumber}
width={width}
renderTextLayer={false}
/>
))}
</Document>
</div>
);
};
export default PdfViewer; myfile.mdx:
In my docusaurus.config.js plugins: [
[
// Custom plugin to modify Webpack config
function myCustomPlugin(context, options) {
return {
name: 'custom-webpack-plugin',
configureWebpack(config, isServer, utils, content) {
return {
module: {
rules: [
{
test: /\.pdf$/,
type: 'asset/resource',
},
],
},
};
},
};
},
], |
I only help if you create a runnable minimal repro that I can run in a sandbox. If it is a loader problem, then show me a repro that does not involve react-pdf and prove me that you can't require pdf files. |
I was able to make it work with the following config: plugins: [
[
// Custom plugin to modify Webpack config
function myCustomPlugin(context, options) {
return {
name: 'custom-webpack-plugin',
configureWebpack(config, isServer, utils, content) {
return {
module: {
rules: [
{
test: /\.pdf$/,
use: ["file-loader"],
},
{
test: /\.node$/,
use: ["node-loader"],
}
],
},
};
},
};
},
], |
ok, but I doubt any of this is actually necessary 🤷♂️ it should work without any extra plugin/config |
I want to be able to load So I added this in:
But this does some strange stuff, the But the result is always:
Why is there a |
Ok I realised I followed an incorrect guide on how to do this, so now I can create my own plugin loader for it. However what's the generic way of saying any given path being loaded should be preserved under |
Was the |
🚀 Feature
The ability to provide your own list of file extensions to be loaded via file loader or a specific directory that will always be loaded in via the file loader.
Have you read the Contributing Guidelines on issues?
Yes
Motivation
I currently have a YAML file that needs to be downloaded via the static file server, currently the only way to get download paths are by
import
orrequire
, but the file loader is limited topdf|doc|docx|xls|xlsx|zip|rar
(reference) currently with no way of adding to that list. This will cause the Webpack loader to panic with aYou may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
error.Handling files that are not in this very limited list are impossible to use with Docusaurus for generating download links, this limits the functionality greatly.
Example
Pitch
There should be a configuration option in
docusaurus.config.js
to allow setting a custom list (or appending to the existing list) of allowed file types to be used by the file loader. Alternatively a path (iestatic/downloads
) where all files get handled by the file loader, this path should be configurable.The text was updated successfully, but these errors were encountered: