-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
80 lines (67 loc) · 1.81 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
const mix = require('laravel-mix');
const fs = require('fs');
const escapeStringRegexp = require('escape-string-regexp');
const path = require('path');
class Modernizr {
/**
* All dependencies that should be installed by Mix.
*
* @return {Array}
*/
dependencies() {
return ['webpack-modernizr-loader', 'escape-string-regexp'];
}
/**
* Register the component.
*
* @param {Object} configFiles One or more Modernizr config files
* @return {void}
*/
register(configFiles) {
// Set default identifier if none provided
if (typeof configFiles === 'undefined') configFiles = { Modernizr: '.modernizrrc' };
// Holds file filter regex strings
let regexStrings = [];
// Webpack variables
let autoload = {};
let alias = {};
// Iterate over every provided config file
Object.keys(configFiles).forEach(key => {
let file = configFiles[key];
// Create config file if it doesn't exist
if (!fs.existsSync(file)) {
fs.writeFile(file, ('module.exports = ' + JSON.stringify({
"options": [],
"feature-detects": []
}, null, 2) + ';'), console.error);
}
// Add escaped file name to regex strings
regexStrings.push(escapeStringRegexp(file));
// Set alias and autoload for this build
alias[key] = path.resolve(configFiles[key]);
autoload[key] = key;
});
// Set Webpack test
this.test = new RegExp(regexStrings.join('|'));
// Add aliases for Modernizr builds so they can be called by the provided identifier
mix.webpackConfig({
resolve: {
alias: alias
}
});
// Register autoloading of Modernizr builds
mix.autoload(autoload);
}
/**
* Rules to be merged with the master webpack loaders.
*
* @return {Object}
*/
webpackRules() {
return {
test: this.test,
loader: 'webpack-modernizr-loader'
}
}
}
mix.extend('modernizr', new Modernizr());