Skip to content

Commit

Permalink
fix(esbuild): minify exposes target config for es5 projects (#571)
Browse files Browse the repository at this point in the history
* fix(esbuild): minify using es5

* chore: fix targets

* chore: test case

* chore: test case

* chore: test case

* chore: setTimeout

* chore: esbuild

* chore: target array or string

* chore: es5
  • Loading branch information
ycjcl868 committed Mar 26, 2021
1 parent 8072710 commit cb93b3a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
22 changes: 22 additions & 0 deletions packages/plugin-esbuild/src/fixtures/es5/.umirc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

export default {
nodeModulesTransform: {
type: 'none',
},
targets: {
ie: 11,
},
history: {
type: 'memory',
options: {
initialEntries: ['/'],
},
},
mountElementId: '',
routes: [
{ path: '/', component: 'index' },
],
esbuild: {
target: 'es5',
},
}
3 changes: 3 additions & 0 deletions packages/plugin-esbuild/src/fixtures/es5/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default () => {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react';

export default () => <div>Hello</div>
export default () => {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}
36 changes: 35 additions & 1 deletion packages/plugin-esbuild/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { join } from 'path';
import { existsSync } from 'fs';
import { existsSync, readFileSync } from 'fs';
import { Service } from 'umi';

const fixtures = join(__dirname, 'fixtures');

jest.setTimeout(300000);

describe('normal build', () => {
let err: any;
const cwd = join(fixtures, 'normal');
Expand All @@ -27,6 +29,38 @@ describe('normal build', () => {
it('normal', () => {
expect(err).toBeFalsy();
expect(existsSync(join(cwd, 'dist', 'umi.js'))).toBeTruthy();
expect(readFileSync(join(cwd, 'dist', 'umi.js'), 'utf8')).toContain(
'new TypeError(`Invalid attempt to spread non-iterable',
);
});
});

describe('es5 build', () => {
let err: any;
const cwd = join(fixtures, 'es5');
beforeAll(async () => {
const service = new Service({
cwd,
env: 'production',
plugins: [require.resolve('./index.ts')],
});
let err;
try {
await service.run({
name: 'build',
});
} catch (e) {
console.error('es5 build error', e);
err = true;
}
});

it('es5', () => {
expect(err).toBeFalsy();
expect(existsSync(join(cwd, 'dist', 'umi.js'))).toBeTruthy();
expect(readFileSync(join(cwd, 'dist', 'umi.js'), 'utf8')).toContain(
'new TypeError("Invalid attempt to spread non-iterable',
);
});
});

Expand Down
10 changes: 8 additions & 2 deletions packages/plugin-esbuild/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ export default (api: IApi) => {
key: 'esbuild',
config: {
schema(joi) {
return joi.object();
return joi.object({
target: joi.alternatives(
joi.string(),
joi.array().items(joi.string()),
),
});
},
},
enableBy: api.EnableBy.config,
});

api.modifyBundleConfig((memo, { type }) => {
if (memo.optimization) {
const target = api.config.esbuild?.target || ['es2015'];
const optsMap = {
[BundlerConfigType.csr]: {
target: 'es2015',
target,
minify: true,
},
[BundlerConfigType.ssr]: {
Expand Down

0 comments on commit cb93b3a

Please sign in to comment.