New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

deep-import-loader

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deep-import-loader

Transform imports to final file, avoid importing the whole package.

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

deep-import-loader

  • 中文

Introduce

When building with webpack, it will find the final file which import refers to, then replace the path following by from. This will avoid importing the whole package, to reduce size of js bundle.

https://c-zhuo.github.io/easycanvas/

Difference with webpack tree-shaking

Although webpack supports elimination of unused export, it will NOT work if you transform your project to commonjs such as @babel/plugin-transform-modules-commonjs or babel-plugin-transform-es2015-modules-commonjs. Perhaps, lots of your dependencies does NOT have sideEffects: false in package.json file, which will also stop the elimination, even if we know they have no sideEffects. A way is to write code like import Foo from 'somemodule/lib/foo', but it is not convenient.

How it works

Load the file before other loaders, analyze import ... from .... If a module comes from a deeper file, transform the path. For example:

// Before
import { Foo, Bar } from 'module1';
import { Foo as myFoo } from 'module2';`

// After
// Foo is default export in foo.js
import Foo from 'module/path/foo.js';
// Bar is an export in utils.js
import { Bar } from 'module/path2/utils.js';`
// Support as, tsx and others
import myFoo from 'module/path3/foo.tsx';`

How to use

  1. Install deep-import-loader:

    npm install deep-import-loader or yarn add deep-import-loader

  2. Add this rule at the END of rules in webpack config, an example in webpack@4:

    rules: [{
        test: /\.(js|jsx|ts|tsx)$/,
        use: [
            // ... other rules
            {
                loader: 'deep-import-loader',
                options: {
                    blackList: ['adirtymodule'],
                    whiteList: ['somemodule', 'anothermodule'],
                    log: true,
                    warn: true
                }
            }
        ]
    }]
    

    Why at the end ? Rule run from last to first, so deep-import-loader will transform the import before other loaders make it unrecognizable.

    options:

    PropsDefaultDescription
    blackList[]Package blacklist, import from these packages will never be transformed.
    whiteList[]Package whitelist, import from these package will always be transformed. Otherwise will obey the option in package.json. Transform only happens if sideEffects is false. Set to '*' means always transform.
    logfalseShow the modules and new path during webpack.
    warnfalseShow the modules that fail during webpack.
  3. If you are using vue file, include VueLoaderPlugin in plugins. Not to modify test above. See https://vue-loader.vuejs.org/guide/#webpack-configuration for more information.

Keywords

FAQs

Package last updated on 20 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc