Skip to content
This repository has been 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

Closed
gaearon opened this issue Mar 10, 2017 · 6 comments
Closed

Comments

@gaearon
Copy link

gaearon commented Mar 10, 2017

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.

@Rich-Harris
Copy link
Contributor

Rollup is always going to reserve the right to rename variables as it sees fit to avoid conflicts, so whatever this plugin does can make no guarantees about the final output — but Rollup will always try to respect the developer's intentions.

Given the codebase that you're working with, I think we can get away with the following: on encountering a variable declarator like var x = require('x'), do this...

import x from 'x';

...rather than this:

import require$$0 from 'x';
// ...
var x = require$$0;

That would be generally beneficial anyway, if a tiny bit trickier than the current naive transformation, so I'm in favour of making that change.

Like I say, not bullet-proof, but maybe good enough to unblock you — what do you reckon?

@gaearon
Copy link
Author

gaearon commented Mar 10, 2017

I'm not 100% sure I understand the difference but what you describe is likely what I wanted. For now I hacked around it with a Babel plugin: facebook/react@0a50b6a.

Rich-Harris added a commit that referenced this issue Mar 10, 2017
prefer existing names for dependencies, over require$$x
@Rich-Harris
Copy link
Contributor

Just released 8.0.0 which addresses this issue to the extent possible. As a bonus, will make for slightly cleaner/more readable bundles, for what it's worth. Shout if it doesn't do what you need.

@gaearon
Copy link
Author

gaearon commented Mar 10, 2017

I think this worked.
Thank you so much for quick fix.

@marvinhagemeister
Copy link

@Rich-Harris @gaearon Huge props to you both! It's amazing when people from major repos help each other out. That's what I love about the js community 👍

@gaearon
Copy link
Author

gaearon commented Mar 11, 2017

In this case, for now, we’re just selfishly consuming fruits of hard labor 😊. I do hope we’ll get to contributing back if this experiment succeeds and we’ll depend on Rollup more.

trueadm added a commit to facebook/react that referenced this issue Apr 5, 2017
* WIP

* fbjs support

* WIP

* dev/prod mode WIP

* More WIP

* builds a cjs bundle

* adding forwarding modules

* more progress on forwarding modules and FB config

* improved how certain modules get inlined for fb and cjs

* more forwarding modules

* added comments to the module aliasing code

* made ReactPerf and ReactTestUtils bundle again

* Use -core suffix for all bundles

This makes it easier to override things in www.

* Add a lazy shim for ReactPerf

This prevents a circular dependency between ReactGKJSModule and ReactDOM

* Fix forwarding module for ReactCurrentOwner

* Revert "Add a lazy shim for ReactPerf"

This reverts commit 723b402.

* Rename -core suffix to -fb for clarity

* Change forwarding modules to import from -fb

This is another, more direct fix for ReactPerf circular dependency

* should fix fb and cjs bundles for ReactCurrentOwner

* added provides module for ReactCurrentOwner

* should improve console output

* fixed typo with argument passing on functon call

* Revert "should improve console output"

This breaks the FB bundles.

This reverts commit 65f11ee.

* Work around internal FB transform require() issue

* moved  ReactInstanceMap out of React and into ReactDOM and ReactDOMFiber

* Expose more internal modules to www

* Add missing modules to Stack ReactDOM to fix UFI

* Fix onlyChild module

* improved the build tool

* Add a rollup npm script

* Rename ReactDOM-fb to ReactDOMStack-fb

* Fix circular dependencies now that ReactDOM-fb is a GK switch

* Revert "Work around internal FB transform require() issue"

This reverts commit 0a50b6a.

* Bump rollup-plugin-commonjs to include a fix for rollup/rollup-plugin-commonjs#176

* Add more forwarding modules that are used on www

* Add even more forwarding modules that are used on www

* Add DOMProperty to hidden exports

* Externalize feature flags

This lets www specify them dynamically.

* Remove forwarding modules with implementations

Instead I'm adding them to react-fb in my diff.

* Add all injection necessary for error logging

* Add missing forwarding module (oops)

* Add ReactART builds

* Add ReactDOMServer bundle

* Fix UMD build of ReactDOMFiber

* Work in progress: start adding ReactNative bundle

* tidied up the options for bundles, so they can define what types they output and exclude

* Add a working RN build

* further improved and tidied up build process

* improved how bundles are built by exposing externals and making the process less "magical", also tidied up code and added more comments

* better handling of bundling ReactCurrentOwner and accessing it from renderer modules

* added NODE_DEV and NODE_PROD

* added NPM package creation and copying into build chain

* Improved UMD bundles, added better fixture testing and doc plus prod builds

* updated internal modules (WIP)

