Skip to content

Commit

Permalink
fix: support multiple targets (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
giuem committed Dec 31, 2021
1 parent 529be68 commit 2a42a02
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 33 deletions.
3 changes: 3 additions & 0 deletions src/cli-main.ts
Expand Up @@ -97,6 +97,9 @@ export async function main(options: Options = {}) {
const external = ensureArray(flags.external)
options.external = external
}
if (flags.target) {
options.target = flags.target.indexOf(',') >= 0 ? flags.target.split(',') : flags.target
}
if (flags.dts || flags.dtsResolve || flags.dtsOnly) {
options.dts = {}
if (typeof flags.dts === 'string') {
Expand Down
2 changes: 1 addition & 1 deletion src/options.ts
Expand Up @@ -37,7 +37,7 @@ export type Options = {
*
* default to `node12`
*/
target?: string
target?: string | string[]
minify?: boolean
minifyWhitespace?: boolean
minifyIdentifiers?: boolean
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.ts
Expand Up @@ -760,3 +760,20 @@ test('es5 target', async (t) => {
t.regex(output, /createClass/)
t.deepEqual(outFiles, ['input.js'])
})

test('multiple targets', async (t) => {
const { output, outFiles } = await run(
t.title,
{
'input.ts': `
export const answer = 42
`
},
{
entry: ['input.ts'],
flags: ['--target', 'es2020,chrome58,firefox57,safari11,edge16'],
}
)
t.snapshot(output)
t.deepEqual(outFiles, ['input.js'])
})
87 changes: 55 additions & 32 deletions test/snapshots/index.test.ts.md
Expand Up @@ -41,38 +41,6 @@ Generated by [AVA](https://avajs.dev).
export { MarkRequired };␊
`

## es5 target

> Snapshot 1
`var __defProp = Object.defineProperty;␊
var __markAsModule = function (target) { return __defProp(target, "__esModule", { value: true }); };␊
var __export = function (target, all) {␊
__markAsModule(target);␊
for (var name in all)␊
{ __defProp(target, name, { get: all[name], enumerable: true }); }␊
};␊
// input.ts␊
__export(exports, {␊
Foo: function () { return Foo; }␊
});␊
var Foo = /*@__PURE__*/(function () {␊
function Foo () {}␊
Foo.prototype.hi = function hi () {␊
var a = function () { return "foo"; };␊
console.log(a());␊
};␊
return Foo;␊
}());␊
// Annotate the CommonJS export names for ESM import in node:␊
0 && (module.exports = {␊
Foo: Foo␊
});␊
`

## multiple formats

> Snapshot 1
Expand Down Expand Up @@ -468,3 +436,58 @@ Generated by [AVA](https://avajs.dev).
export { bar };␊
`

## es5 target

> Snapshot 1
`var __defProp = Object.defineProperty;␊
var __markAsModule = function (target) { return __defProp(target, "__esModule", { value: true }); };␊
var __export = function (target, all) {␊
__markAsModule(target);␊
for (var name in all)␊
{ __defProp(target, name, { get: all[name], enumerable: true }); }␊
};␊
// input.ts␊
__export(exports, {␊
Foo: function () { return Foo; }␊
});␊
var Foo = /*@__PURE__*/(function () {␊
function Foo () {}␊
Foo.prototype.hi = function hi () {␊
var a = function () { return "foo"; };␊
console.log(a());␊
};␊
return Foo;␊
}());␊
// Annotate the CommonJS export names for ESM import in node:␊
0 && (module.exports = {␊
Foo: Foo␊
});␊
`

## multiple targets

> Snapshot 1
`var __defProp = Object.defineProperty;␊
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });␊
var __export = (target, all) => {␊
__markAsModule(target);␊
for (var name in all)␊
__defProp(target, name, { get: all[name], enumerable: true });␊
};␊
// input.ts␊
__export(exports, {␊
answer: () => answer␊
});␊
var answer = 42;␊
// Annotate the CommonJS export names for ESM import in node:␊
0 && (module.exports = {␊
answer␊
});␊
`
Binary file modified test/snapshots/index.test.ts.snap
Binary file not shown.

1 comment on commit 2a42a02

@vercel
Copy link

@vercel vercel bot commented on 2a42a02 Dec 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.