question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Changing imports to reduce bundle size?

See original GitHub issue

I just spent a couple hours tonight trying to figure out why my import on demand per Ant’s docs was not working. Turns out that it was a couple imports from formik-antd.

I wonder if it’s possible for formik-antd to import similarly, e.g. instead of import { Form } from 'antd' you would do import Form from 'antd/lib/form' or import Form from 'antd/es/form'.

Thanks in advance!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:40 (24 by maintainers)

github_iconTop GitHub Comments

4reactions
edongashicommented, Oct 19, 2019

I found the problem. This library has "sideEffects": false whereas ant design has

"sideEffects": [
  "dist/*",
  "es/**/style/*",
  "lib/**/style/*",
  "*.less"
]

Marking all files as side effect free tells webpack to shake off files that have no used tokens (this happens for styles). I will send a PR now.

3reactions
claneocommented, Jun 28, 2019

This is my babel-plugin-import config:

[
  'import',
  {
    libraryName: '@jbuschke/formik-antd',
    style: libraryName => {
      let name = libraryName.replace('@jbuschke/formik-antd/es/', '');
      if (name === 'FormikDebug') return false;
      if (name === 'SubmitButton') name = 'Button';
      name = name[0].toLowerCase() + name.substr(1);
      name = name.replace(/([A-Z])/g, $1 => `-${$1.toLowerCase()}`);
      return `antd/es/${name}/style`;
    },
    libraryDirectory: 'es',
    camel2DashComponentName: false,
    transformToDefaultImport: false,
  },
  'formik-antd',
]

So my suggestion is to use same filename style with antd, and include style entry file, which looks like this:

es
├─date-picker
│ ├─index.js
│ └─style
│   ├─index.js
│   └─css.js
├─form
│ └─...
└─...

style/index.js and style/css.js just import antd/lib/xxx/style and antd/lib/xxx/style/css.js

After doing that, we can use a simple babel-plugin-import config:

[
  'import',
  {
    libraryName: '@jbuschke/formik-antd',
    style: true,
    // or
    // style: 'css',
    libraryDirectory: 'es'
  },
  'formik-antd',
]

@jannikbuschke

Read more comments on GitHub >

github_iconTop Results From Across the Web

Correctly Use Javascript's Imports To Improve Bundle Size
Importing libraries incorrectly will rapidly increase your bundle size, increase your build time and will make the user eventually wait more ...
Read more >
Everything you can do to reduce bundle size for webapps
Web bundlers such as Webpack are a great tool to deploy web apps, but for more complex programs, bundle size can quickly become...
Read more >
5 Methods to Reduce JavaScript Bundle Size - Bits and Pieces
5 Methods to Reduce JavaScript Bundle Size · 1. Code Splitting with Webpack · 2. Using Webpack Plugins for Tree Shaking · 3....
Read more >
Reduce JS Bundle Size by Dynamically Importing es6 Modules
One way to reduce JavaScript bundle size is to dynamically import es6 modules which are not required for the initial loading of the...
Read more >
4 Ways to Shrink Your Javascript Bundle by 50% - Riley Shenk
4 Ways to Shrink Your Javascript Bundle by 50% · 1. Ignore moment.js locales · 2. Directly import lodash functions you need. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found