-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
111 lines (106 loc) · 3.48 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
const path = require("path");
const webpack = require("webpack");
const fs = require("fs");
// Shot package name will be used for prefix of filename
const fnPrefix = JSON.parse(fs.readFileSync(path.resolve(__dirname, "package.json"), "utf-8")).name.replace("grimoirejs", "grimoire");
// Copy task for optimization
class CopyPlugin {
constructor(copyFrom, copyTo) {
this.copyFrom = copyFrom;
this.copyTo = copyTo;
}
apply(compiler) {
compiler.plugin("after-emit", (compiler, callback) => {
fs.createReadStream(this.copyFrom).pipe(fs.createWriteStream(this.copyTo));
callback();
});
}
}
// Couldron generate-expose and generate-reference execute with build tasks.
class CauldronPlugin {
apply(compiler) {
compiler.plugin("compile", () => {
try {
console.log(require('child_process').execSync('npm run generate-expose').toString());
} catch (e) {
console.log(e);
}
});
compiler.plugin('after-emit', (compiler, callback) => {
try {
console.log(require('child_process').execSync('npm run generate-reference').toString());
} catch (e) {
console.log(e);
} finally {
callback();
}
});
}
}
const getBuildTask = (fileName, plugins, needPolyfill) => {
return {
cache: true,
entry: needPolyfill ? ['babel-polyfill', path.resolve(__dirname, "src/index.ts")] : path.resolve(__dirname, "src/index.ts"),
output: {
path: __dirname,
filename: "./register/" + fileName,
libraryTarget: "umd"
},
module: {
loaders: [{
test: /\.ts$/,
exclude: /node_modules/,
loader: "babel-loader?presets[]=es2015,presets[]=stage-2!ts-loader"
}, {
test: /\.png$/,
loader: 'url-loader'
}]
},
resolve: {
extensions: ['.ts', '.js']
},
plugins: plugins,
devtool: 'source-map'
}
};
module.exports = (env) => {
let includeCore = false;
// if this package was preset including core or core package,
// Script for browser needs babel-polyfill
let polyfills = includeCore ? [true, false, true] : [false, false, false];
env = env || {};
let buildTasks = [];
let isDefault = !env.browser && !env.npm && !env.prod;
let skipIndex = false;
let cauldron = new CauldronPlugin();
if (env.browser || env.prod) {
const plugins = [cauldron];
// if needs index also and it was not including core, index.js must be copied from fnPrefix.js
// since these are completely same build task. Yeah, optimization.
if (!includeCore && (env.prod || env.npm)) {
plugins.push(new CopyPlugin(`./register/${fnPrefix}.js`, './register/index.js'));
plugins.push(new CopyPlugin(`./register/${fnPrefix}.js.map`, './register/index.js.map'));
skipIndex = true;
console.log('index.js would be copied from original.');
}
buildTasks.push(getBuildTask(`${fnPrefix}.js`, plugins, polyfills[0]));
}
if (!skipIndex && (isDefault || env.npm || env.prod)) {
console.log('index.js would be generated separately');
buildTasks.push(getBuildTask("index.js", [cauldron], polyfills[1]));
}
if (env.prod) {
console.log('Minified version would be generated also');
buildTasks.push(getBuildTask(fnPrefix + ".min.js", [
new webpack.optimize.UglifyJsPlugin({
cauldron,
compress: {
warnings: false
}
}),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.AggressiveMergingPlugin()
], polyfills[2]));
}
return buildTasks;
};