You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
webpack-plugin/index.js

47 lines
1.4 KiB

const HappySite = require('./src/happySite.js')
const fs = require('fs')
class HappySiteWebpackPlugin {
constructor(source, views, destination = null) {
this._options = {
source: source,
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!')
}
}
apply(compiler) {
const pluginName = HappySiteWebpackPlugin.name
// webpack module instance can be accessed from the compiler object,
// this ensures that correct version of the module is used
// (do not require/import the webpack or any symbols from it directly).
const { webpack } = compiler
// Compilation object gives us reference to some useful constants.
const { Compilation } = webpack
compiler.hooks.done.tap(pluginName, ({ compilation }) => {
// if destination is null, use from webpack configuration
if (!this._options.destination) {
this._options.destination = compilation.outputOptions.path
}
const happySite = new HappySite(this._options.source, this._options.views, this._options.destination)
happySite.run()
})
}
}
module.exports = HappySiteWebpackPlugin