-
Notifications
You must be signed in to change notification settings - Fork 126
'import' and 'export' may only appear at the top level #90
'import' and 'export' may only appear at the top level #90
Comments
So, there are a few things going on here, it turns out. The error message you saw was caused by this plugin attempting to transform a module which had an After that, the bundle builds fine. But it doesn't run. Because (function (fs, path) {
/* bundle goes here */
}(fs, path)); ...but We can use the // rollup.config.js
export default {
entry: 'scripts/main.js',
dest: 'public/assets/js/bundle.js',
...,
globals: { fs: '{}', path: '{}' }
}; That results in this: (function (fs, path) {
/* bundle goes here */
}({}, {})); But then we get this error...
...because of this line inside the if (typeof define !== 'function') {
var define = amdefine_1(module, require);
} In case you weren't familiar with it, amdefine is a package that allows you to write AMD modules and have them converted into CommonJS modules at runtime by adding that snippet. It's exactly as ridiculous an idea as it sounds, and it means your bundle has to include this dead weight (in fact, now that I investigate, that's the only reason the bundle needs Of course, we don't actually need the try {
/* istanbul ignore next */
if (typeof define !== 'function' || !define.amd) {
// We don't support this in AMD environments. For these environments, we asusme that
// they are running on the browser and thus have no need for the source-map library.
let SourceMap = require('source-map');
SourceNode = SourceMap.SourceNode;
}
} catch (err) {
/* NOP */
} Rollup doesn't have the luxury of running the code to find out what's necessary and what isn't, so it has to import // rollup.config.js
plugins: [
...,
nodeResolve({
jsnext: true,
main: true,
browser: true,
preferBuiltins: false,
skip: [ 'source-map' ]
})
],
external: [ 'source-map' ], Then we hit another 'require is not defined' error caused by this code: if ('function' !== 'undefined' && require.extensions) {
require.extensions['.handlebars'] = extension;
require.extensions['.hbs'] = extension;
} Another runtime check, this time to see if we want to make it possible for Node users to compile templates at runtime by doing We can get rid of that whole block by replacing // rollup.config.js
plugins: [
...,
replace({
'require.extensions': 'null'
})
] And after that, it works. Whether it will continue to work as you use more of Handlebars remains to be seen. So the blame is shared between Rollup (for making it too easy for plugins to be in the wrong order) and the ghosts of module systems past. I wouldn't judge you for using Rollup to bundle your own code as a CommonJS module, then handing the result off to Browserify or Webpack to deal with the various layers of insanity that have accreted in the Node ecosystem over the past few years. There's also Rollupify, if you wanted to take a Browserify-first approach. I've put a working version of the example in this repo, so you can poke and prod it: https://github.com/Rich-Harris/rollup-plugin-commonjs-issue-90 Thanks for coming on this wild adventure with me, hope it helps! |
@Rich-Harris Wow, didn't know it was this bad with the dependencies! That is some quality investigation of yours! Really appreciate your detailed response. Regarding the complexity of the bundling layers I'm totally on your side. In the past years people have found really crazy ways to make their libraries run both on node and inside the browser. I agree that this is more the fault of library authors and nothing which rollup should invest time in supporting. I can't wait for the day when most modules are es6 based. Thanks for an awesome bundler! We've been using it with great success at work! Thanks for the working repo, I can only imagine how much time it took to step through all these corner cases 🎉 |
I'm having trouble bundling handlebars. On bundling I get the following error:
My first try to fix this, was to use the named exports feature. But although I've explicitly specified handlebars as a named export, it seems to be ignored and I always get the same error message as above.
Here is my setup:
The text was updated successfully, but these errors were encountered: