Skip to content

Commit 19849b6

Browse files
authored
fix: correct babel plugin default signature to allow any source of 'loadable' (#972)
* fix: correct babel plugin default signature to allow any source of 'loadable', fixes #971 * Update packages/babel-plugin/src/index.js
1 parent 0e4b57f commit 19849b6

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

packages/babel-plugin/src/__snapshots__/index.test.js.snap

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,63 @@ loadable({
586586
});"
587587
`;
588588
589+
exports[`plugin custom signatures (default) should support old named import 1`] = `
590+
"import loadable from './loadable-utils';
591+
loadable({
592+
resolved: {},
593+
594+
chunkName() {
595+
return \`ModA\`.replace(/[^a-zA-Z0-9_$()=\\\\-^°]+/g, \\"-\\");
596+
},
597+
598+
isReady(props) {
599+
const key = this.resolve(props);
600+
601+
if (this.resolved[key] !== true) {
602+
return false;
603+
}
604+
605+
if (typeof __webpack_modules__ !== 'undefined') {
606+
return !!__webpack_modules__[key];
607+
}
608+
609+
return false;
610+
},
611+
612+
importAsync: () => import(
613+
/* webpackChunkName: \\"ModA\\" */
614+
\`./ModA\`),
615+
616+
requireAsync(props) {
617+
const key = this.resolve(props);
618+
this.resolved[key] = false;
619+
return this.importAsync(props).then(resolved => {
620+
this.resolved[key] = true;
621+
return resolved;
622+
});
623+
},
624+
625+
requireSync(props) {
626+
const id = this.resolve(props);
627+
628+
if (typeof __webpack_require__ !== 'undefined') {
629+
return __webpack_require__(id);
630+
}
631+
632+
return eval('module.require')(id);
633+
},
634+
635+
resolve() {
636+
if (require.resolveWeak) {
637+
return require.resolveWeak(\`./ModA\`);
638+
}
639+
640+
return eval('require.resolve')(\`./ModA\`);
641+
}
642+
643+
});"
644+
`;
645+
589646
exports[`plugin custom signatures named signature should not match default import 1`] = `
590647
"import myLoadable from 'myLoadablePackage';
591648
myLoadable(() => import(\`./ModA\`));"

packages/babel-plugin/src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ const properties = [
2020

2121
const LOADABLE_COMMENT = '#__LOADABLE__'
2222

23+
const DEFAULT_SIGNATURE = [{name: 'default', from: '@loadable/component'}];
24+
2325
const loadablePlugin = declare((api, {
24-
signatures = []
26+
signatures = DEFAULT_SIGNATURE
2527
}) => {
26-
if (!signatures.find(sig => sig.from == '@loadable/component')) {
27-
signatures.push({name: 'default', from: '@loadable/component'})
28-
}
2928
const { types: t } = api
3029

3130
function collectImportCallPaths(startPath) {
@@ -124,7 +123,8 @@ const loadablePlugin = declare((api, {
124123
Program: {
125124
enter(programPath) {
126125
let lazyImportSpecifier = false
127-
const loadableSpecifiers = []
126+
// default to "loadable" detection. Remove defaults if signatures are configured
127+
const loadableSpecifiers = signatures === DEFAULT_SIGNATURE ? ['loadable']: [];
128128

129129
programPath.traverse({
130130
ImportDefaultSpecifier(path) {

packages/babel-plugin/src/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ describe('plugin', () => {
203203
})
204204

205205
describe('custom signatures', () => {
206+
it('(default) should support old named import', () => {
207+
const result = testPlugin(`
208+
import loadable from './loadable-utils'
209+
loadable(() => import(\`./ModA\`))
210+
`)
211+
expect(result).toMatchSnapshot()
212+
});
206213
it('should match simple default import', () => {
207214
const result = testPlugin(`
208215
import loadable from '@loadable/component'

packages/server/src/ChunkExtractor.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('ChunkExtrator', () => {
4545
statsFile: path.resolve(__dirname, '../__fixtures__/stats.json'),
4646
})
4747

48-
expect(extractor.stats).toBe(stats)
48+
expect(extractor.stats).toEqual(stats)
4949
})
5050
})
5151

0 commit comments

Comments
 (0)