{"action":"created","issue":{"url":"https://api.github.com/repos/brunch/brunch/issues/1240","repository_url":"https://api.github.com/repos/brunch/brunch","labels_url":"https://api.github.com/repos/brunch/brunch/issues/1240/labels{/name}","comments_url":"https://api.github.com/repos/brunch/brunch/issues/1240/comments","events_url":"https://api.github.com/repos/brunch/brunch/issues/1240/events","html_url":"https://github.com/brunch/brunch/pull/1240","id":141608095,"number":1240,"title":"[WIP] Compilers JS exports","user":{"login":"goshakkk","id":315596,"avatar_url":"https://avatars.githubusercontent.com/u/315596?v=3","gravatar_id":"","url":"https://api.github.com/users/goshakkk","html_url":"https://github.com/goshakkk","followers_url":"https://api.github.com/users/goshakkk/followers","following_url":"https://api.github.com/users/goshakkk/following{/other_user}","gists_url":"https://api.github.com/users/goshakkk/gists{/gist_id}","starred_url":"https://api.github.com/users/goshakkk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/goshakkk/subscriptions","organizations_url":"https://api.github.com/users/goshakkk/orgs","repos_url":"https://api.github.com/users/goshakkk/repos","events_url":"https://api.github.com/users/goshakkk/events{/privacy}","received_events_url":"https://api.github.com/users/goshakkk/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-03-17T14:58:54Z","updated_at":"2016-03-18T01:21:49Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/brunch/brunch/pulls/1240","html_url":"https://github.com/brunch/brunch/pull/1240","diff_url":"https://github.com/brunch/brunch/pull/1240.diff","patch_url":"https://github.com/brunch/brunch/pull/1240.patch"},"body":"**:construction: :construction: :construction: Work in progress. Do not merge. :construction: :construction: :construction:**\r\n\r\nAllows non-JS compilers to additionally expose a JS version of a file (use case: CSS modules, the compiler needs to both pre-process the source file + obfuscate class name, as well as create a JS module with class name mappings)\r\n\r\nTo achieve that, plugins that used to return `{data, map}` can now return `{data, map, exports}` where `exports` is a string containing javascript code:\r\n\r\n```javascript\r\nclass MyCompiler {\r\n  compile(params) {\r\n    const path = params.path;\r\n    const data = params.data;\r\n\r\n    const compiled = magic(data);\r\n    const exports = \"module.exports = 42\";\r\n\r\n    return Promise.resolve({ data: compiled, exports });\r\n  }\r\n}\r\n```\r\n\r\nInternally it is achieved by allowing a single file have multiple *targets* — at max, one per file type (javascript, stylesheet, template), which is enough to get the example above working. It's not public API so should we need more flexible architecture some time later, we can also have multiple outputs of the same file type or something.\r\n\r\nA less flexible option would be to hardcode store `jsExports` on source file, which also requires more code to make this \"special case\" work. (See fa762b8 for hardcoded jsExports vs targets)\r\n\r\nTo do:\r\n\r\n- [x] allow plugins to emit JS modules for non-JS files\r\n- [x] introduce the notion of targets internally\r\n- [ ] check if existing APIs were not affected:\r\n  - [x] `onCompile` callback and the `sourceFiles` it receives (without JS-exported ones; create and document a new attribute for these in the callback))\r\n  - [ ] make sure resolving file's dependencies still works\r\n- [x] document the `onCompile` callback changes\r\n- [x] document the plugin API changes\r\n- [ ] make sure changing a style file also causes its JS exports to update\r\n- [ ] update brunch-maintained style plugins to have an option to produce CSS modules + JS exports with mappings:\r\n  - [ ] css-brunch\r\n  - [ ] stylus-brunch\r\n  - [ ] less-brunch\r\n  - [ ] sass-brunch"},"comment":{"url":"https://api.github.com/repos/brunch/brunch/issues/comments/198152156","html_url":"https://github.com/brunch/brunch/pull/1240#issuecomment-198152156","issue_url":"https://api.github.com/repos/brunch/brunch/issues/1240","id":198152156,"user":{"login":"colinbate","id":1060912,"avatar_url":"https://avatars.githubusercontent.com/u/1060912?v=3","gravatar_id":"","url":"https://api.github.com/users/colinbate","html_url":"https://github.com/colinbate","followers_url":"https://api.github.com/users/colinbate/followers","following_url":"https://api.github.com/users/colinbate/following{/other_user}","gists_url":"https://api.github.com/users/colinbate/gists{/gist_id}","starred_url":"https://api.github.com/users/colinbate/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/colinbate/subscriptions","organizations_url":"https://api.github.com/users/colinbate/orgs","repos_url":"https://api.github.com/users/colinbate/repos","events_url":"https://api.github.com/users/colinbate/events{/privacy}","received_events_url":"https://api.github.com/users/colinbate/received_events","type":"User","site_admin":false},"created_at":"2016-03-18T01:21:49Z","updated_at":"2016-03-18T01:21:49Z","body":"This is great to see.\n\nI wanted to import some external CSS using the import syntax in the JS file and found it worked, but the module wasn't added to the JS so it would error not found at run time. I was going to ask if there an official solution planned.\n\nIn the mean time I wrote colinbate/wrap-require-brunch to make the CSS require statement a no op in the JS file. It works but I like the flexibility of this as I was looking into CSS modules as the next step.\n\nLet me know if you want help testing it out. "}}