Skip to content

Commit 3983336

Browse files
authored
feat: Support Dev Expressions (Fix #47) (#48)
1 parent b48af32 commit 3983336

File tree

9 files changed

+36
-32
lines changed

9 files changed

+36
-32
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ The `init` command will create a minimal `package.json` with `source`, `main`, `
8585
8686
Then use `npm run` or `yarn` to invoke npm scripts as you normally would.
8787

88+
> See [examples](examples) for common use cases using `klap`.
89+
8890
### :anger: Granular Control
8991

9092
`klap` uses sensible defaults for most part. However, as needed, use below properties in `package.json` to fine tune `klap`. You can also use `cli flags` to control config options for `klap`.
@@ -134,6 +136,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
134136

135137
<!-- markdownlint-enable -->
136138
<!-- prettier-ignore-end -->
139+
137140
<!-- ALL-CONTRIBUTORS-LIST:END -->
138141

139142
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

cli.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ const { init, klap, read, log, error, info } = require('./dist');
44
const { name, version } = require('./package.json');
55
const command = process.argv[2];
66

7+
const defaultEnvironment = {
8+
build: 'production',
9+
prod: 'production',
10+
watch: 'development',
11+
start: 'development',
12+
};
13+
714
(async () => {
815
switch (command) {
916
case 'init':
@@ -15,7 +22,11 @@ const command = process.argv[2];
1522
case 'watch':
1623
case 'start':
1724
log(`${name}@${version} - Working on ${command}...`);
18-
const pkg = JSON.parse(await read(path.join(process.cwd(), 'package.json')));
25+
process.env.NODE_ENV =
26+
process.env.NODE_ENV || defaultEnvironment[command];
27+
const pkg = JSON.parse(
28+
await read(path.join(process.cwd(), 'package.json'))
29+
);
1930
await klap(command, pkg);
2031
break;
2132
case 'help':

examples/README.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/dev-expressions/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "dev-expressions",
3+
"version": "0.0.0",
4+
"private": true,
5+
"main": "dist/index.cjs.js",
6+
"module": "dist/index.esm.js",
7+
"source": "src/index.js",
8+
"browser": "dist/index.js"
9+
}

examples/dev-expressions/src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const sum = (a, b) => {
2+
if (__DEV__) {
3+
console.log('this console log should not end up in dist code');
4+
}
5+
return a + b;
6+
};

examples/minimal/public/index.html

Lines changed: 0 additions & 13 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"@types/node": "14.0.1",
5454
"@zeit/ncc": "0.22.1",
5555
"babel-plugin-codegen": "4.0.1",
56+
"babel-plugin-dev-expression": "0.2.2",
5657
"babel-plugin-emotion": "10.0.33",
5758
"babel-plugin-macros": "2.8.0",
5859
"babel-plugin-styled-components": "1.10.7",

src/babel.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import presetTs from '@babel/preset-typescript';
66
import presetReact from '@babel/preset-react';
77

88
// babel plugins
9+
import pluginDevExpression from 'babel-plugin-dev-expression';
910
import pluginObjectRestSpread from '@babel/plugin-proposal-object-rest-spread';
1011
import pluginAsyncToPromise from 'babel-plugin-transform-async-to-promises';
1112
import pluginDecorators from '@babel/plugin-proposal-decorators';
@@ -56,6 +57,7 @@ export const babelConfig = (command, pkg, options) => {
5657
];
5758

5859
const plugins = [
60+
pluginDevExpression,
5961
[pluginObjectRestSpread, { loose: true, useBuiltIns: true }],
6062
[pluginAsyncToPromise, { inlineHelpers: true, externalHelpers: true }],
6163
[pluginDecorators, { legacy: true }],

test.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env bash
2-
# set -x
2+
set -x
33

44
git clean -fdX
55

6-
npm install
6+
npm link
77

88
npm run build
99

1010
cd examples
1111

12-
for dir in minimal scaffold scaffold-typescript react-component react-typescript react-sc-typescript dynamic-imports
12+
for dir in $(ls)
1313
do
1414
cd ${dir} && klap build && rm -rf dist/*.map && cd ..
1515
done

0 commit comments

Comments
 (0)