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