Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Is there a way to keep top-level import names constant in the output? #176

@gaearon

Description

@gaearon

We are looking with @trueadm into using Rollup for React.
My bundle (with a bunch of external dependencies) currently looks like this:

'use strict';

var require$$0 = require('react/lib/React');
var require$$0$1 = require('fbjs/lib/invariant');
var require$$2 = require('fbjs/lib/warning');
var require$$0$2 = require('fbjs/lib/ExecutionEnvironment');
var emptyFunction = require('fbjs/lib/emptyFunction');
var EventListener = require('fbjs/lib/EventListener');
var getUnboundedScrollPosition = require('fbjs/lib/getUnboundedScrollPosition');
var containsNode = require('fbjs/lib/containsNode');
var focusNode = require('fbjs/lib/focusNode');
var emptyObject = require('fbjs/lib/emptyObject');

var invariant$1 = require$$0$1;

// ...

Notice how some top-level imports have nice names (e.g. EventListener) while others were renamed in place (e.g. require$$2). I noticed that renaming only seems to happen if there's a variable down below that “steals” the nice name:

'use strict';

var require$$0 = require('React');

// ...

var React = require$$0;

Would it be possible to make sure that it’s always the other way around: the top-level imports keep their names, and any further references to them are renamed?

This is important for us because our internal require() transform mandates that the variable name matches the import name. So it always has to be var x = require('x'). In this example, the desired output would be:

'use strict';

var React = require('react/lib/React');
var invariant = require('fbjs/lib/invariant');
var warning = require('fbjs/lib/warning');
var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
var emptyFunction = require('fbjs/lib/emptyFunction');
var EventListener = require('fbjs/lib/EventListener');
var getUnboundedScrollPosition = require('fbjs/lib/getUnboundedScrollPosition');
var containsNode = require('fbjs/lib/containsNode');
var focusNode = require('fbjs/lib/focusNode');
var emptyObject = require('fbjs/lib/emptyObject');

// ...

var require$$0 = React;
// etc

Thanks for consideration!

If you want to give this a try, you can run:

git clone https://github.com/trueadm/react.git
cd react
git checkout rollup
npm i
node scripts/rollup/build.js

and explore build/rollup/ReactDOM-core.js.

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