Skip to content

Commit

Permalink
Add ability to inline other assets; update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
skozin committed Nov 15, 2015
1 parent 55e61ea commit 02ffc32
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
41 changes: 36 additions & 5 deletions es6/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const PATH_REGEXP = /"\[\[(.*?)\]\]"/g,
PATH_MATCH_INDEX = 1,
PATH_REPLACER = '"[path]"'

const INLINE_REGEXP = /\[\[\s*INLINE\(([^)]*)\)\s*\]\]/g

const ABS_PATH_REGEXP = /^[/]|^\w+:[/][/]/,
HASH_REGEXP_SRC = '[\\w\\d_-]+[=]*'

Expand Down Expand Up @@ -80,6 +82,7 @@ class PathRewriter
}

var query = extend({}, rwOpts, (key) => key != 'loader' && key != 'loaders')

if (query.pathRegExp) {
let re = query.pathRegExp; query.pathRegExp = re instanceof RegExp ? {
source: re.source,
Expand All @@ -90,6 +93,16 @@ class PathRewriter
}
}

if (query.inlineRegExp) {
let re = query.inlineRegExp; query.inlineRegExp = re instanceof RegExp ? {
source: re.source,
flags: (re.ignoreCase ? 'i' : '') + (re.multiline ? 'm' : '')
} : {
source: '' + re,
flags: ''
}
}

// we need to temporarily replace all exclamation marks because they have
// special meaning in loader strings v
//
Expand Down Expand Up @@ -138,11 +151,12 @@ class PathRewriter
pathReplacer: undefined,
pathMatchIndex: undefined
}, opts)
this.pathRegExp = PathRewriter.makePathRegExp(this.opts.pathRegExp) || PATH_REGEXP
this.pathRegExp = PathRewriter.makeRegExp(this.opts.pathRegExp) || PATH_REGEXP
this.pathMatchIndex = this.opts.pathMatchIndex == undefined
? PATH_MATCH_INDEX
: this.opts.pathMatchIndex
this.pathReplacer = this.opts.pathReplacer || PATH_REPLACER
this.inlineRegExp = PathRewriter.makeRegExp(this.opts.inlineRegExp) || INLINE_REGEXP
this.rwPathsCache = {}
this.modules = []
this.modulesByRequest = {}
Expand Down Expand Up @@ -181,12 +195,13 @@ class PathRewriter
request: this.request,
context: this.context,
relPath: path.relative(topLevelContext, this.resourcePath),
pathRegExp: PathRewriter.makePathRegExp(query.pathRegExp) || rewriter.pathRegExp,
pathRegExp: PathRewriter.makeRegExp(query.pathRegExp) || rewriter.pathRegExp,
pathReplacer: query.pathReplacer || rewriter.pathReplacer,
pathMatchIndex: +(query.pathMatchIndex == undefined
? rewriter.pathMatchIndex
: query.pathMatchIndex
)
),
inlineRegExp: PathRewriter.makeRegExp(query.inlineRegExp) || rewriter.inlineRegExp
}

var exportStatement = 'module.exports = "' + publicPath + url + (rewriter.opts.includeHash
Expand All @@ -206,7 +221,7 @@ class PathRewriter
}


static makePathRegExp(desc)
static makeRegExp(desc)
{
if (desc == undefined) {
return undefined
Expand Down Expand Up @@ -401,6 +416,18 @@ class PathRewriter
compiler.errors.push(e)
return srcPath
}
}).replace(moduleData.inlineRegExp, (match, assetUrl) => {
let asset = compiler.assets[ assetUrl ]
if (!asset) {
compiler.errors.push(new Error(
`Cannot inline asset "${ assetUrl }" in ${ moduleData.relPath }: not found`
))
return match
}
else if (!this.opts.silent) {
console.log(`PathRewriter[ ${ moduleData.relPath } ]: inlined "${ assetUrl }"`)
}
return asset.source()
})
compiler.assets[ moduleData.url ] = {
source: () => content,
Expand Down Expand Up @@ -511,10 +538,14 @@ function PathRewriterError(msg, moduleData)
PathRewriterError.prototype = Object.create(Error.prototype)


export default function PathRewriterEntry(arg)
function PathRewriterEntry(arg)
{
return this instanceof PathRewriterEntry
? new PathRewriter(arg) // called with new => plugin
: PathRewriter.loader.call(this, arg) // called as a funtion => loader
}
PathRewriterEntry.rewriteAndEmit = PathRewriter.rewriteAndEmit


module.exports = PathRewriterEntry
export default PathRewriterEntry
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "webpack-path-rewriter",
"version": "1.1.2",
"version": "1.1.3",
"description": "Webpack plugin for replacing resources' paths with public ones",
"author": "Sam Kozin <[email protected]>",
"scripts": {
"prepublish": "babel ./es6 --out-dir .",
"watch": "babel --watch ./es6 --out-dir .",
"prepublish": "babel ./es6 --presets es2015 --out-dir .",
"watch": "babel --watch ./es6 --presets es2015 --out-dir .",
"test": "echo 'Sorry, no tests yet' && exit 1"
},
"repository": {
Expand All @@ -25,10 +25,11 @@
},
"homepage": "https://github.com/skozin/webpack-path-rewriter",
"dependencies": {
"loader-utils": "^0.2.6"
"loader-utils": "^0.2.11"
},
"devDependencies": {
"babel": "^4.7.9"
"babel-cli": "^6.1.18",
"babel-preset-es2015": "^6.1.18"
},
"files": [
"index.js",
Expand Down

0 comments on commit 02ffc32

Please sign in to comment.