style: fix linting issues in webpack plugin

This commit is contained in:
Ben Heidemann 2021-04-04 02:17:01 +01:00 committed by David Humphrey
parent bfb50bf608
commit 3b9fafc53e
4 changed files with 132 additions and 132 deletions

View File

@ -8,81 +8,81 @@ const OPTIONS_PROCESSORS = require('./processors');
module.exports = class FilerWebpackPlugin { module.exports = class FilerWebpackPlugin {
constructor(options = {}) { constructor(options = {}) {
utils.validateOptions(options, OPTIONS_SCHEMA); utils.validateOptions(options, OPTIONS_SCHEMA);
this.options = utils.processOptions(options, OPTIONS_PROCESSORS); this.options = utils.processOptions(options, OPTIONS_PROCESSORS);
} }
apply(compiler) { apply(compiler) {
compiler.hooks.normalModuleFactory.tap( compiler.hooks.normalModuleFactory.tap(
PLUGIN_NAME, PLUGIN_NAME,
(factory) => { (factory) => {
factory.hooks.resolve.tap( factory.hooks.resolve.tap(
PLUGIN_NAME, PLUGIN_NAME,
(resolveData) => { (resolveData) => {
// Resolve fsProvider if required // Resolve fsProvider if required
if ( if (
resolveData.request === 'fsProvider' resolveData.request === 'fsProvider'
&& resolveData.context === this.options.shimsDir && resolveData.context === this.options.shimsDir
) { ) {
return this.resolveFsProvider(resolveData); return this.resolveFsProvider(resolveData);
} }
// Ignore filer files (these should resolve modules normally) // Ignore filer files (these should resolve modules normally)
if (resolveData.context.startsWith(this.options.filerDir)) return; if (resolveData.context.startsWith(this.options.filerDir)) return;
// Apply fs, path and buffer shims if required // Apply fs, path and buffer shims if required
switch (resolveData.request) { switch (resolveData.request) {
case 'fs': case 'fs':
if (!this.options.shimFs) return; if (!this.options.shimFs) return;
return this.applyFsShim(resolveData); return this.applyFsShim(resolveData);
case 'path': case 'path':
if (!this.options.shimPath) return; if (!this.options.shimPath) return;
return this.applyPathShim(resolveData); return this.applyPathShim(resolveData);
case 'buffer': case 'buffer':
if (!this.options.shimBuffer) return; if (!this.options.shimBuffer) return;
return this.applyBufferShim(resolveData); return this.applyBufferShim(resolveData);
default:
return;
}
}
);
},
)
}
resolveFsProvider(resolveData) {
switch (this.options.fsProvider) {
case 'default':
resolveData.request = path.join(this.options.fsProviderDir, 'default.js');
break;
case 'indexeddb':
resolveData.request = path.join(this.options.fsProviderDir, 'indexeddb.js');
break;
case 'memory':
resolveData.request = path.join(this.options.fsProviderDir, 'memory.js');
break;
case 'custom':
resolveData.request = path.join(this.options.fsProviderDir, 'custom.js');
break;
default: default:
throw new Error([ return;
`Invalid option for fsProvider.`, }
`fsProvider must be one of 'default', 'indexeddb', 'memory' or 'custom'.`, }
`If using a custom fsProvider, you must also provide the fsProviderDir option.` );
].join(' ')); },
} );
} }
applyFsShim(resolveData) { resolveFsProvider(resolveData) {
resolveData.request = path.join(this.options.shimsDir, 'fs.js'); switch (this.options.fsProvider) {
case 'default':
resolveData.request = path.join(this.options.fsProviderDir, 'default.js');
break;
case 'indexeddb':
resolveData.request = path.join(this.options.fsProviderDir, 'indexeddb.js');
break;
case 'memory':
resolveData.request = path.join(this.options.fsProviderDir, 'memory.js');
break;
case 'custom':
resolveData.request = path.join(this.options.fsProviderDir, 'custom.js');
break;
default:
throw new Error([
'Invalid option for fsProvider.',
'fsProvider must be one of \'default\', \'indexeddb\', \'memory\' or \'custom\'.',
'If using a custom fsProvider, you must also provide the fsProviderDir option.'
].join(' '));
} }
}
applyPathShim(resolveData) { applyFsShim(resolveData) {
resolveData.request = path.join(this.options.shimsDir, 'path.js'); resolveData.request = path.join(this.options.shimsDir, 'fs.js');
} }
applyBufferShim(resolveData) { applyPathShim(resolveData) {
resolveData.request = path.join(this.options.shimsDir, 'buffer.js'); resolveData.request = path.join(this.options.shimsDir, 'path.js');
} }
}
applyBufferShim(resolveData) {
resolveData.request = path.join(this.options.shimsDir, 'buffer.js');
}
};

