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

Adds config option for libraries #6

Merged
merged 3 commits into from Apr 8, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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