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

fix: support multiple targets #525

Merged
merged 3 commits into from Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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.split(',')
}
Copy link
Owner

Choose a reason for hiding this comment

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

I guess you can just do .split without checking index

Copy link
Owner

Choose a reason for hiding this comment

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

oops I guess this is necessary otherwise this will fail:

if (options.target === 'es5') {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I will revert previous change

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.