bholloway / resolve-url-loader

Webpack loader that resolves relative paths in url() statements based on the original source file
563 stars 71 forks source link

Refactor join function #149

Closed bholloway closed 4 years ago

bholloway commented 4 years ago

concept

The aim is to produce a factory which orders the possible base directories and a predicate which tests the base directories. The separation of concerns should make it easier to order or possible base directories without changing the predicate.

The factory is given the possible bases as hash. So it can arbitrarily set the precedence of subString, value, property, selector source-map locations and return Array<string>.

The factory should be lazy since it might traverse the file-system search in order to suggest base paths. In that case we would want to abort if the predicate matches. It can optionally return Iterator<string>.

The predicate is largely unchanged. However it now uses fs from Webpack. This should solve any lingering issues we webpack-dev-server (untested).

changes

examples

Considering the example given by @MightyPork in this comment.

We can now add a fallback theme directory with minimum code.

const compose = require('compose-function');
const {
  createJoinFunction,
  defaultJoinFactory,
  defaultJoinPredicate
} = require('resolve-url-loader');

const appendThemeDirectory = (list) =>
  list.concat(path.join(__dirname, 'resources', 'assets', 'sass', 'theme'));

const customJoin = createJoinFunction(
  'customJoin',
  compose(appendThemeDirectory, defaultJoinFactory),
  defaultJoinPredicate
);
bholloway commented 4 years ago
bholloway commented 4 years ago

Thanks @runfaj.

My gut feel is that it is better than what we have at v3 but probably needs some tweaks or changes before release.

I will write up some better docs for the whole package and I think this will draw out any major problems.