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

Run cli.js through babel #246

Merged
merged 9 commits into from
Dec 19, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 18 additions & 7 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Search open/closed issues before submitting since someone might have asked the s
<!--- Provide a general summary of the issue in the title above -->

### 🎛 Configuration (.babelrc, package.json, cli command)

<!--- If describing a bug, tell us what your babel configuration looks like -->

```js
Expand All @@ -18,31 +19,41 @@ Search open/closed issues before submitting since someone might have asked the s
```

### 🤔 Expected Behavior

<!--- If you're describing a bug, tell us what should happen -->

<!--- If you're suggesting a change/improvement, tell us how it should work -->

### 😯 Current Behavior

<!--- If describing a bug, tell us what happens instead of the expected behavior -->

<!--- If you are seeing an error, please include the full error message and stack trace -->

<!--- If suggesting a change/improvement, explain the difference from current behavior -->

### 💁 Possible Solution

<!--- Not obligatory, but suggest a fix/reason for the bug, -->

<!--- or ideas how to implement the addition or change -->

### 🔦 Context

<!--- How has this issue affected you? What are you trying to accomplish? -->

<!--- Providing context helps us come up with a solution that is most useful in the real world -->

### 🌍 Your Environment

<!--- Include as many relevant details about the environment you experienced the bug in -->

| Software | Version(s)
| ---------------- | ----------
| Parcel |
| Node |
| npm/Yarn |
| Operating System |
| Software | Version(s) |
| ---------------- | ---------- |
| Parcel |
| Node |
| npm/Yarn |
| Operating System |

<!-- Love parcel? Please consider supporting our collective:
👉 https://opencollective.com/parcel/donate -->
👉 https://opencollective.com/parcel/donate -->
7 changes: 1 addition & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,24 @@ yarn test
[node]: https://nodejs.org/
[yarn]: https://yarnpkg.com/


## Financial contributions

