forked from dantrain/react-stonecutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.babel.js
196 lines (169 loc) · 4.69 KB
/
gulpfile.babel.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import gulp from 'gulp';
import gutil from 'gulp-util';
import notifier from 'node-notifier';
import { create as browserSyncCreate } from 'browser-sync';
import webpack from 'webpack';
import eslint from 'gulp-eslint';
import filter from 'gulp-filter';
import rename from 'gulp-rename';
import shell from 'gulp-shell';
import runSequence from 'run-sequence';
import assign from 'lodash.assign';
import packageJson from './package.json';
const browserSync = browserSyncCreate();
browserSync.use({
plugin() {},
hooks: {
'client:js': '___browserSync___.socket.on("disconnect", window.close.bind(window));'
}
});
const sharedWebpackConfig = {
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /dist|public|node_modules/,
loader: 'babel'
}
]
},
resolve: {
extensions: ['', '.js', '.jsx']
},
watch: false
};
const libWebpackConfig = assign({}, sharedWebpackConfig, {
entry: './src/index.js',
output: {
path: './lib',
filename: 'react-stonecutter.js',
library: 'reactStonecutter',
libraryTarget: 'commonjs2'
},
externals: [
...Object.keys(packageJson.dependencies),
...Object.keys(packageJson.peerDependencies)
].reduce((acc, val) => {
acc[val] = val;
return acc;
}, {}),
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurrenceOrderPlugin()
]
});
const demoWebpackConfig = assign({}, sharedWebpackConfig, {
entry: './demo/src/main.js',
output: {
path: './demo/public',
filename: 'demo.js'
}
});
// var devCompiler = webpack([
// assign({}, libWebpackConfig, { devtool: 'inline-source-map' }),
// assign({}, demoWebpackConfig, { devtool: 'inline-source-map' })
// ]);
const devCompiler = webpack([libWebpackConfig, demoWebpackConfig]);
gulp.task('build', done =>{
devCompiler.run((err,stats)=>{
if (err) {
notifier.notify({ title: 'Webpack Error', message: err });
throw new gutil.PluginError('webpack', err);
}
gutil.log(gutil.colors.cyan('[webpack]'), stats.toString({
chunks: false,
version: false,
colors: true
}));
done()
})
})
gulp.task('webpack', done => {
let firstTime = true;
devCompiler.watch({
aggregateTimeout: 100
}, (err, stats) => {
if (err) {
notifier.notify({ title: 'Webpack Error', message: err });
throw new gutil.PluginError('webpack', err);
}
const jsonStats = stats.toJson();
if (jsonStats.errors.length > 0) {
notifier.notify({ title: 'Webpack Error', message: jsonStats.errors[0] });
} else if (jsonStats.warnings.length > 0) {
notifier.notify({ title: 'Webpack Warning', message: jsonStats.warnings[0] });
}
gutil.log(gutil.colors.cyan('[webpack]'), stats.toString({
chunks: false,
version: false,
colors: true
}));
if (jsonStats.assets[0].name === 'demo.js') {
browserSync.reload();
if (firstTime) {
firstTime = false;
done();
}
}
});
});
gulp.task('browser-sync', ['webpack', 'demo-html-css'], () => {
browserSync.init({
notify: false,
ghostMode: false,
server: {
baseDir: 'demo/public'
}
});
});
gulp.task('demo-html-css', () => {
const sliderCssFilter = filter('node_modules/rc-slider/assets/index.css',
{ restore: true });
return gulp.src(['demo/src/*.@(html|css)',
'node_modules/rc-slider/assets/index.css'])
.pipe(sliderCssFilter)
.pipe(rename('rc-slider.css'))
.pipe(sliderCssFilter.restore)
.pipe(gulp.dest('demo/public'))
.pipe(browserSync.reload({ stream: true }));
});
gulp.task('watch', () => {
gulp.watch('src/js/**/*', e => {
const path = gutil.colors.magenta(e.path.substring(e.path.lastIndexOf('/') + 1));
const type = gutil.colors.cyan(`${e.type}...`);
gutil.log(`${path} ${type}`);
});
gulp.watch('demo/src/*.@(html|css)', ['demo-html-css']);
});
gulp.task('gh-pages-start', shell.task([
'git stash',
'git checkout gh-pages'
]));
gulp.task('copy-demo-to-root', () =>
gulp.src('./demo/public/**/*')
.pipe(gulp.dest('./')));
gulp.task('gh-pages-end', shell.task([
'git add index.html demo.js *.css',
'git commit --amend --no-edit',
'git push github gh-pages --force',
'git checkout master',
'git stash apply'
]));
gulp.task('gh-pages', done => {
runSequence('gh-pages-start', 'copy-demo-to-root', 'gh-pages-end', done);
});
gulp.task('lint', () =>
gulp.src([
'src/**/*.js*(x)',
'demo/src/**/*.js*(x)',
'test/**/*.js'
])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError()));
gulp.task('default', ['browser-sync', 'watch']);