UNPKG

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