We also welcome financial contributions in full transparency on our [open collective](https://opencollective.com/parcel).
Anyone can file an expense. If the expense makes sense for the development of the community, it will be "merged" in the ledger of our open collective by the core contributors and the person who filed the expense will be reimbursed.


## Credits


### Contributors

Thank you to all the people who have already contributed to parcel!
<a href="graphs/contributors"><img src="https://opencollective.com/parcel/contributors.svg?width=890" /></a>


### Backers

Thank you to all our backers! [[Become a backer](https://opencollective.com/parcel#backer)]

<a href="https://opencollective.com/parcel#backers" target="_blank"><img src="https://opencollective.com/parcel/backers.svg?width=890"></a>


### Sponsors

Thank you to all our sponsors! (please ask your company to also support this open source project by [becoming a sponsor](https://opencollective.com/parcel#sponsor))
Expand All @@ -71,4 +66,4 @@ Thank you to all our sponsors! (please ask your company to also support this ope
<a href="https://opencollective.com/parcel/sponsor/6/website" target="_blank"><img src="https://opencollective.com/parcel/sponsor/6/avatar.svg"></a>
<a href="https://opencollective.com/parcel/sponsor/7/website" target="_blank"><img src="https://opencollective.com/parcel/sponsor/7/avatar.svg"></a>
<a href="https://opencollective.com/parcel/sponsor/8/website" target="_blank"><img src="https://opencollective.com/parcel/sponsor/8/avatar.svg"></a>
<a href="https://opencollective.com/parcel/sponsor/9/website" target="_blank"><img src="https://opencollective.com/parcel/sponsor/9/avatar.svg"></a>
<a href="https://opencollective.com/parcel/sponsor/9/website" target="_blank"><img src="https://opencollective.com/parcel/sponsor/9/avatar.svg"></a>
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
```shell
yarn global add parcel-bundler
```

or with npm:

```shell
npm install -g parcel-bundler
```
Expand Down Expand Up @@ -97,14 +99,12 @@ All feedback and suggestions are welcome!
This project exists thanks to all the people who contribute. [[Contribute]](CONTRIBUTING.md).
<a href="graphs/contributors"><img src="https://opencollective.com/parcel/contributors.svg?width=890" /></a>


## Backers

Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/parcel#backer)]

<a href="https://opencollective.com/parcel#backers" target="_blank"><img src="https://opencollective.com/parcel/backers.svg?width=890"></a>


## Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/parcel#sponsor)]
Expand All @@ -120,8 +120,6 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
<a href="https://opencollective.com/parcel/sponsor/8/website" target="_blank"><img src="https://opencollective.com/parcel/sponsor/8/avatar.svg"></a>
<a href="https://opencollective.com/parcel/sponsor/9/website" target="_blank"><img src="https://opencollective.com/parcel/sponsor/9/avatar.svg"></a>



## License

MIT
109 changes: 5 additions & 104 deletions bin/cli.js
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,104 +1,5 @@
#!/usr/bin/env node

require('v8-compile-cache');
const chalk = require('chalk');
const program = require('commander');
const version = require('../package.json').version;

program.version(version);

program
.command('serve [input]')
.description('starts a development server')
.option('-p, --port <port>', 'set the port to serve on. defaults to 1234')
.option('-o, --open', 'automatically open in default browser')
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
)
.option(
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--no-hmr', 'disable hot module replacement')
.option('--no-cache', 'disable the filesystem cache')
.option('-V, --version', 'output the version number')
.action(bundle);

program
.command('watch [input]')
.description('starts the bundler in watch mode')
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
)
.option(
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--no-hmr', 'disable hot module replacement')
.option('--no-cache', 'disable the filesystem cache')
.action(bundle);

program
.command('build [input]')
.description('bundles for production')
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
)
.option(
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--no-minify', 'disable minification')
.option('--no-cache', 'disable the filesystem cache')
.action(bundle);

program
.command('help [command]')
.description('display help information for a command')
.action(function(command) {
let cmd = program.commands.find(c => c.name() === command) || program;
cmd.help();
});

program.on('--help', function() {
console.log('');
console.log(
' Run `' +
chalk.bold('parcel help <command>') +
'` for more information on specific commands'
);
console.log('');
});

// Make serve the default command
var args = process.argv;
if (!args[2] || !program.commands.some(c => c.name() === args[2])) {
args.splice(2, 0, 'serve');
}

program.parse(args);

async function bundle(main, command) {
// Require bundler here so the help command is fast
const Bundler = require('../');

if (command.name() === 'build') {
process.env.NODE_ENV = 'production';
} else {
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
}

const bundler = new Bundler(main, command);

if (command.name() === 'serve') {
const server = await bundler.serve(command.port || 1234);
if (command.open) {
require('opn')(`http://localhost:${server.address().port}`);
}
} else {
bundler.bundle();
}
}
// Node 8 supports native async functions - no need to use compiled code!
module.exports =
parseInt(process.versions.node, 10) < 8
? require('../lib/cli')
: require('../src/cli');
8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"build": "babel src -d lib",
"prepublish": "yarn build",
"precommit": "lint-staged",
"postinstall": "node -e \"console.log('\\u001b[35m\\u001b[1mLove Parcel? You can now donate to our open collective:\\u001b[22m\\u001b[39m\\n > \\u001b[34mhttps://opencollective.com/parcel/donate\\u001b[0m')\""
"postinstall":
"node -e \"console.log('\\u001b[35m\\u001b[1mLove Parcel? You can now donate to our open collective:\\u001b[22m\\u001b[39m\\n > \\u001b[34mhttps://opencollective.com/parcel/donate\\u001b[0m')\""
},
"bin": {
"parcel": "bin/cli.js"
Expand All @@ -75,10 +76,7 @@
"node": ">= 6.0.0"
},
"lint-staged": {
"*.{js,json,md}": [
"prettier --write",
"git add"
]
"*.{js,json,md}": ["prettier --write", "git add"]
},
"collective": {
"type": "opencollective",
Expand Down
104 changes: 104 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env node

require('v8-compile-cache');
const chalk = require('chalk');
const program = require('commander');
const version = require('../package.json').version;

program.version(version);

program
.command('serve [input]')
.description('starts a development server')
.option('-p, --port <port>', 'set the port to serve on. defaults to 1234')
.option('-o, --open', 'automatically open in default browser')
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
)
.option(
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--no-hmr', 'disable hot module replacement')
.option('--no-cache', 'disable the filesystem cache')
.option('-V, --version', 'output the version number')
.action(bundle);

program
.command('watch [input]')
.description('starts the bundler in watch mode')
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
)
.option(
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--no-hmr', 'disable hot module replacement')
.option('--no-cache', 'disable the filesystem cache')
.action(bundle);

program
.command('build [input]')
.description('bundles for production')
.option(
'-d, --out-dir <path>',
'set the output directory. defaults to "dist"'
)
.option(
'--public-url <url>',
'set the public URL to serve on. defaults to the same as the --out-dir option'
)
.option('--no-minify', 'disable minification')
.option('--no-cache', 'disable the filesystem cache')
.action(bundle);

program
.command('help [command]')
.description('display help information for a command')
.action(function(command) {
let cmd = program.commands.find(c => c.name() === command) || program;
cmd.help();
});

program.on('--help', function() {
console.log('');
console.log(
' Run `' +
chalk.bold('parcel help <command>') +
'` for more information on specific commands'
);
console.log('');
});

// Make serve the default command
var args = process.argv;
if (!args[2] || !program.commands.some(c => c.name() === args[2])) {
args.splice(2, 0, 'serve');
}

program.parse(args);

async function bundle(main, command) {
// Require bundler here so the help command is fast
const Bundler = require('../');

if (command.name() === 'build') {
process.env.NODE_ENV = 'production';
} else {
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
}

const bundler = new Bundler(main, command);

if (command.name() === 'serve') {
const server = await bundler.serve(command.port || 1234);
if (command.open) {
require('opn')(`http://localhost:${server.address().port}`);
}
} else {
bundler.bundle();
}
}
4 changes: 3 additions & 1 deletion test/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe('plugins', function() {
});

it('should load package.json from parent tree', async function() {
let b = await bundle(__dirname + '/integration/plugins/sub-folder/index.js');
let b = await bundle(
__dirname + '/integration/plugins/sub-folder/index.js'
);

assertBundleTree(b, {
name: 'index.js',
Expand Down