最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to remove folder with npm before build - Stack Overflow

programmeradmin1浏览0评论

I've got a scripts "build": "rimraf dist webpack --progress --config webpack/prod.js", But actually,it removed not dist but all file inside webpack folder. But I need delete only dist

Structure:
 -dist
 -webpack
     -somefiles.js

I've got a scripts "build": "rimraf dist webpack --progress --config webpack/prod.js", But actually,it removed not dist but all file inside webpack folder. But I need delete only dist

Structure:
 -dist
 -webpack
     -somefiles.js
Share Improve this question asked Mar 1, 2018 at 14:29 Palaniichuk DmytroPalaniichuk Dmytro 3,17312 gold badges39 silver badges69 bronze badges 1
  • 1 you can try this rimraf dist; webpack --progress --config webpack/prod.js – Arnold Gandarillas Commented Mar 1, 2018 at 14:32
Add a comment  | 

5 Answers 5

Reset to default 20

Npm scripts are basically the same as running the commands directly in bash. In your case, you are running rimraf dist webpack which means webpack is an argument for rimraf. To separate commands, you can use ; or & if you want to make sure the first command ran successfully before running the second one use && So your script should look like this.

rimraf dist && webpack --progress --config webpack/prod.js

Quick and easy cross platform solution. No need of any library

[+] Updated from the comments - Handles error and prints to console

In package.json adding a script that employs node.js api to execute via inline execution

add below code in package.json script

{
  ...
  "scripts": {
     "rmdir": "node -e \"var fs = require('fs');process.argv.slice(1).map((fpath)=>{ try {fs.rmdirSync(fpath,{recursive: true})} catch(e){console.warn('Error folder:', fpath, e)}});process.exit(0);\"",
  }

}

Then execute as below in command window

npm run rmdir -- folder1 folder2

You can clean your output directory before emit by adding one line in your webpack.config.js file

clean: true,

Take a look at webpack output management : https://webpack.js.org/guides/output-management/#cleaning-up-the-dist-folder

i usually use clean: argv.mode === "production"

 "rmdir": "node -e \"var fs = require('fs'); try{process.argv.slice(1).map((fpath) => fs.rmdirSync(fpath, { recursive: true }))}catch(err){console.log(`Dist not found`)}; process.exit(0);\""

The previous versions don't work if the directory isn't present. But this will to run it with a typescript configuration use

"scripts": {
"rmdir": "node -e \"var fs = require('fs'); try{process.argv.slice(1).map((fpath) => fs.rmdirSync(fpath, { recursive: true }))}catch(err){console.log(`Dist not found`)}; process.exit(0);\"",
"build":"npm run rmdir dist && tsc"

Then use npm run build to compile the TSX

npm install --save-dev del-cli

"scripts": { "prebuild": "del-cli --force ../folder-to-delete" } notice "--force" when you want to delete a parent folder

发布评论

评论列表(0)

  1. 暂无评论