Skip to content

Commit 41e628c

Browse files
Bundle dependencies (#242)
1 parent 91b0f04 commit 41e628c

File tree

8 files changed

+184
-16
lines changed

8 files changed

+184
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2+
build
23
yarn.lock

package.json

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@
1313
"type": "module",
1414
"exports": {
1515
"types": "./index.d.ts",
16-
"default": "./source/index.js"
16+
"default": "./build/index.js"
1717
},
18+
"sideEffects": false,
1819
"engines": {
1920
"node": ">=16.10"
2021
},
2122
"scripts": {
22-
"test": "xo && ava && tsd"
23+
"prepare": "npm run build",
24+
"build": "rollup --config",
25+
"test": "xo && tsd && npm run build && ava"
2326
},
2427
"files": [
25-
"source",
28+
"build",
2629
"index.d.ts"
2730
],
2831
"keywords": [
@@ -44,34 +47,56 @@
4447
"cmd",
4548
"console"
4649
],
47-
"dependencies": {
50+
"_actualDependencies": [
51+
"@types/minimist",
52+
"camelcase-keys",
53+
"decamelize",
54+
"decamelize-keys",
55+
"hard-rejection",
56+
"minimist-options",
57+
"normalize-package-data",
58+
"read-pkg-up",
59+
"redent",
60+
"trim-newlines",
61+
"type-fest",
62+
"yargs-parser"
63+
],
64+
"devDependencies": {
65+
"@rollup/plugin-commonjs": "^25.0.3",
66+
"@rollup/plugin-json": "^6.0.0",
67+
"@rollup/plugin-node-resolve": "^15.1.0",
4868
"@types/minimist": "^1.2.2",
69+
"ava": "^5.3.1",
4970
"camelcase-keys": "^8.0.2",
71+
"common-tags": "^2.0.0-alpha.1",
5072
"decamelize": "^6.0.0",
5173
"decamelize-keys": "^2.0.1",
74+
"delete_comments": "^0.0.2",
75+
"execa": "^7.2.0",
76+
"globby": "^13.2.2",
5277
"hard-rejection": "^2.1.0",
78+
"indent-string": "^5.0.0",
5379
"minimist-options": "4.1.0",
5480
"normalize-package-data": "^5.0.0",
81+
"read-pkg": "^8.0.0",
5582
"read-pkg-up": "^10.0.0",
5683
"redent": "^4.0.0",
84+
"rollup": "^3.27.0",
85+
"rollup-plugin-license": "^3.0.1",
5786
"trim-newlines": "^5.0.0",
87+
"tsd": "^0.28.1",
5888
"type-fest": "^4.2.0",
89+
"xo": "^0.56.0",
5990
"yargs-parser": "^21.1.1"
6091
},
61-
"devDependencies": {
62-
"ava": "^5.3.1",
63-
"common-tags": "^1.8.2",
64-
"execa": "^7.2.0",
65-
"indent-string": "^5.0.0",
66-
"read-pkg": "^8.0.0",
67-
"tsd": "^0.28.1",
68-
"xo": "^0.56.0"
69-
},
7092
"xo": {
7193
"rules": {
7294
"unicorn/no-process-exit": "off",
7395
"unicorn/error-message": "off"
74-
}
96+
},
97+
"ignores": [
98+
"build"
99+
]
75100
},
76101
"ava": {
77102
"files": [

rollup.config.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {nodeResolve} from '@rollup/plugin-node-resolve';
2+
import commonjs from '@rollup/plugin-commonjs';
3+
import json from '@rollup/plugin-json';
4+
import license from 'rollup-plugin-license';
5+
import {globby} from 'globby';
6+
import {createTag, replaceResultTransformer} from 'common-tags';
7+
import {delete_comments as deleteComments} from 'delete_comments';
8+
import {defineConfig} from 'rollup';
9+
10+
/** Matches empty lines: https://stackoverflow.com/q/16369642/10292952 */
11+
const emptyLineRegex = /^\s*[\r\n]/gm;
12+
13+
const stripComments = createTag(
14+
{onEndResult: deleteComments},
15+
replaceResultTransformer(emptyLineRegex, ''),
16+
);
17+
18+
const outputDirectory = 'build';
19+
20+
export default defineConfig({
21+
input: await globby('source/**/*.js'),
22+
output: {
23+
dir: outputDirectory,
24+
interop: 'esModule',
25+
generatedCode: {
26+
preset: 'es2015',
27+
},
28+
chunkFileNames: '[name].js',
29+
manualChunks(id) {
30+
if (id.includes('node_modules')) {
31+
return 'dependencies';
32+
}
33+
},
34+
hoistTransitiveImports: false,
35+
plugins: [{
36+
name: 'strip-dependency-comments',
37+
renderChunk(code, chunk) {
38+
return chunk.name === 'dependencies' ? stripComments(code) : null;
39+
},
40+
}],
41+
},
42+
treeshake: {
43+
moduleSideEffects: 'no-external',
44+
},
45+
plugins: [
46+
nodeResolve(),
47+
commonjs({
48+
include: 'node_modules/**',
49+
}),
50+
json(),
51+
license({
52+
thirdParty: {
53+
output: `${outputDirectory}/licenses.md`,
54+
},
55+
}),
56+
],
57+
});

test/_utils.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'node:path';
22
import {fileURLToPath} from 'node:url';
33
import {execa} from 'execa';
4+
import {createTag, stripIndentTransformer, trimResultTransformer} from 'common-tags';
45

56
export const __dirname = path.dirname(fileURLToPath(import.meta.url));
67

@@ -15,3 +16,9 @@ export const spawnFixture = async (fixture = 'fixture.js', args = []) => {
1516

1617
return execa(getFixture(fixture), args);
1718
};
19+
20+
// Use old behavior prior to zspecza/common-tags#165
21+
export const stripIndent = createTag(
22+
stripIndentTransformer(),
23+
trimResultTransformer(),
24+
);

test/build.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import test from 'ava';
2+
import {readPackage} from 'read-pkg';
3+
import meow from '../build/index.js';
4+
import {spawnFixture} from './_utils.js';
5+
6+
test('main', t => {
7+
const cli = meow(`
8+
Usage
9+
foo <input>
10+
`, {
11+
importMeta: import.meta,
12+
argv: 'foo --foo-bar --u cat -- unicorn cake'.split(' '),
13+
flags: {
14+
unicorn: {
15+
shortFlag: 'u',
16+
},
17+
meow: {
18+
default: 'dog',
19+
},
20+
'--': true,
21+
},
22+
});
23+
24+
t.like(cli, {
25+
input: ['foo'],
26+
flags: {
27+
fooBar: true,
28+
meow: 'dog',
29+
unicorn: 'cat',
30+
'--': [
31+
'unicorn',
32+
'cake',
33+
],
34+
},
35+
pkg: {
36+
name: 'meow',
37+
},
38+
help: '\n CLI app helper\n\n Usage\n foo <input>\n',
39+
});
40+
});
41+
42+
test('spawn cli and show version', async t => {
43+
const pkg = await readPackage();
44+
const {stdout} = await spawnFixture(['--version']);
45+
t.is(stdout, pkg.version);
46+
});

test/choices.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava';
2-
import {stripIndent} from 'common-tags';
32
import meow from '../source/index.js';
3+
import {stripIndent} from './_utils.js';
44

55
const importMeta = import.meta;
66

test/errors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava';
2-
import {stripIndent} from 'common-tags';
32
import meow from '../source/index.js';
3+
import {stripIndent} from './_utils.js';
44

55
const importMeta = import.meta;
66

test/fixtures/build.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
import process from 'node:process';
3+
import meow from '../../build/index.js';
4+
5+
const cli = meow(`
6+
Usage
7+
foo <input>
8+
`, {
9+
importMeta: import.meta,
10+
description: 'Custom description',
11+
autoVersion: !process.argv.includes('--no-auto-version'),
12+
autoHelp: !process.argv.includes('--no-auto-help'),
13+
flags: {
14+
unicorn: {
15+
shortFlag: 'u',
16+
},
17+
meow: {
18+
default: 'dog',
19+
},
20+
camelCaseOption: {
21+
default: 'foo',
22+
},
23+
},
24+
});
25+
26+
if (cli.flags.camelCaseOption === 'foo') {
27+
for (const flagKey of Object.keys(cli.flags)) {
28+
console.log(flagKey);
29+
}
30+
} else {
31+
console.log(cli.flags.camelCaseOption);
32+
}

0 commit comments

Comments
 (0)