rollup.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { join, resolve as presolve } from 'path'
  2. import { rollup } from 'rollup'
  3. import svelte from 'rollup-plugin-svelte'
  4. import resolve from 'rollup-plugin-node-resolve'
  5. import commonjs from 'rollup-plugin-commonjs'
  6. import json from 'rollup-plugin-json'
  7. import * as EventEmitter from 'events'
  8. // svelte components möchten svelte importieren. Da wir aber auch components
  9. // ohne node_modules zulassen, müssen wir das verzwichnis an svelte weiterreichen
  10. const __nodeModules = process.env.PROD
  11. ? presolve(__dirname, 'node_modules/svelte')
  12. : presolve(__dirname, '../../node_modules/svelte')
  13. function moduleIds (event) {
  14. return {
  15. name: 'rollup-plugin-module-ids',
  16. buildEnd () {
  17. event(this.moduleIds)
  18. return null
  19. }
  20. }
  21. }
  22. export default class rollupBuild extends EventEmitter {
  23. constructor () {
  24. super()
  25. this.cache = null
  26. this.options = null
  27. }
  28. async build (options) {
  29. this.options = options ? { ...options, dirty: true } : this.options
  30. const inputOptions = {
  31. input: this.options.source,
  32. cache: this.cache,
  33. perf: true,
  34. treeshake: false,
  35. plugins: [
  36. json({ preferConst: true }),
  37. svelte({
  38. onwarn: (warning, handler) => {
  39. if (warning.code === 'css-unused-selector') return
  40. this.emit('message', warning)
  41. handler(warning)
  42. },
  43. sveltePath: __nodeModules,
  44. dev: !!this.options.debug
  45. }),
  46. moduleIds(ids => this.emit('moduleIDs', ids)),
  47. resolve({ preferBuiltins: false, browser: true }),
  48. commonjs()
  49. ]
  50. }
  51. const outputOptions = {
  52. file: join(this.options.dest, '/bundle.js'),
  53. format: 'cjs',
  54. name: 'components',
  55. sourcemap: true
  56. }
  57. try {
  58. const bundle = await rollup(inputOptions)
  59. this.cache = bundle.cache
  60. await bundle.write(outputOptions)
  61. console.log('Komponenten erfolgreich kompiliert')
  62. console.log(bundle.getTimings())
  63. if (this.options.dirty) {
  64. this.emit('bundle', bundle)
  65. delete this.options.dirty
  66. }
  67. } catch (error) {
  68. this.emit('message', error)
  69. throw error
  70. }
  71. }
  72. }