forked from auth0/lock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
169 lines (161 loc) · 4.57 KB
/
Gruntfile.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
'use strict';
const path = require('path');
const fs = require('fs');
const tmp = require('tmp');
const pkg = require('./package');
const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');
const UnminifiedWebpackPlugin = require('unminified-webpack-plugin');
const { spawnSync } = require('child_process');
/**
* This is a helper function to generate valid certs using mkcert.
* If mkcert is not installed it will return false.
*/
function getDevCerts() {
let result = false;
const tmpDir = tmp.dirSync({ unsafeCleanup: true, prefix: 'lock-dev-' });
try {
spawnSync('mkcert', ['localhost'], { cwd: tmpDir.name });
result = {
key: fs.readFileSync(path.join(tmpDir.name, 'localhost-key.pem')),
cert: fs.readFileSync(path.join(tmpDir.name, 'localhost.pem'))
};
} catch (err) {}
tmpDir.removeCallback();
return result;
}
module.exports = function(grunt) {
const pkg_info = grunt.file.readJSON('package.json');
grunt.initConfig({
pkg: pkg_info,
clean: {
build: ['build/'],
dev: ['build/'],
dist: ['lib/']
},
babel: {
dist: {
files: [
{
expand: true,
cwd: 'src',
src: ['**/*.js', '**/*.jsx'],
dest: 'lib',
ext: '.js'
}
]
}
},
env: {
build: {
NODE_ENV: 'production'
}
},
exec: {
touch_index: 'touch src/index.js'
},
webpack: {
options: webpackConfig,
build: {
devtool: 'source-map',
output: {
path: path.join(__dirname, 'build'),
filename: 'lock.min.js'
},
watch: false,
plugins: [
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false
}),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: { warnings: false, screw_ie8: true },
sourceMap: true,
comments: false
}),
new UnminifiedWebpackPlugin(),
new webpack.BannerPlugin({
raw: false,
entryOnly: true,
banner: `lock v${pkg_info.version}\n\nAuthor: ${
pkg_info.author
}\nDate: ${new Date().toLocaleString()}\nLicense: ${pkg_info.license}\n`
})
]
}
},
'webpack-dev-server': {
options: {
webpack: webpackConfig,
publicPath: '/build/'
},
dev: {
hot: true,
port: 3000,
https: getDevCerts() || true,
webpack: {
devtool: 'source-map'
}
},
design: {
webpack: {
entry: './support/design/index.js',
output: {
path: path.join(__dirname, 'build'),
filename: 'lock.design.js'
},
devtool: 'cheap-module-source-map'
}
}
},
i18n: {
build: {
files: [
{
expand: true,
cwd: 'lib/i18n',
src: '**/*.js',
dest: 'build',
ext: '.js'
}
]
}
}
});
grunt.loadNpmTasks('grunt-babel');
grunt.loadNpmTasks('grunt-webpack');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-env');
grunt.loadNpmTasks('grunt-exec');
grunt.registerTask('build', ['clean:build', 'env:build', 'webpack:build', 'i18n:build']);
grunt.registerTask('dist', ['clean:dist', 'babel:dist']);
grunt.registerTask('prepare_dev', ['clean:dev']);
grunt.registerTask('dev', ['prepare_dev', 'webpack-dev-server:dev']);
grunt.registerTask('design', ['prepare_dev', 'webpack-dev-server:design']);
grunt.registerMultiTask('i18n', 'Prepares i18n files to be hosted in CDN', function() {
var languages = {};
var Auth0 = {
registerLanguageDictionary: function(lang, dict) {
languages[lang] = dict;
}
};
this.files.forEach(function(file) {
var filename = file.src[0];
var lang = path.basename(filename, '.js');
var dict = require('./' + filename).default || require('./' + filename);
var jsonp = `Auth0.registerLanguageDictionary("${lang}", ${JSON.stringify(dict)});`;
eval(jsonp.toString());
if (languages[lang] === undefined) {
grunt.fail.fatal(`Failed to process language file ${filename}`);
}
grunt.file.write(file.dest, jsonp);
grunt.log.ok(`Built ${lang}.js file`);
});
});
};