Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

next.js - Custom webpack config for PDFs using next-compose-plugins

General recommendation for processing a PDF file in Webpack keeps pointing me back to: https://webpack.js.org/loaders/file-loader/

Webpack documentation doesn't really explain static generation, and Next's config documentation doesn't really explain file loading for PDFs.

I put my PDFs into Next's /public folder and when I run yarn build the file paths are generated as /public/file-name.pdf, which is what I asked for. However, all of those links are dead. I think this might have to do with the way Next bundles things under the hood but that isn't explained anywhere I can find.

I've cobbled the following together, anyone have a sense of where I'm going wrong?

// next.config.js
const withPlugins = require('next-compose-plugins')
const withImages = require('next-images')

const { 
  PHASE_DEVELOPMENT_SERVER, 
  PHASE_PRODUCTION_BUILD 
} = require('next/constants')

const nextConfig = {
  webpack: (config, options) => {
 
    config.module.rules.push({
      test: /.pdf$/,
      use: [
        {
          loader: 'file-loader',
          options: {
            name: '[path][name].[ext]',
            outputPath: '/public'
          }
        } 
      ]
    })
 
    return config;
  }
}
module.exports = withPlugins([
  [withImages, {
    [PHASE_DEVELOPMENT_SERVER]: 'http://localhost:3001',
    [PHASE_PRODUCTION_BUILD]: 'https://www.example.com'
  }]
], nextConfig)
question from:https://stackoverflow.com/questions/65936937/custom-webpack-config-for-pdfs-using-next-compose-plugins

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...