|
| 1 | +const rollup = require('rollup').rollup |
| 2 | +const buble = require('rollup-plugin-buble') |
| 3 | +const resolve = require('rollup-plugin-node-resolve') |
| 4 | +const uglify = require('uglify-js') |
| 5 | +const mkdirp = require('mkdirp') |
| 6 | +const { logError, write } = require('./utils') |
| 7 | + |
| 8 | +const { pwd } = require('shelljs') |
| 9 | +const { join } = require('path') |
| 10 | + |
| 11 | +const cwd = pwd().toString() |
| 12 | +const packageData = require(join(cwd, 'package.json')) |
| 13 | +const { version, author, name } = packageData |
| 14 | +// remove the email at the end |
| 15 | +const authorName = author.name |
| 16 | +const moduleName = 'Vuefire' |
| 17 | +console.log({ name, author, moduleName }) |
| 18 | + |
| 19 | +// Make sure dist dir exists |
| 20 | +const distFolder = join(cwd, 'dist') |
| 21 | +mkdirp(distFolder) |
| 22 | + |
| 23 | +const bundleOptions = { |
| 24 | + exports: 'auto', |
| 25 | + format: 'umd', |
| 26 | +} |
| 27 | + |
| 28 | +const plugins = [ |
| 29 | + // replace({ |
| 30 | + // __VERSION__: version, |
| 31 | + // // 'process.env.NODE_ENV': '"development"', |
| 32 | + // }), |
| 33 | + resolve({ |
| 34 | + extensions: ['.js', '.vue', '.jsx', '.json'], |
| 35 | + }), |
| 36 | + buble({ |
| 37 | + objectAssign: 'Object.assign', |
| 38 | + transforms: { |
| 39 | + dangerousForOf: true, |
| 40 | + }, |
| 41 | + }), |
| 42 | +] |
| 43 | + |
| 44 | +function createBundle({ filename, format, moduleName, banner }) { |
| 45 | + rollup({ |
| 46 | + input: join(cwd, 'src/index.js'), |
| 47 | + plugins, |
| 48 | + }) |
| 49 | + .then(bundle => { |
| 50 | + const options = Object.assign({ banner, name: moduleName }, bundleOptions) |
| 51 | + if (format) options.format = format |
| 52 | + return bundle.generate(options) |
| 53 | + }) |
| 54 | + .then(({ code }) => { |
| 55 | + if (/min$/.test(filename)) { |
| 56 | + const minified = uglify.minify(code, { |
| 57 | + output: { |
| 58 | + preamble: banner, |
| 59 | + ascii_only: true, |
| 60 | + }, |
| 61 | + }).code |
| 62 | + return write(`${distFolder}/${filename}.js`, minified) |
| 63 | + } else { |
| 64 | + return write(`${distFolder}/${filename}.js`, code) |
| 65 | + } |
| 66 | + }) |
| 67 | + .catch(logError) |
| 68 | +} |
| 69 | + |
| 70 | +module.exports = function run(moduleName) { |
| 71 | + const banner = |
| 72 | + '/*!\n' + |
| 73 | + ` * ${name} v${version}\n` + |
| 74 | + ` * (c) ${new Date().getFullYear()} ${authorName}\n` + |
| 75 | + ' * Released under the MIT License.\n' + |
| 76 | + ' */' |
| 77 | + |
| 78 | + // Browser bundle (can be used with script) |
| 79 | + createBundle({ |
| 80 | + banner, |
| 81 | + filename: name, |
| 82 | + moduleName, |
| 83 | + }) |
| 84 | + |
| 85 | + // Commonjs bundle (preserves process.env.NODE_ENV) so |
| 86 | + // the user can replace it in dev and prod mode |
| 87 | + createBundle({ |
| 88 | + banner, |
| 89 | + filename: `${name}.esm`, |
| 90 | + format: 'es', |
| 91 | + moduleName, |
| 92 | + }) |
| 93 | + |
| 94 | + createBundle({ |
| 95 | + banner, |
| 96 | + filename: `${name}.common`, |
| 97 | + format: 'cjs', |
| 98 | + moduleName, |
| 99 | + }) |
| 100 | + |
| 101 | + // Minified version for browser |
| 102 | + createBundle({ |
| 103 | + banner, |
| 104 | + filename: `${name}.min`, |
| 105 | + moduleName, |
| 106 | + }) |
| 107 | +} |
0 commit comments