Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
[UPDATE] add minify param
Browse files Browse the repository at this point in the history
  • Loading branch information
poppinlp committed Aug 2, 2017
1 parent 79f2faf commit 1b3ad8a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,2 @@
- 0.1.1 add `minify` param
- 0.1.0 init
14 changes: 12 additions & 2 deletions README.md
Expand Up @@ -13,7 +13,7 @@ Build your own lodash since there's too many methods in lodash which you may not

- packed by rollup
- output in IIFE format or ECMAScript Module
- use in CLI or nodejs
- use in nodejs or [CLI]()
- produce minify dist and source map

## Install
Expand Down Expand Up @@ -44,6 +44,14 @@ Default: `[]`

Description: lodash methods which you want.

#### minify

Type: `<boolean>`

Default: `true`

Description: minify the dist or not.

#### output

Type: `<string>`
Expand All @@ -60,11 +68,13 @@ The export module will response a promise which include `code`.

```js
const lodashBuilder = require('lodash-builder');

lodashBuilder({
methods: [
'assignIn',
'clone'
]
],
output: 'path/to/target'
});
```

Expand Down
29 changes: 16 additions & 13 deletions index.js
Expand Up @@ -4,20 +4,21 @@ const
const
rollup = require('rollup'),
mkdirp = require('mkdirp'),
uglify = require('uglify-es'),
assign = require('lodash/assign'),
commonjs = require('rollup-plugin-commonjs'),
nodeResolve = require('rollup-plugin-node-resolve');
const DEFAULT_CONFIG = {
methods: [],
minify: true
};

module.exports = ({
methods = [],
moduleName = '',
context = 'window',
format = 'iife',
output
}) => {
module.exports = (config = {}) => {
const
BUILD_FILE_PATH = path.normalize(`_lodash-builder-${Date.now()}.js`),
lodashImportList = [],
lodashExportList = [];
lodashExportList = [],
{ methods, minify, output } = assign({}, DEFAULT_CONFIG, config);

methods.forEach(name => {
lodashImportList.push(`import _${name} from 'lodash/${name}';`);
Expand All @@ -33,7 +34,7 @@ module.exports = ({

return rollup.rollup({
entry: BUILD_FILE_PATH,
context,
context: 'window',
plugins: [
nodeResolve({
jsnext: true,
Expand All @@ -49,14 +50,16 @@ module.exports = ({
})
]
}).then(bundle => bundle.generate({
format,
moduleName
format: 'iife',
moduleName: ''
})).then(({ code }) => {
const result = minify ? uglify.minify(code).code : code;

if (output) {
mkdirp(path.parse(output).dir);
fs.writeFileSync(output, code);
fs.writeFileSync(output, result);
} else {
console.log(code);
console.log(result);
}

fs.unlinkSync(BUILD_FILE_PATH);
Expand Down
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "lodash-builder",
"version": "0.1.0",
"version": "0.1.1",
"description": "Build your own lodash by config.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -33,7 +33,8 @@
"mkdirp": "0.5.1",
"rollup": "0.45.2",
"rollup-plugin-commonjs": "8.0.2",
"rollup-plugin-node-resolve": "3.0.0"
"rollup-plugin-node-resolve": "3.0.0",
"uglify-es": "3.0.27"
},
"devDependencies": {
"ava": "0.21.0",
Expand Down
18 changes: 16 additions & 2 deletions test/test.js
Expand Up @@ -4,7 +4,11 @@ import fs from 'fs';
import test from 'ava';
import lodashBuilder from '../index.js';

test('[output stdout] should build with no error', async t => {
test('default params', async t => {
await t.notThrows(lodashBuilder());
});

test('minify and output to stdout', async t => {
await t.notThrows(lodashBuilder({
methods: [
'assignIn',
Expand All @@ -13,7 +17,7 @@ test('[output stdout] should build with no error', async t => {
}));
});

test('[output file] should build with no error', async t => {
test('minify and output to file', async t => {
const tmpFile = path.join(os.tmpdir(), 'lodash-builder-tmp-dist.js');

await t.notThrows(lodashBuilder({
Expand All @@ -26,3 +30,13 @@ test('[output file] should build with no error', async t => {

fs.unlinkSync(tmpFile);
});

test('no minify and output to stdout', async t => {
await t.notThrows(lodashBuilder({
methods: [
'assignIn',
'clone'
],
minify: false
}));
});
9 changes: 8 additions & 1 deletion yarn.lock
Expand Up @@ -903,7 +903,7 @@ combined-stream@^1.0.5, combined-stream@~1.0.5:
dependencies:
delayed-stream "~1.0.0"

commander@^2.9.0:
commander@^2.9.0, commander@~2.11.0:
version "2.11.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-2.11.0.tgz#157152fd1e7a6c8d98a5b715cf376df928004563"

Expand Down Expand Up @@ -3473,6 +3473,13 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

uglify-es@^3.0.27:
version "3.0.27"
resolved "https://registry.yarnpkg.com/uglify-es/-/uglify-es-3.0.27.tgz#391790388f369196be23a49caeb0d5c424fa774e"
dependencies:
commander "~2.11.0"
source-map "~0.5.1"

uglify-js@^2.6:
version "2.8.29"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-2.8.29.tgz#29c5733148057bb4e1f75df35b7a9cb72e6a59dd"
Expand Down

0 comments on commit 1b3ad8a

Please sign in to comment.