2017-02-09 10:26:02 +00:00

60 lines
1.7 KiB
TypeScript

import * as rimraf from 'rimraf';
import * as path from 'path';
const Task = require('../ember-cli/lib/models/task');
const SilentError = require('silent-error');
import * as webpack from 'webpack';
import { BuildTaskOptions } from '../commands/build';
import { NgCliWebpackConfig } from '../models/webpack-config';
import { getWebpackStatsConfig } from '../models/webpack-configs/utils';
import { CliConfig } from '../models/config';
export default Task.extend({
run: function (runTaskOptions: BuildTaskOptions) {
const project = this.cliProject;
const outputPath = runTaskOptions.outputPath || CliConfig.fromProject().config.apps[0].outDir;
if (project.root === outputPath) {
throw new SilentError ('Output path MUST not be project root directory!');
}
rimraf.sync(path.resolve(project.root, outputPath));
const webpackConfig = new NgCliWebpackConfig(runTaskOptions).config;
const webpackCompiler = webpack(webpackConfig);
const statsConfig = getWebpackStatsConfig(runTaskOptions.verbose);
return new Promise((resolve, reject) => {
const callback: webpack.compiler.CompilerCallback = (err, stats) => {
if (err) {
return reject(err);
}
this.ui.writeLine(stats.toString(statsConfig));
if (runTaskOptions.watch) {
return;
}
if (stats.hasErrors()) {
reject();
} else {
resolve();
}
};
if (runTaskOptions.watch) {
webpackCompiler.watch({}, callback);
} else {
webpackCompiler.run(callback);
}
})
.catch((err: Error) => {
if (err) {
this.ui.writeError('\nAn error occured during the build:\n' + ((err && err.stack) || err));
}
throw err;
});
}
});