View File

@ -2,32 +2,32 @@ const ROOT_DIR_TAG = '<rootDir>';
const CWD = process.cwd(); const CWD = process.cwd();
module.exports = { module.exports = {
filerDir: { filerDir: {
process: function(value) { process: function(value) {
if (!value) { if (!value) {
return path.join(CWD, 'node_modules', 'filer'); return path.join(CWD, 'node_modules', 'filer');
} }
return value.replace(ROOT_DIR_TAG, CWD); return value.replace(ROOT_DIR_TAG, CWD);
},
}, },
shimsDir: { },
process: function(value) { shimsDir: {
if (!value) { process: function(value) {
return path.join(CWD, 'node_modules', 'filer', 'shims'); if (!value) {
} return path.join(CWD, 'node_modules', 'filer', 'shims');
return value.replace(ROOT_DIR_TAG, CWD); }
} return value.replace(ROOT_DIR_TAG, CWD);
}, }
shimFs: { default: true }, },
shimPath: { default: true}, shimFs: { default: true },
shimBuffer: { default: true}, shimPath: { default: true},
fsProvider: { default: 'default'}, shimBuffer: { default: true},
fsProviderDir: { fsProvider: { default: 'default'},
process: function(value) { fsProviderDir: {
if (!value) { process: function(value) {
return path.join(CWD, 'node_modules', 'filer', 'shims', 'providers'); if (!value) {
} return path.join(CWD, 'node_modules', 'filer', 'shims', 'providers');
return value.replace(ROOT_DIR_TAG, CWD); }
}, return value.replace(ROOT_DIR_TAG, CWD);
}, },
},
}; };

View File

@ -1,26 +1,26 @@
module.exports = { module.exports = {
type: 'object', type: 'object',
properties: { properties: {
filerDir: { filerDir: {
type: 'string', type: 'string',
}, },
shimsDir: { shimsDir: {
type: 'string', type: 'string',
}, },
shimFs: { shimFs: {
type: 'boolean', type: 'boolean',
}, },
shimPath: { shimPath: {
type: 'boolean', type: 'boolean',
}, },
shimBuffer: { shimBuffer: {
type: 'boolean', type: 'boolean',
}, },
fsProvider: { fsProvider: {
type: 'string', type: 'string',
}, },
fsProviderDir: { fsProviderDir: {
type: 'string', type: 'string',
}, },
} }
}; };

View File

@ -1,26 +1,26 @@
var { validate } = require('schema-utils'); var { validate } = require('schema-utils');
function validateOptions(options, schema) { function validateOptions(options, schema) {
validate(schema, options); validate(schema, options);
} }
function processOptions(options, processors) { function processOptions(options, processors) {
const processedOptions = {}; const processedOptions = {};
for (const [property, processor] of Object.entries(processors)) { for (const [property, processor] of Object.entries(processors)) {
processedOptions[property] = options[property]; processedOptions[property] = options[property];
if (processedOptions[property] === undefined) { if (processedOptions[property] === undefined) {
processedOptions[property] = processor.default; processedOptions[property] = processor.default;
}
if (processor.process) {
processedOptions[property] = processor.process(processedOptions[property]);
}
} }
if (processor.process) {
processedOptions[property] = processor.process(processedOptions[property]);
}
}
return processedOptions; return processedOptions;
} }
module.exports = { module.exports = {
validateOptions, validateOptions,
processOptions, processOptions,
}; };