Skip to content

Give plugins bundle access? #803

Closed
Closed

Description

@skeggse

I've got a couple of places where I'd like to be able to call resolveId from a plugin. One such place is the handlebars.js plugin (see jibhaine/rollup-plugin-handlebars#2), and another is in rollup-plugin-root-import (see mixmaxhq/rollup-plugin-root-import#1).

In the former case, I'd like to be able to resolve the paths used to reference partials (handlebars imports), because handlebars uses its own mechanism for linking templates and partials. Handlebars generally works by providing a registerPartial method, and partial invocations use the internal map of partials. A more ideal solution would involve explicitly passing dependencies to each handlebars template, but that would break interoperability with conventional handlebars usage.

In the latter case, with root-import, I really only need access to the entryId, as that's what would enable cleaner default behavior, but a more general solution would involve exposing the ability to run resolveId from the plugin itself:

import {rollup} from 'rollup';
import rootImport from 'rollup-plugin-root-import';

// ideal usage
rollup.rollup({
  // absolute paths would be resolved relative to this
  entry: 'src/index.js'
  plugins: [rootImport]
}).then(...);

// current usage
rollup.rollup({
  entry: 'src/index.js',
  plugins: [
    rootImport({
      // currently needs this to resolve accurately
      root: `${__dirname}/src`
    })
  ]
}).then(...);

Without this functionality, rootImport would need to be explicitly passed its root

Thoughts? Maybe there's a way to accomplish these goals without introducing weird coupling behavior?

Activity

  1. mstijak commented on Dec 7, 2016

    @mstijak

    I would like to write a babel plugin which rewrites package imports to use src files instead of compiled (dist) files, similar to babel-plugin-lodash.
    Bundle would provide access to entryModule.exports and entryModule.exportsAll mappings that could be used to generate a manifest file required for such a plugin.

  2. Rich-Harris commented on Jan 6, 2017

    @Rich-Harris
    Contributor

    Sorry for slow reply. Can you be more specific about what APIs you would need to accomplish these tasks?

    FYI you can pass an options hook to intercept and manipulate options:

    // rollup.config.js
    function logEntryFile () {
      return {
        name: 'a fairly useless plugin',
        options ( options ) {
          console.log( options.entry ); // logs 'src/index.js'
        }
      };
    }
    
    export default {
      entry: 'src/index.js',
      dest: 'bundle.js',
      format: 'iife',
      plugins: [ logEntryFile() ]
    };
  3. mstijak commented on Jan 7, 2017

    @mstijak

    @Rich-Harris
    In my case, I would like to get access to the instance of the Bundle class.
    Maybe it can be the second argument passed to the promise resolve function or a property of the result.

    rollup(options).then((result, bundle) => {...})
    

    I managed to solve my problem using rollup-plugin-babel and a custom Babel plugin, so it's fine if you decide not to make any changes.

  4. Rich-Harris commented on Jan 7, 2017

    @Rich-Harris
    Contributor

    @mstijak giving plugins access to internal implementation details is one of those things that sounds harmless at the time but almost always ends up being a mistake, because as soon as that happens you've basically made it part of the API, and a seemingly minor refactor in a patch release can have far-reaching breaking changes that are impossible to anticipate.

    If we were to expose any additional functionality it would have to be done so explicitly, with documentation and tests.

  5. guybedford commented on Jun 6, 2018

    @guybedford
    Contributor

    This has been opened up in #2208. Feedback welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions