Hey all,
I am trying to use the react-pdf
package in order to display some PDF files in my frontity project. I have tried service the PDF files both as a URL from my WP media URL (working fine in a browser) and as a static file in the code.
When importing the file import pdfFile from "./sample-pdf.pdf";
and passing it to the component, webpack does not build and I get this error:
Error: ./packages/client-name/src/components/flipbook/sample-pdf.pdf 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
@ ./packages/client-name/src/components/flipbook/pdfViewer.js 3:0-38 4:259-266
@ ./packages/client-name/src/components/index.js
@ ./packages/client-name/src/index.js
@ ./build/bundling/entry-points/client-name/client.ts
@ multi webpack-hot-middleware/client ./build/bundling/entry-points/client-name/client.ts
And when using the URL from WP Media, I get the following error:
ModuleParseError: Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
(Source code omitted for this binary file)
at handleParseError (/home/samplep182/SP/client-name/dev/client-name-front-end-dev-frontity/node_modules/webpack/lib/NormalModule.js:469:19)
at /home/samplep182/SP/client-name/dev/client-name-front-end-dev-frontity/node_modules/webpack/lib/NormalModule.js:503:5
at /home/samplep182/SP/client-name/dev/client-name-front-end-dev-frontity/node_modules/webpack/lib/NormalModule.js:358:12
at /home/samplep182/SP/client-name/dev/client-name-front-end-dev-frontity/node_modules/loader-runner/lib/LoaderRunner.js:373:3
at iterateNormalLoaders (/home/samplep182/SP/client-name/dev/client-name-front-end-dev-frontity/node_modules/loader-runner/lib/LoaderRunner.js:214:10)
at Array.<anonymous> (/home/samplep182/SP/client-name/dev/client-name-front-end-dev-frontity/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
at Storage.finished (/home/samplep182/SP/client-name/dev/client-name-front-end-dev-frontity/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16)
at /home/samplep182/SP/client-name/dev/client-name-front-end-dev-frontity/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9
at /home/samplep182/SP/client-name/dev/client-name-front-end-dev-frontity/node_modules/graceful-fs/graceful-fs.js:123:16
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:63:3)
This lead me to dive into how I can customize webpack in Frontity to enable the file loader, where I came accross this thread: https://github.com/frontity/frontity/pull/812
Here’s what I tried to do so far:
- I updated frontity and its dependencies to the latest versions.
- Tried to create the
frontity.config.js
file in my project’s root folder and added the following:
export const webpack = ({ config, mode, target }) ={
// Add support for a new file type.
config.modules.rules.push({
test: /\.(otf|pdf)$/,
loader: "file-loader",
});
};
I also tried out a few different variations of the above to no avail.
And if it is of any use, here’s the code where I’m using react-pdf (which might not even by correct, I am using @frontity/react-easy-state
here, which is the first time I use it and the only place it is used in this project.
Now I might be doing something extremely wrong, or missing a step for the frontity.config.js file to be working, but I couldn’t find a way around this. Any help is greately appreciated!
P.S. I created this project well before the custom webpack config was made, but updated afterwards. The project was creased using npx frontity create project-name command.