Skip to content

Commit 288bdaf

Browse files
authored
Merge pull request #169 from theKashey/fixes-2019
fix: terser might compress different files differently, fixes #166
2 parents e8bea90 + 985327a commit 288bdaf

File tree

17 files changed

+236
-213
lines changed

17 files changed

+236
-213
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__tests__/__fixtures__

.size-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ module.exports = [
1212
{
1313
path: 'dist/es2015/boot.js',
1414
ignore: ['tslib'],
15-
limit: '1.7 KB',
15+
limit: '1.8 KB',
1616
},
1717
];

.size.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
{
33
"name": "dist/es2015/index.js, dist/es2015/boot.js",
44
"passed": true,
5-
"size": 3407
5+
"size": 3428
66
},
77
{
88
"name": "dist/es2015/index.js",
99
"passed": true,
10-
"size": 3123
10+
"size": 3133
1111
},
1212
{
1313
"name": "dist/es2015/boot.js",
1414
"passed": true,
15-
"size": 1695
15+
"size": 1756
1616
}
1717
]

__tests__/__fixtures__/babel/node/expected.js

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__tests__/__fixtures__/babel/webpack/expected.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
var importedWrapper = function (marker, realImport) {
2-
if (typeof __deoptimization_sideEffect__ !== 'undefined') {
3-
__deoptimization_sideEffect__(marker, realImport);
4-
}
5-
6-
return realImport;
7-
};
1+
var importedWrapper = require('react-imported-component/wrapper');
82

93
import { lazy, useImported } from "react-imported-component";
104
import { assignImportedComponents } from "react-imported-component/boot";

