Skip to content

Commit

Permalink
feat: generate dist files, close: #16
Browse files Browse the repository at this point in the history
  • Loading branch information
benjycui committed Nov 15, 2016
1 parent d981ed1 commit b2a916e
Showing 1 changed file with 79 additions and 3 deletions.
82 changes: 79 additions & 3 deletions lib/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var through2 = require('through2');
var webpack = require('webpack');
var shelljs = require('shelljs');
var jsx2example = require('gulp-jsx2example');
var getWebpackCommonConfig = require('./getWebpackCommonconfig');
var getWebpackConfig = require('./getWebpackConfig');
var babel = require('gulp-babel');
var runCmd = require('./util').runCmd;
Expand All @@ -27,6 +28,8 @@ const merge2 = require('merge2');
const tsConfig = require('./getTSCommonConfig')();
const glob = require('glob');
const watch = require('gulp-watch');
const assign = require('object-assign');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

const tsDefaultReporter = ts.reporter.defaultReporter();

Expand Down Expand Up @@ -85,7 +88,9 @@ gulp.task('ts-lint', ['check-deps'], (done) => {
gulp.task('lint', ['ts-lint', 'js-lint']);

function printResult(stats) {
stats = stats.toJson();
if (stats.toJson) {
stats = stats.toJson();
}

(stats.errors || []).forEach((err) => {
console.error('error', err);
Expand Down Expand Up @@ -131,6 +136,77 @@ gulp.task('webpack', ['cleanBuild'], (done) => {
}
});

gulp.task('dist', (done) => {
const entry = pkg.entry;
if (!entry) {
done();
return;
}

const webpackConfig = assign({
devtool: '#source-map',
resolve: getWebpackCommonConfig.getResolve(),
module: {
noParse: [/moment.js/],
loaders: getWebpackCommonConfig.getLoaders()
.concat(getWebpackCommonConfig.getCssLoaders(true)),
},
output: {
path: path.join(cwd, 'dist'),
filename: '[name].js',
library: pkg.name,
libraryTarget: 'umd',
},
externals: {
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react',
},
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom',
},
},
plugins: [
new ExtractTextPlugin('[name].css', {
disable: false,
allChunks: true,
}),
],
}, getWebpackCommonConfig.getLoaderConfig());

const compressedWebpackConfig = Object.assign({}, webpackConfig);
compressedWebpackConfig.entry = {};
Object.keys(entry).forEach(e => compressedWebpackConfig.entry[`${e}.min`] = entry[e]);
compressedWebpackConfig.UglifyJsPluginConfig = {
output: {
ascii_only: true,
},
compress: {
warnings: false,
},
};
compressedWebpackConfig.plugins = webpackConfig.plugins.concat([
new webpack.optimize.UglifyJsPlugin(webpackConfig.UglifyJsPluginConfig),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production'),
}),
]);

webpackConfig.entry = entry;
webpack([webpackConfig, compressedWebpackConfig], (err, stats) => {
if (err) {
console.error('error', err);
}
stats.toJson().children.forEach(printResult);
done(err);
});
});

gulp.task('clean', clean);

gulp.task('cleanCompile', cleanCompile);
Expand Down Expand Up @@ -285,7 +361,7 @@ gulp.task('karma', (done) => {
runCmd('node', args, done);
});

gulp.task('publish', ['compile'], () => {
gulp.task('publish', ['compile', 'dist'], () => {
console.log('publishing');
var npm = argv.tnpm ? 'tnpm' : 'npm';
const beta = !pkg.version.match(/^\d+\.\d+\.\d+$/);
Expand Down Expand Up @@ -331,7 +407,7 @@ function compileTs(stream) {
this.push(file);
next();
}))
.pipe(gulp.dest(process.cwd()));
.pipe(gulp.dest(cwd));
}

const tsFiles = [
Expand Down

0 comments on commit b2a916e

Please sign in to comment.