webpack打包img和css时如何保留子文件夹?

发布于 2022-09-05 03:03:34 字数 888 浏览 14 评论 0

1.使用url-loader和extract-text-webpack-plugin这俩插件处理css和img时最后打包后如何保留子目录结构呢?而不是全部在css/,images/下?

2.比如我有这样的结构

clipboard.png

我目前打包是所有css/都通过这个extract-text-webpack-plugin的设置,(下图)

clipboard.png

打包在css/下了,之前的common文件夹没有了,我还想实现打包后保留common子文件夹目录,该如何设置呢?还有img图片也是想保留子目录怎么设置?

clipboard.png
图片时用url-loader处理的。

3.我还有个疑问webpack如何抽取公共的css呢?比如我在a.js require('base.css'),同样在b.js也 require('base.css'),这样这个base.css就会被extract-text-webpack-plugin打包到两个入口的css中了?怎么设置可以抽取公共的css出来呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幼儿园老大 2022-09-12 03:03:34

You can accomplish both of these thing but it varies per plugin/loader.

ExtractTextWebpackPlugin

For extract-text-webpack-plugin, the filename property can actually be a function. It passes getPath to process the format like css/[name].css and returns the real file name, css/js/a.css. You can replace css/js with css then you will get the new path css/a.css.

plugins: [
  new ExtractTextPlugin({
    filename:  (getPath) => {
      return getPath('css/[name].css').replace('css/js', 'css');
    },
    allChunks: true
  })
] 

File Loader

For file-loader you can specify custom output and public paths by using the outputPath, publicPath and useRelativePath query name parameters (and also could be just option properties too):

use: "file-loader?name=[name].[ext]&publicPath=assets/foo/&outputPath=app/images/"

Shared CSS

When you use webpack.optimize.CommonsChunkPlugin, it should behave the same way with CSS as it does with JavaScript. You can find more information about its usage at our new webpack docs page (there is also a Chinese version click top right corner)

Hopefully this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文