rollup.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import * as rollup from 'rollup'
  2. import globImport from 'rollup-plugin-glob-import'
  3. import resolve from 'rollup-plugin-node-resolve'
  4. import commonjs from 'rollup-plugin-commonjs'
  5. import svelte from 'rollup-plugin-svelte'
  6. import replace from 'rollup-plugin-replace'
  7. import copy from 'rollup-plugin-cpy'
  8. import json from 'rollup-plugin-json'
  9. import path from 'path'
  10. let inputOptions, outputOptions, watchOptions
  11. export const rollupSetup = (options) => {
  12. const statics = options.statics
  13. const source = options.source
  14. const dest = options.dest
  15. const is = options.is
  16. inputOptions = {
  17. input: path.join(source, '/plugin-loader.js'),
  18. plugins: [
  19. json({ preferConst: true }),
  20. resolve({
  21. customResolveOptions: {
  22. moduleDirectory: 'node_modules'
  23. }
  24. }),
  25. commonjs(),
  26. globImport({
  27. format: 'default',
  28. intercept (sources, importer, importee) {
  29. return sources.filter(s => !path.basename(s, '.html').startsWith('_'))
  30. },
  31. rename (name, id) {
  32. return `${path.basename(path.dirname(id))}___${path.basename(id, '.html')}`
  33. }
  34. }),
  35. svelte({
  36. parser: 'v2',
  37. onwarn: (warning, handler) => {
  38. if (warning.code === 'css-unused-selector') return
  39. console.log(warning.code)
  40. // handler(warning)
  41. }
  42. }),
  43. replace({
  44. daten: id => is.development ? `plugins/${path.basename(path.dirname(id))}/daten` : `${dest}/${path.basename(path.dirname(id))}/daten`
  45. }),
  46. copy({
  47. files: '*/daten/*',
  48. dest: is.development ? `${statics}/plugins` : dest,
  49. options: {
  50. parents: true,
  51. cwd: source,
  52. verbose: true
  53. }
  54. })
  55. ]
  56. }
  57. outputOptions = {
  58. file: is.development ? path.join(statics, '/plugins/bundle.js') : path.join(dest, '/bundle.js'),
  59. format: 'cjs',
  60. name: 'components'
  61. }
  62. watchOptions = Object.assign(
  63. {
  64. output: [outputOptions],
  65. watch: {
  66. }
  67. },
  68. inputOptions
  69. )
  70. }
  71. export const rollupBuild = () => {
  72. return rollup
  73. .rollup(inputOptions)
  74. .then(bundle => {
  75. return bundle.write(outputOptions)
  76. })
  77. .then(() => {
  78. console.log('Komponenten erfolgreich kompiliert')
  79. })
  80. .catch(err => {
  81. throw err
  82. })
  83. }
  84. export const rollupWatch = () => {
  85. const watcher = rollup.watch(watchOptions).on('event', event => {
  86. // event.code: START, BUNDLE_START, BUNDLE_END, END, ERROR, FATAL
  87. console.log(new Date().toLocaleDateString('de-DE', { hour: 'numeric', minute: 'numeric', second: 'numeric' }))
  88. console.log(event)
  89. })
  90. return watcher
  91. }