|
| 1 | +// rollup.config.js |
| 2 | +import fs from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import vue from 'rollup-plugin-vue'; |
| 5 | +import alias from '@rollup/plugin-alias'; |
| 6 | +import commonjs from '@rollup/plugin-commonjs'; |
| 7 | +import replace from '@rollup/plugin-replace'; |
| 8 | +import babel from 'rollup-plugin-babel'; |
| 9 | +import { terser } from 'rollup-plugin-terser'; |
| 10 | +import minimist from 'minimist'; |
| 11 | + |
| 12 | +// Get browserslist config and remove ie from es build targets |
| 13 | +const esbrowserslist = fs.readFileSync('./.browserslistrc') |
| 14 | + .toString() |
| 15 | + .split('\n') |
| 16 | + .filter((entry) => entry && entry.substring(0, 2) !== 'ie'); |
| 17 | + |
| 18 | +const argv = minimist(process.argv.slice(2)); |
| 19 | + |
| 20 | +const projectRoot = path.resolve(__dirname, '..'); |
| 21 | + |
| 22 | +const baseConfig = { |
| 23 | + input: 'src/entry.js', |
| 24 | + plugins: { |
| 25 | + preVue: [ |
| 26 | + alias({ |
| 27 | + resolve: ['.js', '.jsx', '.ts', '.tsx', '.vue'], |
| 28 | + entries: { |
| 29 | + '@': path.resolve(projectRoot, 'src'), |
| 30 | + }, |
| 31 | + }), |
| 32 | + ], |
| 33 | + replace: { |
| 34 | + 'process.env.NODE_ENV': JSON.stringify('production'), |
| 35 | + 'process.env.ES_BUILD': JSON.stringify('false'), |
| 36 | + }, |
| 37 | + vue: { |
| 38 | + css: true, |
| 39 | + template: { |
| 40 | + isProduction: true, |
| 41 | + }, |
| 42 | + }, |
| 43 | + babel: { |
| 44 | + exclude: 'node_modules/**', |
| 45 | + extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue'], |
| 46 | + }, |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +// ESM/UMD/IIFE shared settings: externals |
| 51 | +// Refer to https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency |
| 52 | +const external = [ |
| 53 | + 'axios', |
| 54 | + 'qs', |
| 55 | + 'vue', |
| 56 | + 'vuedraggable', |
| 57 | + 'vuex' |
| 58 | +]; |
| 59 | + |
| 60 | +// UMD/IIFE shared settings: output.globals |
| 61 | +// Refer to https://rollupjs.org/guide/en#output-globals for details |
| 62 | +const globals = { |
| 63 | + axios: 'axios', |
| 64 | + qs: 'qs', |
| 65 | + vue: 'Vue', |
| 66 | + vuedraggable: 'VueDraggable', |
| 67 | + vuex: 'vuex' |
| 68 | +}; |
| 69 | + |
| 70 | +// Customize configs for individual targets |
| 71 | +const buildFormats = []; |
| 72 | +if (!argv.format || argv.format === 'es') { |
| 73 | + const esConfig = { |
| 74 | + ...baseConfig, |
| 75 | + external, |
| 76 | + output: { |
| 77 | + file: 'dist/vue-table.esm.js', |
| 78 | + format: 'esm', |
| 79 | + exports: 'named', |
| 80 | + }, |
| 81 | + plugins: [ |
| 82 | + replace({ |
| 83 | + ...baseConfig.plugins.replace, |
| 84 | + 'process.env.ES_BUILD': JSON.stringify('true'), |
| 85 | + }), |
| 86 | + ...baseConfig.plugins.preVue, |
| 87 | + vue(baseConfig.plugins.vue), |
| 88 | + babel({ |
| 89 | + ...baseConfig.plugins.babel, |
| 90 | + presets: [ |
| 91 | + [ |
| 92 | + '@babel/preset-env', |
| 93 | + { |
| 94 | + targets: esbrowserslist, |
| 95 | + }, |
| 96 | + ], |
| 97 | + ], |
| 98 | + }), |
| 99 | + commonjs(), |
| 100 | + ], |
| 101 | + }; |
| 102 | + buildFormats.push(esConfig); |
| 103 | +} |
| 104 | + |
| 105 | +if (!argv.format || argv.format === 'cjs') { |
| 106 | + const umdConfig = { |
| 107 | + ...baseConfig, |
| 108 | + external, |
| 109 | + output: { |
| 110 | + compact: true, |
| 111 | + file: 'dist/vue-table.ssr.js', |
| 112 | + format: 'cjs', |
| 113 | + name: 'VueTable', |
| 114 | + exports: 'named', |
| 115 | + globals, |
| 116 | + }, |
| 117 | + plugins: [ |
| 118 | + replace(baseConfig.plugins.replace), |
| 119 | + ...baseConfig.plugins.preVue, |
| 120 | + vue({ |
| 121 | + ...baseConfig.plugins.vue, |
| 122 | + template: { |
| 123 | + ...baseConfig.plugins.vue.template, |
| 124 | + optimizeSSR: true, |
| 125 | + }, |
| 126 | + }), |
| 127 | + babel(baseConfig.plugins.babel), |
| 128 | + commonjs(), |
| 129 | + ], |
| 130 | + }; |
| 131 | + buildFormats.push(umdConfig); |
| 132 | +} |
| 133 | + |
| 134 | +if (!argv.format || argv.format === 'iife') { |
| 135 | + const unpkgConfig = { |
| 136 | + ...baseConfig, |
| 137 | + external, |
| 138 | + output: { |
| 139 | + compact: true, |
| 140 | + file: 'dist/vue-table.min.js', |
| 141 | + format: 'iife', |
| 142 | + name: 'VueTable', |
| 143 | + exports: 'named', |
| 144 | + globals, |
| 145 | + }, |
| 146 | + plugins: [ |
| 147 | + replace(baseConfig.plugins.replace), |
| 148 | + ...baseConfig.plugins.preVue, |
| 149 | + vue(baseConfig.plugins.vue), |
| 150 | + babel(baseConfig.plugins.babel), |
| 151 | + commonjs(), |
| 152 | + terser({ |
| 153 | + output: { |
| 154 | + ecma: 5, |
| 155 | + }, |
| 156 | + }), |
| 157 | + ], |
| 158 | + }; |
| 159 | + buildFormats.push(unpkgConfig); |
| 160 | +} |
| 161 | + |
| 162 | +// Export config |
| 163 | +export default buildFormats; |
0 commit comments