Skip to content

Commit

Permalink
Support renamed imports in babel-plugin (#929)
Browse files Browse the repository at this point in the history
* Support renamed imports in babel-plugin

* Added back default behaviour with a plugin option
  • Loading branch information
fivethreeo authored Dec 12, 2022
1 parent 3ec9b64 commit ede5558
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 25 deletions.
107 changes: 92 additions & 15 deletions packages/babel-plugin/src/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ exports[`plugin Magic comment should transpile shortand properties 1`] = `
`;

exports[`plugin aggressive import should work with destructuration 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName({
Expand Down Expand Up @@ -295,7 +296,8 @@ exports[`plugin aggressive import should work with destructuration 1`] = `
`;
exports[`plugin aggressive import with "webpackChunkName" should keep it 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName(props) {
Expand Down Expand Up @@ -351,7 +353,8 @@ exports[`plugin aggressive import with "webpackChunkName" should keep it 1`] = `
`;
exports[`plugin aggressive import with "webpackChunkName" should replace it 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName(props) {
Expand Down Expand Up @@ -407,7 +410,8 @@ exports[`plugin aggressive import with "webpackChunkName" should replace it 1`]
`;
exports[`plugin aggressive import without "webpackChunkName" should support complex request 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName(props) {
Expand Down Expand Up @@ -463,7 +467,8 @@ exports[`plugin aggressive import without "webpackChunkName" should support comp
`;
exports[`plugin aggressive import without "webpackChunkName" should support destructuring 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName({
Expand Down Expand Up @@ -525,7 +530,8 @@ exports[`plugin aggressive import without "webpackChunkName" should support dest
`;
exports[`plugin aggressive import without "webpackChunkName" should support simple request 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName(props) {
Expand Down Expand Up @@ -581,7 +587,8 @@ exports[`plugin aggressive import without "webpackChunkName" should support simp
`;
exports[`plugin loadable.lib should be transpiled too 1`] = `
"loadable.lib({
"import loadable from '@loadable/component';
loadable.lib({
resolved: {},
chunkName() {
Expand Down Expand Up @@ -637,7 +644,8 @@ exports[`plugin loadable.lib should be transpiled too 1`] = `
`;
exports[`plugin simple import in a complex promise should work 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName() {
Expand Down Expand Up @@ -692,8 +700,14 @@ exports[`plugin simple import in a complex promise should work 1`] = `
});"
`;
exports[`plugin simple import should not work with renamed specifier by default 1`] = `
"import renamedLoadable from '@loadable/component';
renamedLoadable(() => import(\`./ModA\`));"
`;
exports[`plugin simple import should transform path into "chunk-friendly" name 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName() {
Expand Down Expand Up @@ -749,7 +763,8 @@ exports[`plugin simple import should transform path into "chunk-friendly" name 1
`;
exports[`plugin simple import should work with * in name 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName() {
Expand Down Expand Up @@ -805,7 +820,8 @@ exports[`plugin simple import should work with * in name 1`] = `
`;
exports[`plugin simple import should work with + concatenation 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName() {
Expand Down Expand Up @@ -917,8 +933,66 @@ lazy({
});"
`;
exports[`plugin simple import should work with renamed lazy specifier 1`] = `
"import { lazy as renamedLazy } from '@loadable/component';
renamedLazy({
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 simple import should work with template literal 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName() {
Expand Down Expand Up @@ -974,7 +1048,8 @@ exports[`plugin simple import should work with template literal 1`] = `
`;
exports[`plugin simple import with "webpackChunkName" comment should use it 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName() {
Expand Down Expand Up @@ -1030,7 +1105,8 @@ exports[`plugin simple import with "webpackChunkName" comment should use it 1`]
`;
exports[`plugin simple import with "webpackChunkName" comment should use it even if comment is separated by "," 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName() {
Expand Down Expand Up @@ -1086,7 +1162,8 @@ exports[`plugin simple import with "webpackChunkName" comment should use it even
`;
exports[`plugin simple import without "webpackChunkName" comment should add it 1`] = `
"loadable({
"import loadable from '@loadable/component';
loadable({
resolved: {},
chunkName() {
Expand Down
36 changes: 26 additions & 10 deletions packages/babel-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { declare } from "@babel/helper-plugin-utils";
import syntaxDynamicImport from '@babel/plugin-syntax-dynamic-import'
import chunkNameProperty from './properties/chunkName'
import isReadyProperty from './properties/isReady'
Expand All @@ -19,7 +20,7 @@ const properties = [

const LOADABLE_COMMENT = '#__LOADABLE__'

const loadablePlugin = api => {
const loadablePlugin = declare((api, { defaultImportSpecifier = 'loadable' }) => {
const { types: t } = api

function collectImportCallPaths(startPath) {
Expand All @@ -34,21 +35,22 @@ const loadablePlugin = api => {

const propertyFactories = properties.map(init => init(api))

function isValidIdentifier(path, hasLazyImport) {
function isValidIdentifier(path, loadableImportSpecifier, lazyImportSpecifier) {
// `loadable()`
if (path.get('callee').isIdentifier({ name: 'loadable' })) {
if (loadableImportSpecifier && path.get('callee').isIdentifier({ name: loadableImportSpecifier })) {
return true
}

// `lazy()`
if (path.get('callee').isIdentifier({ name: 'lazy' }) && hasLazyImport) {
if (lazyImportSpecifier && path.get('callee').isIdentifier({ name: lazyImportSpecifier })) {
return true
}

// `loadable.lib()`
return (
loadableImportSpecifier &&
path.get('callee').isMemberExpression() &&
path.get('callee.object').isIdentifier({ name: 'loadable' }) &&
path.get('callee.object').isIdentifier({ name: loadableImportSpecifier }) &&
path.get('callee.property').isIdentifier({ name: 'lib' })
)
}
Expand Down Expand Up @@ -117,14 +119,28 @@ const loadablePlugin = api => {
visitor: {
Program: {
enter(programPath) {
let hasLazyImport = false
let loadableImportSpecifier = defaultImportSpecifier
let lazyImportSpecifier = false

programPath.traverse({
ImportDeclaration(path) {
hasLazyImport = hasLazyImport || path.node.source.value == '@loadable/component' && path.node.specifiers.some(({ imported })=>imported.name=='lazy')
ImportDefaultSpecifier(path) {
if (!loadableImportSpecifier) {
const { parent } = path
const { local } = path.node
loadableImportSpecifier = parent.source.value == '@loadable/component' &&
local && local.name
}
},
ImportSpecifier(path) {
if (!lazyImportSpecifier) {
const { parent } = path
const { imported, local } = path.node
lazyImportSpecifier = parent.source.value == '@loadable/component' &&
imported && imported.name == 'lazy' && local && local.name
}
},
CallExpression(path) {
if (!isValidIdentifier(path, hasLazyImport)) return
if (!isValidIdentifier(path, loadableImportSpecifier, lazyImportSpecifier)) return
transformImport(path)
},
'ArrowFunctionExpression|FunctionExpression|ObjectMethod': path => {
Expand All @@ -136,6 +152,6 @@ const loadablePlugin = api => {
},
},
}
}
})

export default loadablePlugin
Loading

0 comments on commit ede5558

Please sign in to comment.