Skip to content

Instantly share code, notes, and snippets.

@kevinweber
Last active September 13, 2019 14:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinweber/82a871446fb63408834504d1f63abf77 to your computer and use it in GitHub Desktop.
Save kevinweber/82a871446fb63408834504d1f63abf77 to your computer and use it in GitHub Desktop.
AEM Front Webpack Plugin. Part of the AEM Front toolset. Learn more: https://kevinw.de/aem-front/
const path = require('path');
const AEMFrontPlugin = require(path.join(__dirname, './webpack.plugin.aem-front.js'));
//... your Webpack configuration ...
plugins: [
new AEMFrontPlugin({
exclude: '**/webpack.module/**',
watchDir: './../'
}),
],
//...
/**
Webpack plugin for AEM Front.
It pushes changes of bundled files while Webpack runs in watch mode.
Usage:
Install dependencies:
`npm install aem-front --save-dev`
Add plugin to Webpack configuration file:
```
const path = require('path');
const AEMFrontPlugin = require(path.join(__dirname, './webpack.plugin.aem-front.js'));
//...
plugins: [
new AEMFrontPlugin({
// Options (by default, AEM Front's default configuration is used)
exclude: '**/webpack.module/**',
watchDir: './../'
}),
],
//...
```
*/
const path = require('path');
const IS_WATCH_MODE = process.argv.indexOf('--watch') > -1;
const pathToAemFront = path.join(process.cwd(), 'node_modules', 'aem-front/bin/aem-front');
const defaults = {
command: null, // Use this command to override any other options, e.g. using `aem-front -w ./../`
exclude: null,
module: pathToAemFront, // Module used to run this command; should be path to AEM Front module
pushInterval: null,
startPage: null,
startBrowser: null,
targets: null,
watchDir: null
}
function AEMFrontPlugin(options) {
this.options = Object.assign(
defaults,
options
);
}
function spawnCommand(command) {
const exec = require('child_process').exec;
const spawn = require('child_process').spawn;
const ls = spawn(command, {
stdio: 'inherit',
shell: true,
});
ls.on('error', function (err) {
console.error(err);
});
}
function buildCommand(options) {
let command = options.module;
const args = [];
// Map options to arguments available for AEM Front
options.watchDir ? args.push('-w ' + options.watchDir) : '';
options.exclude ? args.push('-e ' + options.exclude) : '';
options.targets ? args.push('-t ' + options.targets) : '';
options.pushInterval ? args.push('-i ' + options.pushInterval) : '';
options.startPage ? args.push('-o ' + options.startPage) : '';
options.startBrowser ? args.push('-b ' + options.startBrowser) : '';
options.version ? args.push('-v ' + options.version) : '';
command += args.length > 0 ? ' ' + args.join(' ') : '';
return command;
}
AEMFrontPlugin.prototype.apply = function (compiler) {
const _options = this.options;
function watch() {
compiler.plugin('after-environment', function () {
spawnCommand(_options.command || buildCommand(_options));
});
}
if (IS_WATCH_MODE) {
watch();
}
};
module.exports = AEMFrontPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment