rollup.config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import autoprefixer from 'autoprefixer';
  2. import babel from '@rollup/plugin-babel';
  3. import browsersync from 'rollup-plugin-browsersync';
  4. import commonjs from '@rollup/plugin-commonjs';
  5. import copy from 'rollup-plugin-copy';
  6. import cssnano from 'cssnano';
  7. import del from 'rollup-plugin-delete';
  8. import { eslint } from 'rollup-plugin-eslint';
  9. import json from 'rollup-plugin-json';
  10. import path from 'path';
  11. import postcss from 'rollup-plugin-postcss';
  12. import resolve from '@rollup/plugin-node-resolve';
  13. import sourcemaps from 'rollup-plugin-sourcemaps';
  14. import stylelint from 'stylelint';
  15. import { terser } from 'rollup-plugin-terser';
  16. import config from './build.json';
  17. const printWarning = (message) => {
  18. if (message.code === 'CIRCULAR_DEPENDENCY') {
  19. return;
  20. }
  21. console.error(message); // eslint-disable-line
  22. };
  23. const generateConfiguration = (target, option) => {
  24. target = target === undefined ? 'development' : target;
  25. // Common postcs configruation for development and production build.
  26. let postcssconf = {
  27. extract: true,
  28. plugins: [
  29. autoprefixer(),
  30. stylelint(),
  31. (target === 'production' && cssnano())
  32. ],
  33. };
  34. // Browsersync configruation
  35. let browsersyncconf = {
  36. server: {
  37. baseDir: config.path.output,
  38. index: config.html.index.name, // first html file in array is served as index
  39. },
  40. watch: true,
  41. };
  42. // Default rollup plugins
  43. const plugins = {
  44. development: [sourcemaps()],
  45. production: [terser()],
  46. prepare: [
  47. (option !== 'serve' && del({ targets: [path.join(config.path.output, '/*'), path.join(config.path.build, '/*')] })),
  48. resolve({browser: true}),
  49. commonjs(),
  50. json(),
  51. eslint({
  52. extends: ['plugin:json/recommended', 'eslint:recommended'],
  53. exclude: [path.join(config.path.input, config.path.css, '/**'), 'html/**']
  54. }),
  55. babel({ babelHelpers: 'bundled' }),
  56. ],
  57. postProcess: [
  58. postcss(postcssconf),
  59. copy({
  60. targets: [
  61. { src: path.join(config.path.build, '/*.css'), dest: path.join(config.path.output, config.path.css) },
  62. { src: path.join(config.path.build, '/*.js*'), dest: path.join(config.path.output, config.path.js) },
  63. { src: path.join(config.path.assets, config.path.js), dest: config.path.output },
  64. { src: path.join(config.path.assets, config.path.css), dest: config.path.output },
  65. { src: path.join(config.path.assets, config.path.img), dest: config.path.output },
  66. { src: path.join(config.path.assets, config.path.fonts), dest: config.path.output },
  67. ],
  68. hook: 'writeBundle',
  69. verbose: true
  70. }),
  71. (option === 'serve' && browsersync(browsersyncconf))
  72. ]
  73. };
  74. let rconf = {
  75. onwarn: printWarning,
  76. input: path.join(config.path.input, config.path.js, `${config.main}.js`),
  77. plugins: plugins.prepare.concat(plugins[target], plugins.postProcess), // Concatenate plugin arrays
  78. output: {
  79. file: path.join(
  80. config.path.build,
  81. `${config.main}${config[target].infix}.js`
  82. ),
  83. format: 'iife',
  84. sourcemap: config[target].sourcemap
  85. }
  86. };
  87. return rconf;
  88. };
  89. const rollup_config = generateConfiguration(process.env.NODE_ENV, process.env.NODE_OPT);
  90. export default rollup_config;