-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.common.config.js
129 lines (111 loc) · 3.46 KB
/
webpack.common.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
const path = require("path");
const ReplaceInFileWebpackPlugin = require("replace-in-file-webpack-plugin");
// This files contains common webpack code that can be potentially
// reused by other contributions
// All build files for a component are generated inside
// a dedicated folder within the distribution folder
const GetTargetPath = () => {
return path.resolve("./dist");
}
// Following webpack settings are common to all components
// that are targeted to run in Node environments
const FillDefaultNodeSettings = (c, env, component) => {
c.target = "node";
// Do not minimize files by default because non-minimized
// files are easier to read and hence easier to debug
c.optimization = { minimize: false };
// Webpack overrides "__dirname" to "/" by default. We need the behavior
// to be similar to Node's behavior where it refers to the current working directory
c.node = { __dirname: false };
c.module = { rules: [ TsLoaderRule ] };
c.output = {
filename: "[name].js",
path: path.join(GetTargetPath(), component)
}
c.mode = "production";
c.resolve = {
modules: ["node_modules"],
extensions: [".ts", ".js"],
};
c.devtool = "inline-source-map";
console.log(`Component: ${component}`);
console.log(`Env: ${env}`);
return c;
};
// This replacement is required to bypass the loading for package.json
// by azure-devops-node-api library for reading library version. This
// directly injects the version as text.
const PackageJsonLoadFixer = (dir, files) => {
return new ReplaceInFileWebpackPlugin([
{
dir,
files,
rules: [
{
search: /JSON\.parse.{0,50}package\.json.{0,20}version/,
replace: '"7.2.0"'
}
]
}
]);
};
// This helps in obtaining a timestamp based version string for local development.
const VersionStringReplacer = (dir, files) => {
const now = new Date();
const major = `1`;
const minor = `${Pad(now.getFullYear(), 4)}${Pad(now.getMonth(), 2)}${Pad(now.getDate(), 2)}`;
const patch = `${Pad(now.getSeconds() + (60 * (now.getMinutes() + (60 * now.getHours()))), 5)}`;
const version =`${major}.${minor}.${patch}`;
console.log(`Version: ${version}`);
return new ReplaceInFileWebpackPlugin([
{
dir,
files,
rules: [
{
search: /{{version}}/ig,
replace: version
},
{
search: /({{major}}|\"{{major}}\")/ig,
replace: major
},
{
search: /({{minor}}|\"{{minor}}\")/ig,
replace: minor
},
{
search: /({{patch}}|\"{{patch}}\")/ig,
replace: patch
}
]
}
]);
};
function Pad(text, width, z = '0')
{
text = text + ''; // convert to string
while(text.length < width) {
text = z + text;
}
return text;
}
const TsLoaderRule = {
test: /\.ts?$/,
exclude: /node_modules/,
enforce: "pre",
use: [
{
loader: "ts-loader",
options: {
configFile: "tsconfig.json"
}
}
]
};
module.exports = {
GetTargetPath,
FillDefaultNodeSettings,
PackageJsonLoadFixer,
VersionStringReplacer
}