简体   繁体   English

字体网址路径错误,在 WebPack 构建之后包含 css 路径

[英]Font urls path is wrong and includes css path after WebPack Build

I can't figure out the right combo here to make this all happy.我无法在这里找到正确的组合来让这一切都开心。 I'm having MiniCssExtractPlugin create css or less files encountered.我让MiniCssExtractPlugin创建 css 或遇到更少的文件。

I am importing a less and a css file in src/client/ index.tsx :我在 src/client/ index.tsx中导入了一个 less 和一个 css 文件:

import './less/master.less';
import '../../ext/ink-3.1.10/css/ink.min.css';

webpack.config.js is then doing its magic with MiniCssExtractPlugin : webpack.config.js然后用MiniCssExtractPlugin它的魔力:

Note : the reasons I have publicPath : '' for the css loader is because when I had it as publicPath : '/lib/css' , it was appending that to the font url so by removing that the font-urls no longer had the issue at least after webpack created dist of the underlying problem I still see at runtime.注意:我有publicPath : '' for the css loader 的原因是因为当我将它作为publicPath : '/lib/css'时,它会将其附加到字体 url 中,因此通过删除 font-urls 不再具有至少在 webpack 创建我在运行时仍然看到的基础问题的 dist 之后问题。 Honestly I don't know if I even need to set publicPath here anyway because it defaults to "" anyway.老实说,我什至不知道是否需要在此处设置 publicPath,因为无论如何它默认为“”

Changing it then to publicPath : '' , stripped out the /lib/css part from /lib/css/lib/assets/fonts and I thought that fixed my issue but then during runtime it's still trying to hit fonts via /lib/css/lib/assets/fonts which baffles me然后将其更改为publicPath : '' ,从/lib/css/lib/assets/fonts中删除/lib/css部分,我认为这解决了我的问题,但在运行时它仍然试图通过/lib/css/lib/assets/fonts访问 fonts /lib/css/lib/assets/fonts让我感到困惑

const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const HtmlWebPackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');

const isProduction = process.env.NODE_ENV === 'production';

const html = () => {
 return new HtmlWebPackPlugin({
  template: path.resolve(__dirname, 'src/client', 'index.html'),
  filename: 'index.html',
  hash: true,
 });
};

const copyAllOtherDistFiles = () => {
 return new CopyPlugin({
  patterns: [
   { from: 'src/client/assets', to: 'lib/assets' },
   { from: 'package.json', to: './' },
   { from: 'ext/ink-3.1.10/js/ink-all.min.js', to: 'lib/js' },
   { from: 'ext/ink-3.1.10/js/autoload.min.js', to: 'lib/js' },
   { from: 'ext/js/jquery-2.2.3.min.js', to: 'lib/js' },
   { from: 'feed.xml', to: './' },
   {
    from: 'src/shared',
    to: './shared',
    globOptions: {
     ignore: ['**/*suppressed.json'],
    },
   },
  ],
 });
};

module.exports = {
 entry: './src/client/index.tsx',
 output: {
  filename: 'scripts/app.[hash].bundle.js',
  publicPath: '',
  path: path.resolve(__dirname, 'dist'),
 },
 resolve: {
  extensions: ['.ts', '.tsx', '.js'],
 },
 devtool: isProduction ? '' : 'eval-cheap-source-map',
 devServer: {
  writeToDisk: true,
  contentBase: path.resolve(__dirname, 'dist'),
  port: 8080,
 },
 optimization: {
  minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
  splitChunks: {
   cacheGroups: {
    styles: {
     name: 'styles',
     test: /\.css$/,
     chunks: 'all',
     enforce: true,
    },
   },
  },
 },
 module: {
  rules: [
   {
    test: /\.(js)$/,
    exclude: /node_modules/,
    use: {
     loader: 'babel-loader',
    },
   },
   {
    test: /\.(tsx|ts)?$/,
    use: 'ts-loader',
    exclude: /node_modules/,
   },
   {
    test: /\.html$/,
    use: [
     {
      loader: 'html-loader',
     },
    ],
   },
   {
    test: /\.less$/,
    use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'],
   },
   {
    test: /\.css$/,
    use: [
     {
      loader: MiniCssExtractPlugin.loader,
      options: {
       publicPath: '',
      },
     },
     'css-loader',
    ],
   },
   {
    test: /\.(woff|woff2|eot|ttf|otf)$/,
    loader: 'file-loader',
    options: {
     outputPath: 'lib/assets/fonts',
    },
   },
   {
    test: /\.(png|svg|jpg|gif)$/,
    use: ['url-loader'],
   },
  ],
 },
 plugins: [
  new CleanWebpackPlugin(),
  copyAllOtherDistFiles(),
  new MiniCssExtractPlugin({
   filename: isProduction ? 'lib/css/[name].[hash].css' : 'lib/css/main.css',
  }),
  html(),
 ],
};

