Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magic-comments

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magic-comments

Utility for adding webpack magic comments at build time.

  • 2.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
576
increased by211.35%
Maintainers
1
Weekly downloads
 
Created
Source

magic-comments

CI codecov NPM version

Tooling utility to add configurable webpack magic comments to dynamic import() expressions at build time.

Useful when working with:

Getting Started

First install magic-comments:

npm install magic-comments

Next generate an AST or RegExp that provides the following information for each file processed:

  • The absolute filename of the file being processed (modulePath).
  • The import specifier used in the dynamic imports found (importPath).

Then pass that information along to magic-comments to generate a magic comment that can be inserted into an import() expression:

src/file.js

const mod = import('./folder/module.js')

tooling

import { getMagicComment } from 'magic-comments'

const modulePath = resolve(directory, './src/file.js')
const code = fs.readFileSync(modulePath)
const ast = parse(code)
const dynamicImportsMeta = getDynamicImportMetaFrom(ast)

dynamicImportsMeta.forEach(meta) => {
  const magicComment = getMagicComment({
    modulePath,
    importPath: meta.importPath,
    options: {
      webpackChunkName: ['**/src/**/*.js'],
      webpackMode: 'eager'
      webpackFetchPriority: 'high'
    }
  })

  // prints /* webpackChunkName: "folder-module", webpackMode: "eager", webpackFetchPriority: "high" */
  console.log(magicComment)
})

Examples

Keywords

FAQs

Package last updated on 14 Jul 2023

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