1 | import 'babel-core/external-helpers';
|
2 |
|
3 | import gulp from 'gulp';
|
4 | import gulpUtil from 'gulp-util';
|
5 | import gulpPlumber from 'gulp-plumber';
|
6 | import vinylNamed from 'vinyl-named';
|
7 | import webpackStream from 'webpack-stream';
|
8 | import gulpNotify from 'gulp-notify';
|
9 | import path, { join as pathJoin } from 'path';
|
10 | import webpack, { DefinePlugin } from 'webpack';
|
11 | import packageJson from './package.json';
|
12 | import yargs from 'yargs';
|
13 | import _ from 'lodash';
|
14 |
|
15 | import { Server as KarmaServer } from 'karma';
|
16 | import karmaWebpack from 'karma-webpack';
|
17 | import karmaJasmine from 'karma-jasmine';
|
18 | import karmaJasmineMatchers from 'karma-jasmine-matchers';
|
19 | import karmaChromeLauncher from 'karma-chrome-launcher';
|
20 | import karmaFirefoxLauncher from 'karma-firefox-launcher';
|
21 | import karmaSafariLauncher from 'karma-safari-launcher';
|
22 | import karmaMochaReporter from 'karma-mocha-reporter';
|
23 | import karmaSourcemapLoader from 'karma-sourcemap-loader';
|
24 |
|
25 | function handleError (...args) {
|
26 | gulpNotify.onError({ title: 'COMPILE ERROR:', message: '<%= error %>' })(...args);
|
27 | this.emit('end');
|
28 | }
|
29 |
|
30 | export const fromRoot = pathJoin.bind(path, __dirname);
|
31 |
|
32 | export const PATHS = {
|
33 | root: fromRoot('./'),
|
34 | build: fromRoot('dist')
|
35 | };
|
36 |
|
37 | export const ENVIRONMENTS = {
|
38 | DEV: 'development',
|
39 | PROD: 'production'
|
40 | };
|
41 |
|
42 | const ARGV = yargs.argv;
|
43 |
|
44 | export const IS_DEBUG = !!ARGV.debug;
|
45 | export const IS_WATCH = !!ARGV.watch;
|
46 | export const IS_PROD = !!ARGV[ENVIRONMENTS.PROD];
|
47 | export const IS_DEV = !IS_PROD;
|
48 |
|
49 | export const BUNDLE_POSTFIX = `${IS_DEV ? 'dev' : 'min'}`;
|
50 | export const PACKAGE_VERSION = packageJson.version;
|
51 |
|
52 | let 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 |
|
62 | if (!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 |
|
70 | const 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 |
|
109 | loader: 'babel-loader'
|
110 | }
|
111 | ]
|
112 | }
|
113 | };
|
114 |
|
115 | const KARMA_CONFIG = {
|
116 | basePath: PATHS.root,
|
117 | browsers: ARGV.browsers ? ARGV.browsers.split(',') : ['Chrome'],
|
118 | browserNoActivityTimeout: 240 * 1000,
|
119 | autoWatch: IS_WATCH,
|
120 |
|
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 |
|
160 | gulp.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 |
|
172 | gulp.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 |
|
181 | gulp.task('default', ['build']);
|