Skip to content

Commit

Permalink
fix: correct babel plugin default signature to allow any source of 'l…
Browse files Browse the repository at this point in the history
…oadable' (#972)

* fix: correct babel plugin default signature to allow any source of 'loadable', fixes #971

* Update packages/babel-plugin/src/index.js
  • Loading branch information
theKashey authored Jul 19, 2023
1 parent 0e4b57f commit 19849b6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
57 changes: 57 additions & 0 deletions packages/babel-plugin/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,63 @@ loadable({
});"
`;
exports[`plugin custom signatures (default) should support old named import 1`] = `
"import loadable from './loadable-utils';
loadable({
resolved: {},
chunkName() {
return \`ModA\`.replace(/[^a-zA-Z0-9_$()=\\\\-^°]+/g, \\"-\\");
},
isReady(props) {
const key = this.resolve(props);
if (this.resolved[key] !== true) {
return false;
}
if (typeof __webpack_modules__ !== 'undefined') {
return !!__webpack_modules__[key];
}
return false;
},
importAsync: () => import(
/* webpackChunkName: \\"ModA\\" */
\`./ModA\`),
requireAsync(props) {
const key = this.resolve(props);
this.resolved[key] = false;
return this.importAsync(props).then(resolved => {
this.resolved[key] = true;
return resolved;
});
},
requireSync(props) {
const id = this.resolve(props);
if (typeof __webpack_require__ !== 'undefined') {
return __webpack_require__(id);
}
return eval('module.require')(id);
},
resolve() {
if (require.resolveWeak) {
return require.resolveWeak(\`./ModA\`);
}
return eval('require.resolve')(\`./ModA\`);
}
});"
`;
exports[`plugin custom signatures named signature should not match default import 1`] = `
"import myLoadable from 'myLoadablePackage';
myLoadable(() => import(\`./ModA\`));"
Expand Down
10 changes: 5 additions & 5 deletions packages/babel-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ const properties = [

const LOADABLE_COMMENT = '#__LOADABLE__'

const DEFAULT_SIGNATURE = [{name: 'default', from: '@loadable/component'}];

const loadablePlugin = declare((api, {
signatures = []
signatures = DEFAULT_SIGNATURE
}) => {
if (!signatures.find(sig => sig.from == '@loadable/component')) {
signatures.push({name: 'default', from: '@loadable/component'})
}
const { types: t } = api

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

programPath.traverse({
ImportDefaultSpecifier(path) {
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-plugin/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ describe('plugin', () => {
})

describe('custom signatures', () => {
it('(default) should support old named import', () => {
const result = testPlugin(`
import loadable from './loadable-utils'
loadable(() => import(\`./ModA\`))
`)
expect(result).toMatchSnapshot()
});
it('should match simple default import', () => {
const result = testPlugin(`
import loadable from '@loadable/component'
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/ChunkExtractor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('ChunkExtrator', () => {
statsFile: path.resolve(__dirname, '../__fixtures__/stats.json'),
})

expect(extractor.stats).toBe(stats)
expect(extractor.stats).toEqual(stats)
})
})

Expand Down

0 comments on commit 19849b6

Please sign in to comment.