-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
114 lines (98 loc) · 2.98 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const path = require('path');
const glob = require('glob');
const mix = require('laravel-mix');
const MjmlPlugin = require('./plugin');
const File = require('laravel-mix/src/File')
class Mjml {
constructor() {
this.toCompile = [];
}
/**
* The API name for the component.
*/
name() {
return 'mjml';
}
/**
* Required dependencies for the component.
*/
dependencies() {
return ['mjml'];
}
/**
* Register the component.
*
* @param {String} entry
* @param {String} output
* @param {Object} options
*/
register(entry = 'resources/mail', output = 'resources/views/mail', options = {}) {
options.sourceRoot = new File(this.findSourceRoot(entry)).relativePath();
if (entry.includes('*')) {
entry = glob.sync(entry);
} else if (! /\.mjml$/.test(entry)) {
entry = glob.sync(path.join(entry, '**/*.mjml'));
}
options.extension = options.extension || '.blade.php';
options.mjmlOptions = options.mjmlOptions || {};
output = new File(output);
this.toCompile.push(
...[].concat(entry).map(file => this.buildToCompileEntry(new File(file), output, options))
);
}
/**
* Webpack plugins to be appended to the master config.
*/
webpackPlugins() {
return new MjmlPlugin(this.toCompile);
}
/**
* Build a toCompile entry for the given entry/output files.
*
* @param {File} entry
* @param {File} output
* @param {Object} options
*/
buildToCompileEntry(entry, output, options) {
const result = {
entry: entry.path(),
mjmlOptions: Object.assign({
filePath: entry.path()
}, options.mjmlOptions),
};
if (! output.isDirectory()) {
result.output = this.relativeToPublicPath(output.relativePath());
} else {
result.output = this.relativeToPublicPath(
entry.relativePath()
.replace(`${options.sourceRoot}`, `${output.relativePath()}`)
.replace(/\.mjml$/, options.extension)
);
}
return result;
}
/**
* Find the common source root for the entry path.
*
* @param {String} entry
*/
findSourceRoot(entry) {
if (! entry.includes('*')) {
return /\.mjml$/.test(entry) ? path.dirname(entry) : entry;
}
const segments = entry.replace(/\\/g, '/').split('/');
return segments.slice(0, segments.findIndex(segment => segment.includes('*'))).join('/');
}
/**
* Make the given output path path relative to the public path.
*
* @param {String} outputPath
*/
relativeToPublicPath(outputPath) {
return path.join(
path.relative(Mix.paths.root(Config.publicPath), Mix.paths.root()),
outputPath.replace(/\\/g, '/')
);
}
}
mix.extend('mjml', new Mjml);