如何使 webpack public 路径与相对子文件夹一起使用

How to make webpack public path work with relative sub folder

我有以下 webpack 配置文件:

const path = require('path'),
    MiniCSSExtractPlugin = require('mini-css-extract-plugin'),
    CleanWebpackPlugin = require('clean-webpack-plugin'),
    postCssPresetEnv = require('postcss-preset-env'),
    postCssScss = require('postcss-scss'),
    glob = require('glob'),
    autoprefixer = require('autoprefixer');

module.exports = {
    entry: {
        'material': glob.sync("./src/js/**/*.js"),
        'material-light': './src/sass/material.scss'
    },
    output: {
        path: path.resolve(__dirname, '/'),
        filename: process.env.NODE_ENV === 'development' ? './dist/js/[name].js' : './docs/dist/js/[name].js',
        publicPath: 'docs/dist/'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: ['/node_modules/', './dist/', '/src/css', '/docs'],
                use: [
                    'babel-loader',
                    {
                        loader: 'eslint-loader',
                        options: {
                            fix: true
                        }
                    }
                ]
            },
            {
                test: /\.(sa|sc|c)ss$/,
                exclude: ['/node_modules', './dist', '/src/js', '/docs'],
                use: [
                    MiniCSSExtractPlugin.loader,
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true,
                            minimize: process.env.NODE_ENV === 'production',
                            publicPath: 'docs/dist/'
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            sourceMap: true,
                            syntax: postCssScss,
                            plugins: () => [
                                autoprefixer,
                                postCssPresetEnv({
                                    stage: 0
                                }),
                            ],
                        },
                    },
                    {
                        loader: 'sass-loader',
                        options: {
                            sourceMap: true
                        }
                    }
                ]
            }
        ]
    },
    devServer: {
        overlay: true,
        contentBase: path.join(__dirname, './docs/'),
        publicPath: '/dist/',
        watchContentBase: true,
        open: true,
    },
    devtool: process.env.NODE_ENV === 'development' ? 'cheap-module-eval-source-map' : 'source-map',
    plugins: [
        new MiniCSSExtractPlugin({
            filename: process.env.NODE_ENV === 'development' ? './dist/css/[name].css' : './docs/dist/css/[name].css',
        }),
        new CleanWebpackPlugin(['./dist/', './docs/dist']),
    ]
};

文件结构如下:

/
   /docs/
       All html files
   /src/
      /js/
          All js files
      /sass/
          All sass files
   webpack.config.js
   packages.json

我想做的是以下内容。对于开发,我正在使用 webpack 开发服务器来提供来自 /docs directroy 的内容。但是我希望文档中的 urls 是相对于 /docs url 的,这样我就可以使用 GitHub 页面。 /docs/index.html 中的含义我想将 material.js 文件引用为:./dist/jsmaterial-light.css 为:./dist/css。我想这样做是因为在运行生产时 js,css 文件被保存到 docs/dist/(css/js) 目录。

但是我找不到正确的参考。在上面的配置中,public 路径有 docs/dist/,但 docs/dist/js 也不起作用。该问题与 js 和 (s)css 文件有关。

所以我的问题是如何让 publicPath 同时适用于 webpack 开发服务器和 github 页面?

相关但无效:

配置存在多个问题。在 github 上问了同样的问题后,我得到了以下配置:

const
    autoprefixer = require('autoprefixer'),
    CleanWebpackPlugin = require('clean-webpack-plugin'),
    glob = require('glob'),
    MiniCSSExtractPlugin = require('mini-css-extract-plugin'),
    path = require('path'),
    postCssPresetEnv = require('postcss-preset-env'),
    postCssScss = require('postcss-scss');

const OUTPUT_DIR = path.resolve(__dirname, 'docs');

module.exports = {
    entry: {
        'material': glob.sync("./src/js/**/*.js"),
        'material-light': './src/sass/material.scss'
    },
    output: {
        path: OUTPUT_DIR,
        filename: 'dist/js/[name].js',
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: ['/node_modules/', './dist/', '/src/css', '/docs'],
                use: [
                    'babel-loader',
                    {
                        loader: 'eslint-loader',
                        options: {
                            fix: true
                        }
                    }
                ]
            },
            {
                test: /\.(sa|sc|c)ss$/,
                exclude: ['/node_modules', './dist', '/src/js', '/docs'],
                use: [
                    MiniCSSExtractPlugin.loader,
                    {
                        loader: 'css-loader',
                        options: {
                            sourceMap: true,
                            minimize: process.env.NODE_ENV === 'production',
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            sourceMap: true,
                            syntax: postCssScss,
                            plugins: () => [
                                autoprefixer,
                                postCssPresetEnv({
                                    stage: 0
                                }),
                            ],
                        },
                    },
                    {
                        loader: 'sass-loader',
                        options: {
                            sourceMap: true
                        }
                    }
                ]
            }
        ]
    },
    devServer: {
        overlay: true,
        contentBase: OUTPUT_DIR,
        watchContentBase: true,
    },
    devtool: process.env.NODE_ENV === 'development' ? 'cheap-module-eval-source-map' : 'source-map',
    resolve: {
        extensions: ['.js', '.css', '.styl', '.scss'],
        alias: {
            'js': path.resolve(__dirname, './src/js'),
            'css': path.resolve(__dirname, './src/css')
        }
    },
    plugins: [
        new MiniCSSExtractPlugin({
            filename: 'dist/css/[name].css',
        }),
        new CleanWebpackPlugin(['./dist/', './docs/dist']),
    ]
};