通过节点自动化Webpack项目设置

我的目标是通过节点命令crateEntireWepbackProject.js文件设置一个webpack项目。

我想从一个js文件在shell上执行命令,所以我可以让它们自动运行,稍后也会包含项目的自定义规范。

js文件不是来自webpack内部的,而是有从头开始创build一个wepback项目的命令,并且通过我inputnode createEntireWebpackProject.js来进行安装等

你不需要从头开始写。 最好的做法是使用yeoman 。 有很多webpack的发电机。 例如:

 const Generator = require('yeoman-generator'); const mkdirp = require('mkdirp'); const path = require('path'); module.exports = class extends Generator { prompting() { this.log('Welcome to the classy example generator!'); const prompts = [ { type: 'input', name: 'name', message: 'Name?', default: this.appname, }]; return this.prompt(prompts).then((props) => { this.props = props; }); } default() { if (path.basename(this.destinationPath()) !== this.props.name) { this.log( `Your application must be inside a folder named ${this.props.name}`); this.log('I\'ll automatically create this folder.'); mkdirp(this.props.name); this.destinationRoot(this.destinationPath(this.props.name)); } } writing() { this.createPackageJson(); this.copyFiles(); this.fillTemplates(); this.makeCommands(); } install() { this.npmInstall(['bunyan', 'dotenv-safe'], { save: true }); this.npmInstall(['eslint', 'eslint-config-airbnb-base', 'eslint-plugin-import', 'jest'], { 'save-dev': true }); } createPackageJson() { this.fs.extendJSON('package.json', { name: this.props.name, version: '0.1.0', main: 'src/app.js', scripts: { cs: 'eslint src/* __tests__/*', 'cs:fix': 'eslint src/* __tests__/* --fix', start: 'node src/app.js', test: 'npm run cs && jest', }, dependencies: {}, devDependencies: {}, engines: { node: '^8.1.0', }, private: true, jest: { testEnvironment: 'node', transform: {}, collectCoverage: true, }, }); } copyFiles() { [ '.dockerignore', '.eslintrc.json', 'src/app.js', ].forEach(name => this.fs.copy(this.templatePath(name), this.destinationPath(name))); } fillTemplates() { this.fs.copyTpl( this.templatePath('README.md'), this.destinationPath('README.md'), { name: this.props.name, }); } makeCommands() { this.spawnCommandSync('git' ['init']); this.spawnCommandSync('git', ['add', '.']); this.spawnCommandSync('git', ['commit', '-am', '"yo scaffolded app"']); } };