* removed all react/lib/* dependencies from appearing in bundles created on build

* added react-test-renderer bundles

* renamed bundles and paths

* fixed fixture path changes

* added extract-errors support

* added extractErrors warning

* moved shims to shims directory in rollup scripts

* changed pathing to use build rather than build/rollup

* updated release doc to reflect some rollup changes

* Updated ReactNative findNodeHandle() to handle number case (#9238)

* Add dynamic injection to ReactErrorUtils (#9246)

* Fix ReactErrorUtils injection (#9247)

* Fix Haste name

* Move files around

* More descriptive filenames

* Add missing ReactErrorUtils shim

* Tweak reactComponentExpect to make it standalone-ish in www

* Unflowify shims

* facebook-www shims now get copied over correctly to build

* removed unnecessary resolve

* building facebook-www/build is now all sync to prevent IO issues plus handles extra facebook-www src assets

* removed react-native-renderer package and made build make a react-native build dir instead

* 😭😭😭

* Add more SSR unit tests for elements and children. (#9221)

* Adding more SSR unit tests for elements and children.

* Some of my SSR tests were testing for react-text and react-empty elements that no longer exist in Fiber. Fixed the tests so that they expect correct markup in Fiber.

* Tweaked some test names after @gaearon review comment #9221 (comment) . Also realized that one of the tests was essentially a direct copy of another, so deleted it.

* Responding to code review #9221 (review) . Thanks @spicyj!

* ReactElementValidator uses temporary ReactNative View propTypes getter (#9256)

* Updating packages for 16.0.0-alpha.6 release

* Revert "😭😭😭"

This reverts commit 7dba33b.

* Work around Jest issue with CurrentOwner shared state in www

* updated error codes

* splits FB into FB_DEV and FB_PROD

* Remove deps on specific builds from shims

* should no longer mangle FB_PROD output

* Added init() dev block to ReactTestUtils

* added shims for DEV only code so it does not get included in prod bundles

* added a __DEV__ wrapping code to FB_DEV

* added __DEV__ flag behind a footer/header

* Use right haste names

* keeps comments in prod

* added external babel helpers plugin

* fixed fixtures and updated cjs/umd paths

* Fixes Jest so it run tests correctly

* fixed an issue with stubbed modules not properly being replaced due to greedy replacement

* added a WIP solution for ReactCurrentOwner on FB DEV

* adds a FB_TEST bundle

* allows both ReactCurrentOwner and react/lib/ReactCurrentOwner

* adds -test to provides module name

* Remove TEST env

* Ensure requires stay at the top

* added basic mangle support (disbaled by default)

* per bundle property mangling added

* moved around plugin order to try and fix deadcode requires as per rollup/rollup#855

* Fix flow issues

* removed gulp and grunt and moved tasks to standalone node script

* configured circleci to use new paths

* Fix lint

* removed gulp-extract-errors

* added test_build.sh back in

* added missing newline to flow.js

* fixed test coverage command

* changed permissions on test_build.sh

* fixed test_html_generations.sh

* temp removed html render test

* removed the warning output from test_build, the build should do this instead

* fixed test_build

* fixed broken npm script

* Remove unused ViewportMetrics shim

* better error output

* updated circleci to node 7 for async/await

* Fixes

* removed coverage test from circleci run

* circleci run tets

* removed build from circlci

* made a dedicated jest script in a new process

* moved order around of circlci tasks

* changing path to jest in more circleci tests

* re-enabled code coverage

* Add file header to prod bundles

* Remove react-dom/server.js (WIP: decide on the plan)

* Only UMD bundles need version header

* Merge with master

* disabled const evaluation by uglify for <script></script> string literal

* deal with ART modules for UMD bundles

* improved how bundle output gets printed

* fixed filesize difference reporting

* added filesize dep

* Update yarn lockfile for some reason

* now compares against the last run branch built on

* added react-dom-server

* removed un-needed comment

* results only get saved on full builds

* moved the rollup sized plugin into a plugins directory

* added a missing commonjs()

* fixed missing ignore

* Hack around to fix RN bundle

* Partially fix RN bundles

* added react-art bundle and a fixture for it

* Point UMD bundle to Fiber and add EventPluginHub to exported internals

* Make it build on Node 4

* fixed eslint error with resolve being defined in outer scope

* Tweak how build results are calculated and stored

* Tweak fixtures build to work on Node 4

* Include LICENSE/PATENTS and fix up package.json files

* Add Node bundle for react-test-renderer

* Revert "Hack around to fix RN bundle"

We'll do this later.

This reverts commit 59445a6.

* Revert more RN changes

We'll do them separately later

* Revert more unintentional changes

* Revert changes to error codes

* Add accidentally deleted RN externals

* added RN_DEV/RN_PROD bundles

* fixed typo where RN_DEV and RN_PROD were the wrong way around

* Delete/ignore fixture build outputs

* Format scripts/ with Prettier

* tidied up the Rollup build process and split functions into various different files to improve readability

* Copy folder before files

* updated yarn.lock

* updated results and yarn dependencies to the latest versions
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants