Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transpiling ES6 to ES5 on "prepublish" -> fixing Safari "const" #4

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions .babelrc
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
2 changes: 1 addition & 1 deletion .gitignore
@@ -1 +1 @@
node_modules
node_modules
3 changes: 3 additions & 0 deletions .npmignore
@@ -0,0 +1,3 @@
.babelrc
webpack.config.js
/src/
46 changes: 1 addition & 45 deletions index.js
@@ -1,45 +1 @@
'use strict';
const sliceAnsi = require('slice-ansi');
const stringWidth = require('string-width');

module.exports = (input, columns, opts) => {
opts = Object.assign({
position: 'end'
}, opts);

const position = opts.position;
const ellipsis = '…';

if (typeof input !== 'string') {
throw new TypeError(`Expected \`input\` to be a string, got ${typeof input}`);
}

if (typeof columns !== 'number') {
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
}

if (columns < 1) {
return '';
}

if (columns === 1) {
return ellipsis;
}

const length = stringWidth(input);

if (length <= columns) {
return input;
}

if (position === 'start') {
return ellipsis + sliceAnsi(input, length - columns + 1, length);
} else if (position === 'middle') {
const half = Math.floor(columns / 2);
return sliceAnsi(input, 0, half) + ellipsis + sliceAnsi(input, length - (columns - half) + 1, length);
} else if (position === 'end') {
return sliceAnsi(input, 0, columns - 1) + ellipsis;
}

throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
};
module.exports = require("./lib/bundle.js");
1 change: 1 addition & 0 deletions lib/bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion package.json
Expand Up @@ -13,7 +13,9 @@
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava",
"build": "webpack --progress --colors --display-error-details --config webpack.config.js",
"prepublish": "npm run build"
},
"files": [
"index.js"
Expand All @@ -37,6 +39,12 @@
},
"devDependencies": {
"ava": "*",
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"uglify-es": "git://github.com/mishoo/UglifyJS2.git#harmony",
"uglifyjs-webpack-plugin": "^0.4.3",
"webpack": "^2.5.1",
"xo": "*"
}
}
45 changes: 45 additions & 0 deletions src/index.js
@@ -0,0 +1,45 @@
'use strict';
const sliceAnsi = require('slice-ansi');
const stringWidth = require('string-width');

module.exports = (input, columns, opts) => {
opts = Object.assign({
position: 'end'
}, opts);

const position = opts.position;
const ellipsis = '…';

if (typeof input !== 'string') {
throw new TypeError(`Expected \`input\` to be a string, got ${typeof input}`);
}

if (typeof columns !== 'number') {
throw new TypeError(`Expected \`columns\` to be a number, got ${typeof columns}`);
}

if (columns < 1) {
return '';
}

if (columns === 1) {
return ellipsis;
}

const length = stringWidth(input);

if (length <= columns) {
return input;
}

if (position === 'start') {
return ellipsis + sliceAnsi(input, length - columns + 1, length);
} else if (position === 'middle') {
const half = Math.floor(columns / 2);
return sliceAnsi(input, 0, half) + ellipsis + sliceAnsi(input, length - (columns - half) + 1, length);
} else if (position === 'end') {
return sliceAnsi(input, 0, columns - 1) + ellipsis;
}

throw new Error(`Expected \`options.position\` to be either \`start\`, \`middle\` or \`end\`, got ${position}`);
};
22 changes: 22 additions & 0 deletions webpack.config.js
@@ -0,0 +1,22 @@
const path = require('path');
const webpack = require('webpack');
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
entry: ['./src/index.js'],
output: {
path: path.join(__dirname, './lib'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.js$/,
use: ['babel-loader']
}
]
},
plugins: [
new UglifyJSPlugin()
],
};