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