|
| 1 | +import { camelCase } from "lodash"; |
| 2 | +import path from "path"; |
| 3 | +import babel from "rollup-plugin-babel"; |
| 4 | +import commonjs from "rollup-plugin-commonjs"; |
| 5 | +import filesize from "rollup-plugin-filesize"; |
| 6 | +import json from "rollup-plugin-json"; |
| 7 | +import license from "rollup-plugin-license"; |
| 8 | +import postcss from 'rollup-plugin-postcss' |
| 9 | +import resolve from "rollup-plugin-node-resolve"; |
| 10 | +import replace from "rollup-plugin-replace"; |
| 11 | +import magicImporter from 'node-sass-magic-importer'; |
| 12 | + |
| 13 | +import { uglify } from "rollup-plugin-uglify"; |
| 14 | +import vue from "rollup-plugin-vue"; |
| 15 | +import { minify } from "uglify-es"; |
| 16 | + |
| 17 | +import pack from "./package.json"; |
| 18 | + |
| 19 | +const projectName = "coreui-vue"; |
| 20 | + |
| 21 | +// compute globals from dependencies |
| 22 | +const globals = pack.dependencies && Object.assign({}, ...Object.keys(pack.dependencies).map((key) => ({ |
| 23 | + [key]: camelCase(key) |
| 24 | +}))); |
| 25 | + |
| 26 | +const builds = { |
| 27 | + // (CommonJS). Used by bundlers e.g. Webpack & Browserify |
| 28 | + cjs: { |
| 29 | + entry: "src/index.js", |
| 30 | + dest: `dist/${projectName}.common.js`, |
| 31 | + format: "cjs" |
| 32 | + }, |
| 33 | + // (ES Modules). Used by bundlers that support ES Modules, |
| 34 | + // e.g. Rollup & Webpack 2 |
| 35 | + esm: { |
| 36 | + entry: "src/index.js", |
| 37 | + dest: `dist/${projectName}.esm.js`, |
| 38 | + format: "esm" |
| 39 | + }, |
| 40 | + // build (Browser) |
| 41 | + "umd-dev": { |
| 42 | + entry: "src/index.umd.js", |
| 43 | + dest: `dist/${projectName}.js`, |
| 44 | + format: "umd", |
| 45 | + env: "development" |
| 46 | + }, |
| 47 | + // production build (Browser) |
| 48 | + "umd-prod": { |
| 49 | + entry: "src/index.umd.js", |
| 50 | + dest: `dist/${projectName}.min.js`, |
| 51 | + format: "umd", |
| 52 | + env: "production" |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +function genConfig(name) { |
| 57 | + const opts = builds[name]; |
| 58 | + const config = { |
| 59 | + input: opts.entry, |
| 60 | + // external: (id) => pack.dependencies && pack.dependencies[id], // exclude dependencies from build |
| 61 | + // external: ['@coreui/icons', '@coreui/icons/vue'], |
| 62 | + // preserveModules: true, |
| 63 | + treeshake: { |
| 64 | + // moduleSideEffects: false, |
| 65 | + pureExternalModules: true, |
| 66 | + propertyReadSideEffects: false |
| 67 | + }, |
| 68 | + plugins: [ |
| 69 | + resolve({ |
| 70 | + // module: true, |
| 71 | + // browser: true, |
| 72 | + // jsnext: true, |
| 73 | + // preferBuiltins: false, |
| 74 | + extensions: [".js", ".json", ".vue"] |
| 75 | + }), |
| 76 | + commonjs(), |
| 77 | + postcss(), |
| 78 | + vue({ |
| 79 | + compileTemplate: true, |
| 80 | + css: false, |
| 81 | + style: { |
| 82 | + preprocessOptions: { |
| 83 | + scss: { |
| 84 | + importer: magicImporter(), |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + }), |
| 89 | + json(), |
| 90 | + babel({ |
| 91 | + exclude: "node_modules/**", |
| 92 | + runtimeHelpers: true, |
| 93 | + presets: [ |
| 94 | + ["@vue/app", { modules: false }] |
| 95 | + ] |
| 96 | + }), |
| 97 | + filesize() |
| 98 | + ].concat(opts.plugins || []), |
| 99 | + output: { |
| 100 | + exports: "named", |
| 101 | + file: opts.dest, |
| 102 | + // dir: 'dist/es', |
| 103 | + format: opts.format, |
| 104 | + // define globals in window from external dependencies |
| 105 | + globals, |
| 106 | + name: opts.moduleName || projectName |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + if (opts.env) { |
| 111 | + config.plugins.push( |
| 112 | + replace({ |
| 113 | + "process.env.NODE_ENV": JSON.stringify(opts.env) |
| 114 | + }) |
| 115 | + ); |
| 116 | + |
| 117 | + // minify on production targets |
| 118 | + // if (opts.env === "production") { |
| 119 | + // config.plugins.push(uglify({}, minify)); |
| 120 | + // } |
| 121 | + } |
| 122 | + |
| 123 | + // output a license to builds |
| 124 | + config.plugins.push( |
| 125 | + license({ |
| 126 | + sourceMap: true, |
| 127 | + banner: { |
| 128 | + file: path.resolve("LICENSE.md") |
| 129 | + } |
| 130 | + }) |
| 131 | + ); |
| 132 | + |
| 133 | + Object.defineProperty(config, "_name", { |
| 134 | + enumerable: false, |
| 135 | + value: name |
| 136 | + }); |
| 137 | + |
| 138 | + return config; |
| 139 | +} |
| 140 | + |
| 141 | +const target = process.env.TARGET || "umd-prod"; |
| 142 | +module.exports = genConfig(target); |
0 commit comments