This repository has been archived by the owner on Jul 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwebpack.chrome.config.js
110 lines (90 loc) · 2.44 KB
/
webpack.chrome.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
const baseConfig = require('./webpack.base.config');
const {resolve} = require('path');
const {removeSync, copySync, outputFileSync} = require('fs-extra');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const packageJson = require('./package.json');
const manifestJson = require('./source/export/chrome/manifest.json');
const DIST_CHROME = resolve(__dirname, './dist/chrome');
const SOURCE_CHROME = resolve(__dirname, './source/export/chrome');
const DIST_CHROME_ZIP = resolve(__dirname, `./dist/chrome.zip`);
/**
* Copy images & manifest.json
*/
removeSync(DIST_CHROME);
copySync(resolve(__dirname, './source/img'), resolve(DIST_CHROME, 'img'));
const manifest = Object.assign({}, manifestJson, {
name: packageJson.fullName,
description: packageJson.description,
version: packageJson.version,
author: packageJson.author,
});
outputFileSync(resolve(DIST_CHROME, 'manifest.json'), JSON.stringify(manifest, null, 2));
/**
* Webpack config for Chrome Store
*/
const config = Object.assign({}, baseConfig, {
context: SOURCE_CHROME,
entry: {
'injector': './injector',
'ext': './ext',
},
output: {
path: DIST_CHROME,
filename: '[name].js',
// library: 'ext',
// libraryTarget: 'var',
},
});
config.module.rules.push({
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
url: false,
},
},
{
loader: 'postcss-loader',
},
],
}),
});
config.plugins.push(
new ExtractTextPlugin('[name].css')
);
/* archive plugin */
config.plugins.push(
{
apply: function(compiler) {
compiler.plugin('after-emit', (stats, cb) => {
zipDir(DIST_CHROME, DIST_CHROME_ZIP, cb);
});
},
}
);
function zipDir(dir, dest, callback) {
const fs = require('fs');
const archiver = require('archiver');
// create a file to stream archive data to.
const output = fs.createWriteStream(dest);
const archive = archiver('zip');
// listen for all archive data to be written
output.on('close', () => {
console.log('[Archive] Total size:', archive.pointer() + ' bytes');
callback();
});
// good practice to catch this error explicitly
archive.on('error', err => {
console.error('[Archive] Error:', err);
throw err;
});
// pipe archive data to the file
archive.pipe(output);
archive.directory(dir, '/');
// finalize the archive (ie we are done appending files but streams have to finish yet)
archive.finalize();
}
module.exports = config;