Skip to content

Treating relative paths as externals via resolveId does not work #763

Description

@kaste

Hi!

For a specific build I want to treat a (normal) (relative) import as an external resource. Given::

import {throttle} from './Lib/decorators'

throttle(/* function */)...

I want a build roughly as follows::

(function(Lib_decorators){ ... }(Lib.decorators))

I first tried resolveId with something maybe naive like if (importee.endsWith('Lib/decorators')) return false b/c you say that returning a "falsy value signals that importee should be treated as an external module and not included in the bundle" (https://github.com/rollup/rollup/wiki/Plugins).

But that doesn't work at all; it throws Could not resolve... and aborts. Reading the source code https://github.com/rollup/rollup/blob/master/src/Bundle.js#L228-L254 hints me that I have to use options.external which is otherwise not pluggable instead. But then rollup treats './Lib/decorators' and ../Lib/decorators (and so on) as different modules. Output::

(function (__Lib_decorators,___Lib_decorators) {
        'use strict';

}(__Lib_decorators,___Lib_decorators));

From my perspective the codepath I linked to is already too complex and just open for bugs.

The best workaround I found is letting resolveId return a name e.g. Lib.decorators, then treating this name as external via options.external, lastly rename the wrongly normalized name Lib_decorators back to Lib.decorators via options.globals::

const plugin = function() {
  return {
    resolveId(importee, importer) {
      if (importee.endsWith('Lib/decorators')) return 'Lib.decorators'
    }
  }
}

export default {
  format: 'iife',
  external: function(id) {
    if (id === 'Lib.decorators') return true
  },
  globals: function(id) {
    if (id === 'Lib.decorators') return 'Lib.decorators'
  },
  plugins: [ plugin() ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions