Skip to content

Commit

Permalink
feat: allow to disable default sources (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Feb 10, 2021
1 parent 3ddf5dc commit 480656f
Show file tree
Hide file tree
Showing 5 changed files with 1,550 additions and 534 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ module.exports = {
type: 'src',
filter: (tag, attribute, attributes, resourcePath) => {
if (
attributes.property === 'og:image' ||
attributes.value === 'og:image' ||
attributes.name === 'twitter:image'
) {
return true;
Expand Down
39 changes: 15 additions & 24 deletions src/plugins/sources-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,25 @@ export default (options) =>
const urlFilter = getFilter(maybeUrlFilter, (value) =>
isUrlRequestable(value)
);
const getAttribute = (tag, attribute, attributes, resourcePath) =>
list.find((element) => {
const foundTag =
typeof element.tag === 'undefined' ||
(typeof element.tag !== 'undefined' &&
element.tag.toLowerCase() === tag.toLowerCase());

if (!foundTag) {
return false;
}
const getAttribute = (tag, attribute, attributes, resourcePath) => {
const foundTag = list.get(tag.toLowerCase()) || list.get('*');

const foundAttribute =
element.attribute.toLowerCase() === attribute.toLowerCase();
if (!foundTag) {
return false;
}

if (!foundAttribute) {
return false;
}
const foundAttribute = foundTag.get(attribute.toLowerCase());

const adaptedAttributes = attributes.reduce((accumulator, item) => {
// eslint-disable-next-line no-param-reassign
accumulator[item.name] = item.value;
return accumulator;
}, {});
if (!foundAttribute) {
return false;
}

return element.filter
? element.filter(tag, attribute, adaptedAttributes, resourcePath)
: true;
});
const result = foundAttribute.filter
? foundAttribute.filter(tag, attribute, attributes, resourcePath)
: true;

return result ? foundAttribute : false;
};

const { resourcePath } = options;
const parser5 = new SAXParser({ sourceCodeLocationInfo: true });
Expand Down
83 changes: 54 additions & 29 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,9 @@ function getMinimizeOption(rawOptions, loaderContext) {
}

function getAttributeValue(attributes, name) {
const lowercasedAttributes = Object.keys(attributes).reduce((keys, k) => {
// eslint-disable-next-line no-param-reassign
keys[k.toLowerCase()] = k;
const [result] = attributes.filter((i) => i.name.toLowerCase() === name);

return keys;
}, {});

return attributes[lowercasedAttributes[name.toLowerCase()]];
return typeof result === 'undefined' ? result : result.value;
}

function scriptSrcFilter(tag, attribute, attributes) {
Expand Down Expand Up @@ -737,45 +732,75 @@ const defaultAttributes = [
},
];

function smartMergeSources(array, factory) {
if (typeof array === 'undefined') {
return factory();
function rewriteSourcesList(sourcesList, attribute, source) {
for (const key of sourcesList.keys()) {
const item = sourcesList.get(key);

if (!item.has(attribute)) {
// eslint-disable-next-line no-continue
continue;
}

item.set(attribute, {
...item.get(attribute),
...source,
});

sourcesList.set(key, item);
}
}

const newArray = [];
function createSourcesList(sources, accumulator = new Map()) {
for (const source of sources) {
if (source === '...') {
// eslint-disable-next-line no-continue
continue;
}

for (let i = 0; i < array.length; i++) {
const item = array[i];
let { tag = '*', attribute = '*' } = source;

if (item === '...') {
const items = factory();
tag = tag.toLowerCase();
attribute = attribute.toLowerCase();

if (typeof items !== 'undefined') {
// eslint-disable-next-line no-shadow
for (const item of items) {
newArray.push(item);
}
}
} else if (typeof newArray !== 'undefined') {
newArray.push(item);
if (tag === '*') {
rewriteSourcesList(accumulator, attribute, source);
}

if (!accumulator.has(tag)) {
accumulator.set(tag, new Map());
}

accumulator.get(tag).set(attribute, source);
}

return newArray;
return accumulator;
}

function smartMergeSources(array, factory) {
if (typeof array === 'undefined') {
return factory();
}

const result = array.some((i) => i === '...')
? createSourcesList(array, factory())
: createSourcesList(array);

return result;
}

function getSourcesOption(rawOptions) {
if (typeof rawOptions.sources === 'undefined') {
return { list: defaultAttributes };
return { list: createSourcesList(defaultAttributes) };
}

if (typeof rawOptions.sources === 'boolean') {
return rawOptions.sources === true ? { list: defaultAttributes } : false;
return rawOptions.sources === true
? { list: createSourcesList(defaultAttributes) }
: false;
}

const sources = smartMergeSources(
rawOptions.sources.list,
() => defaultAttributes
const sources = smartMergeSources(rawOptions.sources.list, () =>
createSourcesList(defaultAttributes)
);

return {
Expand Down
1,906 changes: 1,426 additions & 480 deletions test/__snapshots__/sources-option.test.js.snap

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions test/sources-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,60 @@ describe("'sources' option", () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should work with "..." syntax and disable source', async () => {
const compiler = getCompiler('simple.js', {
sources: {
list: [
'...',
{
tag: 'img',
attribute: 'src',
type: 'src',
filter: (tag) => tag.toLowerCase() !== 'img',
},
{
tag: 'img',
attribute: 'srcset',
type: 'srcset',
filter: (tag) => tag.toLowerCase() !== 'img',
},
],
},
});
const stats = await compile(compiler);

expect(getModuleSource('./simple.html', stats)).toMatchSnapshot('module');
expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should handle default src sources in all HTML tags except img tag (testing filter option)', async () => {
const compiler = getCompiler('simple.js', {
sources: {
list: [
'...',
{
attribute: 'src',
type: 'src',
// eslint-disable-next-line no-unused-vars
filter: (tag, attribute, sources) => tag.toLowerCase() !== 'img',
},
],
},
});
const stats = await compile(compiler);

expect(getModuleSource('./simple.html', stats)).toMatchSnapshot('module');
expect(
execute(readAsset('main.bundle.js', compiler, stats))
).toMatchSnapshot('result');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it.skip('should handle the "include" tags', async () => {
const compiler = getCompiler('include.js', {
sources: {
Expand Down

0 comments on commit 480656f

Please sign in to comment.