我正在尝试gulp
,但我无法让我的内置后端运行,也可以在文件上观看。
以下是我的gulpfile.js
代码段的底部是gulp任务的名称:
var gulp = require('gulp');
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var DeepMerge = require('deepmerge');
var nodemon = require('nodemon');
// for excluding the building of node_modules in the backend
var nodeModules = {};
fs.readdirSync('node_modules').filter(function(x) {
return ['.bin'].indexOf(x) === -1;
}).forEach(function(mod) {
nodeModules[mod] = 'commonjs ' + mod;
});
// generic config
var defaultConfig = {
module: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: [
'react',
'es2015'
]
}
}
]
}
};
// if not production build
if (process.env.NODE_ENV !== 'production') {
// defaultConfig.devtool = '#eval-source-map';
defaultConfig.debug = true;
}
// build config using overrides
function buildConfig(config) {
return DeepMerge(defaultConfig, config || {});
}
var backendConfig = buildConfig({
entry: path.resolve(__dirname, "server/server.js"),
// tells webpack not to touch any built-in modules
target: "node",
externals: nodeModules,
output: {
path: path.resolve(__dirname, "build"),
filename: 'index.js'
},
pluguns: [
]
});
var host = "localhost";
var port = 3000;
var frontendConfig = buildConfig({
entry: path.resolve(__dirname, "app/index.js"),
output: {
path: path.resolve(__dirname, "public/bundle"),
filename: "main.js"
// publicPath: 'http://' + host + ':' + port + 'pubic/bundle'
},
plugins: [
]
});
function onBuild(done) {
return function(err, stats) {
if (err) {
console.log("ERROR: " + err);
} else {
console.log(stats.toString());
}
if (done) {
done();
}
}
}
// build frontend
gulp.task('build-frontend', function(done) {
webpack(frontendConfig).run(onBuild(done));
});
// watch frontend
gulp.task('watch-frontend', function(done) {
webpack(frontendConfig).watch(100, onBuild());
});
// build backend
gulp.task('build-backend', function(done) {
webpack(backendConfig).run(onBuild(done));
});
// watch backend
gulp.task('watch-backend', function(done) {
webpack(backendConfig).watch(100, function(err, stats) {
onBuild()(err, stats);
nodemon.restart();
});
});
gulp.task('build', ['build-frontend', 'build-backend']);
gulp.task('watch', ['watch-frontend', 'watch-backend']);
gulp.task('run', ['watch-frontend', 'watch-backend'], function() {
nodemon({
execMap: {
js: 'node'
},
verbose: true,
watch: ['server'],
script: 'build/index.js',
ext: 'js json'
}).on('restart', function(files) {
console.log('Restarted due to: ', files);
});
});
项目结构:
目前,正在运行gulp run
挂起,这是我的输出:
$ gulp run
[09:58:17] Using gulpfile ~/Development/projects/slack/slack/gulpfile.js
[09:58:17] Starting 'watch-frontend'...
[09:58:17] Starting 'watch-backend'...
Hash: 381ef159ea681b942d5f
Version: webpack 1.12.14
Time: 5300ms
Asset Size Chunks Chunk Names
index.js 2.25 kB 0 [emitted] main
chunk {0} index.js (main) 765 bytes [rendered]
[0] ./server/server.js 723 bytes {0} [built]
[1] external "hapi" 42 bytes {0} [not cacheable]
Hash: f7da7679b954a4fc9f6f
Version: webpack 1.12.14
Time: 6230ms
Asset Size Chunks Chunk Names
main.js 832 kB 0 [emitted] main
chunk {0} main.js (main) 787 kB [rendered]
[0] ./app/index.js 273 bytes {0} [built]
[1] ./~/react/react.js 56 bytes {0} [built]
...
...
[211] ./~/react-router/lib/browserHistory.js 596 bytes {0} [built]
[212] ./~/react-router/~/history/lib/createBrowserHistory.js 5.22 kB {0} [built]
[213] ./~/react-router/lib/createRouterHistory.js 579 bytes {0} [built]
[214] ./~/react-router/lib/hashHistory.js 581 bytes {0} [built]
[215] ./~/react-dom/index.js 63 bytes {0} [built]
代码正在构建属性,因为我可以在构建之后手动运行index.js
并且一切正常运行。