npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

extract-loader-path-correction

v1.0.3

Published

webpack loader to extract HTML and CSS from the bundle

Downloads

11

Readme

extract-loader-path-correction

extract-loader的一个修改,主要是校正引用文件路径不正确的问题

关于extract-loader的使用方式和api等请查看原项目extract-loader


为什么要进行修改?

首先看下项目结构

现在项目的目录结构如下

  app
   | index.tpl.html
   | vender
        | css
           | index.css
           | image
               | test.png

index.tpl.html 中的内容如下

  <html>
    <head>
        <link rel="stylesheet" href="./vender/index.css">
    </head>
    <body>   
    </body>
  </html>

index.css 中的内容如下

  html{
    background: url(./image/test.png);
  }

index.js 是空文件

webpack.config.js 中的内容如下

var path = require('path');
var HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports={
    entry:{
       bundle:path.join(__dirname,"app/index.js")
    },
    output:{
        path:path.join(__dirname,"dist"),
        filename:"[name].js"
    },
    module:{
        rules:[{
            test:/\.png$/,
            use: [{
                loader:'file-loader',
                options: {
                    name: '[path][name].[ext]'
                }
            }],
            include:[path.join(__dirname, 'app')]  
        },{
            test:/\.html$/,
            use: [{
                loader:'html-loader',
                options: {
                    attrs: ['img:src', 'link:href','script:src','audio:src'],
                }
            }],
            include:[path.join(__dirname, 'app')]  
        },{
            test: /\.css$/,
            use: [
                {
                    loader: "file-loader",
                    options: {
                        name: "[path][name].[ext]"
                    }
                },
                {
                    loader: "extract-loader"
                },
                {
                    loader: 'css-loader'
                }
            ],
            include:[path.join(__dirname, '/app')]
        }]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template:path.join(__dirname,"app/index.tpl.html")
        })
    ]
 }

输出到 dist 中目录结构如下

app
  | index.css
  | vender
      | image
          | test.png
bundle.js
index.html

index.html 中的内容如下

<html>
    <head>
        <link rel="stylesheet" href="app/vender/index.css">
    </head>
    <body>   
    <script type="text/javascript" src="bundle.js"></script></body>
</html>

index.css 中的内容如下

html{
    background: url(app/vender/image/test.png);
}

上面向目标文件夹(dist)输出的内容中index.css引用图片test.png的路径 app/vender/image/test.png 是有问题的,这时候我们希望test.png的引用路径应该是./image/test.png,这才是正确的.

因此对extract-loader的进行了修改.

将 webpack.config.js 中的 loader: "extract-loader" 改为 loader: "extract-loader-path-correction"

{
  loader: "extract-loader-path-correction"
},

再次进行打包后项目结构没有发生变化,但index.css中的内容变成了这样

html{
    background: url(./image/test.png);
}

这正是我们所想要的!


该修改仅仅是使得引用路径进行修正,对extract-loader的其他功能以及配置等没有任何影响.

上面的解释中只是使用了图片作为示例,实际上可以修正引入的任何资源的路径,比如字体文件,svg文件等等等等...

当然想要正确引入各种资源文件还需要使用各种loader才行,其他的疑惑和使用方式请前往extract-loader查看