UNPKG

4.17 kBJavaScriptView Raw
1import gulp from 'gulp';
2import gulpPlumber from 'gulp-plumber';
3import vinylNamed from 'vinyl-named';
4import webpackStream from 'webpack-stream';
5import gulpNotify from 'gulp-notify';
6import path, { join as pathJoin } from 'path';
7import webpack, { DefinePlugin } from 'webpack';
8import packageJson from './package.json';
9import yargs from 'yargs';
10
11import { Server as KarmaServer } from 'karma';
12import karmaWebpack from 'karma-webpack';
13import karmaJasmine from 'karma-jasmine';
14import karmaJasmineMatchers from 'karma-jasmine-matchers';
15import karmaChromeLauncher from 'karma-chrome-launcher';
16import karmaFirefoxLauncher from 'karma-firefox-launcher';
17import karmaSafariLauncher from 'karma-safari-launcher';
18import karmaMochaReporter from 'karma-mocha-reporter';
19import karmaSourcemapLoader from 'karma-sourcemap-loader';
20
21function handleError (...args) {
22 gulpNotify.onError({ title: 'COMPILE ERROR:', message: '<%= error %>' })(...args);
23 this.emit('end');
24}
25
26export const fromRoot = pathJoin.bind(path, __dirname);
27
28export const PATHS = {
29 root: fromRoot('./'),
30 build: fromRoot('dist')
31};
32
33export const ENVIRONMENTS = {
34 DEV: 'development',
35 PROD: 'production'
36};
37
38const ARGV = yargs.argv;
39
40export const IS_DEBUG = !!ARGV.debug;
41export const IS_WATCH = !!ARGV.watch;
42export const IS_PROD = !!ARGV[ENVIRONMENTS.PROD];
43export const IS_DEV = !IS_PROD;
44
45export const BUNDLE_POSTFIX = `${IS_DEV ? 'dev' : 'min'}`;
46export const PACKAGE_VERSION = packageJson.version;
47
48let plugins = [
49 new webpack.optimize.OccurenceOrderPlugin(),
50 new DefinePlugin({
51 PACKAGE_VERSION: JSON.stringify(PACKAGE_VERSION),
52 IS_DEBUG: IS_DEBUG,
53 IS_DEV: IS_DEV,
54 IS_PROD: IS_PROD
55 })
56];
57
58if (!IS_DEV) {
59 plugins = plugins.concat([
60 new webpack.optimize.DedupePlugin(),
61 new webpack.optimize.UglifyJsPlugin({ sourceMap: false }),
62 new webpack.optimize.AggressiveMergingPlugin()
63 ]);
64}
65
66const WEBPACK_CONFIG = {
67 entry: {
68 crypto: fromRoot('browser.js')
69 },
70
71 output: {
72 path: PATHS.build,
73 filename: `virgil-[name].${BUNDLE_POSTFIX}.js`,
74 libraryTarget: 'umd'
75 },
76
77 target: 'web',
78
79 cache: IS_DEBUG,
80 debug: IS_DEBUG,
81 useMemoryFs: true,
82 progress: true,
83 watch: IS_WATCH,
84
85 stats: {
86 colors: true,
87 reasons: IS_DEBUG
88 },
89
90 plugins: plugins,
91
92 devtool: IS_DEBUG ? 'inline-source-map' : false,
93
94 resolve: {
95 modulesDirectories: ['node_modules'],
96 extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx']
97 },
98
99 module: {
100 loaders: [
101 {
102 test: /\.js$/,
103 exclude: /node_modules|virgil-emscripten\.js/,
104 // use runtime to optimize the code, but it make sense when you have a lot of ES6 files
105 loader: 'babel-loader'
106 }
107 ]
108 }
109};
110
111const KARMA_CONFIG = {
112 basePath: PATHS.root,
113 browsers: ARGV.browsers ? ARGV.browsers.split(',') : ['Chrome'],
114 browserNoActivityTimeout: 240 * 1000,
115 autoWatch: IS_WATCH,
116 // to avoid spamming and double running just increase the watch delay
117 autoWatchBatchDelay: 300,
118 singleRun: !IS_WATCH,
119 frameworks: ['jasmine', 'jasmine-matchers'],
120 port: 9876,
121 colors: true,
122 files: [
123 './src/browser/__TEST__/tests.bundle.js'
124 ],
125 preprocessors: {
126 './src/browser/__TEST__/tests.bundle.js': ['webpack'].concat(IS_DEBUG ? ['sourcemap'] : [])
127 },
128 reporters: ['mocha'],
129 webpack: {
130 devtool: 'inline-source-map',
131 resolve: WEBPACK_CONFIG.resolve,
132 plugins: WEBPACK_CONFIG.plugins,
133 module: {
134 loaders: WEBPACK_CONFIG.module.loaders
135 }
136 },
137 plugins: [
138 karmaWebpack,
139 karmaJasmine,
140 karmaJasmineMatchers,
141 karmaChromeLauncher,
142 karmaFirefoxLauncher,
143 karmaSafariLauncher,
144 karmaMochaReporter,
145 karmaSourcemapLoader
146 ],
147 webpackMiddleware: {
148 noInfo: true
149 },
150 webpackServer: {
151 noInfo: true
152 },
153 logLevel: 'INFO'
154};
155
156gulp.task('test', function(done) {
157 let isDoneCallbackCalled = false;
158
159 let karmaServer = new KarmaServer(KARMA_CONFIG, () => {
160 if (!isDoneCallbackCalled) {
161 done();
162 }
163 });
164
165 karmaServer.start();
166});
167
168gulp.task('build', () => {
169 return gulp.src(Object.values(WEBPACK_CONFIG.entry))
170 .pipe(vinylNamed())
171 .pipe(gulpPlumber(handleError))
172 .pipe(webpackStream(WEBPACK_CONFIG, webpack))
173 .pipe(gulpPlumber.stop())
174 .pipe(gulp.dest(WEBPACK_CONFIG.output.path));
175});
176
177gulp.task('default', ['build']);