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

magic-comments-loader

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-comments-loader

Add webpack magic comments to your dynamic imports during build time

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.9K
decreased by-4.78%
Maintainers
1
Weekly downloads
 
Created
Source

magic-comments-loader

Adds magic coments to your dynamic import statements. Currently only supports webpackChunkName and webpackMode.

Usage

First npm install magic-comments-loader.

Configuration

Add this inside your webpack.config.js.

Without options

Adds webpackChunkName to all dynamic imports (same as webpackChunkName: true when using options).

module: {
  rules: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      use: ['magic-comments-loader']
    }
  ]
}
With options

When using the loaders options configure the magic comments by using their name as a key in the options object. You can provide a simple value to take on default behavior of the comment.

module: {
  rules: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      use: {
        loader: 'magic-comments-loader',
        options: {
          webpackChunkName: true
          webpackMode: 'lazy'
        }
      }
    }
  ]
}

For more control you can provide an object literal with futher configuration options specific to each comment type. All comment types have a configuration option of active which is a boolean to enable or disable the addition of the magic comment. When using an object literal the configuration must be passed under the config key.

You can also override the configuration passed in the config key by file location when using the overrides key, which is an array of objects that look like:

overrides: [
 {
   files: ['src/**/*.js', '!/src/skip/**/*.js'] // Uses micromatch,
   config: {
     // Other configuration keys for the comment type can go here too
     active: false
   }
 }
]

Here's a more complete example:

module: {
  rules: [
    {
      test: /\.js$/,
      exclude: /node_modules/,
      use: {
        loader: 'magic-comments-loader',
        options: {
          verbose: true,
          webpackChunkName: {
            config: {
              basename: false
            },
            overrides: [
              {
                files: 'src/unique/**/*.js',
                config: {
                  basename: true
                }
              },
              {
                files: 'src/off/**/*.js',
                config: {
                  active: false
                }
              }
            ]
          },
          webpackMode: {
            config: {
              mode: 'lazy'
            },
            overrides: [
              {
                files: 'src/noMode/**/*.js',
                config: {
                  active: false
                }
              },
              {
                files: [
                  'src/**/*.js',
                  '!src/weak/**/*.js'
                ],
                config: {
                  mode: 'eager'
                }
              }
            ]
          }
        }
      }
    }
  ]
}

Magic Comments

With loader options configured like:

  {
    loader: 'magic-comments-loader',
    options: {
      webpackChunkName: true,
      webpackMode: 'lazy'
    }
  }

An import statement like:

const dynamicModule = await import('./path/to/some/module')

Becomes:

const dynamicModule = await import(/* webpackChunkName: "path-to-some-module", webpackMode: "lazy" */ './path/to/some/module')

Keywords

FAQs

Package last updated on 08 Sep 2021

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