{"action":"created","issue":{"url":"https://api.github.com/repos/rollup/rollup/issues/1418","repository_url":"https://api.github.com/repos/rollup/rollup","labels_url":"https://api.github.com/repos/rollup/rollup/issues/1418/labels{/name}","comments_url":"https://api.github.com/repos/rollup/rollup/issues/1418/comments","events_url":"https://api.github.com/repos/rollup/rollup/issues/1418/events","html_url":"https://github.com/rollup/rollup/issues/1418","id":231916912,"number":1418,"title":"Hide require in order to do bundle splitting","user":{"login":"vjeux","id":197597,"avatar_url":"https://avatars3.githubusercontent.com/u/197597?v=3","gravatar_id":"","url":"https://api.github.com/users/vjeux","html_url":"https://github.com/vjeux","followers_url":"https://api.github.com/users/vjeux/followers","following_url":"https://api.github.com/users/vjeux/following{/other_user}","gists_url":"https://api.github.com/users/vjeux/gists{/gist_id}","starred_url":"https://api.github.com/users/vjeux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vjeux/subscriptions","organizations_url":"https://api.github.com/users/vjeux/orgs","repos_url":"https://api.github.com/users/vjeux/repos","events_url":"https://api.github.com/users/vjeux/events{/privacy}","received_events_url":"https://api.github.com/users/vjeux/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2017-05-29T02:58:36Z","updated_at":"2017-05-29T03:22:47Z","closed_at":null,"body":"Bundle splitting is not handled by rollup but I want to use rollup so here's my attempt:\r\n\r\nI have two files `a.js` and `b.js` that I want to bundle separately and `a.js` requires `b.js`:\r\n\r\n```js\r\n// a.js\r\nconst b = require(\"./b\");\r\n```\r\n\r\nWhat I want is to hide the fact that a requires b so that rollup leaves `require(\"b\")` there.\r\n\r\n## Attempt 1: externals\r\n\r\n```js\r\nexternals: ['b.js']\r\n```\r\n\r\nUsing the absolute file path doesn't do anything.\r\n\r\n```js\r\nexternals: ['./b']\r\n```\r\n\r\nUsing the path the same way I put it inside of require seems to be doing something but now it won't compile my file.\r\n\r\n```js\r\nCould not resolve './b' from a.js\r\n```\r\n\r\n## Attempt 2: use another variable\r\n\r\n```js\r\nconst b = (0, require)(\"./b\");\r\n```\r\n\r\nIf you try to use a different AST than `require(x)` it doesn't include it in the bundle (yay!) but it still replaces require by a local function that throws:\r\n\r\n```js\r\nthrow new Error('Dynamic requires are not currently supported by rollup-plugin-commonjs');\r\n```\r\n\r\nSad panda :(\r\n\r\n## Attempt 3: using eval\r\n\r\n```js\r\nconst b = eval(\"require\")(\"./b\");\r\n```\r\n\r\nThis works: it doesn't include `b` in the bundle and works at runtime! Yay! But, yeahh... there's a but: you've got a huge warning when building:\r\n\r\n```js\r\n⚠️   Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification\r\nhttps://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval\r\nparser.js (14:20)\r\n12:     parseFunction = eval(\"require\")(\"./src/parser-typescript\");\r\n13:   } else if (opts.parser === \"postcss\") {\r\n14:     parseFunction = eval(\"require\")(\"./src/parser-postcss\");\r\n                        ^\r\n15:   } else {\r\n16:     parseFunction = eval(\"require\")(\"./src/parser-babylon\");\r\n```\r\n\r\nI haven't found a way to mute this warning unfortunately.\r\n\r\nAm I missing something?"},"comment":{"url":"https://api.github.com/repos/rollup/rollup/issues/comments/304564653","html_url":"https://github.com/rollup/rollup/issues/1418#issuecomment-304564653","issue_url":"https://api.github.com/repos/rollup/rollup/issues/1418","id":304564653,"user":{"login":"Rich-Harris","id":1162160,"avatar_url":"https://avatars0.githubusercontent.com/u/1162160?v=3","gravatar_id":"","url":"https://api.github.com/users/Rich-Harris","html_url":"https://github.com/Rich-Harris","followers_url":"https://api.github.com/users/Rich-Harris/followers","following_url":"https://api.github.com/users/Rich-Harris/following{/other_user}","gists_url":"https://api.github.com/users/Rich-Harris/gists{/gist_id}","starred_url":"https://api.github.com/users/Rich-Harris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Rich-Harris/subscriptions","organizations_url":"https://api.github.com/users/Rich-Harris/orgs","repos_url":"https://api.github.com/users/Rich-Harris/repos","events_url":"https://api.github.com/users/Rich-Harris/events{/privacy}","received_events_url":"https://api.github.com/users/Rich-Harris/received_events","type":"User","site_admin":false},"created_at":"2017-05-29T03:22:47Z","updated_at":"2017-05-29T03:22:47Z","body":"I think you were close with option a — the trick is to resolve the path, like so:\r\n\r\n```js\r\n// rollup.config.js\r\nimport path from 'path';\r\n\r\nexport default {\r\n  entry: 'src/a.js',\r\n  dest: 'dist/bundle.js',\r\n  format: 'cjs',\r\n  externals: [\r\n    path.resolve('src/b.js')\r\n  ],\r\n  // ...\r\n};\r\n```\r\n\r\nThen, an `import b from './b.js'` declaration would be turned into a `require` statement. You can also use the `paths` option to control the path of the import in the bundle:\r\n\r\n```js\r\n// rollup.config.js\r\nimport path from 'path';\r\n\r\nconst b = path.resolve('src/b.js');\r\n\r\nexport default {\r\n  entry: 'src/a.js',\r\n  dest: 'dist/bundle.js',\r\n  format: 'cjs',\r\n  externals: [b],\r\n  paths: {\r\n    [b]: '../src/b.js' // would result in `b = require('../src/b.js')`\r\n  }\r\n  // ...\r\n};\r\n```"}}
{"actor":{"display_login":"Rich-Harris"}}