Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var getFieldAsFn = require('./get-field-as-fn'),
|
---|
| 4 | CustomError = require('./get-error');
|
---|
| 5 |
|
---|
| 6 | /**
|
---|
| 7 | * Create an encoder for output sources using the given codec hash
|
---|
| 8 | * @throws Error Where the given codec is missing an encode function
|
---|
| 9 | * @this {object} A loader or compilation
|
---|
| 10 | * @param {{encode:function}} codec A single codec with an `encode` function
|
---|
| 11 | * @returns {function(string):string|Error|false} An encode function that takes an absolute path
|
---|
| 12 | */
|
---|
| 13 | function encodeSourcesWith(codec) {
|
---|
| 14 | /* jshint validthis:true */
|
---|
| 15 | var context = this,
|
---|
| 16 | encoder = getFieldAsFn('encode')(codec);
|
---|
| 17 | if (!encoder) {
|
---|
| 18 | return new CustomError('Specified format does not support encoding (it lacks an "encoder" function)');
|
---|
| 19 | }
|
---|
| 20 | else {
|
---|
| 21 | return function encode(absoluteSource) {
|
---|
| 22 |
|
---|
| 23 | // call the encoder
|
---|
| 24 | var encoded;
|
---|
| 25 | try {
|
---|
| 26 | encoded = absoluteSource && encoder.call(context, absoluteSource);
|
---|
| 27 | }
|
---|
| 28 | catch (exception) {
|
---|
| 29 | return getNamedError(exception);
|
---|
| 30 | }
|
---|
| 31 | return encoded;
|
---|
| 32 |
|
---|
| 33 | function getNamedError(details) {
|
---|
| 34 | var name = codec.name || '(unnamed)',
|
---|
| 35 | message = [
|
---|
| 36 | 'Encoding with codec: ' + name,
|
---|
| 37 | 'Absolute source: ' + absoluteSource,
|
---|
| 38 | details && (details.stack ? details.stack : details)
|
---|
| 39 | ]
|
---|
| 40 | .filter(Boolean)
|
---|
| 41 | .join('\n');
|
---|
| 42 | return new Error(message);
|
---|
| 43 | }
|
---|
| 44 | };
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | module.exports = encodeSourcesWith;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.