Comments (17)
Changed the title of this issue since there are a couple of different things going wrong. Working on fixes
from rollup.
I've also noticed this problem. I haven't pushed anything yet, but I will tomorrow. If nothing else, we can compare notes.
from rollup.
Just pushed https://github.com/rollup/rollup/tree/gh-59 but have to run now - most stuff working but still a couple of problems with ES6 output
from rollup.
Please take a look at some changes to gh-59. Fixes the issue of ES6's import * as ns from '..'
in particular.
from rollup.
This issue also made me think about the UMD and IIFE formats, and how they manage import names. Say I want to use Albatross (random library name), like so:
import A from 'albatross';
A.someMethod('...');
// Yields the IIFE:
(function (A) { 'use strict';
A.someMethod('...');
})(A); // <-- Assumed name of Albatross global.
Albatross is assumed to be exposed through a global named exactly A, the same as the import name. This seems really fragile. Looking into rollups predecessors revealed JsModuleTranspiler by Katz. In one part of the README, his solution is described. Users supply a map from import names (in the example albatross
) to global name (Albatross
).
This problem is made more obvious by the test extensions.
import * as containers from 'shipping-port';
...
// Yields the IIFE:
(function (containers) { 'use strict';
...
})(containers); // <-- That doesn't seem right!
from rollup.
Nice - have merged your changes into gh-59. I think the only remaining problem here is imported bindings being erroneously renamed in ES6 bundles.
Re the Albatross example - this is in fact supported, it's the docs that are missing π¬ You just need to specify globals
, like so:
import $ from 'jquery';
$( function () {
$( 'body' ).html( '<h1>hello world!</h1>' );
});
rollup.rollup( opts ).then( bundle => {
const result = bundle.generate({
globals: { 'jquery': 'jQuery' },
format: 'umd'
});
});
Result:
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('jquery')) :
typeof define === 'function' && define.amd ? define(['jquery'], factory) :
factory(global.jQuery);
}(this, function ($) { 'use strict';
$( function () {
$( 'body' ).html( '<h1>hello world!</h1>' );
});
}));
from rollup.
@Rich-Harris Nice! Missed that one... π Should the issue be closed?
Do you have somewhere to place documentation? Do you prefer the Wiki or some Markdown file?
from rollup.
What are your thoughts on the ES6 renaming issue of imported bindings? Should getCanonicalName
be changed to be ES6 aware? Unfortunately it is used before generate
is called, and only then is the final output format known.
Should we perhaps use some other form of identifier mapping technique?
from rollup.
@Victorystick we're definitely thinking along the same lines. It might be easier to wait until generate
is called before deconflicting (wherein getCanonicalName
is called) for this exact reason. Will have a quick crack at that and see what falls out
from rollup.
Have got all the tests passing. Something about it seems slightly hacky (particularly caching the results of getCanonicalName
separately depending on whether the output is an ES6 module), but not catastrophically so (I hope). In any case, I'm thinking about a slightly wider-reaching refactor made possible by the fact that sorting now happens at the module (rather than statement) level β it means that modules could be responsible for rendering themselves, which (as well as being a good deal more memory-efficient, since each statement doesn't need its own MagicString
instance) might have implications for how bundle-level canonical names are determined.
from rollup.
Do you have somewhere to place documentation? Do you prefer the Wiki or some Markdown file?
There's a bare-bones wiki, with some stuff cribbed from the Esperanto wiki, and a lot of 'coming soon...'
from rollup.
What do you think of storing all names referenced in a bundle in some central collection? It could handle deconfliction of names separately from the Bundle/Module code. Later (during generation) it could be queried on a per-module basis (including perhaps some format specific information) to get the name to use when generating the output code.
Something like (in the context of a Module):
this.nameStore.getCanonicalName( name: string, direct: boolean ) // -> string
The direct
boolean could indicate the directness of the identifier access.
ES6 modules and SystemJS modules access variables directly, while CJS, AMD, UMD, and IIFE do not.
import { basename } from 'path';
basename('x/y.js');
// In CJS, AMD, UMD, and IIFE this becomes
path.basename('x/y.js');
// While in SystemJS it stays:
basename('x/y.js');
I think that would be nicer than passing es6
along, although it's a fair solution to the immediate problem.
from rollup.
Certainly feels easier to reason about, having a bundle-level collection. It wouldn't be flat though βΒ in situations like this...
// foo.js
export default 'a';
// bar.js
import a from './foo';
export { a as b };
// baz.js
import { b as c } from './bar';
...either each module needs to keep track of which names it needs to change, or the bundle needs to do that on behalf of each module, so at the very least it would need to be this:
this.nameStore.getCanonicalName( module, moduleLocalName, direct )
Not really sure which is better (or if there's a third way).
from rollup.
I'll look into the possibilities of such an approach. π
from rollup.
Shall we merge #62 in the meantime, so we've at least got test coverage and a base to refactor from, or you do reckon it's better to drop that and pursue the alternative?
from rollup.
Yes, I think so. Merge it!
from rollup.
Great, have merged that into master - will close this
from rollup.
Related Issues (20)
- Only transform input file and keep index.html untouched. HOT 1
- CJS output contains a reference to non-existent variable when exporting default and reexporting everything with interop: compat/auto HOT 2
- Document behavior of dynamic expressions in import() function
- Webpack promise external equivalent?
- Support for OpenBSD/adJ
- TypeError: Cannot read properties of null (reading 'render') HOT 12
- Treeshaking breaks acorn-walk "simple" 4.16.0+ HOT 7
- Support for FreeBSD HOT 7
- 4.16.4 throws Cannot find module @rollup/rollup-darwin-arm64 error MODULE_NOT_FOUND in Ember Embroider app HOT 4
- Node 16 not supported any longer, can't run more modern Node's either HOT 5
- Provide a mechanism to build manualChunks which can be reused across multiple outputs.
- Upgrade from vite 4 to vite 5 crashes in rollup with maximum call stack exceeded HOT 2
- function parameter tracking does not handle spread syntax in arguments correctly HOT 3
- top level return for IIFEs HOT 2
- Function body being mistakenly optimized away HOT 3
- After Babel starts runtime and rolls up preserveModules=true, it will result in an extra layer of output structure HOT 5
- I have a query about the rollup build process with export classes as opposed to export functions HOT 3
- Unknown Fatal Error if code is not an int
- Error: 'default' is not exported by node_modules/interactjs/dist/interact.min.js,
- Don't create dynamic import objects when never used
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rollup.