Skip to content

Commit

Permalink
refactoring: extract library access
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianS90 committed Jan 25, 2019
1 parent 128201e commit 027d5f0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 35 deletions.
32 changes: 2 additions & 30 deletions src/PluginState.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const
loaderUtils = require('loader-utils'),
polyfillLibrary = require('polyfill-library');
{getPolyfillsSource} = require('./library.js');

class PluginState {
constructor(compilation, options) {
Expand All @@ -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),
Expand All @@ -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) {
Expand Down
34 changes: 34 additions & 0 deletions src/library.js
Original file line number Diff line number Diff line change
@@ -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,
};
5 changes: 3 additions & 2 deletions src/loader.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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`);
Expand Down
7 changes: 4 additions & 3 deletions src/plugin.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
})
Expand Down

0 comments on commit 027d5f0

Please sign in to comment.