├── .gitignore ├── .npmignore ├── README.md ├── package.json ├── rollup.config.js ├── src └── index.ts ├── test └── index.test.ts ├── tsconfig.json └── tslint.json /.gitignore: -------------------------------------------------------------------------------- 1 | *_cache 2 | 3 | coverage 4 | 5 | dist 6 | 7 | node_modules 8 | 9 | package-lock.json -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | *_cache 2 | 3 | coverage 4 | 5 | node_modules 6 | 7 | src 8 | 9 | test 10 | 11 | package-lock.json 12 | 13 | .* 14 | 15 | rollup.config.js 16 | 17 | tsconfig.json 18 | 19 | tslint.json -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rollup-plugin-clear 2 | Rollup clear plugin 3 | ### Installation 4 | ```shell 5 | npm install --save-dev rollup-plugin-clear 6 | ``` 7 | ### Usage 8 | ```javascript 9 | // rollup.config.js 10 | import clear from 'rollup-plugin-clear' 11 | 12 | const config = { 13 | // ... 14 | plugins: [ 15 | // ... 16 | clear({ 17 | // required, point out which directories should be clear. 18 | targets: ['some directory'], 19 | // optional, whether clear the directores when rollup recompile on --watch mode. 20 | watch: true, // default: false 21 | }) 22 | ] 23 | } 24 | 25 | export default config; 26 | ``` 27 | This plugin can help you to clear the specific directories when the rollup bundle your resource. -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rollup-plugin-clear", 3 | "version": "2.0.7", 4 | "description": "Rollup clean plugin", 5 | "main": "./dist/index.js", 6 | "scripts": { 7 | "test": "node_modules/.bin/jest --coverage", 8 | "build": "node_modules/.bin/rollup -c" 9 | }, 10 | "keywords": [ 11 | "rollup", 12 | "rollup-plugin", 13 | "clear", 14 | "clean" 15 | ], 16 | "author": "Shelton Dong ", 17 | "license": "ISC", 18 | "publishConfig": { 19 | "registry": "https://registry.npmjs.org/" 20 | }, 21 | "bugs": { 22 | "url": "https://github.com/DongShelton/rollup-plugin-clear/issues", 23 | "email": "dongxu2048@gmail.com" 24 | }, 25 | "repository": { 26 | "type": "git", 27 | "url": "https://github.com/DongShelton/rollup-plugin-clear" 28 | }, 29 | "devDependencies": { 30 | "@types/jest": "^22.1.2", 31 | "@types/rimraf": "^2.0.2", 32 | "jest": "^22.2.2", 33 | "rollup": "^0.55.5", 34 | "rollup-plugin-commonjs": "^8.3.0", 35 | "rollup-plugin-filesize": "^1.5.0", 36 | "rollup-plugin-node-resolve": "^3.0.2", 37 | "rollup-plugin-progress": "^0.4.0", 38 | "rollup-plugin-typescript2": "^0.11.1", 39 | "ts-jest": "^22.0.4", 40 | "typescript": "^2.7.1" 41 | }, 42 | "dependencies": { 43 | "rimraf": "^2.6.2" 44 | }, 45 | "jest": { 46 | "transform": { 47 | "^.+\\.ts$": "ts-jest" 48 | }, 49 | "testMatch": [ 50 | "/**/*.test.ts" 51 | ], 52 | "moduleFileExtensions": [ 53 | "ts", 54 | "js", 55 | "json", 56 | "node" 57 | ] 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | import resolve from 'rollup-plugin-node-resolve'; 2 | import typescript from 'rollup-plugin-typescript2'; 3 | import filesize from 'rollup-plugin-filesize'; 4 | import progress from 'rollup-plugin-progress'; 5 | import commonjs from 'rollup-plugin-commonjs'; 6 | 7 | const config = { 8 | input: './src/index.ts', 9 | output: { 10 | file: './dist/index.js', 11 | format: 'cjs', 12 | }, 13 | plugins: [ 14 | // rollup-plugin-progress 15 | progress(), 16 | // rollup-plugin-node-resolve 17 | resolve(), 18 | // rollup-plugin-typescript2 19 | typescript(), 20 | // rollup-plugin-filesize 21 | filesize(), 22 | // rollup-plugin-commonjs 23 | commonjs(), 24 | // rollup-plugin-alias 25 | ], 26 | external: Object.keys(require('./package.json').dependencies) 27 | }; 28 | 29 | export default config; -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import * as fs from 'fs'; 2 | import * as path from 'path'; 3 | import * as rimraf from 'rimraf'; 4 | 5 | interface Options { 6 | targets: string[]; 7 | watch?: boolean; 8 | } 9 | 10 | const clear = (options: Options) => { 11 | const targets = options.targets || []; 12 | // 在rollup watch模式下,当recompile的时候是否clear,默认false 13 | const watch = options.watch === true ? true : false; 14 | const workspace = process.cwd(); 15 | 16 | /** 17 | * 清楚目标路径 18 | * 19 | * @param {array} targets 20 | */ 21 | const clear = (targets: string[]) => { 22 | for (let index = 0; index < targets.length; index++) { 23 | const e = targets[index]; 24 | const target = path.resolve(workspace, e); 25 | if (fs.existsSync(target)) { 26 | rimraf.sync(target); 27 | console.log('cleared: ', target); 28 | } 29 | } 30 | }; 31 | clear(targets); 32 | 33 | return { 34 | name: 'clear', 35 | load: (id: string) => { 36 | if (watch) { 37 | clear(targets); 38 | } 39 | return null; 40 | } 41 | }; 42 | }; 43 | 44 | export default clear; 45 | -------------------------------------------------------------------------------- /test/index.test.ts: -------------------------------------------------------------------------------- 1 | import 'jest'; 2 | import * as path from 'path'; 3 | 4 | const workspace = process.cwd(); 5 | 6 | describe('Test clear plugin', () => { 7 | const mockExistsSync = jest.fn(); 8 | const mockRmrfSync = jest.fn(); 9 | jest.mock('fs', () => { 10 | return { 11 | existsSync: mockExistsSync 12 | }; 13 | }); 14 | jest.mock('rimraf', () => { 15 | return { 16 | sync: mockRmrfSync 17 | }; 18 | }); 19 | 20 | test('Test file system api', () => { 21 | const clear = require('../src').default; 22 | const plugin = clear({ 23 | targets: ['lib', 'dist'], 24 | }); 25 | 26 | expect(mockExistsSync).toHaveBeenCalledWith(path.resolve(workspace, 'lib')); 27 | 28 | expect(mockExistsSync).toHaveBeenCalledWith(path.resolve(workspace, 'dist')); 29 | }); 30 | 31 | test('Test rm -r -f api', () => { 32 | const clear = require('../src').default; 33 | mockExistsSync.mockReturnValue(true); 34 | const plugin = clear({ 35 | targets: ['lib', 'dist'], 36 | }); 37 | 38 | expect(mockRmrfSync).toHaveBeenCalledWith(path.resolve(workspace, 'lib')); 39 | 40 | expect(mockRmrfSync).toHaveBeenCalledWith(path.resolve(workspace, 'dist')); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "include": [ 3 | "src" 4 | ], 5 | "exclude": [ 6 | "node_modules" 7 | ], 8 | "compilerOptions": { 9 | /* Basic Options */ 10 | "target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */ 11 | "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ 12 | "lib": ["es2015"], /* Specify library files to be included in the compilation: */ 13 | // "allowJs": true, /* Allow javascript files to be compiled. */ 14 | // "checkJs": true, /* Report errors in .js files. */ 15 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 16 | // "declaration": true, /* Generates corresponding '.d.ts' file. */ 17 | // "sourceMap": true, /* Generates corresponding '.map' file. */ 18 | // "outFile": "./", /* Concatenate and emit output to single file. */ 19 | // "outDir": "./", /* Redirect output structure to the directory. */ 20 | // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 21 | // "removeComments": true, /* Do not emit comments to output. */ 22 | // "noEmit": true, /* Do not emit outputs. */ 23 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 24 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 25 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 26 | 27 | /* Strict Type-Checking Options */ 28 | "strict": true /* Enable all strict type-checking options. */ 29 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 30 | // "strictNullChecks": true, /* Enable strict null checks. */ 31 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 32 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 33 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 34 | 35 | /* Additional Checks */ 36 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 37 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 38 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 39 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 40 | 41 | /* Module Resolution Options */ 42 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 43 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 44 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 45 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 46 | // "typeRoots": [], /* List of folders to include type definitions from. */ 47 | // "types": [], /* Type declaration files to be included in compilation. */ 48 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 49 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 50 | 51 | /* Source Map Options */ 52 | // "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 53 | // "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */ 54 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 55 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 56 | 57 | /* Experimental Options */ 58 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 59 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 60 | } 61 | } -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "defaultSeverity": "error", 3 | "extends": ["tslint:recommended"], 4 | "jsRules": {}, 5 | "rules": { 6 | "no-console": false, 7 | "quotemark": [true, "single", "jsx-double"], 8 | "interface-name": [false], 9 | "jsdoc-format": [false], 10 | "no-empty-interface": false, 11 | "object-literal-sort-keys": false, 12 | "prefer-for-of": false, 13 | "no-var-requires": true, 14 | "ordered-imports": false, 15 | "no-string-literal": false, 16 | "align": [true, "statements", "member", "elements"], 17 | "prefer-object-spread": true, 18 | "max-line-length": false, 19 | "trailing-comma": [ 20 | true, 21 | { 22 | "multiline": { 23 | "objects": "alwals", 24 | "arrays": "always", 25 | "functions": "never", 26 | "typeLiterals": "ignore" 27 | }, 28 | "esSpecCompliant": true 29 | } 30 | ], 31 | "whitespace": [ 32 | true, 33 | "check-branch", 34 | "check-decl", 35 | "check-module", 36 | "check-operator", 37 | "check-separator", 38 | "check-type", 39 | "check-typecast" 40 | ], 41 | "member-ordering": [ 42 | true, 43 | { 44 | "order": [ 45 | "static-field", 46 | "static-method", 47 | "instance-field", 48 | "constructor", 49 | "instance-method" 50 | ] 51 | } 52 | ], 53 | "array-type": [true, "array"], 54 | "arrow-parens": [true, "ban-single-arg-parens"], 55 | "class-name": true, 56 | "curly": [true, "ignore-same-line"], 57 | "forin": false, 58 | "label-position": true, 59 | "max-classes-per-file": false, 60 | "member-access": false, 61 | "no-bitwise": false, 62 | "no-consecutive-blank-lines": [true, 2], 63 | "no-debugger": true, 64 | "no-empty": [true, "allow-empty-catch"], 65 | "no-shadowed-variable": false, 66 | "no-switch-case-fall-through": true, 67 | "no-unused-expression": [true, "allow-fast-null-checks"], 68 | "object-literal-key-quotes": [true, "as-needed"], 69 | "only-arrow-functions": false, 70 | "space-before-function-paren": [ 71 | true, 72 | { 73 | "anonymous": "always", 74 | "asyncArrow": "always", 75 | "constructor": "never", 76 | "method": "never", 77 | "named": "never" 78 | } 79 | ], 80 | "typedef": [true, "parameter", "property-declaration"], 81 | "typedef-whitespace": [ 82 | true, 83 | { 84 | "call-signature": "nospace", 85 | "index-signature": "nospace", 86 | "parameter": "nospace", 87 | "property-declaration": "nospace", 88 | "variable-declaration": "nospace" 89 | } 90 | ], 91 | "variable-name": [ 92 | true, 93 | "ban-keywords", 94 | "check-format", 95 | "allow-leading-underscore", 96 | "allow-pascal-case" 97 | ] 98 | } 99 | } 100 | --------------------------------------------------------------------------------