Skip to content

Commit

Permalink
postcss-preset-env : make logger a class instance (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke authored Jan 31, 2022
1 parent 56d8af6 commit fd17611
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 122 deletions.
12 changes: 7 additions & 5 deletions plugin-packs/postcss-preset-env/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import autoprefixer from 'autoprefixer';
import cssdb from 'cssdb';
import writeToExports from './side-effects/write-to-exports.mjs';
import { pluginIdHelp } from './plugins/plugin-id-help.mjs';
import { dumpLogs, resetLogger } from './log/helper.mjs';
import { newLogger } from './log/helper.mjs';
import logFeaturesList from './log/features-list.mjs';
import { listFeatures } from './lib/list-features.mjs';
import { initializeSharedOptions } from './lib/shared-options.mjs';

const plugin = (opts) => {
const logger = newLogger();

// initialize options
const options = Object(opts);
const featureNamesInOptions = Object.keys(Object(options.features));
const browsers = options.browsers;
const sharedOptions = initializeSharedOptions(options);

const features = listFeatures(cssdb, options, sharedOptions);
const features = listFeatures(cssdb, options, sharedOptions, logger);
const plugins = features.map((feature) => {
return feature.plugin;
});
Expand All @@ -25,7 +27,7 @@ const plugin = (opts) => {
);
}

logFeaturesList(features, options);
logFeaturesList(features, options, logger);

const internalPlugin = () => {
return {
Expand All @@ -34,11 +36,11 @@ const plugin = (opts) => {
pluginIdHelp(featureNamesInOptions, root, result);

if (options.debug) {
dumpLogs(result);
logger.dumpLogs(result);
}

// Always reset the logger, if when debug is false
resetLogger();
logger.resetLogger();

if (options.exportTo) {
writeToExports(sharedOptions.exportTo, opts.exportTo);
Expand Down
4 changes: 2 additions & 2 deletions plugin-packs/postcss-preset-env/src/lib/format-feature.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export function formatPolyfillableFeature(feature) {
};
}

export function formatStagedFeature(cssdbList, browsers, features, feature, sharedOptions) {
export function formatStagedFeature(cssdbList, browsers, features, feature, sharedOptions, logger) {
let options;
let plugin;

options = getOptionsForBrowsersByFeature(browsers, feature, cssdbList);
options = getOptionsForBrowsersByFeature(browsers, feature, cssdbList, logger);

if (features[feature.id] === true) {
if (sharedOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import browserslist from 'browserslist';
import getUnsupportedBrowsersByFeature from './get-unsupported-browsers-by-feature.mjs';
import { log } from '../log/helper.mjs';

// add extra options for certain browsers by feature
export default function getOptionsForBrowsersByFeature(browsers, feature, cssdbList) {
export default function getOptionsForBrowsersByFeature(browsers, feature, cssdbList, logger) {
const supportedBrowsers = browserslist(browsers, { ignoreUnknownVersions: true });

switch (feature.id) {
Expand All @@ -21,7 +20,7 @@ export default function getOptionsForBrowsersByFeature(browsers, feature, cssdbL
const feature = cssdbList.find(feature => feature.id === 'is-pseudo-class');

if (needsOptionFor(feature, supportedBrowsers)) {
log('Disabling :is on "nesting-rules" due to lack of browser support.');
logger.log('Disabling :is on "nesting-rules" due to lack of browser support.');
return {
noIsPseudoSelector: true,
};
Expand All @@ -37,7 +36,7 @@ export default function getOptionsForBrowsersByFeature(browsers, feature, cssdbL
});

if (hasIEOrEdge) {
log('Adding area[href] fallbacks for ":any-link" support in Edge and IE.');
logger.log('Adding area[href] fallbacks for ":any-link" support in Edge and IE.');
return {
subFeatures: {
areaHrefNeedsFixing: true,
Expand Down
23 changes: 10 additions & 13 deletions plugin-packs/postcss-preset-env/src/lib/list-features.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import browserslist from 'browserslist';
import { pluginHasSideEffects } from '../side-effects/plugins.mjs';
import { log, resetLogger } from '../log/helper.mjs';
import { featuresWithClientSide } from '../client-side-polyfills/plugins.mjs';
import { stageFromOptions } from './stage.mjs';
import { prepareFeaturesList } from './prepare-features-list.mjs';
Expand All @@ -9,7 +8,7 @@ import { clamp } from '../util/clamp.mjs';
import { intOrZero } from '../util/int-or-zero.mjs';
import { insertAfterKey, insertBeforeKey } from '../own-keys/keys.mjs';

export function listFeatures(cssdbList, options, sharedOptions) {
export function listFeatures(cssdbList, options, sharedOptions, logger) {
// initialize options
const features = Object(options.features);
const enableClientSidePolyfills = 'enableClientSidePolyfills' in options ? options.enableClientSidePolyfills : true;
Expand All @@ -24,13 +23,11 @@ export function listFeatures(cssdbList, options, sharedOptions) {
3, // There are currently only 3 vendors that are tracked (Blink, Webkit, Gecko)
);

resetLogger();

if (minimumVendorImplementations > 0) {
log(`Using features with ${minimumVendorImplementations} or more vendor implementations`);
logger.log(`Using features with ${minimumVendorImplementations} or more vendor implementations`);
}

const stage = stageFromOptions(options);
const stage = stageFromOptions(options, logger);

// TODO : remove this hack in the next major
// see : https://github.com/csstools/cssdb/pull/78
Expand Down Expand Up @@ -65,7 +62,7 @@ export function listFeatures(cssdbList, options, sharedOptions) {
return true;
}

log(` ${feature.id} with ${feature.vendors_implementations} vendor implementations has been disabled`);
logger.log(` ${feature.id} with ${feature.vendors_implementations} vendor implementations has been disabled`);
return false;
});

Expand All @@ -78,20 +75,20 @@ export function listFeatures(cssdbList, options, sharedOptions) {
const isAllowedFeature = features[feature.id] ? features[feature.id] : isAllowedStage && isAllowedByType;

if (isDisabled) {
log(` ${feature.id} has been disabled by options`);
logger.log(` ${feature.id} has been disabled by options`);
} else if (!isAllowedStage) {
if (isAllowedFeature) {
log(` ${feature.id} has been enabled by options`);
logger.log(` ${feature.id} has been enabled by options`);
} else {
log(` ${feature.id} with stage ${feature.stage} has been disabled`);
logger.log(` ${feature.id} with stage ${feature.stage} has been disabled`);
}
} else if (!isAllowedByType) {
log(` ${feature.id} has been disabled by "enableClientSidePolyfills: false".`);
logger.log(` ${feature.id} has been disabled by "enableClientSidePolyfills: false".`);
}

return isAllowedFeature;
}).map((feature) => {
return formatStagedFeature(cssdbList, browsers, features, feature, sharedOptions);
return formatStagedFeature(cssdbList, browsers, features, feature, sharedOptions, logger);
});

// browsers supported by the configuration
Expand Down Expand Up @@ -119,7 +116,7 @@ export function listFeatures(cssdbList, options, sharedOptions) {
});

if (!needsPolyfill) {
log(`${feature.id} disabled due to browser support`);
logger.log(`${feature.id} disabled due to browser support`);
}

return needsPolyfill;
Expand Down
9 changes: 4 additions & 5 deletions plugin-packs/postcss-preset-env/src/lib/stage.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { log } from '../log/helper.mjs';
import { clamp } from '../util/clamp.mjs';

export const DEFAULT_STAGE = 2;
export const OUT_OF_RANGE_STAGE = 5;

export function stageFromOptions(options) {
export function stageFromOptions(options, logger) {
let stage = DEFAULT_STAGE;

if (typeof options.stage === 'undefined') {
log(`Using features from Stage ${stage} (default)`);
logger.log(`Using features from Stage ${stage} (default)`);
return stage;
}

Expand All @@ -24,9 +23,9 @@ export function stageFromOptions(options) {
}

if (stage === OUT_OF_RANGE_STAGE) {
log('Stage has been disabled, features will be handled via the "features" option.');
logger.log('Stage has been disabled, features will be handled via the "features" option.');
} else {
log(`Using features from Stage ${stage}`);
logger.log(`Using features from Stage ${stage}`);
}

return stage;
Expand Down
13 changes: 6 additions & 7 deletions plugin-packs/postcss-preset-env/src/log/features-list.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { log } from './helper.mjs';
import { clientSideDocumentation } from '../client-side-polyfills/plugins.mjs';

export default function logFeaturesList(supportedFeatures, options) {
export default function logFeaturesList(supportedFeatures, options, logger) {
if (options.debug) {
log('Enabling the following feature(s):');
logger.log('Enabling the following feature(s):');
const clientSideFeatures = [];

supportedFeatures.forEach(feature => {
if (feature.id.startsWith('before') || feature.id.startsWith('after')) {
log(` ${feature.id} (injected via options)`);
logger.log(` ${feature.id} (injected via options)`);
} else {
log(` ${feature.id}`);
logger.log(` ${feature.id}`);
}

if (typeof clientSideDocumentation[feature.id] !== 'undefined') {
Expand All @@ -19,8 +18,8 @@ export default function logFeaturesList(supportedFeatures, options) {
});

if (clientSideFeatures.length) {
log('These feature(s) need a browser library to work:');
clientSideFeatures.forEach(featureId => log(` ${featureId}: ${clientSideDocumentation[featureId]}`));
logger.log('These feature(s) need a browser library to work:');
clientSideFeatures.forEach(featureId => logger.log(` ${featureId}: ${clientSideDocumentation[featureId]}`));
}
}
}
30 changes: 19 additions & 11 deletions plugin-packs/postcss-preset-env/src/log/helper.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
const logs = [];
class Logger {
constructor() {
this.logs = [];
}

export function log(str) {
logs.push(str);
}
log(str) {
this.logs.push(str);
}

export function resetLogger() {
logs.length = 0;
}
resetLogger() {
this.logs.length = 0;
}

dumpLogs(result) {
if (result) {
this.logs.forEach(line => result.warn(line));
}

export function dumpLogs(result) {
if (result) {
logs.forEach(line => result.warn(line));
this.resetLogger();
}
}

resetLogger();
export function newLogger() {
return new Logger();
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { formatStagedFeature } from '../../lib/format-feature.mjs';
import { strict as assert } from 'assert';
import { newTestLogger } from '../log/test-logger.mjs';

const testLogger = newTestLogger();

assert.deepStrictEqual(
formatStagedFeature(
Expand All @@ -15,6 +18,7 @@ assert.deepStrictEqual(
vendors_implementations: 1,
},
undefined,
testLogger.logger,
),
{
browsers: [
Expand Down Expand Up @@ -43,6 +47,7 @@ assert.deepStrictEqual(
vendors_implementations: 1,
},
undefined,
testLogger.logger,
),
{
browsers: [
Expand Down Expand Up @@ -73,6 +78,7 @@ assert.deepStrictEqual(
{
shared: true,
},
testLogger.logger,
),
{
browsers: [
Expand Down Expand Up @@ -101,6 +107,7 @@ assert.deepStrictEqual(
{
shared: true,
},
testLogger.logger,
),
{
browsers: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { testLogger } from '../../log/test-logger.mjs';
import { newTestLogger } from '../../log/test-logger.mjs';
import { strict as assert } from 'assert';
import { dumpLogs, resetLogger } from '../../../log/helper.mjs';
import { listFeatures } from '../../../lib/list-features.mjs';
import { cssdb } from './cssdb-fixture.mjs';

const logger = testLogger();
const testLogger = newTestLogger();

resetLogger();
testLogger.logger.resetLogger();
assert.deepStrictEqual(
cleanResult(listFeatures(cssdb, {stage: 0, enableClientSidePolyfills: false})),
cleanResult(listFeatures(cssdb, {stage: 0, enableClientSidePolyfills: false}, undefined, testLogger.logger)),
[
{
browsers: [
Expand All @@ -28,19 +27,19 @@ assert.deepStrictEqual(
],
);

dumpLogs(logger);
testLogger.logger.dumpLogs(testLogger);
assert.deepStrictEqual(
logger.getLogs(),
testLogger.getLogs(),
[
'Using features from Stage 0',
' blank-pseudo-class has been disabled by "enableClientSidePolyfills: false".',
'Adding area[href] fallbacks for ":any-link" support in Edge and IE.',
],
);

resetLogger();
testLogger.logger.resetLogger();
assert.deepStrictEqual(
cleanResult(listFeatures(cssdb, {stage: 0, enableClientSidePolyfills: true})),
cleanResult(listFeatures(cssdb, {stage: 0, enableClientSidePolyfills: true}, undefined, testLogger.logger)),
[
{
browsers: [
Expand Down Expand Up @@ -75,9 +74,9 @@ assert.deepStrictEqual(
],
);

dumpLogs(logger);
testLogger.logger.dumpLogs(testLogger);
assert.deepStrictEqual(
logger.getLogs(),
testLogger.getLogs(),
[
'Using features from Stage 0',
'Adding area[href] fallbacks for ":any-link" support in Edge and IE.',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { testLogger } from '../../log/test-logger.mjs';
import { newTestLogger } from '../../log/test-logger.mjs';
import { strict as assert } from 'assert';
import { dumpLogs, resetLogger } from '../../../log/helper.mjs';
import { listFeatures } from '../../../lib/list-features.mjs';
import { cssdb } from './cssdb-fixture.mjs';

const logger = testLogger();
const testLogger = newTestLogger();

resetLogger();
testLogger.logger.resetLogger();
assert.deepStrictEqual(
cleanResult(listFeatures(cssdb, {})),
cleanResult(listFeatures(cssdb, {}, undefined, testLogger.logger)),
[
{
browsers: [
Expand All @@ -28,9 +27,9 @@ assert.deepStrictEqual(
],
);

dumpLogs(logger);
testLogger.logger.dumpLogs(testLogger);
assert.deepStrictEqual(
logger.getLogs(),
testLogger.getLogs(),
[
'Using features from Stage 2 (default)',
' blank-pseudo-class with stage 1 has been disabled',
Expand Down
Loading

0 comments on commit fd17611

Please sign in to comment.