├── .travis.yml ├── CHANGELOG.md ├── .jshintrc ├── gulpfile.js ├── index.js ├── LICENSE ├── .gitignore ├── package.json ├── test └── test.js └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | sudo: false 3 | node_js: 4 | - "4" 5 | - "6" 6 | - "8" 7 | script: 8 | - "npm run lint" 9 | - "npm test" 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Revision history for webpack-unassert-loader 2 | 3 | ## 1.2.0 (2017-08-10) 4 | 5 | - Add configuration options for unassert. 6 | - Update dependencies. 7 | 8 | 9 | ## 1.1.0 (2016-10-28) 10 | 11 | - Support preserve comment. 12 | - Update esprima. 13 | 14 | 15 | ## 1.0.0 (2015-05-27) 16 | 17 | - Initial release. 18 | 19 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "bitwise": true, 3 | "camelcase": true, 4 | "curly": true, 5 | "eqeqeq": true, 6 | "eqnull": true, 7 | "esnext": true, 8 | "evil": true, 9 | "forin": false, 10 | "immed": true, 11 | "indent": 2, 12 | "latedef": false, 13 | "maxdepth": 4, 14 | "maxparams": 4, 15 | "newcap": true, 16 | "noarg": true, 17 | "node": true, 18 | "nonew": true, 19 | "onevar": true, 20 | "quotmark": "single", 21 | "smarttabs": true, 22 | "strict": true, 23 | "trailing": true, 24 | "undef": true, 25 | "unused": "vars", 26 | "white": true 27 | } 28 | 29 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var mocha = require('gulp-mocha'); 3 | var jshint = require('gulp-jshint'); 4 | var stylish = require('jshint-stylish'); 5 | 6 | var config = { 7 | jshint: { 8 | src: 'index.js', 9 | rcfile: '.jshintrc' 10 | }, 11 | test: { 12 | src: 'test/**/*.js', 13 | option: { 14 | ui: 'bdd', 15 | reporter: 'dot' 16 | } 17 | } 18 | }; 19 | 20 | gulp.task('jshint', function() { 21 | return gulp.src(config.jshint.src) 22 | .pipe(jshint(config.jshint.rcfile)) 23 | .pipe(jshint.reporter(stylish)); 24 | }); 25 | 26 | gulp.task('test', function() { 27 | require('intelli-espower-loader'); 28 | return gulp.src(config.test.src) 29 | .pipe(mocha(config.test.option)); 30 | }); 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * webpack-unassert-loader - A webpack loader for unassert 3 | * Encourages programming with assertions by providing tools to compile them away 4 | * 5 | * https://github.com/unassert-js/webpack-unassert-loader 6 | * 7 | * Copyright (c) 2015- Kenta Mori and Contributors 8 | * Licensed under the MIT license. 9 | * https://github.com/unassert-js/webpack-unassert-loader/blob/master/LICENSE 10 | */ 11 | 'use strict'; 12 | 13 | var esprima = require('esprima'); 14 | var escodegen = require('escodegen'); 15 | var estraverse = require('estraverse'); 16 | var unassert = require('unassert'); 17 | 18 | module.exports = function(jsCode) { 19 | var options = this.options; 20 | var ast = esprima.parse(jsCode, { 21 | range: true, 22 | tokens: true, 23 | comment: true 24 | }); 25 | var attachedAst = escodegen.attachComments(ast, ast.comments, ast.tokens); 26 | var modifiedAst = estraverse.replace(attachedAst, unassert.createVisitor(options)); 27 | this.callback(null, escodegen.generate(modifiedAst, { 28 | comment: true 29 | }), null); 30 | }; 31 | 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015- Kenta Mori and Contributors 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 13 | all 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 21 | THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io 2 | 3 | ### OSX ### 4 | .DS_Store 5 | .AppleDouble 6 | .LSOverride 7 | 8 | # Icon must end with two \r 9 | Icon 10 | 11 | 12 | # Thumbnails 13 | ._* 14 | 15 | # Files that might appear on external disk 16 | .Spotlight-V100 17 | .Trashes 18 | 19 | # Directories potentially created on remote AFP share 20 | .AppleDB 21 | .AppleDesktop 22 | Network Trash Folder 23 | Temporary Items 24 | .apdisk 25 | 26 | 27 | ### vim ### 28 | [._]*.s[a-w][a-z] 29 | [._]s[a-w][a-z] 30 | *.un~ 31 | Session.vim 32 | .netrwhist 33 | *~ 34 | 35 | 36 | ### Node ### 37 | # Logs 38 | logs 39 | *.log 40 | 41 | # Runtime data 42 | pids 43 | *.pid 44 | *.seed 45 | 46 | # Directory for instrumented libs generated by jscoverage/JSCover 47 | lib-cov 48 | 49 | # Coverage directory used by tools like istanbul 50 | coverage 51 | 52 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 53 | .grunt 54 | 55 | # node-waf configuration 56 | .lock-wscript 57 | 58 | # Compiled binary addons (http://nodejs.org/api/addons.html) 59 | build/Release 60 | 61 | # Dependency directory 62 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- 63 | node_modules 64 | 65 | 66 | ### Bower ### 67 | bower_components 68 | 69 | 70 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-unassert-loader", 3 | "description": "A webpack loader for unassert: Encourages programming with assertions by providing tools to compile them away", 4 | "version": "1.2.0", 5 | "author": { 6 | "name": "Kenta Mori", 7 | "email": "zoncoen@gmail.com", 8 | "url": "http://github.com/zoncoen" 9 | }, 10 | "bugs": { 11 | "url": "https://github.com/unassert-js/webpack-unassert-loader/issues" 12 | }, 13 | "keywords": [ 14 | "DbC", 15 | "assert", 16 | "assertion", 17 | "webpack" 18 | ], 19 | "license": "MIT", 20 | "main": "index.js", 21 | "repository": { 22 | "type": "git", 23 | "url": "https://github.com/unassert-js/webpack-unassert-loader" 24 | }, 25 | "homepage": "https://github.com/unassert-js/webpack-unassert-loader", 26 | "scripts": { 27 | "fmt": "jsfmt -w ./*.js ./**/*-test.js ./gulpfile.js", 28 | "lint": "gulp jshint", 29 | "test": "gulp test" 30 | }, 31 | "dependencies": { 32 | "escodegen": "^1.6.1", 33 | "esprima": "^4.0.0", 34 | "estraverse": "^4.0.0", 35 | "unassert": "^1.5.0" 36 | }, 37 | "devDependencies": { 38 | "gulp": "^3.8.11", 39 | "gulp-jshint": "^2.0.4", 40 | "gulp-mocha": "^4.3.1", 41 | "intelli-espower-loader": "^1.0.0", 42 | "jsfmt": "^0.5.3", 43 | "jshint": "^2.9.5", 44 | "jshint-stylish": "^2.0.0", 45 | "power-assert": "^1.0.0" 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('power-assert'); 4 | var unassert = require('../index'); 5 | 6 | describe('webpack-unassert-loader', function() { 7 | it('should return source removed assertions', function() { 8 | var source = '/** comment */\nfunction add(a, b) {\n assert(!isNaN(a));\n return a + b;\n}'; 9 | 10 | // set context for webpack loader 11 | var context = {}; 12 | context.callback = function(err, result, map) { 13 | var expected = '/** comment */\nfunction add(a, b) {\n return a + b;\n}'; 14 | assert.equal(result, expected, 'remove assertions from the source'); 15 | }; 16 | 17 | unassert.call(context, source, null); 18 | }); 19 | 20 | it('custom options', function () { 21 | var source = 'var invariant = require("invariant");\n\nfunction add(a, b) {\n invariant(!isNaN(a));\n return a + b;\n}'; 22 | 23 | // set context for webpack loader 24 | var context = {}; 25 | context.options = { 26 | assertionPatterns: [ 27 | 'invariant(value, [message])' 28 | ], 29 | requirePatterns: [ 30 | 'invariant = require("invariant")' 31 | ] 32 | }; 33 | context.callback = function(err, result, map) { 34 | var expected = 'function add(a, b) {\n return a + b;\n}'; 35 | assert.equal(result, expected, 'remove invariants from the source'); 36 | }; 37 | 38 | unassert.call(context, source, null); 39 | }); 40 | }); 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status][travis-image]][travis-url] 2 | [![NPM package][npm-image]][npm-url] 3 | [![Dependency Status][depstat-image]][depstat-url] 4 | [![License][license-image]][license-url] 5 | 6 | # webpack-unassert-loader 7 | 8 | A webpack loader for unassert: Encourages programming with assertions by providing tools to compile them away. 9 | 10 | 11 | ## Description 12 | 13 | `webpack-unassert-loader` is a webpack loader module to remove assertions on build. 14 | `webpack-unassert-loader` applies [unassert](https://github.com/unassert-js/unassert) to target sources through webpack loader chain. 15 | 16 | See [unassert](https://github.com/unassert-js/unassert) project for more documentation. 17 | 18 | ## Installation 19 | 20 | Install `webpack-unassert-loader` via npm: 21 | 22 | ```console 23 | $ npm install --save-dev webpack-unassert-loader 24 | ``` 25 | 26 | ## Usage 27 | 28 | Configure `webpack.config.js` to apply `webpack-unassert-loader` through webpack loader transformation chain. 29 | 30 | ```js 31 | { 32 | module: { 33 | loaders: [ 34 | { test: /\.js$/, loader: "webpack-unassert-loader" } 35 | ] 36 | } 37 | } 38 | ``` 39 | 40 | You can pass unassert options by including to webpack configuration object (e.g. webpack.config.js). 41 | If not passed, default options (Same as [unassert.defaultOptions()](https://github.com/unassert-js/unassert#var-options--unassertdefaultoptions)) will be used. 42 | 43 | ```js 44 | { 45 | assertionPatterns: [ 46 | 'assert(value, [message])', 47 | 'assert.ok(value, [message])', 48 | 'assert.equal(actual, expected, [message])', 49 | 'assert.notEqual(actual, expected, [message])', 50 | 'assert.strictEqual(actual, expected, [message])', 51 | 'assert.notStrictEqual(actual, expected, [message])', 52 | 'assert.deepEqual(actual, expected, [message])', 53 | 'assert.notDeepEqual(actual, expected, [message])', 54 | 'assert.deepStrictEqual(actual, expected, [message])', 55 | 'assert.notDeepStrictEqual(actual, expected, [message])', 56 | 'assert.fail(actual, expected, message, operator)', 57 | 'assert.throws(block, [error], [message])', 58 | 'assert.doesNotThrow(block, [message])', 59 | 'assert.ifError(value)', 60 | 'console.assert(value, [message])' 61 | ], 62 | requirePatterns: [ 63 | 'assert = require("assert")', 64 | 'assert = require("power-assert")' 65 | ], 66 | importPatterns: [ 67 | 'import assert from "assert"', 68 | 'import * as assert from "assert"', 69 | 'import assert from "power-assert"', 70 | 'import * as assert from "power-assert"' 71 | ] 72 | } 73 | ``` 74 | 75 | ## Changelog 76 | 77 | See [CHANGELOG.md](https://github.com/unassert-js/webpack-unassert-loader/blob/master/CHANGELOG.md). 78 | 79 | ## License 80 | 81 | Licensed under the MIT license. See [LICENSE](https://github.com/unassert-js/webpack-unassert-loader/blob/master/LICENSE). 82 | 83 | [travis-url]: https://travis-ci.org/unassert-js/webpack-unassert-loader 84 | [travis-image]: https://secure.travis-ci.org/unassert-js/webpack-unassert-loader.svg?branch=master 85 | 86 | [npm-url]: https://npmjs.org/package/webpack-unassert-loader 87 | [npm-image]: https://badge.fury.io/js/webpack-unassert-loader.svg 88 | 89 | [depstat-url]: https://gemnasium.com/unassert-js/webpack-unassert-loader 90 | [depstat-image]: https://gemnasium.com/unassert-js/webpack-unassert-loader.svg 91 | 92 | [license-url]: https://github.com/unassert-js/webpack-unassert-loader/blob/master/LICENSE 93 | [license-image]: http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat 94 | --------------------------------------------------------------------------------