Skip to content

Commit

Permalink
fix: add generateTypes cli option, check false value correctly (#830)
Browse files Browse the repository at this point in the history
* fix: add generateTypes cli option, check false value correctly

* chore: add changeset file

* docs: cli option should be generateTypes

* chore: remove unnecessary comment
  • Loading branch information
JounQin authored May 5, 2021
1 parent edcd777 commit 54402ac
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-moose-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"microbundle": patch
---

fix: add generateTypes cli option, check false value correctly
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ Importing CSS files is supported via `import "./foo.css"`. By default, generated

```js
// with the default external CSS:
import './foo.css'; // generates a minified .css file in the output directory
import './foo.css'; // generates a minified .css file in the output directory

// with `microbundle --css inline`:
import css from './foo.css';
console.log(css); // the generated minified stylesheet
console.log(css); // the generated minified stylesheet
```

**CSS Modules:** CSS files with names ending in `.module.css` are treated as a [CSS Modules](https://github.com/css-modules/css-modules).
Expand Down Expand Up @@ -237,11 +237,11 @@ It's also possible to configure repeatable short names for each mangled property

The `--define` option can be used to inject or replace build-time constants when bundling. In addition to injecting string or number constants, prefixing the define name with `@` allows injecting JavaScript expressions.

| Build command | Source code | Output |
|---------------|-------------|--------|
`microbundle --define VERSION=2` | `console.log(VERSION)` | `console.log(2)`
`microbundle --define API_KEY='abc123'` | `console.log(API_KEY)` | `console.log("abc123")`
`microbundle --define @assign=Object.assign` | `assign(a, b)` | `Object.assign(a, b)`
| Build command | Source code | Output |
| -------------------------------------------- | ---------------------- | ----------------------- |
| `microbundle --define VERSION=2` | `console.log(VERSION)` | `console.log(2)` |
| `microbundle --define API_KEY='abc123'` | `console.log(API_KEY)` | `console.log("abc123")` |
| `microbundle --define @assign=Object.assign` | `assign(a, b)` | `Object.assign(a, b)` |

### All CLI Options <a name="options"></a>

Expand Down Expand Up @@ -279,6 +279,7 @@ Options
--jsx A custom JSX pragma like React.createElement (default: h)
--jsxImportSource Specify the automatic import source for JSX like preact
--tsconfig Specify the path to a custom tsconfig.json
--generateTypes Whether or not to generate types, if `types` or `typings` is set in `package.json` then it will default to be `true`
--css Where to output CSS: "inline" or "external" (default: "external")
--css-modules Configures .css to be treated as modules (default: null)
-h, --help Displays this message
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,10 @@ function createConfig(options, entry, format, writeMeta) {
: () => resolve(options.cwd, 'mangle.json');

const useTypescript = extname(entry) === '.ts' || extname(entry) === '.tsx';
const emitDeclaration = !!(options.generateTypes || pkg.types || pkg.typings);

const emitDeclaration =
options.generateTypes == null
? !!(pkg.types || pkg.typings)
: options.generateTypes;
const escapeStringExternals = ext =>
ext instanceof RegExp ? ext.source : escapeStringRegexp(ext);
const externalPredicate = new RegExp(
Expand Down
5 changes: 5 additions & 0 deletions src/prog.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export default handler => {
'A custom JSX pragma like React.createElement (default: h)',
)
.option('--tsconfig', 'Specify the path to a custom tsconfig.json')
.option(
'--generateTypes',
'Whether or not to generate types , if `types` or `typings` is set in `package.json` then it will default to be `true`',
)
.example('microbundle build --tsconfig tsconfig.build.json');

prog
Expand All @@ -89,5 +93,6 @@ export default handler => {
i: ['entry', 'entries', 'e'],
w: ['watch'],
},
boolean: ['generateTypes'],
});
};
57 changes: 57 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2538,6 +2538,63 @@ exports[`fixtures build shebang with microbundle 5`] = `
"
`;
exports[`fixtures build ts-custom-declaration with microbundle 1`] = `
"Used script: microbundle --generateTypes false
Directory tree:
ts-custom-declaration
dist
index.esm.js
index.esm.js.map
index.js
index.js.map
index.umd.js
index.umd.js.map
package.json
src
index.js
tsconfig.json
types
index.d.ts
Build \\"tsCustomDeclarations\\" to dist:
55 B: index.js.gz
33 B: index.js.br
61 B: index.esm.js.gz
45 B: index.esm.js.br
175 B: index.umd.js.gz
125 B: index.umd.js.br"
`;
exports[`fixtures build ts-custom-declaration with microbundle 2`] = `6`;
exports[`fixtures build ts-custom-declaration with microbundle 3`] = `
"function n(){return 42}export{n as foo};
//# sourceMappingURL=index.esm.js.map
"
`;
exports[`fixtures build ts-custom-declaration with microbundle 4`] = `
"exports.foo=function(){return 42};
//# sourceMappingURL=index.js.map
"
`;
exports[`fixtures build ts-custom-declaration with microbundle 5`] = `
"!function(e,o){\\"object\\"==typeof exports&&\\"undefined\\"!=typeof module?o(exports):\\"function\\"==typeof define&&define.amd?define([\\"exports\\"],o):o((e||self).tsCustomDeclarations={})}(this,function(e){e.foo=function(){return 42}});
//# sourceMappingURL=index.umd.js.map
"
`;
exports[`fixtures build ts-custom-declaration with microbundle 6`] = `1`;
exports[`fixtures build ts-custom-declaration with microbundle 7`] = `
"export const foo: () => number;
"
`;
exports[`fixtures build ts-declaration with microbundle 1`] = `
"Used script: microbundle
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/ts-custom-declaration/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "ts-custom-declarations",
"main": "dist/index.js",
"types": "types/index.d.ts",
"source": "src/index.js",
"scripts": {
"build": "microbundle --generateTypes false"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/ts-custom-declaration/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {
return 42;
}
6 changes: 6 additions & 0 deletions test/fixtures/ts-custom-declaration/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"noEmit": true,
"allowJs": true
}
}
1 change: 1 addition & 0 deletions test/fixtures/ts-custom-declaration/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo: () => number;
4 changes: 3 additions & 1 deletion tools/build-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export const buildDirectory = async fixtureDir => {
const dist = resolve(`${fixturePath}/dist`);
// clean up
await rimraf(dist);
await rimraf(resolve(`${fixturePath}/types`));
if (!fixturePath.endsWith('ts-custom-declaration')) {
await rimraf(resolve(`${fixturePath}/types`));
}
await rimraf(resolve(`${fixturePath}/.rts2_cache_cjs`));
await rimraf(resolve(`${fixturePath}/.rts2_cache_es`));
await rimraf(resolve(`${fixturePath}/.rts2_cache_umd`));
Expand Down

0 comments on commit 54402ac

Please sign in to comment.