0.0.5 • Published 9 years ago
path-rewrite-plugin v0.0.5
Path Rewrite Plugin
A webpack plugin for replace source url from expected pattern to emitted files url.
Install
npm i -D path-rewrite-plugin
Usage
This is a webpack compile plugin, work before emit
files to dist. So, you just
need to add the plugin to your webpack.config.js
as follow:
// in your webpack.config.js
import {PathRewritePlugin} from 'path-rewrite-plugin'
export default {
// ... other configurations
plugins: [
// ... other plugins
new PathRewritePlugin,
],
}
Options
You can input your some custom options to the plugin when call constructor
by new PathRewritePlugin(options)
.
The options as follow:
targetExt
, string[], the files that with any element of the array as suffix will be invoked to parse, default is[".html"]
, just parse html file.prefix
, stringsuffix
, string the replace pattern prefix and suffix, default is{{
and}}
, the substring in wrapped by the prefix and suffix will be treated as a regexp, and then if matched any assets of the current compilation, will be replaced by the assets url.
For example, The content in your html file as follow:
<script src="{{bundle\..\.js}}"></script>
will be replaced as:
<!-- the <hash> is your bundled asset's hash -->
<script src="/static/bundle.<hash>.js"></script>
if your webpack config set output
as follow:
{
"publicPath": "/static/",
"filename": "bundle.[hash].js",
"path": "./dist"
}
Warning
- This plugin just work on
assets
, which will be emit as file, so, you need to usefile-loader
to emit the file asassets
- The replacement will change the content
hash
, so, if your config set the.html
file name contains hash code, will to correct!