__tests__/__snapshots__/macro.spec.ts.snap

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ function _interopRequireWildcard(obj) {
3333
}
3434
}
3535
36-
var importedWrapper = function(marker, realImport) {
37-
if (typeof __deoptimization_sideEffect__ !== \\"undefined\\") {
38-
__deoptimization_sideEffect__(marker, realImport);
39-
}
40-
41-
return realImport;
42-
};
36+
var importedWrapper = require(\\"react-imported-component/wrapper\\");
4337
4438
import { lazy } from \\"react-imported-component\\";
4539
import { assignImportedComponents } from \\"react-imported-component/boot\\";
@@ -92,13 +86,7 @@ function _interopRequireWildcard(obj) {
9286
}
9387
}
9488
95-
var importedWrapper = function(marker, realImport) {
96-
if (typeof __deoptimization_sideEffect__ !== \\"undefined\\") {
97-
__deoptimization_sideEffect__(marker, realImport);
98-
}
99-
100-
return realImport;
101-
};
89+
var importedWrapper = require(\\"react-imported-component/wrapper\\");
10290
10391
importedWrapper(
10492
\\"imported_-1ko6oiq_component\\",
@@ -140,13 +128,7 @@ function _interopRequireWildcard(obj) {
140128
}
141129
}
142130
143-
var importedWrapper = function(marker, realImport) {
144-
if (typeof __deoptimization_sideEffect__ !== \\"undefined\\") {
145-
__deoptimization_sideEffect__(marker, realImport);
146-
}
147-
148-
return realImport;
149-
};
131+
var importedWrapper = require(\\"react-imported-component/wrapper\\");
150132
151133
import { lazy } from \\"react-imported-component\\";
152134
const v = lazy(() =>
@@ -192,13 +174,7 @@ function _interopRequireWildcard(obj) {
192174
}
193175
}
194176
195-
var importedWrapper = function(marker, realImport) {
196-
if (typeof __deoptimization_sideEffect__ !== \\"undefined\\") {
197-
__deoptimization_sideEffect__(marker, realImport);
198-
}
199-
200-
return realImport;
201-
};
177+
var importedWrapper = require(\\"react-imported-component/wrapper\\");
202178
203179
import { imported, useImported } from \\"react-imported-component\\";
204180
const v = imported(() =>
@@ -249,13 +225,7 @@ const x = () => useImported(() => import('./b'));
249225
250226
↓ ↓ ↓ ↓ ↓ ↓
251227
252-
var importedWrapper = function(marker, realImport) {
253-
if (typeof __deoptimization_sideEffect__ !== \\"undefined\\") {
254-
__deoptimization_sideEffect__(marker, realImport);
255-
}
256-
257-
return realImport;
258-
};
228+
var importedWrapper = require(\\"react-imported-component/wrapper\\");
259229
260230
function _interopRequireWildcard(obj) {
261231
if (obj && obj.__esModule) {
@@ -282,13 +252,7 @@ function _interopRequireWildcard(obj) {
282252
}
283253
}
284254
285-
var importedWrapper = function(marker, realImport) {
286-
if (typeof __deoptimization_sideEffect__ !== \\"undefined\\") {
287-
__deoptimization_sideEffect__(marker, realImport);
288-
}
289-
290-
return realImport;
291-
};
255+
var importedWrapper = require(\\"react-imported-component/wrapper\\");
292256
293257
import { imported, useImported } from \\"react-imported-component\\";
294258
const v = imported(() =>

__tests__/loadable.spec.ts

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getFunctionSignature, getLoadable, importMatch} from "../src/loadable";
1+
import { getLoadable } from '../src/loadable';
22

33
describe('getLoadable', () => {
44
const importedWrapper = (_: any, b: any) => b;
@@ -7,61 +7,13 @@ describe('getLoadable', () => {
77
const l1 = getLoadable(() => importedWrapper('imported_mark1_component', Promise.resolve(42)));
88
const l2 = getLoadable(() => importedWrapper('imported_mark1_component', Promise.resolve(42)));
99

10-
expect(l1).toEqual(l2)
10+
expect(l1).toEqual(l2);
1111
});
1212

1313
it('cache test - no mark present', () => {
1414
const l1 = getLoadable(() => Promise.resolve(42));
1515
const l2 = getLoadable(() => Promise.resolve(42));
1616

17-
expect(l1).not.toEqual(l2)
17+
expect(l1).not.toEqual(l2);
1818
});
1919
});
20-
21-
describe('importMatch', () => {
22-
it('standard', () => {
23-
expect(
24-
importMatch(getFunctionSignature(`() => importedWrapper('imported_mark1_component', Promise.resolve(TargetComponent)), true)`))
25-
).toEqual(['mark1']);
26-
});
27-
28-
it('webpack', () => {
29-
expect(
30-
importMatch(getFunctionSignature(`() => importedWrapper("imported_mark1_component", __webpack_require__.e(/*! import() */ 0).then(__webpack_require__.bind(null, /*! ./components/Another */ "./app/components/Another.tsx")))`))
31-
).toEqual(['mark1']);
32-
});
33-
34-
it('webpack-prod', () => {
35-
expect(
36-
importMatch(getFunctionSignature(`() => importedWrapper('imported_mark1_component',__webpack_require__.e(/*! import() */ 0).then(__webpack_require__.bind(null, /*! ./components/Another */ "./app/components/Another.tsx")))`))
37-
).toEqual(['mark1']);
38-
});
39-
40-
it('functional', () => {
41-
expect(importMatch(getFunctionSignature(`"function loadable() {
42-
return importedWrapper('imported_1ubbetg_component', __webpack_require__.e(/*! import() | namedChunk-1 */ "namedChunk-1").then(__webpack_require__.t.bind(null, /*! ./DeferredRender */ "./src/DeferredRender.js", 7)));
43-
}"`))).toEqual(['1ubbetg']);
44-
});
45-
46-
it('parcel', () => {
47-
expect(importMatch(getFunctionSignature(`function _() {
48-
return importedWrapper('imported_mark1_component', require("_bundle_loader")(require.resolve('./HelloWorld3')));
49-
}`))).toEqual(['mark1']);
50-
});
51-
52-
it('ie11 uglify', () => {
53-
expect(importMatch(getFunctionSignature(`function _() {
54-
var t = 'imported_mark1_component';
55-
}`))).toEqual(['mark1']);
56-
});
57-
58-
it('multiple imports in one line', () => {
59-
expect(importMatch(getFunctionSignature(`function _() {
60-
"imported_1pn9k36_component", blablabla- importedWrapper("imported_-1556gns_component")
61-
}`))).toEqual(['1pn9k36', '-1556gns']);
62-
});
63-
64-
it('maps function signatures', () => {
65-
expect(getFunctionSignature(`import('file')`)).toEqual(getFunctionSignature(`import(/* */'file')`))
66-
})
67-
});

__tests__/signatures.spec.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { getFunctionSignature, importMatch } from '../src/signatures';
2+
3+
describe('signatures', () => {
4+
const a = (i: any) => i;
5+
const b = (i: any) => i;
6+
7+
it('extract markers from function', () => {
8+
expect(importMatch(getFunctionSignature(() => a('imported_XXYY_component')))).toEqual(['XXYY']);
9+
});
10+
11+
it('work similar for similar functions', () => {
12+
expect(getFunctionSignature(() => a('imported_XXYY_component'))).toBe(
13+
getFunctionSignature(() => b('imported_XXYY_component'))
14+
);
15+
});
16+
});
17+
18+
describe('importMatch', () => {
19+
it('standard', () => {
20+
expect(
21+
importMatch(
22+
getFunctionSignature(
23+
`() => importedWrapper('imported_mark1_component', Promise.resolve(TargetComponent)), true)`
24+
)
25+
)
26+
).toEqual(['mark1']);
27+
});
28+
29+
it('webpack', () => {
30+
expect(
31+
importMatch(
32+
getFunctionSignature(
33+
`() => importedWrapper("imported_mark1_component", __webpack_require__.e(/*! import() */ 0).then(__webpack_require__.bind(null, /*! ./components/Another */ "./app/components/Another.tsx")))`
34+
)
35+
)
36+
).toEqual(['mark1']);
37+
});
38+
39+
it('webpack-prod', () => {
40+
expect(
41+
importMatch(
42+
getFunctionSignature(
43+
`() => importedWrapper('imported_mark1_component',__webpack_require__.e(/*! import() */ 0).then(__webpack_require__.bind(null, /*! ./components/Another */ "./app/components/Another.tsx")))`
44+
)
45+
)
46+
).toEqual(['mark1']);
47+
});
48+
49+
it('functional', () => {
50+
expect(
51+
importMatch(
52+
getFunctionSignature(`"function loadable() {
53+
return importedWrapper('imported_1ubbetg_component', __webpack_require__.e(/*! import() | namedChunk-1 */ "namedChunk-1").then(__webpack_require__.t.bind(null, /*! ./DeferredRender */ "./src/DeferredRender.js", 7)));
54+
}"`)
55+
)
56+
).toEqual(['1ubbetg']);
57+
});
58+
59+
it('parcel', () => {
60+
expect(
61+
importMatch(
62+
getFunctionSignature(`function _() {
63+
return importedWrapper('imported_mark1_component', require("_bundle_loader")(require.resolve('./HelloWorld3')));
64+
}`)
65+
)
66+
).toEqual(['mark1']);
67+
});
68+
69+
it('ie11 uglify', () => {
70+
expect(
71+
importMatch(
72+
getFunctionSignature(`function _() {
73+
var t = 'imported_mark1_component';
74+
}`)
75+
)
76+
).toEqual(['mark1']);
77+
});
78+
79+
it('multiple imports in one line', () => {
80+
expect(
81+
importMatch(
82+
getFunctionSignature(`function _() {
83+
"imported_1pn9k36_component", blablabla- importedWrapper("imported_-1556gns_component")
84+
}`)
85+
)
86+
).toEqual(['1pn9k36', '-1556gns']);
87+
});
88+
89+
it('maps function signatures', () => {
90+
expect(getFunctionSignature(`import('file')`)).toEqual(getFunctionSignature(`import(/* */'file')`));
91+
});
92+
});

__tests__/useImported.spec.tsx

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('useImported', () => {
146146

147147
expect(wrapper.update().html()).toContain('nothing');
148148
wrapper.setProps({ loadit: true });
149-
expect(wrapper.update().html()).toContain('nothing');
149+
expect(wrapper.update().html()).toContain('loading');
150150

151151
await act(async () => {
152152
await done();
@@ -155,4 +155,32 @@ describe('useImported', () => {
155155
expect(wrapper.update().html()).toContain('loaded!');
156156
expect(drainHydrateMarks()).toEqual(['conditional-mark']);
157157
});
158+
159+
it('cached import', async () => {
160+
// this test is not working as it should (it should be broken)
161+
const importer = () => () => <span>loaded!</span>;
162+
163+
const Comp = () => {
164+
const { loading, imported: Component } = useImported(importer as any);
165+
166+
if (Component) {
167+
return <Component />;
168+
}
169+
170+
if (loading) {
171+
return <span>loading</span>;
172+
}
173+
return <span>nothing</span>;
174+
};
175+
176+
const wrapper = mount(<Comp />);
177+
expect(wrapper.html()).toContain('loading');
178+
expect(wrapper.update().html()).toContain('loading');
179+
180+
await act(async () => {
181+
await done();
182+
});
183+
184+
expect(wrapper.update().html()).toContain('loaded!');
185+
});
158186
});

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-imported-component",
3-
"version": "6.2.1",
3+
"version": "6.2.4",
44
"description": "I will import your component, and help to handle it",
55
"main": "dist/es5/index.js",
66
"jsnext:main": "dist/es2015/index.js",
@@ -85,7 +85,8 @@
8585
"boot",
8686
"server",
8787
"macro",
88-
"babel.js"
88+
"babel.js",
89+
"wrapper.js"
8990
],
9091
"husky": {
9192
"hooks": {

0 commit comments

Comments
 (0)