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