From 027d5f0b18e8695f9fc8b29ea1ad53f8cd0c7cf9 Mon Sep 17 00:00:00 2001 From: Sebastian Schweizer Date: Fri, 25 Jan 2019 23:00:31 +0100 Subject: [PATCH] refactoring: extract library access --- src/PluginState.js | 32 ++------------------------------ src/library.js | 34 ++++++++++++++++++++++++++++++++++ src/loader.js | 5 +++-- src/plugin.js | 7 ++++--- 4 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 src/library.js diff --git a/src/PluginState.js b/src/PluginState.js index c1b4434..a7d96de 100644 --- a/src/PluginState.js +++ b/src/PluginState.js @@ -1,6 +1,6 @@ const loaderUtils = require('loader-utils'), - polyfillLibrary = require('polyfill-library'); + {getPolyfillsSource} = require('./library.js'); class PluginState { constructor(compilation, options) { @@ -26,7 +26,7 @@ class PluginState { return this._filenames[index]; } const newIndex = this._requestedPolyfillSets.push(encoded) - 1; - return this._filenames[newIndex] = this.getPolyfillsSource(options.polyfills, options.excludes, false).then( + return this._filenames[newIndex] = getPolyfillsSource(options.polyfills, options.excludes, false).then( content => loaderUtils.interpolateName( {resourcePath: `./polyfills${newIndex === 0 ? '' : '-' + newIndex}.js`}, formatFilename(options.filename, this.defaultHashLength), @@ -44,34 +44,6 @@ class PluginState { ) ); } - - getPolyfillsSource(polyfills, excludes, requiresAll) { - const flags = new Set(['always', 'gated']); - const features = {}; - polyfills.forEach((polyfill) => { - features[polyfill] = {flags}; - }); - if ('Promise.prototype.finally' in features && 'Promise' in features && requiresAll) { - delete features['Promise.prototype.finally']; - } - return polyfillLibrary.getPolyfillString({ - minify: false, - unknown: 'polyfill', - features, - excludes, - }); - } - - async getPolyfillDetector(polyfill) { - const meta = await polyfillLibrary.describePolyfill(polyfill); - if (meta) { - if (meta.detectSource) { - return meta.detectSource; - } - throw new Error(`[webpack-polyfill-injector] The polyfill ${polyfill} does not have a detector! Consider sending a PR with a suitable detect.js file to polyfill-library.`); - } - throw new Error(`[webpack-polyfill-injector] The polyfill ${polyfill} does not exist!`); - } } function formatFilename(filename, defaultHashLength) { diff --git a/src/library.js b/src/library.js new file mode 100644 index 0000000..6b197e2 --- /dev/null +++ b/src/library.js @@ -0,0 +1,34 @@ +const polyfillLibrary = require('polyfill-library'); + +async function getPolyfillDetector(polyfill) { + const meta = await polyfillLibrary.describePolyfill(polyfill); + if (meta) { + if (meta.detectSource) { + return meta.detectSource; + } + throw new Error(`[webpack-polyfill-injector] The polyfill ${polyfill} does not have a detector! Consider sending a PR with a suitable detect.js file to polyfill-library.`); + } + throw new Error(`[webpack-polyfill-injector] The polyfill ${polyfill} does not exist!`); +} + +function getPolyfillsSource(polyfills, excludes, requiresAll) { + const flags = new Set(['always', 'gated']); + const features = {}; + polyfills.forEach((polyfill) => { + features[polyfill] = {flags}; + }); + if ('Promise.prototype.finally' in features && 'Promise' in features && requiresAll) { + delete features['Promise.prototype.finally']; + } + return polyfillLibrary.getPolyfillString({ + minify: false, + unknown: 'polyfill', + features, + excludes, + }); +} + +module.exports = { + getPolyfillDetector, + getPolyfillsSource, +}; diff --git a/src/loader.js b/src/loader.js index 9a6144b..d7eeae4 100644 --- a/src/loader.js +++ b/src/loader.js @@ -1,6 +1,7 @@ const fs = require('fs'), - loaderUtils = require('loader-utils'); + loaderUtils = require('loader-utils'), + {getPolyfillDetector} = require('./library.js'); module.exports = async function loader(content, map, meta) { this.cacheable(); @@ -19,7 +20,7 @@ module.exports = async function loader(content, map, meta) { // Collect all tasks that will be run concurrently. const tasks = polyfills.map( - polyfill => pluginState.getPolyfillDetector(polyfill) + polyfill => getPolyfillDetector(polyfill) ); // -> detectors tasks.push(pluginState.addPolyfills(options)); // -> outputFilename const templateFile = require.resolve(`./injector-${options.singleFile ? 'single' : 'multi'}.js`); diff --git a/src/plugin.js b/src/plugin.js index 139b8c0..24f627a 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -1,6 +1,7 @@ const RawSource = require('webpack-sources').RawSource, - PluginState = require('./PluginState.js'); + PluginState = require('./PluginState.js'), + {getPolyfillsSource} = require('./library.js'); class PolyfillInjectorPlugin { constructor(options) { @@ -30,7 +31,7 @@ class PolyfillInjectorPlugin { await pluginState.iteratePolyfillSets( async ({polyfills, excludes, singleFile, banner}, filename) => { if (singleFile) { - const source = await pluginState.getPolyfillsSource(polyfills, excludes, polyfills.length === 1); + const source = await getPolyfillsSource(polyfills, excludes, polyfills.length === 1); compilation.assets[filename] = new RawSource(banner + source); addAsChunk(filename, compilation); } else { @@ -43,7 +44,7 @@ class PolyfillInjectorPlugin { const currentPolyfills = polyfills.filter((polyfill, i) => choice.charAt(i) === '1'); const supported = polyfills.filter((polyfill, i) => choice.charAt(i) === '0'); tasks.push( - pluginState.getPolyfillsSource(currentPolyfills, excludes.concat(supported), true).then((source) => { + getPolyfillsSource(currentPolyfills, excludes.concat(supported), true).then((source) => { compilation.assets[outputFile] = new RawSource(banner + source); addAsChunk(outputFile, compilation); })