Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from tdeNL/fix/compatibility_issues
Browse files Browse the repository at this point in the history
Fix compatibility issues
  • Loading branch information
jordikroon authored Apr 30, 2018
2 parents 5ef662f + fb05673 commit 68f64e7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 58 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tde-webpack-mjml-plugin",
"version": "1.1.1",
"version": "1.2.0",
"description": "Webpack MJML plugin for converting MJML files to a given location",
"main": "index.js",
"repository": "https://github.com/tdeNL/tde-webpack-mjml-plugin",
Expand Down
118 changes: 61 additions & 57 deletions src/webpack-mjml-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const _ = require('lodash');
* @type {{extension: string, outputPath: string}}
*/
const defaultOptions = {
extension: '.html',
outputPath: ''
extension: '.html',
outputPath: ''
};

/**
Expand All @@ -19,60 +19,64 @@ const defaultOptions = {
* @constructor
*/
const WebpackMjmlStore = function (inputPath, options) {
this.inputPath = inputPath;
this.options = _.defaults(options, defaultOptions);
this.inputPath = inputPath;
this.options = _.defaults(options, defaultOptions);
};

/**
* @param compiler
*/
WebpackMjmlStore.prototype.apply = function (compiler) {
let that = this;

compiler.plugin('emit', function (compilation, callback) {
fs.existsSync(that.options.outputPath) || fs.mkdirSync(that.options.outputPath);

glob(that.inputPath + '/**/*.mjml', function (err, files) {
if ( !files.length ) {
return callback();
}

for (let fileKey in files) {
let file = files[fileKey];
compilation.fileDependencies.push(file);

let outputFile = file
.replace(that.inputPath, that.options.outputPath)
.replace('.mjml', that.options.extension);

that.convertFile(file)
.then((contents) => that.ensureFileExists(outputFile, contents))
.then((contents) => that.writeFile(outputFile, contents))
.then(callback());
}
});
let that = this;

compiler.plugin('emit', function (compilation, callback) {
fs.ensureDirSync(that.options.outputPath);
glob(that.inputPath + '/**/*.mjml', function (err, files) {
if (!files.length) {
return callback();
}

for (let fileKey in files) {
let file = files[fileKey];

if (compilation.fileDependencies.add) {
compilation.fileDependencies.add(file);
} else {
compilation.fileDependencies.push(file);
}

let outputFile = file
.replace(that.inputPath, that.options.outputPath)
.replace('.mjml', that.options.extension);

that.convertFile(file)
.then((contents) => that.ensureFileExists(outputFile, contents))
.then((contents) => that.writeFile(outputFile, contents))
.then(callback());
}
});
});
};

/**
* @param file
* @returns {Promise}
*/
WebpackMjmlStore.prototype.convertFile = function (file) {
return new Promise (function(resolve, reject) {
fs.readFile(file, 'utf8', function (err, contents) {
let response = mjmlEngine.mjml2html(contents);
if (response.errors.length) {
console.log('\x1b[36m', 'MJML Warnings in file "' + file + '":', '\x1b[0m');
}

for (let errorKey in response.errors) {
console.log(" - ", response.errors[errorKey].formattedMessage);
}

resolve(response.html);
});
return new Promise(function (resolve, reject) {
fs.readFile(file, 'utf8', function (err, contents) {
let response = mjmlEngine.mjml2html(contents);
if (response.errors.length) {
console.log('\x1b[36m', 'MJML Warnings in file "' + file + '":', '\x1b[0m');
}

for (let errorKey in response.errors) {
console.log(" - ", response.errors[errorKey].formattedMessage);
}

resolve(response.html);
});
});
};

/**
Expand All @@ -81,15 +85,15 @@ WebpackMjmlStore.prototype.convertFile = function (file) {
* @returns {Promise}
*/
WebpackMjmlStore.prototype.writeFile = function (file, contents) {
return new Promise (function(resolve, reject) {
fs.writeFile(file, contents, function (err) {
if (err) {
throw err;
}

resolve(true);
});
return new Promise(function (resolve, reject) {
fs.writeFile(file, contents, function (err) {
if (err) {
throw err;
}

resolve(true);
});
});
};

/**
Expand All @@ -98,15 +102,15 @@ WebpackMjmlStore.prototype.writeFile = function (file, contents) {
* @returns {Promise}
*/
WebpackMjmlStore.prototype.ensureFileExists = function (file, contents) {
return new Promise(function (resolve, reject) {
fs.ensureFile(file, function (err) {
if (err) {
throw err;
}

resolve(contents);
});
return new Promise(function (resolve, reject) {
fs.ensureFile(file, function (err) {
if (err) {
throw err;
}

resolve(contents);
});
});
};

/**
Expand Down

0 comments on commit 68f64e7

Please sign in to comment.