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

Use Rollup for bundling dependencies #242

Merged
merged 16 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
build
yarn.lock
56 changes: 41 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./source/index.js"
"default": "./build/index.js"
},
"sideEffects": false,
"engines": {
"node": ">=16.10"
},
"scripts": {
"test": "xo && ava && tsd"
"prepare": "npm run build",
"build": "rollup --config",
"test": "xo && ava && tsd && npm run test:build",
"test:build": "npm run build && ava test/build.js"
},
"files": [
"source",
"build",
"index.d.ts"
],
"keywords": [
Expand All @@ -44,38 +48,60 @@
"cmd",
"console"
],
"dependencies": {
"_actualDependencies": [
"@types/minimist",
"camelcase-keys",
"decamelize",
"decamelize-keys",
"hard-rejection",
"minimist-options",
"normalize-package-data",
"read-pkg-up",
"redent",
"trim-newlines",
"type-fest",
"yargs-parser"
],
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.3",
"@rollup/plugin-json": "^6.0.0",
"@rollup/plugin-node-resolve": "^15.1.0",
"@types/minimist": "^1.2.2",
"ava": "^5.3.1",
"camelcase-keys": "^8.0.2",
"common-tags": "^1.8.2",
"decamelize": "^6.0.0",
"decamelize-keys": "^2.0.1",
"execa": "^7.2.0",
"hard-rejection": "^2.1.0",
"indent-string": "^5.0.0",
"minimist-options": "4.1.0",
"normalize-package-data": "^5.0.0",
"read-pkg": "^8.0.0",
"read-pkg-up": "^9.1.0",
"redent": "^4.0.0",
"rollup": "^3.27.0",
"rollup-plugin-license": "^3.0.1",
"strip-comments": "^2.0.1",
"trim-newlines": "^5.0.0",
"tsd": "^0.28.1",
"type-fest": "^3.9.0",
"xo": "^0.55.0",
"yargs-parser": "^21.1.1"
},
"devDependencies": {
"ava": "^5.2.0",
"common-tags": "^1.8.2",
"execa": "^7.1.1",
"indent-string": "^5.0.0",
"read-pkg": "^8.0.0",
"tsd": "^0.28.1",
"xo": "^0.54.2"
},
"xo": {
"rules": {
"unicorn/no-process-exit": "off",
"unicorn/error-message": "off"
}
},
"ignores": [
"build"
]
},
"ava": {
"files": [
"test/*"
"test/*",
"!test/build.js"
]
}
}
51 changes: 51 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import json from '@rollup/plugin-json';
import commonjs from '@rollup/plugin-commonjs';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import license from 'rollup-plugin-license';
/// import strip from 'strip-comments';
import {defineConfig} from 'rollup';

const outputDirectory = 'build';

export default defineConfig({
input: [
'source/index.js',
'source/options.js',
'source/parser.js',
'source/utils.js',
'source/validate.js',
],
output: {
dir: outputDirectory,
interop: 'esModule',
generatedCode: {
preset: 'es2015',
},
chunkFileNames: '[name].js',
manualChunks(id) {
if (id.includes('node_modules')) {
return 'dependencies';
}
},
// TODO: strip deletes `https://...`
// plugins: [{
// name: 'strip-dependency-comments',
// renderChunk(code, chunk) {
// return chunk.name === 'dependencies' ? strip(code) : null;
// },
// }],
},
preserveEntrySignatures: 'allow-extension',
plugins: [
nodeResolve(),
commonjs({
include: 'node_modules/**',
}),
json(),
license({
thirdParty: {
output: `${outputDirectory}/licenses.md`,
},
}),
],
});
40 changes: 40 additions & 0 deletions test/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-disable ava/no-ignored-test-files */
tommy-mitchell marked this conversation as resolved.
Show resolved Hide resolved
import test from 'ava';
import {readPackage} from 'read-pkg';
import meow from '../build/index.js';
import {spawnFixture} from './_utils.js';

test('main', t => {
const cli = meow(`
Usage
foo <input>
`, {
importMeta: import.meta,
argv: 'foo --foo-bar --u cat -- unicorn cake'.split(' '),
flags: {
unicorn: {shortFlag: 'u'},
meow: {default: 'dog'},
'--': true,
},
});

t.like(cli, {
input: ['foo'],
flags: {
fooBar: true,
meow: 'dog',
unicorn: 'cat',
'--': ['unicorn', 'cake'],
},
pkg: {
name: 'meow',
},
help: '\n CLI app helper\n\n Usage\n foo <input>\n',
});
});

test('spawn cli and show version', async t => {
const pkg = await readPackage();
const {stdout} = await spawnFixture(['--version']);
t.is(stdout, pkg.version);
});
26 changes: 26 additions & 0 deletions test/fixtures/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env node
tommy-mitchell marked this conversation as resolved.
Show resolved Hide resolved
import process from 'node:process';
import meow from '../../build/index.js';

const cli = meow(`
Usage
foo <input>
`, {
importMeta: import.meta,
description: 'Custom description',
autoVersion: !process.argv.includes('--no-auto-version'),
autoHelp: !process.argv.includes('--no-auto-help'),
flags: {
unicorn: {shortFlag: 'u'},
meow: {default: 'dog'},
camelCaseOption: {default: 'foo'},
},
});

if (cli.flags.camelCaseOption === 'foo') {
for (const x of Object.keys(cli.flags)) {
console.log(x);
}
} else {
console.log(cli.flags.camelCaseOption);
}