Skip to content

Commit 4dac73a

Browse files
committed
refactor join API again, additional how-to examples
1 parent 21be305 commit 4dac73a

File tree

12 files changed

+1263
-774
lines changed

12 files changed

+1263
-774
lines changed

.jshintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
"maxlen": 120,
2424
"scripturl": true,
2525
"node": true,
26-
"esversion": 6
26+
"esversion": 9
2727
}

packages/resolve-url-loader/docs/advanced-features.md

Lines changed: 297 additions & 123 deletions
Large diffs are not rendered by default.

packages/resolve-url-loader/index.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ var path = require('path'),
1212

1313
var adjustSourceMap = require('adjust-sourcemap-loader/lib/process');
1414

15-
var valueProcessor = require('./lib/value-processor');
16-
var joinFn = require('./lib/join-function');
17-
var logToTestHarness = require('./lib/log-to-test-harness');
15+
var valueProcessor = require('./lib/value-processor'),
16+
joinFn = require('./lib/join-function'),
17+
logToTestHarness = require('./lib/log-to-test-harness');
1818

1919
const DEPRECATED_OPTIONS = {
2020
engine: [
@@ -74,7 +74,6 @@ function resolveUrlLoader(content, sourceMap) {
7474
var rawOptions = loaderUtils.getOptions(loader),
7575
options = Object.assign(
7676
{
77-
fs : loader.fs,
7877
sourceMap: loader.sourceMap,
7978
engine : 'postcss',
8079
silent : false,
@@ -106,10 +105,24 @@ function resolveUrlLoader(content, sourceMap) {
106105
'loader misconfiguration',
107106
'"join" option must be a Function'
108107
);
109-
} else if (options.join.length !== 1) {
108+
} else if (options.join.length !== 2) {
110109
return handleAsError(
111110
'loader misconfiguration',
112-
'"join" Function must take exactly 1 arguments (options hash)'
111+
'"join" Function must take exactly 2 arguments (options, loader)'
112+
);
113+
}
114+
115+
// validate the result of calling the join option
116+
var joinProper = options.join(options, loader);
117+
if (typeof joinProper !== 'function') {
118+
return handleAsError(
119+
'loader misconfiguration',
120+
'"join" option must itself return a Function when it is called'
121+
);
122+
} else if (joinProper.length !== 1) {
123+
return handleAsError(
124+
'loader misconfiguration',
125+
'"join" Function must create a function that takes exactly 1 arguments (item)'
113126
);
114127
}
115128

@@ -198,10 +211,14 @@ function resolveUrlLoader(content, sourceMap) {
198211
Promise
199212
.resolve(engine(loader.resourcePath, content, {
200213
outputSourceMap : !!options.sourceMap,
201-
transformDeclaration: valueProcessor(loader.resourcePath, options),
202214
absSourceMap : absSourceMap,
203215
sourceMapConsumer : sourceMapConsumer,
204-
removeCR : options.removeCR
216+
removeCR : options.removeCR,
217+
transformDeclaration: valueProcessor({
218+
join : joinProper,
219+
root : options.root,
220+
directory: path.dirname(loader.resourcePath)
221+
})
205222
}))
206223
.catch(onFailure)
207224
.then(onSuccess);

packages/resolve-url-loader/lib/engine/rework.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ function requireOptionalPeerDependency(moduleName) {
146146
}
147147
catch (error) {
148148
if (error.message === 'Cannot find module \'' + moduleName + '\'') {
149-
throw new Error('To use the "rework" engine you must install the optionalPeerDependencies');
149+
throw new Error('to use the "rework" engine you must install the optionalPeerDependencies');
150150
}
151151
else {
152152
throw error;

packages/resolve-url-loader/lib/join-function/debug.js

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,56 @@
44
*/
55
'use strict';
66

7-
var path = require('path');
7+
const path = require('path');
88

9-
var PACKAGE_NAME = require('../../package.json').name;
9+
const PACKAGE_NAME = require('../../package.json').name;
10+
11+
/**
12+
* Paths are formatted to have posix style path separators and those within the CWD are made relative to CWD.
13+
*
14+
* @param {string} absolutePath An absolute path to format
15+
* @returns {string} the formatted path
16+
*/
17+
const pathToString = (absolutePath) => {
18+
if (absolutePath === '') {
19+
return '-empty-';
20+
} else {
21+
const relative = path.relative(process.cwd(), absolutePath).split(path.sep);
22+
const segments =
23+
(relative[0] !== '..') ? ['.'].concat(relative).filter(Boolean) :
24+
(relative.lastIndexOf('..') < 2) ? relative :
25+
absolutePath.split(path.sep);
26+
return segments.join('/');
27+
}
28+
};
29+
30+
exports.pathToString = pathToString;
1031

1132
/**
1233
* Format a debug message.
1334
*
14-
* @param {string} file The file being processed by webpack
35+
* @param {string} filename The file being processed by webpack
1536
* @param {string} uri A uri path, relative or absolute
16-
* @param {Array<string>} bases Absolute base paths up to and including the found one
17-
* @param {boolean} isFound Indicates the last base was a positive match
37+
* @param {Array<{base:string,joined:string,isSuccess:boolean}>} attempts An array of attempts, possibly empty
1838
* @return {string} Formatted message
1939
*/
20-
function formatJoinMessage(file, uri, bases, isFound) {
21-
return [PACKAGE_NAME + ': ' + pathToString(file) + ': ' + uri]
22-
.concat(bases.map(pathToString))
23-
.concat(isFound ? 'FOUND' : 'NOT FOUND')
24-
.join('\n ');
40+
const formatJoinMessage = (filename, uri, attempts) => {
41+
const attemptToCells = (_, i, array) => {
42+
const { base: prev } = (i === 0) ? {} : array[i-1];
43+
const { base: curr, joined } = array[i];
44+
return [(curr === prev) ? '' : pathToString(curr), pathToString(joined)];
45+
};
2546

26-
/**
27-
* If given path is within `process.cwd()` then show relative posix path, otherwise show absolute posix path.
28-
*
29-
* @param {string} absolute An absolute path
30-
* @return {string} A relative or absolute path
31-
*/
32-
function pathToString(absolute) {
33-
if (!absolute) {
34-
return '-empty-';
35-
} else {
36-
var relative = path.relative(process.cwd(), absolute)
37-
.split(path.sep);
47+
const formatCells = (lines) => {
48+
const maxWidth = lines.reduce((max, [cellA]) => Math.max(max, cellA.length), 0);
49+
return lines.map(([cellA, cellB]) => [cellA.padEnd(maxWidth), cellB]).map((cells) => cells.join(' --> '));
50+
};
3851

39-
return ((relative[0] === '..') ? absolute.split(path.sep) : ['.'].concat(relative).filter(Boolean))
40-
.join('/');
41-
}
42-
}
43-
}
52+
return [PACKAGE_NAME + ': ' + pathToString(filename) + ': ' + uri]
53+
.concat(attempts.length === 0 ? '-empty-' : formatCells(attempts.map(attemptToCells)))
54+
.concat(attempts.some(({ isSuccess }) => isSuccess) ? 'FOUND' : 'NOT FOUND')
55+
.join('\n ');
56+
};
4457

4558
exports.formatJoinMessage = formatJoinMessage;
4659

@@ -56,20 +69,18 @@ exports.formatJoinMessage = formatJoinMessage;
5669
* @param {function|boolean} debug A boolean or debug function
5770
* @return {function(function, array):void} A logging function possibly degenerate
5871
*/
59-
function createDebugLogger(debug) {
60-
var log = !!debug && ((typeof debug === 'function') ? debug : console.log),
61-
cache = {};
62-
return log ? actuallyLog : noop;
63-
64-
function noop() {}
65-
66-
function actuallyLog(msgFn, params) {
67-
var key = Function.prototype.toString.call(msgFn) + JSON.stringify(params);
68-
if (!cache[key]) {
69-
cache[key] = true;
70-
log(msgFn.apply(null, params));
71-
}
72-
}
73-
}
72+
const createDebugLogger = (debug) => {
73+
const log = !!debug && ((typeof debug === 'function') ? debug : console.log);
74+
const cache = {};
75+
return log ?
76+
((msgFn, params) => {
77+
const key = Function.prototype.toString.call(msgFn) + JSON.stringify(params);
78+
if (!cache[key]) {
79+
cache[key] = true;
80+
log(msgFn.apply(null, params));
81+
}
82+
}) :
83+
(() => undefined);
84+
};
7485

7586
exports.createDebugLogger = createDebugLogger;

0 commit comments

Comments
 (0)