Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

external files are being bundled for commonjs #4273

Closed
dnalborczyk opened this issue Nov 17, 2021 · 7 comments · Fixed by rollup/plugins#1038
Closed

external files are being bundled for commonjs #4273

dnalborczyk opened this issue Nov 17, 2021 · 7 comments · Fixed by rollup/plugins#1038

Comments

@dnalborczyk
Copy link
Contributor

dnalborczyk commented Nov 17, 2021

Rollup Version

2.60.0

Operating System (or Browser)

n/a

Node Version (if applicable)

n/a

Link To Reproduction

n/a

Expected Behaviour

.

Actual Behaviour

foo.js

const foo = require("./foo.js")

console.log(foo)

foo.js

module.exports = "foo";

rollup.config.js

import commonjs from "@rollup/plugin-commonjs";

export default {
  external: ["./foo.js"],
  input: "./index.js",
  output: {
    file: "./build/bundle.js",
    format: "module",
  },
  plugins: [commonjs()],
}

bundled:

import './foo.js';

var requireTest = {};

var require$$0 = "foo";

const foo = require$$0;

console.log(foo);

export { requireTest as default };

possibly a commonjs-plugin bug (also does not work with the json plugin). might ignore the external setting, or rollup is not passing it along. hence I'm filing it here.

works when source files are esm.

import foo from "./foo.js"

console.log(foo)
export default "foo"

bundled:

import foo from './foo.js';

console.log(foo);

failed attempt to cross file: rollup/plugins#1044

@FoxDaxian
Copy link
Contributor

it is works after remove commonjs:

export default {
  external: ["foo.js"],
  input: "./index.js",
  output: {
    exports: 'auto',
    file: "./build/bundle.js",
    format: "cjs",
  },
  plugins: [],
}

output:

'use strict';

const foo = require("./foo.js");

console.log(foo);

seem need file issue to rollup/plugins again.

@FoxDaxian
Copy link
Contributor

actually rollup provides some use case in rollup/test/form/samples/external-imports/main.js

@dnalborczyk
Copy link
Contributor Author

it is works after remove commonjs:

thanks @FoxDaxian , you are right. now I'm entirely lost and confused. 🤔

seem need file issue to rollup/plugins again.

not going to. out of principle.

@lukastaegert
Copy link
Member

This is indeed an issue with the commonjs plugin. I added a fix to the PR rollup/plugins#1038 I am working on at the moment. May take a while until it is merged though (by the way, that one will look for more beta testers soon).

@dnalborczyk
Copy link
Contributor Author

dnalborczyk commented Nov 17, 2021

@FoxDaxian

it got a little late yesterday. I was surprised as I thought he commonjs syntax is unknown to rollup. and as it turns out, it is. this is not really working without the commonjs plugin, as you get to the same result excluding the external parameter. bundling is effectively being ignored for this part of the commonjs code.

export default {
  // external: ["foo.js"], <--- removed
  input: "./index.js",
  output: {
    exports: 'auto',
    file: "./build/bundle.js",
    format: "cjs",
  },
  plugins: [],
}

@FoxDaxian
Copy link
Contributor

@dnalborczyk
I think it is.
and used import after remove external. will output as below:

'use strict';
var foo = "foo";
console.log(foo);

rollup do not have require statement node, it can deal with import statement only. so it result like external even though excluding the external.


can not found some require statement node
image

@FoxDaxian
Copy link
Contributor

maybe this can explain the problem we encountered:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants