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

ibt-webpack-path-rewriter

v1.1.4

Published

Webpack plugin for replacing resources' paths with public ones

Downloads

4

Readme

This is a Webpack plugin that:

  • writes text resources to the file system;
  • in these resources, replaces selected paths with their public counterparts.

Plays nicely with webpack-dev-server; see includeHash option of the constructor.

* 此版本修改了原版中 publicPath 拼接部分,支持[hash]的引用,并不会同步更新版本,请使用原版

Example

index.jade:

doctype html
head
  title My awesome app
  meta( charset="utf-8" )
  link( href="[[ app-*.css ]]", media="all", rel="stylesheet" )
body
  p Hi everyone!
  img( src="[[ images/hi.png ]]" )
  script( src="[[ app-*.js ]]" )

webpack.config.js:

var ExtractTextPlugin = require('extract-text-webpack-plugin'),
    PathRewriterPlugin = require('webpack-path-rewriter')

module.exports = {
  entry: {
    app: './scripts/index'
  },
  output: {
    path: '_dist',
    filename: 'app-[chunkhash].js',
    publicPath: '/public/path/'
  },
  module: {
    loaders: [{
      test: /[/]images[/]/,
      loader: 'file?name=[path][name]-[hash].[ext]'
    }, {
      test: /[.]styl$/,
      loader: ExtractTextPlugin.extract('css?sourceMap!stylus?sourceMap')
    }, {
      test: /[.]jade$/,
      loader: PathRewriterPlugin.rewriteAndEmit({
        name: '[path][name].html',
        loader: 'jade-html?' + JSON.stringify({ pretty: true })
      })
    }]
  },
  plugins: [
    new ExtractTextPlugin('app-[contenthash].css', { allChunks: true }),
    new PathRewriterPlugin()
  ]
}

After the build, _dist/index.html will contain the following:

<!DOCTYPE html>
<head>
  <title>My awesome app</title>
  <meta charset="utf-8">
  <link href="/public/path/app-aeeae55eaf7373d1be14ccc3fa44272d.css" media="all" rel="stylesheet">
</head>
<body>
  <p>Hi everyone!</p>
  <img src="/public/path/images/hi-9bc044c418aba70701582981061b685f.png">
  <script src="/public/path/app-c5db2b3d825a8bccf99b.js"></script>
</body>

Usage

This plugin is content-agnostic, so it doesn't perform any parsing. You need to explicitly mark each path that needs to be rewritten.

By default, you do this by wrapping such paths inside the "[[original/path]]" construction, which after rewriting transforms to the "rewritten/path". You can control this behavior using options, both global and per-resource.

There are two types of assets.

1. Normal assets

Most of the assets already exist in the source tree before you run the build. To rewrite paths to such assets, just mark these paths with the special construction described above.

For example, if you have views/index.jade and images/hi.png, in the index.jade you can specify "[[../images/hi.png]]" and it will be replaced with whatever public path images/hi.png is assigned to, e.g. "/static/images/hi-somelonghash.png".

2. Generated assets

Some of the assets appear as a result of the build. These include JavaScript bundles (chunks) and files produced by extract-text-webpack-plugin. To rewrite path to an asset of this kind, you need to replace any variable parts of this path with asterisk * symbols.

For example, to rewrite path from views/index.jade to the JS bundle which gets placed to [hash]/scripts/app-[chunkhash].js, the path should be specified as "[[../*/scripts/app-*.js]]".

Customizing the path marker

Sometimes it may be inconvenient to use the default "[[...]]" marker. It can be customized using three options: pathRegExp, pathMatchIndex (index of capturing group containing extracted path) and pathReplacer (template of the replacement string).

For example, you can use the following options to rewrite all src and href HTML attributes that end with some extension (non-relative paths are automatically skipped):

{
  pathRegExp: /(src|href)\s*=\s*"(.*?\.[\w\d]{1,6})"/,
  pathMatchIndex: 2,
  pathReplacer: '[1]="[path]"'
}

API

PathRewriter.rewriteAndEmit(loader | opts)

Marks a resource for rewriting paths and emitting to the file system. Use it in conjunction with the new PathRewriter() in the plugins list.

Takes one argument, which is either string or object. If string, then it specifies the resource's loader string along with the options, e.g.

PathRewriter.rewriteAndEmit('?name=[path][name]-[hash].[ext]')
PathRewriter.rewriteAndEmit('jade-html?pretty')
PathRewriter.rewriteAndEmit('?name=[path][name].[ext]!jade-html?pretty')

Object form allows to pass the following options:

  • loader the resource's loader string.
  • loaders an array of loaders; mutually exclusive with the loader option.
  • name the path to the output file. Defaults to "[path][name].[ext]". May contain the following tokens:
    • [ext] the extension of the resource;
    • [name] the basename of the resource;
    • [path] the path of the resource relative to the context option;
    • [hash] the hash of the resource's content;
    • [<hashType>:hash:<digestType>:<length>] see loader-utils docs;
    • [N] content of N-th capturing group obtained from matching the resource's path against the nameRegExp option.
  • nameRegExp, context see name.
  • publicPath allows to override the global output.publicPath setting.
  • pathRegExp, pathMatchIndex, pathReplacer allow to override options for rewriting paths in this resource.

For example:

PathRewriter.rewriteAndEmit({
  name: '[path][name]-[hash].html',
  loader: 'jade-html?pretty'
})

new PathRewriter(opts | undefined)

A plugin that emits to the filesystem all resources that were marked with the PathRewriter.rewriteAndEmit() loader. Options:

  • silent don't print rewritten paths. Defaults to false.
  • emitStats write stats.json file. May be string specifying the file's name. Defaults to true.
  • pathRegExp regular expression for matching paths. Defaults to /"\[\[(.*?)\]\]"/, which tests for "[[...]]" constructions and captures the string between the braces.
  • pathMatchIndex the index of capturing group in the pathRegExp that corresponds to a path. Defaults to 1.
  • pathReplacer template for replacing matched path with the rewritten one. Defaults to "[path]". May contain following tokens:
    • [path] the rewritten path;
    • [N] the content of N-th capturing group of pathRegExp.
  • includeHash make compilation's hash dependent on contents of this resource. Useful with live reload, as it causes the app to reload each time this resources changes. Defaults to false.