Skip to content

Commit

Permalink
Merge pull request #6 from repaygithub/add-config-option
Browse files Browse the repository at this point in the history
Adds config option for libraries
  • Loading branch information
Dhalton committed Apr 8, 2019
2 parents 98f8ab5 + b935faf commit 2f80937
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 15 deletions.
43 changes: 41 additions & 2 deletions modules/repay-scripts/README.md
Expand Up @@ -47,10 +47,49 @@ Options:
--babel-env set the babel environment
[string] [choices: "development", "test", "production"] [default:
"production"]
--config, -c path to override configuration (not supported with --lib)
[string] [default: null]
--config, -c path to override configuration [string] [default: null]
--debug adds extra logging for debugging purposes
[boolean] [default: false]
-h, --help Show help [boolean]
-v, --version Show version number [boolean]
```

### Using `config` option

The config options is passed a file which should export a single function as the `module.exports` which will be called with the pre-made configuration and the options provided to `repay-scripts` as the arguments, and must return a configuration.

#### web app example

```
# shell command
repay-scripts dev --config webpack.config.js src/index.js
```

```js
// webpack.config.js
module.exports = (config, options) => {
// access options to determine what changes to make
if (options.command === "dev") {
config.performance = { hints: false };
}
// returns config whether modified or not
return config;
};
```

#### library example

```
# shell command
repay-scripts dev --config rollup.config.js --lib src/index.ts
```

```js
// rollup.config.js
module.exports = (config, options) => {
// remove ESM build
config.outputs.splice(0, 1);
// allways returns config
return config;
};
```
2 changes: 1 addition & 1 deletion modules/repay-scripts/package.json
@@ -1,6 +1,6 @@
{
"name": "@repay/scripts",
"version": "0.1.1",
"version": "0.2.0-next",
"description": "a build tool for developing front-end applications at REPAY",
"author": "JamesNimlos <jnimlos@repay.com>",
"homepage": "https://github.com/repaygithub/repay-ui#readme",
Expand Down
5 changes: 1 addition & 4 deletions modules/repay-scripts/src/cli.js
Expand Up @@ -29,7 +29,7 @@ function cli(cwd) {
config: {
type: 'string',
alias: 'c',
description: 'path to override configuration (not supported with --lib)',
description: 'path to override configuration',
default: null,
requiresArg: true,
},
Expand Down Expand Up @@ -62,9 +62,6 @@ function parseToConfig(argv) {
let config = argv.config

if (argv.config !== null) {
if (argv.lib) {
throw Error('`config` argument not supported for libraries')
}
const configPath = path.resolve(argv.config)
if (fs.existsSync(configPath)) {
config = configPath
Expand Down
5 changes: 5 additions & 0 deletions modules/repay-scripts/src/commands/build.js
Expand Up @@ -86,8 +86,13 @@ async function build(options) {
console.log('building...')
const input = path.resolve(options.cwd, options.entry)
const isLibrary = options.lib

if (isLibrary) {
let config = getRollupConfig(input, options)
if (options.config) {
config = require(options.config)(config, options)
options.debug && console.log('rollup configuration', config)
}
options.debug && console.log(config)
options.debug && console.log('starting bundler...')
const bundle = await rollup.rollup(pick(config, inputOptions))
Expand Down
17 changes: 16 additions & 1 deletion modules/repay-scripts/src/commands/dev.js
Expand Up @@ -16,8 +16,23 @@ async function dev(options) {
const isLibrary = options.lib
if (isLibrary) {
let config = getRollupConfig(input, options)
if (options.config) {
config = require(options.config)(config, options)
options.debug && console.log('rollup configuration', config)
}
options.debug && console.log('building library...')
rollup.watch(config)
let watcher = rollup.watch(config)
console.log(watcher)
watcher.on('event', event => {
if (event.code === 'ERROR') {
console.error('Error generating bundle')
console.error(event.error)
} else if (event.code === 'FATAL') {
console.error(`\nFatal Rollup Error [${event.error.code}]`)
console.error('\t' + event.error.toString() + '\n')
process.exit(1)
}
})
} else {
const PORT = options.port
let config = getWebpackConfig(input, options)
Expand Down
16 changes: 9 additions & 7 deletions modules/repay-scripts/src/configs/rollup.js
Expand Up @@ -26,27 +26,29 @@ const getSvgExternalizer = (distFile, cwd) => id => {

function getRollupConfig(input, { cwd }) {
const pkg = JSON.parse(fs.readFileSync(path.resolve(cwd, 'package.json'), 'utf8'))
if (!pkg.main || !pkg.module) {
throw new Error('package.json#main && package.json#module are required')
if (!pkg.main && !pkg.module) {
throw new Error('package.json#main or package.json#module are required')
}
const peerDeps = Object.keys(pkg.peerDependencies || {})
const externalDeps = Object.keys(pkg.peerDependencies || {}).concat(
Object.keys(pkg.dependencies || {})
)
return {
input,
external: id => isSvg(id) || peerDeps.includes(id),
external: id => isSvg(id) || externalDeps.includes(id),
output: [
{
pkg.module && {
file: path.resolve(cwd, pkg.module),
format: 'es',
sourcemap: true,
paths: getSvgExternalizer(path.resolve(cwd, pkg.module), cwd),
},
{
pkg.main && {
file: path.resolve(cwd, pkg.main),
format: 'cjs',
sourcemap: true,
paths: getSvgExternalizer(path.resolve(cwd, pkg.main), cwd),
},
],
].filter(Boolean),
plugins: [
rollupResolve({
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json', '.svg'],
Expand Down

0 comments on commit 2f80937

Please sign in to comment.