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-magento2

v1.0.2

Published

Simple ES module bundling for Magento 2

Downloads

4

Readme

rollup-plugin-magento2

Simple ES module bundling for Magento 2.

About

This is a plugin for Rollup that converts your Javascript modules written in ES6/ES7 to modules compatible with RequireJS.

How it works

This plugin works by virtualizing modules that correspond to scripts already included on Magento 2 (ex: jquery, underscore, etc.). Each virtual plugin will be listed as an additional dependency on the define callback. The final result is a script compatible with RequireJS that can be easily deployed in Magento 2.

Examples

Simple module

This example shows how to declare underscore as a virtual module. When a module is declared as virtual, the plugin will append it to the dependency list.

main.js

// File: assets/js/main.js
import _ from 'underscore';

const rollup = "Rollup & Magento";
const message = `Hello from ${_.escape(rollup)}!!!`;
alert(message);

rollup.config.js

// File: rollup.config.js
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import magento2 from 'rollup-plugin-magento2';

export default {
  input: './assets/js/main.js',
  output: {
    file: './view/frontend/web/js/main.js',
    format: 'iife',
    name: 'bundle',
  },
  plugins: [
    babel({
      exclude: 'node_modules/**'
    }),
    resolve(),
    commonjs(),
    magento2({
      virtual: [
        'underscore'
      ]
    }),
  ]
};

Result

define(['underscore'], function(_) {
  'use strict';

  var rollup = "Rollup & Magento";
  var message = "Hello from ".concat(_.escape(rollup), "!!!");
  alert(message);
});

Local import

main.js

// File assets/js/main.js
import sayHello from './sayHello';
import { debounce } from 'underscore';

// Build a debounced version of sayHello
export default {
  sayHello: debounce(sayHello, 300)
};

sayHello.js

// File assets/js/sayHello.js
const sayHello = message => alert( message );
export default sayHello;

rollup.config.js

// File: rollup.config.js
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import magento2 from 'rollup-plugin-magento2';

export default {
  input: './assets/js/main.js',
  output: {
    file: './view/frontend/web/js/main.js',
    format: 'iife',
    name: 'bundle',
  },
  plugins: [
    babel({
      exclude: 'node_modules/**'
    }),
    resolve(),
    commonjs(),
    magento2({
      virtual: [
        'underscore'
      ]
    }),
  ]
};

Result

define(['underscore'], function(underscore) {
  'use strict';

  var debounce = underscore.debounce;

  var sayHello = function sayHello(message) {
    return alert(message);
  };

  var bundle = {
    sayHello: debounce(sayHello, 300)
  };

  return bundle;
});

Virtual directory

In case your module requires a lof of dependencies you can instead define a virtual directory. Any import prefixed with that name will be considered a Magento Javascript module.

main.js

// File assets/js/main.js
import $ from '@magento/jquery';
import { random } from '@magento/underscore';

export default {
  trim: $.trim,
  rand: random
};

rollup.config.js

// File: rollup.config.js
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import magento2 from 'rollup-plugin-magento2';

export default {
  input: './assets/js/main.js',
  output: {
    file: './view/frontend/web/js/main.js',
    format: 'iife',
    name: 'bundle',
  },
  plugins: [
    babel({
      exclude: 'node_modules/**'
    }),
    resolve(),
    commonjs(),
    magento2({
      virtualDir: 'magento'
    }),
  ]
};

Result

define(['jquery', 'underscore'], function($, underscore) {
  'use strict';

  var random = underscore.random;

  var bundle = {
    trim: $.trim,
    rand: random
  };

  return bundle;
});

Changelog

v1.0.2

Updated: Bump dependencies.

v1.0.1

Fixed: Destructured imports using virtualDir.

v1.0

Added: Support for virtualDir.

v0.1.1

Added: Tests.

v0.1

First release

License

MIT