webpack-contrib / bundle-loader

Bundle Loader
MIT License
658 stars 59 forks source link

Add name and nameRegex parameters #7

Closed nickdima closed 10 years ago

nickdima commented 10 years ago

Either one of these parameters is used for naming the chunk file. The name parameter is a simple string while nameRegex is a string containing a regular expression for extracting the name from the path of the file being required, especially useful for dynamic requires.

sokra commented 10 years ago

I like to have a API similar to this:

[name] basename of this.resourcePath [ext] extension of this.resourcePath [path] dirname of this.resourcePath [N] match number N of query param regExp as RegExp

nickdima commented 10 years ago

Great suggestions! I'll see to it. One thing. Is there some shared code already that I can use to parse the placeholders in name?

sokra commented 10 years ago
    .replace(/\[ext\]/ig, function() {
        return ext;
    }).replace(/\[name\]/ig, function() {
        return basename;
    }).replace(/\[path\]/ig, function() {
        return directory;
    });
nickdima commented 10 years ago

One more thing, when you say [path] dirname of this.resourcePath you're referring to the parent directory, the full absolute path or a path relative to something else?

sokra commented 10 years ago

full absolute path...

nickdima commented 10 years ago

what would be the use case for full absolute path in the chunk name?

sokra commented 10 years ago

hmm... yes, maybe the same approach as for the file-loader is more useful. In this case you could move the replacement code into loader-utils.interpolateName(loaderContext, nameQuery) and use it from both, bundle-loader and file-loader.

nickdima commented 10 years ago

Yes, it makes sense having a function in loader-utils for other loaders to use it. Just to make sure we're on the same page, the plan would be this: extract the interpolating functionality together with the hash options, add the RegExp feature and put it all in a function on loader-utils.

sokra commented 10 years ago

yep...

sokra commented 10 years ago
interpolate(loaderContext, strToBeInterpolated, optionsObject)
optionsObject.content: content to be hashed... (from file-loader)
optionsObject.query: parsed query string. (for regExp, context, hash, digest, size)
jhnns commented 10 years ago

@nickdima I'm just curious: Why do you need to set the file's name explicitly?

nickdima commented 10 years ago

Well, it seems more cleaner for my use case. I'm creating a chunk for each page defined in my router and it will be named something like page-home-d527af.js It gives me the feeling that I have more control like this, in case I need to preload the most visited pages or maybe check at a glance which chunks weights more, etc.