Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract functions & update testfile #13

Merged
merged 2 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 55 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,76 +31,76 @@ class ResourceHintWebpackPlugin {
compiler.hooks.compilation.tap('ResourceHintWebpackPlugin', compilation => {
if (compilation.hooks.htmlWebpackPluginAlterAssetTags) {
compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('ResourceHintWebpackPluginAlterAssetTags',
this.resourceHintWebpackPluginAlterAssetTags.bind(this)
resourceHintWebpackPluginAlterAssetTags
);
}
});
} else {
// Webpack 1-3 Plugin System
compiler.plugin('compilation', compilation => {
compilation.plugin('html-webpack-plugin-alter-asset-tags',
this.resourceHintWebpackPluginAlterAssetTags.bind(this)
resourceHintWebpackPluginAlterAssetTags
);
});
}
}
}

/**
* The main processing function
*/
resourceHintWebpackPluginAlterAssetTags (htmlPluginData, callback) {
const htmlWebpackPluginOptions = htmlPluginData.plugin.options;
const pluginData = objectAssign({}, htmlPluginData);
const tags = {
prefetch: [],
// https://w3c.github.io/preload/#link-type-preload
preload: []
};
// Create Resource tags
Object.keys(tags).forEach(resourceHintType => {
// Check if it is disabled for the current htmlWebpackPlugin instance:
// e.g.
// new HtmlWebpackPlugin({
// prefetch: false
// })
if (htmlWebpackPluginOptions[resourceHintType] === false) {
return;
/**
* The main processing function
*/
function resourceHintWebpackPluginAlterAssetTags (htmlPluginData, callback) {
const htmlWebpackPluginOptions = htmlPluginData.plugin.options;
const pluginData = objectAssign({}, htmlPluginData);
const tags = {
prefetch: [],
// https://w3c.github.io/preload/#link-type-preload
preload: []
};
// Create Resource tags
Object.keys(tags).forEach(resourceHintType => {
// Check if it is disabled for the current htmlWebpackPlugin instance:
// e.g.
// new HtmlWebpackPlugin({
// prefetch: false
// })
if (htmlWebpackPluginOptions[resourceHintType] === false) {
return;
}
// If no options are found all files are prefetched / preload
const fileFilters = htmlWebpackPluginOptions[resourceHintType]
? [].concat(htmlWebpackPluginOptions[resourceHintType])
: defaultFilter;
// Process every filter
fileFilters.forEach(filter => {
if (filter.indexOf('*') !== -1) {
Array.prototype.push.apply(tags[resourceHintType], addResourceHintTags(
resourceHintType,
filter,
pluginData.body,
htmlWebpackPluginOptions
));
} else {
tags[resourceHintType].push(createResourceHintTag(filter, resourceHintType, htmlWebpackPluginOptions));
}
// If no options are found all files are prefetched / preload
const fileFilters = htmlWebpackPluginOptions[resourceHintType]
? [].concat(htmlWebpackPluginOptions[resourceHintType])
: defaultFilter;
// Process every filter
fileFilters.forEach(filter => {
if (filter.indexOf('*') !== -1) {
Array.prototype.push.apply(tags[resourceHintType], this.addResourceHintTags(
resourceHintType,
filter,
pluginData.body,
htmlWebpackPluginOptions
));
} else {
tags[resourceHintType].push(createResourceHintTag(filter, resourceHintType, htmlWebpackPluginOptions));
}
});
});
// Add all Resource tags to the head
Array.prototype.push.apply(pluginData.head, tags.preload.map(addPreloadType));
Array.prototype.push.apply(pluginData.head, tags.prefetch);
callback(null, pluginData);
}
});
// Add all Resource tags to the head
Array.prototype.push.apply(pluginData.head, tags.preload.map(addPreloadType));
Array.prototype.push.apply(pluginData.head, tags.prefetch);
callback(null, pluginData);
}

/**
* Adds Resource hint tags
*/
addResourceHintTags (resourceHintType, filter, assetTags, htmlWebpackPluginOptions) {
const urls = assetTags
.map(tag => tag.attributes.src || tag.attributes.href)
.filter(url => url)
.filter(minimatch.filter(filter));
// Add a ResourceHint for every match
return urls.map(url => createResourceHintTag(url, resourceHintType, htmlWebpackPluginOptions));
}
/**
* Adds Resource hint tags
*/
function addResourceHintTags (resourceHintType, filter, assetTags, htmlWebpackPluginOptions) {
const urls = assetTags
.map(tag => tag.attributes.src || tag.attributes.href)
.filter(url => url)
.filter(minimatch.filter(filter));
// Add a ResourceHint for every match
return urls.map(url => createResourceHintTag(url, resourceHintType, htmlWebpackPluginOptions));
}

function createResourceHintTag (url, resourceHintType, htmlWebpackPluginOptions) {
Expand Down
68 changes: 34 additions & 34 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/* eslint-env jasmine */
var fs = require('fs');
var path = require('path');
var MemoryFileSystem = require('memory-fs');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlResourceHintPlugin = require('../');
const fs = require('fs');
const path = require('path');
const MemoryFileSystem = require('memory-fs');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const HtmlResourceHintPlugin = require('../');

var OUTPUT_DIR = path.join(__dirname, '../dist');
const OUTPUT_DIR = path.join(__dirname, '../dist');

describe('HtmlResourceHintPlugin', function () {
it('adds prefetch tags by default', function (done) {
var expected = fs.readFileSync(path.resolve(__dirname, 'fixtures/expected.html')).toString();
var compiler = webpack({
describe('HtmlResourceHintPlugin', () => {
it('adds prefetch tags by default', (done) => {
const expected = fs.readFileSync(path.resolve(__dirname, 'fixtures/expected.html')).toString();
const compiler = webpack({
entry: {
main: path.join(__dirname, 'fixtures', 'entry.js')
},
Expand All @@ -23,21 +23,21 @@ describe('HtmlResourceHintPlugin', function () {
new HtmlWebpackPlugin(),
new HtmlResourceHintPlugin()
]
}, function (err, result) {
}, (err, result) => {
expect(err).toBeFalsy();
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
var html = result.compilation.assets['index.html'].source();
const html = result.compilation.assets['index.html'].source();
expect(html).toBe(expected);
done();
});
compiler.outputFileSystem = new MemoryFileSystem();
});
});

describe('HtmlResourceHintPlugin', function () {
it('adds prefetch tags', function (done) {
var expected = fs.readFileSync(path.resolve(__dirname, 'fixtures/expected.html')).toString();
var compiler = webpack({
describe('HtmlResourceHintPlugin', () => {
it('adds prefetch tags', (done) => {
const expected = fs.readFileSync(path.resolve(__dirname, 'fixtures/expected.html')).toString();
const compiler = webpack({
entry: {
main: path.join(__dirname, 'fixtures', 'entry.js')
},
Expand All @@ -52,20 +52,20 @@ describe('HtmlResourceHintPlugin', function () {
}),
new HtmlResourceHintPlugin()
]
}, function (err, result) {
}, (err, result) => {
expect(err).toBeFalsy();
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
var html = result.compilation.assets['index.html'].source();
const html = result.compilation.assets['index.html'].source();
expect(html).toBe(expected);
done();
});
compiler.outputFileSystem = new MemoryFileSystem();
});
});

describe('HtmlResourceHintPlugin', function () {
it('adds no file which do not match the filter', function (done) {
var compiler = webpack({
describe('HtmlResourceHintPlugin', () => {
it('adds no file which do not match the filter', (done) => {
const compiler = webpack({
entry: {
main: path.join(__dirname, 'fixtures', 'entry.js')
},
Expand All @@ -80,10 +80,10 @@ describe('HtmlResourceHintPlugin', function () {
}),
new HtmlResourceHintPlugin()
]
}, function (err, result) {
}, (err, result) => {
expect(err).toBeFalsy();
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
var html = result.compilation.assets['index.html'].source();
const html = result.compilation.assets['index.html'].source();
expect(html.indexOf('rel="prefetch"') === -1).toBe(true);
expect(html.indexOf('rel="preload"') === -1).toBe(true);
done();
Expand All @@ -92,9 +92,9 @@ describe('HtmlResourceHintPlugin', function () {
});
});

describe('HtmlResourceHintPlugin', function () {
it('allows to add fixed prefetch url', function (done) {
var compiler = webpack({
describe('HtmlResourceHintPlugin', () => {
it('allows to add fixed prefetch url', (done) => {
const compiler = webpack({
entry: {
main: path.join(__dirname, 'fixtures', 'entry.js')
},
Expand All @@ -108,20 +108,20 @@ describe('HtmlResourceHintPlugin', function () {
}),
new HtmlResourceHintPlugin()
]
}, function (err, result) {
}, (err, result) => {
expect(err).toBeFalsy();
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
var html = result.compilation.assets['index.html'].source();
const html = result.compilation.assets['index.html'].source();
expect(!!html.indexOf('<link rel="prefetch" href="demo.json">')).toBe(true);
done();
});
compiler.outputFileSystem = new MemoryFileSystem();
});
});

describe('HtmlResourceHintPlugin', function () {
it('allows to add fixed preload url', function (done) {
var compiler = webpack({
describe('HtmlResourceHintPlugin', () => {
it('allows to add fixed preload url', (done) => {
const compiler = webpack({
entry: {
main: path.join(__dirname, 'fixtures', 'entry.js')
},
Expand All @@ -135,10 +135,10 @@ describe('HtmlResourceHintPlugin', function () {
}),
new HtmlResourceHintPlugin()
]
}, function (err, result) {
}, (err, result) => {
expect(err).toBeFalsy();
expect(JSON.stringify(result.compilation.errors)).toBe('[]');
var html = result.compilation.assets['index.html'].source();
const html = result.compilation.assets['index.html'].source();
expect(!!html.indexOf('<link rel="preload" href="demo.json">')).toBe(true);
done();
});
Expand Down