Skip to content

Instantly share code, notes, and snippets.

@twada
Last active January 15, 2019 08:16
Show Gist options
  • Save twada/ba57689d44f8d0a4e9c470d8169498b7 to your computer and use it in GitHub Desktop.
Save twada/ba57689d44f8d0a4e9c470d8169498b7 to your computer and use it in GitHub Desktop.
example: unassert but preserve comments
module.exports = function (api) {
const presets = [];
const plugins = [];
let retainLines = false;
let comments = true;
if (api.env('production')) {
plugins.push('babel-plugin-unassert');
retainLines = true;
}
return {
comments,
retainLines,
presets,
plugins
};
};
/**
* unassert example with Babel7
*
* Copyright (c) 2019 Takuto Wada
* Licensed under the MIT license.
* http://twada.mit-license.org/
*/
'use strict';
// will be removed under production
const assert = require('assert');
/**
* adds augend and addend
*
* @param {number} augend - the one to which the others are added
* @param {number} addend - a quantity to be added to another
* @return {number} - result of caluclation
*/
function add (augend, addend) {
// checking augend
assert(!isNaN(augend));
/**
* checking addend
*/
assert.ok(!isNaN(addend));
return augend + addend;
}
module.exports = {
add
};
$ npm run gen:prod
> pd-preserve-comment-unassert@1.0.0 gen:prod /path/to/pd-preserve-comment-unassert
> BABEL_ENV=production babel calc.js
/**
* unassert example with Babel7
*
* Copyright (c) 2019 Takuto Wada
* Licensed under the MIT license.
* http://twada.mit-license.org/
*/
'use strict';
// will be removed under production
/**
* adds augend and addend
*
* @param {number} augend - the one to which the others are added
* @param {number} addend - a quantity to be added to another
* @return {number} - result of caluclation
*/
function add(augend, addend) {
// checking augend
/**
* checking addend
*/
return augend + addend;
}
module.exports = {
add };
$
{
"name": "pd-preserve-comment-unassert",
"description": "preserve comment example",
"version": "1.0.0",
"author": {
"name": "Takuto Wada",
"email": "takuto.wada@gmail.com",
"url": "https://github.com/twada"
},
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"babel-plugin-unassert": "^3.0.0"
},
"keywords": [],
"license": "MIT",
"main": "index.js",
"private": true,
"scripts": {
"gen:dev": "babel calc.js",
"gen:prod": "BABEL_ENV=production babel calc.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment