npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rollup-plugin-replace-imports-with-vars

v1.1.0

Published

Replace 'import { x } from y' with 'const|let|var { x } = y'

Downloads

23

Readme

npm size

rollup-plugin-replace-imports-with-vars

Say you're using the dynamic import() on your web app to load some other lib file. Your lib project has some dependencies that shouldn't be bundled with your code since they are part of the target runtime (i.e. the web app provides them via a global var or something similar). If you have import x from 'y', once the browser loads your bundle, it will attempt to load file y, which might not be what you want. You may want to grab the dependencies from where ever they are on the runtime — like some global var. At least that was my case and why I created this plugin.

This plugin transforms code more or less like this:

  • import { x } from 'y' => const { x } = y
  • import x from 'y' => const x = y.default ?? y
  • import x, { z } from 'y' => const x = y.default ?? y; const { z } = y
  • import * as x from 'y' => const x = y

You can tell the plugin whether you want it to use const, let or var. Note it outputs destructuring assignment syntax, so if you need to support browsers without that capability, you may need additional transpiling afterwards.

Sample

// rollup.config.js

const globals = {
  'jss': 'window.libs.jss',
  'react': 'window.libs.React',
  'react-dom': 'window.libs.ReactDOM',
  '@material-ui/core': 'window.libs.MaterialUI'
}

export default {
  input: ...,
  output: ...,
  plugins: [
    ...
    replace({ varType: 'var', replacementLookup: globals }),
    ...
  ]
}
// Original code
import { createElement } from 'react';
import { makeStyles, createStyles, Typography } from '@material-ui/core';
import { useIntl } from 'react-intl';
import jss from 'jss';

// ... 
// Transformed output
var { createElement } = window.libs.React;
var { makeStyles, createStyles, Typography } = window.libs.MaterialUI;
var { useIntl } = window.libs.ReactIntl;
var jss = window.libs.jss && Object.prototype.hasOwnProperty.call(window.libs.jss, 'default') ? window.libs.jss['default'] : window.libs.jss;

// ...