-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
296 lines (278 loc) · 8.32 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var debug = require('debug')('pretzel-assets');
var webpack = require('webpack');
var AssetsPlugin = require('assets-webpack-plugin');
function allFiles(dir, extension) {
return path.join(dir, '**', '*', (extension ? extension : ''));
}
function getEntryPoints(src) {
var entriesDirPath = path.join(src, 'entries');
var entries = {};
var buildEntries = function(dirPath, kind) {
return function(filename) {
var file = fs.statSync(filename);
if (file.isDirectory()) { return; }
var name = filename.replace(dirPath, '');
var extension = path.extname(name);
var chunkName = name;
if (extension) {
// find extention, from: '.js' to: /\.js$/i
var regex = new RegExp('\\' + extension + '$', 'i');
chunkName = chunkName.replace(regex, '');
}
entries[kind + chunkName] = './' + kind + name; // "./" at the start is very important, don't use `path.join()`
};
};
if (src) {
// glob.sync(allFiles(path.join(src, 'stylesheets'))).forEach(buildEntries(path.join(src, 'stylesheets'), 'stylesheets/'));
// might be useful to compile css with webpack
glob.sync(allFiles(entriesDirPath)).forEach(buildEntries(entriesDirPath, 'entries'));
}
debug(entries);
return entries;
}
function getPublicDirecories(topLevelDirecotries){
if ( ! topLevelDirecotries) {
return [];
}
var publicDirecotries = [];
topLevelDirecotries.forEach(function(topLevelDirecotry){
glob.sync(path.join(topLevelDirecotry, '**', '*')).forEach(function(filename) {
var file = fs.statSync(filename);
if (file.isDirectory() && /public$/.test(filename)) {
publicDirecotries.push(filename);
}
});
});
return publicDirecotries;
}
function getVendorEntryPoints(vendors) {
if ( ! vendors) { return {}; }
var entries = {};
Object.keys(vendors).forEach((vendor) => {
entries[vendor] = Object.keys(vendors[vendor]);
});
return entries;
}
function mergedVendorMoudle(vendors) {
if ( ! vendors) { return; }
var merged = {};
Object.keys(vendors).forEach((vendor) => {
Object.keys(vendors[vendor]).forEach((vendorModule) => {
if ( ! vendors[vendor][vendorModule]) { return; }
merged[vendorModule] = vendors[vendor][vendorModule];
});
});
return merged;
}
function getMinifyPlugin() {
return new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
minimize: true,
output: {
comments: false,
},
});
}
function getPlugins(config, shouldMinify, manifestData) {
var plugins = [];
/*
maybe useful later
if (config.plugins.indexOf('extractText') !== -1) {
plugins.push(new ExtractTextPlugin('[name].css'));
}
*/
if (shouldMinify && config.plugins.indexOf('minify') !== -1) {
plugins.push(getMinifyPlugin());
}
if (manifestData && config.plugins.indexOf('manifest') !== -1) {
plugins.push(new AssetsPlugin({
update: true,
filename: 'manifest-entries+dll.json',
path: config.path.dest,
prettyPrint: true,
}));
}
plugins.push(new webpack.DefinePlugin({
MANIFEST_FILES: manifestData
}));
Object.keys(config.dll).forEach((vendor) => {
var jsonDllPath = path.join(config.path.dest, 'dll', vendor + '.json');
try {
fs.accessSync(jsonDllPath);
plugins.push(new webpack.DllReferencePlugin({
context: '.',
manifest: require(jsonDllPath)
}));
} catch(e) {
console.warn('File not found, skiping DllReferencePlugin for "' + vendor + '"');
console.warn(jsonDllPath);
}
});
return plugins;
}
function getJsFilename(hashedName, digestLength){
if (hashedName) {
return '[name].[chunkhash:' + digestLength + '].js';
}
else {
return '[name].js';
}
}
module.exports = function(options) {
var config = Object.assign({
digestLength: 8,
dll: {},
path: {},
loaders: [],
plugins: ['minify', 'manifest']
}, options);
config.dev = Object.assign({
port: 4010,
noLog: true,
lazy: true,
sourceMap: 'cheap-module-source-map',
}, options.dev);
var normalizedPublicPath = path.join(config.path.public);
return {
getDllConfig: (o) => {
o = o || {};
return {
devtool: 'source-map',
entry: getVendorEntryPoints(config.dll),
output: {
filename: getJsFilename(o.hashedName, config.digestLength),
path: path.join(config.path.dest, 'dll'),
publicPath: normalizedPublicPath,
libraryTarget: 'var',
library: '[name]_lib',
},
resolve: {
extensions: config.extensions,
alias: mergedVendorMoudle(config.dll),
},
plugins: [
getMinifyPlugin(),
new webpack.DllPlugin({
path: path.join(config.path.dest, 'dll', '[name].json'),
name: '[name]_lib',
}),
new AssetsPlugin({
update: true,
filename: 'manifest-entries+dll.json',
path: config.path.dest,
processOutput: function (assets) {
var dllMainfest = {};
Object.keys(assets).forEach((key) => {
var dllKey, dllValue;
if (key.substr(0, 4) === 'dll/') {
dllKey = key;
dllValue = path.join(
normalizedPublicPath,
assets[key].js.replace(normalizedPublicPath, '')
);
}
else {
dllKey = path.join('dll', key);
dllValue = path.join(
normalizedPublicPath,
'dll',
assets[key].js.replace(normalizedPublicPath, '')
);
}
dllMainfest[dllKey] = {
js: dllValue,
};
});
return JSON.stringify(dllMainfest, null, 2);
}
}),
],
};
},
getEntryConfig: (o) => {
o = o || {};
return {
devtool: o.sourceMap,
module: {
rules: config.loaders,
},
context: config.path.src,
entry: getEntryPoints(config.path.src, 'entries'),
output: {
filename: getJsFilename(o.hashedName, config.digestLength),
path: config.path.dest,
publicPath: normalizedPublicPath,
},
resolve: {
symlinks: false,
extensions: config.extensions,
alias: mergedVendorMoudle(config.dll),
},
plugins: getPlugins(config, o.shouldMinify, o.manifestData),
};
},
getDevEntryConfig: (entriesConfig) => {
var devConfigs = Object.keys(entriesConfig.entry).map((entry) => {
var newEntry = {};
newEntry[entry] = entriesConfig.entry[entry];
return {
compiler: Object.assign({}, entriesConfig, {entry: newEntry})
};
});
devConfigs.forEach((devConfig) => {
var firstAndOnlyEntryPath = Object.keys(devConfig.compiler.entry)[0];
devConfig.server = {
publicPath: entriesConfig.output.publicPath,
lazy: config.dev.lazy,
noInfo: config.dev.noLog,
filename: new RegExp(firstAndOnlyEntryPath),
stats: {
hash: false,
cached: false,
cachedAssets: false,
colors: true,
},
};
});
return devConfigs;
},
getDigestLength: () => {
return config.digestLength;
},
getDestinationDirectory: () => {
return config.path.dest;
},
getPublicPrefix: () => {
return normalizedPublicPath;
},
getDevConfig: () => {
return config.dev;
},
getPathConfig: () => {
return {
public: normalizedPublicPath,
port: config.dev.port,
};
},
getSassConfig: (o) => {
o = o || {};
var hasSourceMap = !!o.sourceMap;
return {
sourceMap: hasSourceMap,
sourceMapEmbed: hasSourceMap,
sourceMapContents: hasSourceMap,
src: path.join(config.path.src, 'stylesheets'),
dest: path.join(config.path.dest, 'stylesheets'),
prefix: path.join(config.path.public, 'stylesheets'),
indentedSyntax: true,
noLog: config.dev.noLog,
};
},
getFileDirecories: () => {
return getPublicDirecories(config.publicFileDirecories);
},
};
};