This is a Webpack plugin that:
- writes text resources to the file system;
- in these resources, replaces selected paths with their public counterparts.
doctype htmlheadtitle My awesome appmeta( charset="utf-8" )link( href="[[ app-*.css ]]", media="all", rel="stylesheet" )bodyp Hi everyone!img( src="[[ images/hi.png ]]" )script( src="[[ app-*.js ]]" )
var ExtractTextPlugin =PathRewriterPlugin =moduleexports =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: ExtractTextPlugintest: /[.]jade$/loader: PathRewriterPluginplugins:'app-[contenthash].css' allChunks: true
After the build,
_dist/index.html will contain the following:
My awesome appHi everyone!
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
images/hi.png, in the index.jade you can specify
"[[../images/hi.png]]" and it will be replaced with whatever public path
assigned to, e.g.
2. Generated assets
and files produced by
To rewrite path to an asset of this kind, you need to replace any variable parts of this path
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
Customizing the path marker
Sometimes it may be inconvenient to use the default
"[[...]]" marker. It can be customized using
pathMatchIndex (index of capturing group containing extracted path)
pathReplacer (template of the replacement string).
For example, you can use the following options to rewrite all
href HTML attributes that
end with some extension (non-relative paths are automatically skipped):
pathRegExp: /\s*=\s*""/pathMatchIndex: 2pathReplacer: '="[path]"'
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.
Object form allows to pass the following options:
loaderthe resource's loader string.
loadersan array of loaders; mutually exclusive with the
namethe 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
[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
publicPathallows to override the global
pathRegExp, pathMatchIndex, pathReplacerallow to override options for rewriting paths in this resource.
new PathRewriter(opts | undefined)
A plugin that emits to the filesystem all resources that were marked with the
PathRewriter.rewriteAndEmit() loader. Options:
silentdon't print rewritten paths. Defaults to
stats.jsonfile. May be string specifying the file's name. Defaults to
pathRegExpregular expression for matching paths. Defaults to
/"\[\[(.*?)\]\]"/, which tests for
"[[...]]"constructions and captures the string between the braces.
pathMatchIndexthe index of capturing group in the
pathRegExpthat corresponds to a path. Defaults to
pathReplacertemplate 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
includeHashmake 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