Skip to content

Commit

Permalink
Feat: feature.include
Browse files Browse the repository at this point in the history
  • Loading branch information
Timkor committed Mar 31, 2019
1 parent d2135bf commit 6a5b15f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion examples/custom-polyfill/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export default {
because the polyfill will always be loaded, parsed and executed.
*/
{

require: 'url-polyfill' // NPM package or require path of file
},

Expand All @@ -27,6 +28,7 @@ export default {
{
require: 'intersection-observer',
detect: () => 'IntersectionObserver' in window,
include: true,
},

/*
Expand All @@ -42,7 +44,9 @@ export default {
detect: () => 'scrollBehavior' in document.documentElement.style && window.__forceSmoothScrollPolyfill__ !== true,

// Optional install function called client side after the package is required:
install: (smoothscroll) => smoothscroll.polyfill()
install: (smoothscroll) => smoothscroll.polyfill(),

include: true,
}
]
},
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function (moduleOptions) {
const params = {
require: feature.require,
log: options.log,
include: typeof feature.detect === 'function' ? feature.include || false : true,
name: feature.name || (Array.isArray(require) ? require.join(',') : feature.require.toString())
};

Expand Down
8 changes: 7 additions & 1 deletion src/templates/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ export default () => {

<% } %>

<% if (!options.include) { %>
return new Promise((resolve, reject) => {

require.ensure([
<%= JSON.stringify(options.require) %>
], function (require) {

<% } %>

const polyfill = require(<%= JSON.stringify(options.require) %>);

<% if (options.log) { %>
Expand All @@ -41,7 +44,10 @@ export default () => {

<% } %>

<% if (!options.include) { %>

resolve();
});
})
});
<% } %>
}

0 comments on commit 6a5b15f

Please sign in to comment.