ant-design/nico.js

142 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-05-09 17:36:15 +08:00
var path = require('path');
2015-06-04 14:42:24 +08:00
var package = require('./package');
var webpack = require('webpack');
2015-08-19 13:24:51 +08:00
var ProgressPlugin = require('webpack/lib/ProgressPlugin');
2015-08-08 23:37:35 +08:00
var inspect = require('util').inspect;
var Busboy = require('busboy');
2015-08-19 13:24:51 +08:00
var chalk = require('chalk');
var webpackMiddleware = require('webpack-dev-middleware');
2016-02-03 13:34:18 +08:00
var webpackHotMiddleware = require('webpack-hot-middleware');
2016-01-14 02:37:52 +08:00
var webpackConfig = require('./webpack.config');
var webpackCompiler = webpack(webpackConfig);
var handler;
2015-05-09 17:36:15 +08:00
2015-08-19 13:24:51 +08:00
webpackCompiler.apply(new ProgressPlugin(function(percentage, msg) {
var stream = process.stderr;
if (stream.isTTY && percentage < 0.71) {
stream.cursorTo(0);
2015-08-19 21:49:47 +08:00
stream.write('📦 ' + chalk.magenta(msg));
2015-08-19 13:24:51 +08:00
stream.clearLine(1);
} else if (percentage === 1) {
console.log(chalk.green('\nwebpack: bundle build is now finished.'));
}
}));
2015-05-09 17:36:15 +08:00
// {{ settings for nico
2015-05-12 17:50:03 +08:00
exports.site = {
2015-06-04 14:42:24 +08:00
name: package.title,
2015-06-05 15:02:39 +08:00
description: package.description,
repo: package.repository.url,
issues: package.bugs.url
2015-05-12 17:50:03 +08:00
};
2015-08-19 21:44:31 +08:00
// PRODUCTION
if (process.env.NODE_ENV === 'PRODUCTION') {
exports.minimized = '.min';
2015-09-17 17:29:03 +08:00
} else {
exports.minimized = '';
2015-08-19 21:44:31 +08:00
}
2015-07-04 16:20:50 +08:00
exports.package = package;
2015-06-09 15:50:36 +08:00
exports.theme = 'site';
2015-05-09 17:36:15 +08:00
exports.source = process.cwd();
exports.output = path.join(process.cwd(), '_site');
exports.permalink = '{{directory}}/{{filename}}';
2015-09-17 17:29:03 +08:00
exports.antdCssUrl = '../dist/' + package.name + '-' + package.version + exports.minimized + '.css';
exports.antdJsUrl = '../dist/' + package.name + '-' + package.version + exports.minimized + '.js';
2015-05-09 17:36:15 +08:00
exports.ignorefilter = function(filepath, subdir) {
var extname = path.extname(filepath);
if (extname === '.tmp' || extname === '.bak') {
return false;
}
if (/\.DS_Store/.test(filepath)) {
return false;
}
2015-07-05 23:43:05 +08:00
if (/^(_site|_theme|node_modules|site|\.idea)/.test(subdir)) {
2015-05-09 17:36:15 +08:00
return false;
}
return true;
};
2015-08-08 23:37:35 +08:00
exports.middlewares = [
{
name: 'upload',
filter: /upload\.do?$/,
handle: function(req, res, next) {
if (req.method === 'POST') {
var busboy = new Busboy({headers: req.headers});
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
file.on('data', function(data) {
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
});
file.on('end', function() {
console.log('File [' + fieldname + '] Finished');
});
});
busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
console.log('Field [' + fieldname + ']: value: ' + inspect(val));
});
busboy.on('finish', function() {
console.log('Done parsing form!');
//res.writeHead(303, { Connection: 'close', Location: '/' });
res.end(JSON.stringify({
'status': 'success',
'url': '/example.file'
}));
2015-08-08 23:37:35 +08:00
});
req.pipe(busboy);
}
}
},
{
2016-02-03 13:34:18 +08:00
name: 'webpackDevMiddleware',
filter: /\.(js|css|json)(\.map)?(\?.*)?$/,
handle: function(req, res, next) {
handler = handler || webpackMiddleware(webpackCompiler, {
publicPath: webpackConfig.output.publicPath,
lazy: false,
watchOptions: {
aggregateTimeout: 300,
poll: false
},
noInfo: true
});
try {
return handler(req, res, next);
} catch(e) {}
}
},
{
name: 'webpackHotMiddleware',
filter: /.*/,
handle: webpackHotMiddleware(webpackCompiler)
}
2016-02-03 13:34:18 +08:00
];
2015-08-19 21:44:31 +08:00
2015-05-09 17:36:15 +08:00
exports.writers = [
2015-05-18 18:13:16 +08:00
'nico-jsx.PageWriter',
'nico-jsx.StaticWriter',
'nico-jsx.FileWriter'
2015-05-09 17:36:15 +08:00
];
2016-02-11 22:57:53 +08:00
exports.watchFunc = function(e) {
console.log('reload: ' + e.filepath);
var post = e.post;
if (post && post.template === 'code' && post.id) {
var demoNode = $('#' + post.id);
if (demoNode[0]) {
ReactDOM.unmountComponentAtNode(demoNode[0]);
demoNode.next().remove().insertAfter(demoNode);
$(post.html).insertAfter(demoNode.next());
}
} else if (!(/\.js(x?)$/.test(post.filename))) {
location.reload();
}
};
2015-05-09 17:36:15 +08:00
// end settings }}
process.on('uncaughtException', function(err) {
console.log(err);
});