Skip to content

Commit 54402ac

Browse files
authored
fix: add generateTypes cli option, check false value correctly (#830)
* fix: add generateTypes cli option, check false value correctly * chore: add changeset file * docs: cli option should be generateTypes * chore: remove unnecessary comment
1 parent edcd777 commit 54402ac

File tree

10 files changed

+101
-10
lines changed

10 files changed

+101
-10
lines changed

.changeset/cold-moose-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"microbundle": patch
3+
---
4+
5+
fix: add generateTypes cli option, check false value correctly

README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ Importing CSS files is supported via `import "./foo.css"`. By default, generated
175175

176176
```js
177177
// with the default external CSS:
178-
import './foo.css'; // generates a minified .css file in the output directory
178+
import './foo.css'; // generates a minified .css file in the output directory
179179

180180
// with `microbundle --css inline`:
181181
import css from './foo.css';
182-
console.log(css); // the generated minified stylesheet
182+
console.log(css); // the generated minified stylesheet
183183
```
184184

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

238238
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.
239239

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

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

@@ -279,6 +279,7 @@ Options
279279
--jsx A custom JSX pragma like React.createElement (default: h)
280280
--jsxImportSource Specify the automatic import source for JSX like preact
281281
--tsconfig Specify the path to a custom tsconfig.json
282+
--generateTypes Whether or not to generate types, if `types` or `typings` is set in `package.json` then it will default to be `true`
282283
--css Where to output CSS: "inline" or "external" (default: "external")
283284
--css-modules Configures .css to be treated as modules (default: null)
284285
-h, --help Displays this message

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,10 @@ function createConfig(options, entry, format, writeMeta) {
366366
: () => resolve(options.cwd, 'mangle.json');
367367

368368
const useTypescript = extname(entry) === '.ts' || extname(entry) === '.tsx';
369-
const emitDeclaration = !!(options.generateTypes || pkg.types || pkg.typings);
370-
369+
const emitDeclaration =
370+
options.generateTypes == null
371+
? !!(pkg.types || pkg.typings)
372+
: options.generateTypes;
371373
const escapeStringExternals = ext =>
372374
ext instanceof RegExp ? ext.source : escapeStringRegexp(ext);
373375
const externalPredicate = new RegExp(

src/prog.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ export default handler => {
6969
'A custom JSX pragma like React.createElement (default: h)',
7070
)
7171
.option('--tsconfig', 'Specify the path to a custom tsconfig.json')
72+
.option(
73+
'--generateTypes',
74+
'Whether or not to generate types , if `types` or `typings` is set in `package.json` then it will default to be `true`',
75+
)
7276
.example('microbundle build --tsconfig tsconfig.build.json');
7377

7478
prog
@@ -89,5 +93,6 @@ export default handler => {
8993
i: ['entry', 'entries', 'e'],
9094
w: ['watch'],
9195
},
96+
boolean: ['generateTypes'],
9297
});
9398
};

test/__snapshots__/index.test.js.snap

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,6 +2538,63 @@ exports[`fixtures build shebang with microbundle 5`] = `
25382538
"
25392539
`;
25402540
2541+
exports[`fixtures build ts-custom-declaration with microbundle 1`] = `
2542+
"Used script: microbundle --generateTypes false
2543+
2544+
Directory tree:
2545+
2546+
ts-custom-declaration
2547+
dist
2548+
index.esm.js
2549+
index.esm.js.map
2550+
index.js
2551+
index.js.map
2552+
index.umd.js
2553+
index.umd.js.map
2554+
package.json
2555+
src
2556+
index.js
2557+
tsconfig.json
2558+
types
2559+
index.d.ts
2560+
2561+
2562+
Build \\"tsCustomDeclarations\\" to dist:
2563+
55 B: index.js.gz
2564+
33 B: index.js.br
2565+
61 B: index.esm.js.gz
2566+
45 B: index.esm.js.br
2567+
175 B: index.umd.js.gz
2568+
125 B: index.umd.js.br"
2569+
`;
2570+
2571+
exports[`fixtures build ts-custom-declaration with microbundle 2`] = `6`;
2572+
2573+
exports[`fixtures build ts-custom-declaration with microbundle 3`] = `
2574+
"function n(){return 42}export{n as foo};
2575+
//# sourceMappingURL=index.esm.js.map
2576+
"
2577+
`;
2578+
2579+
exports[`fixtures build ts-custom-declaration with microbundle 4`] = `
2580+
"exports.foo=function(){return 42};
2581+
//# sourceMappingURL=index.js.map
2582+
"
2583+
`;
2584+
2585+
exports[`fixtures build ts-custom-declaration with microbundle 5`] = `
2586+
"!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}});
2587+
//# sourceMappingURL=index.umd.js.map
2588+
"
2589+
`;
2590+
2591+
exports[`fixtures build ts-custom-declaration with microbundle 6`] = `1`;
2592+
2593+
exports[`fixtures build ts-custom-declaration with microbundle 7`] = `
2594+
"export const foo: () => number;
2595+
"
2596+
`;
2597+
25412598
exports[`fixtures build ts-declaration with microbundle 1`] = `
25422599
"Used script: microbundle
25432600
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "ts-custom-declarations",
3+
"main": "dist/index.js",
4+
"types": "types/index.d.ts",
5+
"source": "src/index.js",
6+
"scripts": {
7+
"build": "microbundle --generateTypes false"
8+
}
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function foo() {
2+
return 42;
3+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"noEmit": true,
4+
"allowJs": true
5+
}
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo: () => number;

tools/build-fixture.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export const buildDirectory = async fixtureDir => {
4949
const dist = resolve(`${fixturePath}/dist`);
5050
// clean up
5151
await rimraf(dist);
52-
await rimraf(resolve(`${fixturePath}/types`));
52+
if (!fixturePath.endsWith('ts-custom-declaration')) {
53+
await rimraf(resolve(`${fixturePath}/types`));
54+
}
5355
await rimraf(resolve(`${fixturePath}/.rts2_cache_cjs`));
5456
await rimraf(resolve(`${fixturePath}/.rts2_cache_es`));
5557
await rimraf(resolve(`${fixturePath}/.rts2_cache_umd`));

0 commit comments

Comments
 (0)