-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
260 lines (217 loc) · 6.52 KB
/
webpack.config.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
var fs = require("fs");
var path = require("path");
var _ = require("lodash");
var beautify = require("js-beautify").js_beautify
var webpack = require("webpack");
var utils = require("mykoop-utils");
var moduleManager = require("./modules/backend/moduleManager");
var logger = require("mykoop-logger")(module);
//hijack require to parse json5
require('json5/lib/require');
var configs = require("./modules/common/mykoop-config.json5");
var getModulesDefinitions = require("./modules/backend/getModulesDefinitions");
var isDev = utils.__DEV__;
var isProd = utils.__PROD__;
var pluginList = [
new webpack.DefinePlugin({
"__SERVICE_URL__": "\"" + configs.serviceUrl + "\"",
"__DEV__": isDev,
"__PROD__": isProd,
"process.__DEV__": isDev,
"process.__PROD__": isProd,
"process.browser": true
}),
// Only keep English or French locales from moment.
//FIXME: Have this be done dynamically based on available languages.
new webpack.ContextReplacementPlugin(
/moment[\/\\]locale$/,
/fr|en/
),
// This might seem like a wide net, but we don't want anything that has to do
// with jQuery, by default anyway.
new webpack.IgnorePlugin(/jquery/)
];
if (isProd) {
// Minify and optimize assets. (JS, CSS)
// This can take a couple of seconds, only use for
// the production bundles.
pluginList.push(new webpack.optimize.UglifyJsPlugin({
compress: {
drop_console: true
}
}));
}
var modulesDefinitions = getModulesDefinitions({
excludes: configs.mykoopModuleExcludeList,
searchNodeModules: configs.mykoopLoadModuleFromNode,
path: path.resolve(configs.mykoopModulesList || "")
});
moduleManager.loadModules(modulesDefinitions);
var loadedModules = moduleManager.getLoadedModulePairings();
/* Generate dynamic LESS imports for the global scope. */
var lessGlobalStyles = _.reduce(loadedModules, function(lessStyles, moduleName) {
var stylePath = moduleName + "/styles/index.less";
try {
require.resolve(stylePath);
} catch(e) {
// Abort trying to load styles for this module.
return lessStyles;
}
lessStyles.push("@import \"~" + stylePath + "\";");
return lessStyles;
}, []);
fs.writeFileSync(
"./styles/dynamic-global-styles.less",
lessGlobalStyles.join("\n")
);
var styleLoaders = [
"style",
"css",
"autoprefixer?" + {
"browsers": [
"last 2 versions",
"ie 8",
"ie 9",
"android 2.3",
"android 4",
"opera 12"
]
},
"less"
];
var lessLoader = {
test: /\.less$/,
exclude: /\.useable\.less$/,
loaders: _.clone(styleLoaders)
};
styleLoaders[0] += "/useable";
var lessUseableLoader = {
test: /\.useable\.less$/,
loaders: styleLoaders
};
var aliases = _.reduce(loadedModules, function(aliases, moduleName, moduleRole) {
var modulePath;
try {
modulePath = require.resolve(moduleName);
} catch(e) {
// Abort trying to load the alias for this module.
return aliases;
}
if (moduleName !== moduleRole) {
aliases[moduleRole] = moduleName;
}
return aliases;
}, {
"bootstrap-styles": "bootstrap/less",
"font-awesome-styles": "font-awesome/less",
"react-calendar-styles": "react-calendar/less",
"i18next": "i18next-client"
});
/* Generate a temporary file with the meta data. */
dynamicMetadataFileContent = "/** DYNAMICALLY GENERATED - DO NOT EDIT **/\n\n";
var metaData;
moduleManager.getMetaData(function (err, modulesMetaData) {
metaData = modulesMetaData;
});
function generateIntermediaryRequires(obj) {
if (_.isPlainObject(obj)) {
if (
obj.hasOwnProperty("__metadata__origin__")
) {
// Consider this a leaf that needs to be resolved.
var requireString = "__require(";
requireString += obj.__metadata__origin__ + ")";
if (obj.hasOwnProperty("__metadata__property__")) {
requireString += "." + obj.__metadata__property__;
}
requireString += "__";
return requireString;
}
_.forEach(obj, function (value, key) {
obj[key] = generateIntermediaryRequires(value);
});
}
return obj;
}
var metaDataString;
metaData = generateIntermediaryRequires(metaData);
try{
metaDataString = JSON.stringify(metaData);
} catch(e) {
logger.error("Invalid intermediary meta data, couldn't generate dynamic dependencies.");
}
if (metaDataString) {
metaDataString = beautify(metaDataString, {indent_size: 2});
metaDataString = metaDataString.replace(
/\"__require\((.*?)\)(\..*?)?__\"/g,
"function() { return require(\"$1\")$2; }"
);
dynamicMetadataFileContent += "module.exports = " + metaDataString + ";\n";
fs.writeFileSync(
"./modules/frontend/dynamic-metadata.js",
dynamicMetadataFileContent
);
}
var loaderList = [
// Styles.
lessLoader,
lessUseableLoader,
// Fonts.
// See: https://github.com/webpack/webpack/issues/460
{ test: /\.woff($|\?)/, loader: "url?prefix=fonts/&limit=5000&mimetype=application/font-woff" },
{ test: /\.ttf($|\?)/, loader: "file?prefix=fonts/" },
{ test: /\.eot($|\?)/, loader: "file?prefix=fonts/" },
{ test: /\.svg($|\?)/, loader: "file?prefix=fonts/" },
// Images.
{ test: /\.(png|jpg|gif)$/, loader: "url-loader?limit=8192"},
// JSON files (Mostly used for languages).
{ test: /\.json$/, loader: "json" },
{ test: /\.json5$/, loader: "json5" },
// JSX (for React)
{ test: /\.js$/, loader: "jsx" },
{ test: /\.jsx$/, loader: "jsx?insertPragma=React.DOM" },
// TypeScript.
{ test: /\.ts$/, loader: "ts" },
];
if (isDev) {
// Expose React in the global space so we can use the React chrome Dev Tools.
loaderList.push({
test: require.resolve("react"), loader: "expose?React"
});
}
module.exports = {
// Where to look for the entry points.
context: path.join(__dirname, "entry"),
entry: {
"mykoop" : "./mykoop",
},
output: {
path: path.join(__dirname, "public/"),
filename: "[name].bundle.js",
chunkFilename: "[id].chunk.js",
publicPath: configs.assetsUrl + "/"
},
module: {
loaders: loaderList,
// FIXME:: Webpack must not parse this module because cannot be statically
// analysed
noParse: /validate\.js/
},
resolve: {
extensions: ["", ".webpack.js", ".web.js", ".js", ".ts", ".jsx"],
root: [
path.join(__dirname, "styles"),
path.join(__dirname, "locales"),
path.join(__dirname, "modules", "frontend"),
path.join(__dirname, "modules", "common")
],
modulesDirectories: [
"node_modules",
"bower_components"
],
// Map the modules to the files we really need, for hassle-free inclusion
// in our files.
alias: aliases
},
plugins: pluginList
};