【问题标题】:webpack set the outputPath relative to the file where require occurswebpack 设置相对于 require 发生的文件的 outputPath
【发布时间】:2020-10-04 00:35:06
【问题描述】:

有没有办法提示 webpack 根据需要生成相对于生成文件所在的文件的输出文件?假设我有以下配置:

let indexHTML = new HtmlWebpackPlugin({
    template: "./src/pug/index.pug",
    filename: 'index.html',
})

let iframeExample = new HtmlWebpackPlugin({
    template: "./src/pug/iframe-example.pug",
    filename: 'examples/index.html',
})

module.exports = merge(common, {
    mode: "development",
    output: {
        filename: "[name].bundle.js",
        path: path.join(__dirname, "/dist"),
    },
    entry: { 
        index: "./src/js/index.js",
    },
    plugins: [
        new CleanWebpackPlugin(), 
        indexHTML,
        iframeExample
    ],
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    { 
                        loader: 'file-loader',
                        options: {
                            name: "[name].[ext]",
                            outputPath: "css",
                            esModule: false,
                        } 
                    }
                ]
            },
            {
                test: /\.(svg|png|jpg|gif)$/,
                use: {
                    loader: "file-loader",
                    options: {
                        name: "[name].[ext]",
                        outputPath: "imgs",
                        esModule: false,
                    }
                }
            },
            {
                test: /\.(aac|mp3)$/,
                use: {
                    loader: "file-loader",
                    options: {
                        name: "[name].[ext]",
                        outputPath: "audio",
                        esModule: false
                    }
                }
            },
            {
                test: /\.(webm|mp4)$/,
                use: {
                    loader: "file-loader",
                    options: {
                        name: "[name].[ext]",
                        outputPath: "video",
                        esModule: false
                    }
                }
            },
        ]
    },
});

在这个配置中,HtmlWebpackPlugin 生成了两个html页面:一个是'dist/index.html'('dist/'是我的输出文件夹),另一个是'dist/examples/index.html'(即'examples/' 文件夹中的'index.html',HtmlWebpackPlugin 将为我创建)。

当文件加载器在我生成的 html 文件中遇到 require/import 语句时,它会尝试将其解析为 url 并将该文件从我的工作文件夹结构复制到输出文件夹中。在 file-loader 的 outputPath 中,我为输出内容(“imgs”、“video”等)指定了相对路径,这些路径将作为 file-loader 生成的每个 url 的前缀,从而在每个相应的 html 中生成相对 url .

问题是,文件夹本身将在输出文件夹(在我的情况下为“dist”)中生成,而不是在输出文件夹结构中出现相应 html 的位置。也就是说,在 'dist/examples/index.html' 中引用的所有 .css 文件都将被放入 'dist/css/' 文件夹中,因此不会在 'dist/examples/index.html' 中看到,它只包含相对路径('css/')。

重申我的问题:如何让文件加载器生成相对于引用位置的输出文件?也就是说,我希望文件加载器将 'dist/examples/index.html' 中引用的任何 .css 文件输出到 '/dist/examples/css/'。

【问题讨论】:

    标签: javascript webpack webpack-file-loader pug-loader


    【解决方案1】:

    你可以从 webpack 的输出对象中使用 publicPath,

    module.exports = merge(common, {
        mode: "development",
        output: {
            filename: "[name].bundle.js",
            path: path.join(__dirname, "/dist"),
            publicPath: 'https://test.org/example/'
        },
    ..
    ..
    

    上述更改将允许两个 index.html 文件在生产中具有相同的相对路径。

    index.html

    <script type="text/javascript" src="https://test.org/example/test.js"></script>
    

    示例/index.html

    <script type="text/javascript" src="https://test.org/example/test.js"></script>
    

    更多关于publicPath请参考答案https://stackoverflow.com/a/38748466/3679048

    【讨论】:

    • 我确信它不会改变文件加载器处理这些文件的方式。它们仍然会直接发送到输出目录,而不考虑 html 的位置。我不想更改相对链接,我想告诉文件加载器将引用的文件(.css、图像、视频文件等)发送到相对于 html 的某个目录中。
    • 嗯,在我看来你可以使用CopyWebpackPlugin 吗?
    • 我想,我可以。但这将迫使我更改当前的源文件夹结构,为输出文件夹中的每个相应文件夹以及每种类型的内容创建一堆新文件夹,以便 CopyWebpack 可以找到文件,然后为每个文件添加 CopyWebpack 模式他们。显然它不需要特殊的函数语法来确定模式中的'from'和'to',也不支持除了**和*之外的glob的匹配模式,所以我只能使用字符串文字,导致样板配置代码过多,如果我必须在输出中有复杂的文件夹结构。
    猜你喜欢
    • 2017-12-08
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 2020-03-15
    • 2021-10-14
    • 2015-03-22
    相关资源
    最近更新 更多