diff --git a/index.js b/index.js index 6d3793b..3fea1af 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,21 @@ const HappySite = require('./src/happySite.js') +const fs = require('fs') class HappySiteWebpackPlugin { - constructor(source, views) { + constructor(source, views, destination = null) { this._options = { source: source, - views: views + views: views, + destination: destination + } + + if (!fs.existsSync(source)) { + throw new Error('source "' + source + '" not found!') + } + + if (!fs.existsSync(views)) { + throw new Error('views "' + views + '" not found!') } } @@ -21,28 +31,15 @@ class HappySiteWebpackPlugin { // Compilation object gives us reference to some useful constants. const { Compilation } = webpack - // RawSource is one of the "sources" classes that should be used - // to represent asset sources in compilation. - const { RawSource } = webpack.sources - - compiler.hooks.thisCompilation.tap(pluginName, (compilation) => { - - const compilationHash = compilation.hash - const webpackPublicPath = '.' + compilation.getAssetPath(compilation.outputOptions.publicPath, { hash: compilationHash }) - - // Tapping to the assets processing pipeline on a specific stage. - compilation.hooks.processAssets.tap({ - name: pluginName, + compiler.hooks.done.tap(pluginName, ({ compilation }) => { - // Using one of the later asset processing stages to ensure - // that all assets were already added to the compilation by other plugins. - stage: Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE - }, + // if destination is null, use from webpack configuration + if (!this._options.destination) { + this._options.destination = compilation.outputOptions.path + } - (assets) => { - const happySite = new HappySite(webpackPublicPath + this._options.source, this._options.views, webpackPublicPath) - happySite.run() - }) + const happySite = new HappySite(this._options.source, this._options.views, this._options.destination) + happySite.run() }) } } diff --git a/resources/site/site.yml b/resources/site/site.yml index 9458ec3..b95c95e 100644 --- a/resources/site/site.yml +++ b/resources/site/site.yml @@ -1,3 +1,4 @@ title: "test" language: "en" -domain: "test.io" \ No newline at end of file +domain: "test.io" +https: true \ No newline at end of file diff --git a/resources/views/helpers/meta.njk b/resources/views/helpers/meta.njk index f3e3836..71a0549 100644 --- a/resources/views/helpers/meta.njk +++ b/resources/views/helpers/meta.njk @@ -1,7 +1,29 @@ -{% macro meta(page) %} +{# handle meta in templates #} + +{% macro meta(page, site) %} {% if (page.meta) %} + + {% set hasCannocial = false %} + {% for key, content in page.meta %} + + {# check for index in robots if set page has canonical #} + {% if (key === 'robots' and content.indexOf('index') !== -1 and content.indexOf('noindex') === -1) %} + {% set hasCannocial = true %} + {% endif %} {% endfor %} + + {# check if page has canonical #} + {% if (hasCannocial) %} + {% set http = 'http' %} + + {% if (site.https) %} + {% set http = site.https %} + {% endif %} + + + + {% endif %} {% endif %} {% endmacro %} \ No newline at end of file diff --git a/resources/views/home.njk b/resources/views/home.njk index b209dda..afb1500 100644 --- a/resources/views/home.njk +++ b/resources/views/home.njk @@ -3,6 +3,6 @@ {% block main %} - {{ page.content | safe }} + {% endblock %} \ No newline at end of file diff --git a/src/engine.js b/src/engine.js index fa2dacc..c9a85b3 100644 --- a/src/engine.js +++ b/src/engine.js @@ -2,7 +2,9 @@ const nunjucks = require('nunjucks') const { minify } = require('html-minifier') const fs = require('fs') -const { asset, resize } = require('./engine.js') +const { asset, resize } = require('./helpers.js') +const PageQuery = require('./queries/pages.js') +const configStore = require('./config.js') /** * engine - handle eta.js @@ -36,7 +38,8 @@ class Engine { // adding defaults for view, function and data = config.yml this._defaults = { site: site, - asset: asset + asset: asset, + pageQuery: new PageQuery(configStore.get('source')) } } diff --git a/src/happySite.js b/src/happySite.js index 158629e..96b3762 100644 --- a/src/happySite.js +++ b/src/happySite.js @@ -62,7 +62,7 @@ class HappySite { const sitemap = new Sitemap(this._site) // run through pages and generate html files - results.forEach((page) => { + results.forEach((page, index) => { page.render(this._engine, (error, content) => { // show errors @@ -82,11 +82,13 @@ class HappySite { }) sitemap.addPage(page) + + // if run is finish, write sitemap.xml + if ((index + 1) === results.length) { + fs.writeFileSync(this._destination + '/sitemap.xml', sitemap.getXmlAsString()) + } }) }) - - // write sitemap - fs.writeFileSync(this._destination + '/sitemap.xml', sitemap.getXmlAsString()) } } diff --git a/src/helpers.js b/src/helpers.js index c8fc7cd..4a2dbaf 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,7 +1,7 @@ const path = require('path') const fs = require('fs') -const Media = require('./../media.js') +const Media = require('./media.js') /** * asset - checks manifest.json for given path and return @@ -50,4 +50,4 @@ async function resize(src, sizes, options, done) done(null, src) } -export { asset, resize } \ No newline at end of file +module.exports = { asset, resize } \ No newline at end of file diff --git a/src/media.js b/src/media.js index a1bb7a7..ef5b90e 100644 --- a/src/media.js +++ b/src/media.js @@ -98,4 +98,4 @@ class Media { } } -module.exports =Media \ No newline at end of file +module.exports = Media \ No newline at end of file diff --git a/src/queries/pages.js b/src/queries/pages.js index 9c9470f..f928b6e 100644 --- a/src/queries/pages.js +++ b/src/queries/pages.js @@ -30,7 +30,8 @@ class Pages { // default options for find this._options = { - parent: '' + parent: '', + deep: -1 } this._dirPath = dirPath @@ -87,7 +88,12 @@ class Pages { } // if directory going deep - if (file.isDirectory()) { + if (file.isDirectory() && (options.deep > 0 || options.deep === -1)) { + + if (options.deep > 0) { + options.deep-- + } + const childrenOptions = Object.assign({}, options, { 'parent': options.parent + '/' + file.name }) diff --git a/test.js b/test.js index 34c71bd..b007457 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,4 @@ -import HappySite from './src/happySite.js' +const HappySite = require('./src/happySite.js') -const happySite = new HappySite('./resources/site', './resources/views', ) +const happySite = new HappySite('./resources/site', './resources/views', './public') happySite.run() \ No newline at end of file