diff --git a/scripts/helpers/engine.js b/scripts/helpers/engine.js index 6b2f703..13b1e7c 100644 --- a/scripts/helpers/engine.js +++ b/scripts/helpers/engine.js @@ -19,7 +19,9 @@ hexo.extend.helper.register('next_inject', function(point) { hexo.extend.helper.register('next_js', function(file, { pjax = false, - module = false + module = false, + async = false, + defer = !async } = {}) { const { next_version } = this; const { internal, custom_cdn_url } = this.theme.vendors; @@ -32,18 +34,21 @@ hexo.extend.helper.register('next_js', function(file, { custom : custom_cdn_url }); const src = links[internal] || links.local; - return ``; + return ``; }); -hexo.extend.helper.register('next_vendors', function(name) { +hexo.extend.helper.register('next_vendors', function(name, { + async = false, + defer = !async +} = {}) { const { url, integrity } = this.theme.vendors[name]; const type = url.endsWith('css') ? 'css' : 'js'; if (type === 'css') { if (integrity) return ``; return ``; } - if (integrity) return ``; - return ``; + if (integrity) return ``; + return ``; }); hexo.extend.helper.register('next_data', function(name, ...data) { diff --git a/test/helpers/engine.js b/test/helpers/engine.js new file mode 100644 index 0000000..c66f008 --- /dev/null +++ b/test/helpers/engine.js @@ -0,0 +1,73 @@ +'use strict'; + +const helpers = {}; + +global.hexo = { + extend: { + helper: { + register(name, helper) { + helpers[name] = helper; + } + } + } +}; + +require('../../scripts/helpers/engine'); +delete global.hexo; + +describe('engine', () => { + const context = { + next_version: '8.28.0', + theme : { + js : 'js', + vendors: { + internal : 'local', + custom_cdn_url: '', + library : { + url: 'https://cdn.example.com/library.js' + }, + secure_library: { + url : 'https://cdn.example.com/secure.js', + integrity: 'sha256-example' + }, + stylesheet: { + url: 'https://cdn.example.com/library.css' + } + } + }, + url_for(path) { + return '/' + path; + } + }; + + const nextJs = helpers.next_js.bind(context); + const nextVendors = helpers.next_vendors.bind(context); + + it('keeps defer as the default for NexT scripts', () => { + nextJs('main.js').should.eql(''); + }); + + it('supports custom loading options for NexT scripts', () => { + nextJs('main.js', { defer: false }).should.eql(''); + nextJs('main.js', { async: true }).should.eql(''); + nextJs('main.js', { defer: true, async: true }).should.eql(''); + }); + + it('preserves existing NexT script options', () => { + nextJs('main.js', { pjax: true, module: true }).should.eql(''); + }); + + it('keeps defer as the default for vendor scripts', () => { + nextVendors('library').should.eql(''); + nextVendors('secure_library').should.eql(''); + }); + + it('supports custom loading options for vendor scripts', () => { + nextVendors('library', { defer: false }).should.eql(''); + nextVendors('library', { async: true }).should.eql(''); + }); + + it('does not apply script loading options to stylesheets', () => { + nextVendors('stylesheet', { async: true }).should.eql(''); + }); +}); diff --git a/test/helpers/index.js b/test/helpers/index.js index 3ef5dde..9769bca 100644 --- a/test/helpers/index.js +++ b/test/helpers/index.js @@ -1,6 +1,7 @@ 'use strict'; describe('Helpers', () => { + require('./engine'); require('./font'); require('./next-url'); });