├── .npmignore ├── .gitignore ├── .travis.yml ├── README.md ├── package.json ├── LICENSE └── index.js /.npmignore: -------------------------------------------------------------------------------- 1 | test 2 | .travis.yml 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | 3 | # Logs 4 | logs 5 | *.log 6 | 7 | # Runtime data 8 | pids 9 | *.pid 10 | *.seed 11 | 12 | node_modules 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "stable" 5 | - v5 6 | - v4 7 | - "0.12" 8 | - "0.10" 9 | 10 | script: "npm run-script test-travis" 11 | after_script: "cat ./coverage/lcov.info | coveralls" 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CleanTheme for WebPack 2 | A webpack plugin to remove/clean your build js of theme folder(s) before building 3 | 4 | ## Installation 5 | ``` 6 | npm i clean-theme-webpack-plugin --save-dev 7 | ``` 8 | 9 | ## Usage 10 | ```js 11 | const CleanThemeWebpackPlugin = require('clean-theme-webpack-plugin') 12 | 13 | { 14 | plugins: [ 15 | new CleanThemeWebpackPlugin({ options }) 16 | ] 17 | } 18 | ``` 19 | 20 | ## Example Webpack Config 21 | ```javascript 22 | plugins: [ 23 | new CleanWebpackPlugin({ 24 | root: './dist', 25 | theme: ['blue', 'green'] 26 | }) 27 | ] 28 | ``` 29 | 30 | ### Options and defaults (Optional) 31 | ```js 32 | { 33 | // Absolute path to your webpack output folder 34 | root: __dirname + '/dist', 35 | 36 | // Your themes 37 | theme: ['red', 'green'] 38 | } 39 | ``` 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "clean-theme-webpack-plugin", 3 | "version": "0.0.1", 4 | "author": "Dushao", 5 | "description": "A webpack plugin to remove your build folder(s)", 6 | "homepage": "https://github.com/wisestcoder/clean-theme-webpack-plugin", 7 | "license": "MIT", 8 | "main": "index.js", 9 | "keywords": [ 10 | "webpack", 11 | "plugin", 12 | "clean", 13 | "node", 14 | "theme" 15 | ], 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/wisestcoder/clean-theme-webpack-plugin.git" 19 | }, 20 | "bugs": { 21 | "url": "https://github.com/wisestcoder/clean-theme-webpack-plugin/issues" 22 | }, 23 | "scripts": { 24 | }, 25 | "dependencies": {}, 26 | "devDependencies": { 27 | "chai": "^4.1.1", 28 | "chalk": "^2.4.0", 29 | "coveralls": "^2.13.1", 30 | "istanbul": "^0.4.5", 31 | "mocha": "^3.5.0", 32 | "rewire": "^2.5.2", 33 | "rimraf": "^2.6.1" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 John Agan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var fs = require('fs'); 4 | var os = require('os'); 5 | var path = require('path'); 6 | var rimraf = require('rimraf'); 7 | var chalk = require('chalk'); 8 | 9 | function upperCaseWindowsRoot(dir) { 10 | var splitPath = dir.split(path.sep); 11 | splitPath[0] = splitPath[0].toUpperCase(); 12 | return splitPath.join(path.sep); 13 | } 14 | 15 | function CleanThemeWebpackPlugin(options) { 16 | 17 | options = options || {}; 18 | options.root = options.root || path.dirname(module.parent.filename); 19 | 20 | this.options = options; 21 | this.paths = this.options.theme.map((item) => { 22 | return `${this.options.root}/${item}/*.js`; 23 | }); 24 | } 25 | 26 | var clean = function(compilation) { 27 | var _this = this; 28 | var results = []; 29 | var workingDir; 30 | var dirName; 31 | var projectRootDir; 32 | 33 | if (_this.paths === void 0) { 34 | return null; 35 | } 36 | 37 | _this.paths.forEach(function(rimrafPath) { 38 | rimrafPath = path.resolve(_this.options.root, rimrafPath); 39 | 40 | if (os.platform() === 'win32') { 41 | rimrafPath = upperCaseWindowsRoot(rimrafPath); 42 | } 43 | 44 | rimraf.sync(rimrafPath); 45 | Object.keys(compilation.assets).forEach((item) => { 46 | const dir = path.parse(item).dir; 47 | const ext = path.parse(item).ext; 48 | _this.options.theme.forEach((i) => { 49 | if (dir && dir.indexOf(i) > -1 && ext === '.js') { 50 | delete compilation.assets[item]; 51 | } 52 | }) 53 | }) 54 | 55 | }); 56 | }; 57 | 58 | CleanThemeWebpackPlugin.prototype.apply = function(compiler) { 59 | const _this = this; 60 | compiler.plugin("emit", function(compilation, callback) { 61 | clean.call(_this, compilation); 62 | callback(); 63 | }); 64 | }; 65 | 66 | module.exports = CleanThemeWebpackPlugin; 67 | --------------------------------------------------------------------------------