api.js api.js

const compression = require('compression'),
 express = require('express'),
 historyApi = require('connect-history-api-fallback'),
 oneYear = 31536000;

module.exports = express()
 .use(compression())
 .on('error', (err: string) => {
  console.log(err);
 })
 .use(historyApi())
 .use(
  express.static('dist', {
   maxage: oneYear,
  })
 )
 .use((_req: any, res: any) => {
  res.send('Sorry, Page Not Found');
 });

So the issue comes up during runtime with ink.min.css because it's referencing some fonts that I also processed with file-loader as you can see above.所以问题出现在运行时ink.min.css因为它引用了一些 fonts 我也使用file-loader处理过,如您在上面看到的。 The problem is when I run the site in Dev mode via NODE_ENV=development webpack-dev-server --open I end up with a few requests for those fonts because it's appending /lib/css/lib/assets/fonts instead of just /lib/assets/ fonts during runtime:问题是当我通过NODE_ENV=development webpack-dev-server --open在开发模式下运行站点时,我最终收到了一些对 fonts 的请求,因为它附加了/lib/css/lib/assets/fonts而不仅仅是/lib/assets/ fonts 在运行时:

在此处输入图像描述

The weird thing about this though is when I go to my dist folder, when I look at styles.main.css (which apparently it renamed ink.min.css to this for some odd reason, the url to the fonts don't have that extra /lib/css so I don't understand why when my browser loads it's trying to reference it with that extra part still: The weird thing about this though is when I go to my dist folder, when I look at styles.main.css (which apparently it renamed ink.min.css to this for some odd reason, the url to the fonts don't have那个额外的/lib/css所以我不明白为什么当我的浏览器加载时它仍然试图用那个额外的部分来引用它:

在此处输入图像描述

Another thing that's kinda weird is I do see images loading but when I look at the source in the browser, I don't see my assets folder.另一件有点奇怪的事情是我确实看到了图像加载,但是当我在浏览器中查看源代码时,我没有看到我的资产文件夹。 I know dist definitely contains it, but it doesn't show it here:我知道dist肯定包含它,但它没有在这里显示: 在此处输入图像描述

I don't understand why this fixed it.我不明白为什么这解决了它。 This entire thing feels hacky to me.这整件事让我觉得很奇怪。

I changed it to publicPath: '../../'我将其更改为publicPath: '../../'

test: /\.css$/,
    use: [
     {
      loader: MiniCssExtractPlugin.loader,
      options: {
       publicPath: '../../',
      },
     },
     'css-loader',
    ],

I guess I'm loading my.css file and then I'm under the context of /lib/css already so my font urls have /lib/css I guess?我想我正在加载我的 .css 文件,然后我已经在 /lib/css 的上下文中,所以我的字体网址有 /lib/css 我猜? and I need to for some reason have MiniCssExtractPlugin have a public path at the root?由于某种原因,我需要让MiniCssExtractPlugin在根目录下有一个公共路径? I don't get it.我不明白。

Also now I see the assets folder, wt???现在我也看到了资产文件夹,wt???

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM