babel-plugin-after-async-component
Babel plugin to add additional chunk info to asyncComponent() in Afterjs.
How It's Wokring
- search for import statements from these paths
'@jaredpalmer/after'
and'@jaredpalmer/after/asyncComponent'
- then it finds a local name for
asyncComponent
import statement
// example1.js // example2.js // example3.js // example4.js // example5.js // example6.js // example7.js // localname = [`loader`, `asyncComponent`] :)
- then it searches for FunctionCalls that have these conditions:
- name of function that get called matches localname array
- that function act as value of property
- the name of property must be
component
; // localname is asyncComponent // let's look for localname (`asyncComponent`) and see where it get called// ... // oh nice I found a call to that function component: // now let me check and see if it's value of an object propery that named `component` // <- it's an object // 👇 and property name is `component` component:
Examples
In
path: "/product/:name" component:
Out
path: "/product/:name" component: // 👆 as you can see `./pages/ProducDetail` changed to `pages-ProducDetail`,// because for web servers `/` means a folder// and webpackChunkName is name of file that saved on disk,// so `/` in file name may break our app
In
path: "/product/:name" component:
Out
path: "/product/:name" component:
In
const name = "SlimShady" path: "/rap/god" component:
Out
const name = "SlimShady" path: "/rap/god" component:
In
path: "/test" component:
Out
path: "/test" component:
In
path: "/test" component:
Out
path: "/test" component:
Installation
$ npm install babel-plugin-after-async-component --save-dev
or if you use Yarn like me:
$ yarn add -D babel-plugin-after-async-component
Usage
.babelrc
(Recommended)
Via .babelrc
Options
prefix
: string (defaults: "") String used to append beforechunkName
andwebpackChunkName
.
Via CLI
$ babel --plugins after-async-component script.js
Via Node API