-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebpack.production.js
More file actions
89 lines (80 loc) · 2.42 KB
/
webpack.production.js
File metadata and controls
89 lines (80 loc) · 2.42 KB
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
const fs = require('fs-extra')
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
const archiver = require('archiver');
const branchName = require('current-git-branch');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const LAMBDA_NAME = 'AtfAvailabilityFunction';
const OUTPUT_FOLDER = './dist'
const REPO_NAME = 'hvt-atf-availability';
const BRANCH_NAME = branchName().replace("/","-");
class BundlePlugin {
constructor(params) {
this.archives = params.archives;
this.assets = params.assets;
}
apply(compiler) {
compiler.hooks.afterEmit.tap('zip-pack-plugin', async (compilation) => {
this.archives.forEach(async (archive) => {
await this.createArchive(archive.inputPath, archive.outputPath, archive.outputName, archive.ignore);
})
this.assets.forEach((asset) => {
fs.copySync(asset.inputPath, asset.outputPath);
})
});
}
createArchive(inputPath, outputPath, outputName, ignore) {
if (!fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath)
}
const output = fs.createWriteStream(`${outputPath}/${outputName}.zip`);
const archive = archiver('zip');
output.on('close', function () {
console.log(archive.pointer() + ' total bytes');
console.log('archiver has been finalized and the output file descriptor has closed.');
});
archive.on('error', function(err){
throw err;
});
archive.pipe(output);
archive.glob(
`**/*`,
{
cwd: inputPath,
skip: ignore
}
);
return archive.finalize();
}
}
module.exports = env => {
let commit = env ? env.commit ? env.commit : 'local' : 'local' ;
return merge(common, {
mode: 'production',
plugins: [
new BundlePlugin({
archives: [
{
inputPath: `.aws-sam/build/${LAMBDA_NAME}`,
outputPath: `${OUTPUT_FOLDER}`,
outputName: `${REPO_NAME}-${BRANCH_NAME}-${commit}`,
ignore: ['public']
}
],
assets: [
{
inputPath: `./.aws-sam/build/${LAMBDA_NAME}/public`,
outputPath: `${OUTPUT_FOLDER}/${REPO_NAME}-cloudfront-assets-${BRANCH_NAME}-${commit}`,
}
]
}),
],
optimization: {
minimizer: [
new CssMinimizerPlugin(),
new TerserPlugin(),
],
},
});
}