diff --git a/index.js b/index.js index 66e9ced..6d3793b 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,12 @@ -import { validate } from 'schema-utils' -import HappySite from './src/happySite.js' - -// schema for options object -const schema = { - type: 'object', - properties: { - source: { - type: 'string' - }, - destination: { - type: 'string' - }, - views: { - type: 'string' - } - } -} +const HappySite = require('./src/happySite.js') -export default class HappySiteWebpackPlugin { - constructor(options = {}) { - validate(schema, options) +class HappySiteWebpackPlugin { - this._options = options + constructor(source, views) { + this._options = { + source: source, + views: views + } } apply(compiler) { @@ -42,6 +27,9 @@ export default class HappySiteWebpackPlugin { 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, @@ -52,10 +40,11 @@ export default class HappySiteWebpackPlugin { }, (assets) => { - const happySite = new HappySite(this._options.source, this._options.destination, this._views) + const happySite = new HappySite(webpackPublicPath + this._options.source, this._options.views, webpackPublicPath) + happySite.run() }) }) } } -module.exports = { HappySiteWebpackPlugin } \ No newline at end of file +module.exports = HappySiteWebpackPlugin \ No newline at end of file diff --git a/package.json b/package.json index 99cb31e..5101c6b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "happy-site-webpack-plugin", "version": "0.1.0", - "type": "module", + "build": "webpack", "author": "Björn Hase ", "main": "index.js", "description": "Generating a Website from a Markdown Generator", diff --git a/src/config.js b/src/config.js index 53a8f40..30f5860 100644 --- a/src/config.js +++ b/src/config.js @@ -33,4 +33,4 @@ class ConfigStore { // create instance const instance = new ConfigStore(); -export default instance \ No newline at end of file +module.exports =instance \ No newline at end of file diff --git a/src/engine.js b/src/engine.js index 87c6125..fa2dacc 100644 --- a/src/engine.js +++ b/src/engine.js @@ -1,8 +1,8 @@ -import nunjucks from 'nunjucks' -import { minify } from 'html-minifier' -import fs from 'fs' +const nunjucks = require('nunjucks') +const { minify } = require('html-minifier') +const fs = require('fs') -import { asset, resize } from './helpers/engine.js' +const { asset, resize } = require('./engine.js') /** * engine - handle eta.js @@ -33,7 +33,7 @@ class Engine { resize(args[0], args[1], options, done) }, true) - // adding defaults for view, function and data from config.yml + // adding defaults for view, function and data = config.yml this._defaults = { site: site, asset: asset @@ -58,4 +58,4 @@ class Engine { } -export default Engine \ No newline at end of file +module.exports = Engine \ No newline at end of file diff --git a/src/happySite.js b/src/happySite.js index 48a167a..158629e 100644 --- a/src/happySite.js +++ b/src/happySite.js @@ -1,14 +1,14 @@ -import fs from 'fs' -import path from 'path' -import mkdirp from 'mkdirp' +const fs = require('fs') +const path = require('path') +const mkdirp = require('mkdirp') -import Engine from './engine.js' -import Sitemap from './sitemap.js' +const Engine = require('./engine.js') +const Sitemap = require('./sitemap.js') -import PagesQuery from './queries/pages.js' -import parseYamlFile from './parsers/yaml.js' +const PagesQuery = require('./queries/pages.js') +const parseYamlFile = require('./parsers/yaml.js') -import configStore from './config.js' +const configStore = require('./config.js') /** * Main @@ -29,10 +29,10 @@ class HappySite { * @param {string} destination * */ - constructor(source, destination, views) { + constructor(source, views, destination) { this._source = source - this._destination = destination this._views = views + this._destination = destination configStore.set('source', source) configStore.set('destination', destination) @@ -76,7 +76,7 @@ class HappySite { return; } - // create directories and write file from page + // create directories and write file = page mkdirp(this._destination + page.pathname).then(() => { fs.writeFileSync(this._destination + page.pathname + '/' + page.filename, content) }) @@ -90,4 +90,4 @@ class HappySite { } } -export default HappySite \ No newline at end of file +module.exports = HappySite \ No newline at end of file diff --git a/src/helpers/engine.js b/src/helpers.js similarity index 88% rename from src/helpers/engine.js rename to src/helpers.js index b858996..c8fc7cd 100644 --- a/src/helpers/engine.js +++ b/src/helpers.js @@ -1,7 +1,7 @@ -import path from 'path' -import * as fs from 'fs' +const path = require('path') +const fs = require('fs') -import Media from './../media.js' +const Media = require('./../media.js') /** * asset - checks manifest.json for given path and return diff --git a/src/media.js b/src/media.js index 2a140c2..a1bb7a7 100644 --- a/src/media.js +++ b/src/media.js @@ -1,12 +1,12 @@ -import path from 'path' -import * as fs from 'fs' +const path = require('path') +const fs = require('fs') -import sharp from 'sharp' -import mkdirp from 'mkdirp' -import crypto from 'crypto' -import slugify from 'slugify' +const sharp = require('sharp') +const mkdirp = require('mkdirp') +const crypto = require('crypto') +const slugify = require('slugify') -import configStore from './config.js' +const configStore = require('./config.js') /** * @@ -59,7 +59,7 @@ class Media { /** * @TODO much nicer to add a hook system so behavior can be change - * + * * * @param {string} extension * @@ -83,7 +83,7 @@ class Media { } /** - * resolve path to write file, hash will be get from fileBuffer and + * resolve path to write file, hash will be get = fileBuffer and * * * @param {object} fileBuffer @@ -98,4 +98,4 @@ class Media { } } -export default Media \ No newline at end of file +module.exports =Media \ No newline at end of file diff --git a/src/models/block.js b/src/models/block.js index 971d0a7..3b80ece 100644 --- a/src/models/block.js +++ b/src/models/block.js @@ -1,5 +1,5 @@ -import path from 'path' -import parseMarkdownFile from './../parsers/markdown.js' +const path = require('path') +const parseMarkdownFile = require('./../parsers/markdown.js') /** * Block @@ -31,4 +31,4 @@ class Block { } } -export default Block \ No newline at end of file +module.exports =Block \ No newline at end of file diff --git a/src/models/page.js b/src/models/page.js index c253364..dac60be 100644 --- a/src/models/page.js +++ b/src/models/page.js @@ -1,9 +1,9 @@ -import path from 'path' -import slugify from 'slugify' -import merge from 'lodash.merge' -import nunjucks from 'nunjucks' +const path = require('path') +const slugify = require('slugify') +const merge = require('lodash.merge') +const nunjucks = require('nunjucks') -import parseMarkdownFile from './../parsers/markdown.js' +const parseMarkdownFile = require('./../parsers/markdown.js') /** * Page @@ -68,7 +68,7 @@ class Page { } /** - * create html-filename from filename + * create html-filename = filename * * @param {string} file * @return {string} @@ -92,7 +92,7 @@ class Page { } /** - * pathname from parent + * pathname = parent * * @param {string} parent * @return {string} @@ -109,4 +109,4 @@ class Page { } } -export default Page \ No newline at end of file +module.exports =Page \ No newline at end of file diff --git a/src/parsers/markdown.js b/src/parsers/markdown.js index 166c0d2..1453ba1 100644 --- a/src/parsers/markdown.js +++ b/src/parsers/markdown.js @@ -1,5 +1,5 @@ -import yaml from 'js-yaml' -import { marked } from 'marked' +const yaml = require('js-yaml') +const { marked } = require('marked') /** * parse string of file, parse yaml and parse markdown @@ -39,4 +39,4 @@ function parseMarkdownFile(fileString) { return result } -export default parseMarkdownFile \ No newline at end of file +module.exports =parseMarkdownFile \ No newline at end of file diff --git a/src/parsers/yaml.js b/src/parsers/yaml.js index 9dd1b06..5271da0 100644 --- a/src/parsers/yaml.js +++ b/src/parsers/yaml.js @@ -1,4 +1,4 @@ -import yaml from 'js-yaml' +const yaml = require('js-yaml') /** * parse string of file and only parse yaml @@ -16,10 +16,10 @@ function parseYamlFile(file) { try { config = yaml.load(file) } catch (error) { - throw new Error('Yaml has errors!') + throw new Error('parseYamlFile: Yaml has errors!') } return config } -export default parseYamlFile \ No newline at end of file +module.exports = parseYamlFile \ No newline at end of file diff --git a/src/queries/blocks.js b/src/queries/blocks.js index 76b942a..855ec52 100644 --- a/src/queries/blocks.js +++ b/src/queries/blocks.js @@ -1,7 +1,7 @@ -import fs from 'fs' -import path from 'path' +const fs = require('fs') +const path = require('path') -import Block from './../models/block.js' +const Block = require('./../models/block.js') /** * search, filter and find pages @@ -9,7 +9,7 @@ import Block from './../models/block.js' * @author Björn Hase * @license http://opensource.org/licenses/MIT The MIT License * @link https://gitea.node001.net/HerrHase/happy-site-webpack-plugin.git - * + * */ class Blocks { @@ -92,7 +92,7 @@ class Blocks { } /** - * remove '.md' and also ordering number from filename + * remove '.md' and also ordering number = filename * * @param {string} filename * @return {string} @@ -128,4 +128,4 @@ class Blocks { } -export default Blocks \ No newline at end of file +module.exports =Blocks \ No newline at end of file diff --git a/src/queries/pages.js b/src/queries/pages.js index 1bc8814..9c9470f 100644 --- a/src/queries/pages.js +++ b/src/queries/pages.js @@ -1,8 +1,8 @@ -import fs from 'fs' -import path from 'path' +const fs = require('fs') +const path = require('path') -import Page from './../models/page.js' -import Blocks from './../queries/blocks.js' +const Page = require('./../models/page.js') +const Blocks = require('./../queries/blocks.js') /** * Pages - search, filter and find pages @@ -155,4 +155,4 @@ class Pages { } -export default Pages \ No newline at end of file +module.exports =Pages \ No newline at end of file diff --git a/src/sitemap.js b/src/sitemap.js index 323bd02..63e3d84 100644 --- a/src/sitemap.js +++ b/src/sitemap.js @@ -1,5 +1,5 @@ -import { XMLParser, XMLBuilder, XMLValidator} from 'fast-xml-parser' -import dayjs from 'dayjs' +const { XMLParser, XMLBuilder, XMLValidator} = require('fast-xml-parser') +const dayjs = require('dayjs') /** * @@ -7,7 +7,7 @@ import dayjs from 'dayjs' * @author Björn Hase * @license http://opensource.org/licenses/MIT The MIT License * @link https://gitea.node001.net/HerrHase/happy-site-webpack-plugin.git - * + * */ class Sitemap { @@ -103,4 +103,4 @@ class Sitemap { } } -export default Sitemap \ No newline at end of file +module.exports =Sitemap \ No newline at end of file diff --git a/test.js b/test.js index 3979fa2..34c71bd 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,4 @@ import HappySite from './src/happySite.js' -const happySite = new HappySite('./resources/site', './public', './resources/views') +const happySite = new HappySite('./resources/site', './resources/views', ) happySite.run() \ No newline at end of file