Skip to content

Add matchResource feature (for loaders)#7462

Merged
sokra merged 1 commit into
masterfrom
feature/match-resource
Jun 4, 2018
Merged

Add matchResource feature (for loaders)#7462
sokra merged 1 commit into
masterfrom
feature/match-resource

Conversation

@sokra

@sokra sokra commented Jun 1, 2018

Copy link
Copy Markdown
Member

Match rules with custom resource name
Also use this name as rule.issuer or splitChunks test
Show nicely in stats

cc @xtuc (holyc-loader)
cc @yyx990803 (vue-loader)
This is interesting for you.

What kind of change does this PR introduce?
feature

Did you add tests for your changes?
yes

Does this PR introduce a breaking change?
no

What needs to be documented once your changes are merged?
For loader authors:

A new inline request syntax is available. Prefixing <match-resource>!=! to a request will set the matchResource for this request. (A relative matchResource will resolve relative to the current context of the containing module.)
When a matchResource is set, it will be used to match with the module.rules instead of the original resource. This can be useful if further loaders should be applied to the resource, or if the module type need to be changed. It's also displayed in the stats and used for matching Rule.issuer and test in splitChunks.

Example: A loader extracts styles from comments in javascript files.

// file.js
/* STYLE: body { background: red; } */
console.log("yep");

A loader could transform the file into the following file and use the matchResource to apply the user-specified css processing rules:

// file.js transformed by loader
import "./file.js.css!=!extract-style-loader/getStyles!./file.js"
console.log("yep");

This will add a dependency to extract-style-loader/getStyles!./file.js and threat the result as file.js.css. Because module.rules has a rule matching /\.css$/ it will apply to this dependency.

// extract-style-loader
const stringifyRequest = require("loader-utils").stringifyRequest;
const getRemainingRequest = require("loader-utils").getRemainingRequest;
const getStylesLoader = require.resolve("./getStyle");
module.exports = function(source) {
  if(STYLES_REGEXP.test(source)) {
    source = source.replace(STYLES_REGEXP, "");
    const remReq = getRemainingRequest(this);
    return `import ${stringifyRequest(`${this.resource}.css!=!${getStylesLoader}!${remReq}`};${source}`;
  }
  return source;
}
// extract-style-loader/getStyles.js
module.exports = function(source) {
  const match = STYLES_REGEXP.match(source);
  return match[0];
}

@webpack-bot

Copy link
Copy Markdown
Contributor

For maintainers only:

  • This need to be documented (issue in webpack/webpack.js.org will be filed when merged)

Match rules with custom resource name
Also use this name as rule.issuer or splitChunks test
Show nicely in stats
@sokra sokra force-pushed the feature/match-resource branch from 44b59d7 to 530e1fb Compare June 1, 2018 13:38
@yyx990803

yyx990803 commented Jun 1, 2018

Copy link
Copy Markdown
Contributor

This is awesome. Will allow us to get rid of vue-loader's current strategy of cloning rules in a plugin.

@sokra sokra mentioned this pull request Jun 1, 2018
@webpack-bot

Copy link
Copy Markdown
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

@webpack-bot

Copy link
Copy Markdown
Contributor

I've created an issue to document this in webpack/webpack.js.org.

@sokra sokra deleted the feature/match-resource branch June 4, 2018 11:09
@yyx990803

Copy link
Copy Markdown
Contributor

Exciting! @sokra is this going to land in the next minor?

@xtuc

xtuc commented Jun 4, 2018

Copy link
Copy Markdown
Contributor

The way you can share memory in WASM looks interesting, I haven't really looked into yet! I'll test it during the week. Thanks for the ping @sokra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants