|
| 1 | +// @ts-nocheck - Disable TypeScript checks here |
| 2 | +import path from 'path' |
| 3 | +import ts from 'rollup-plugin-typescript2' |
| 4 | +import replace from '@rollup/plugin-replace' |
| 5 | +import resolve from '@rollup/plugin-node-resolve' |
| 6 | +import commonjs from '@rollup/plugin-commonjs' |
| 7 | +import pascalcase from 'pascalcase' |
| 8 | + |
| 9 | +const pkg = require('./package.json') |
| 10 | +const name = pkg.name |
| 11 | + |
| 12 | +const getAuthors = (pkg) => { |
| 13 | + const { contributors, author } = pkg |
| 14 | + |
| 15 | + const authors = new Set() |
| 16 | + if (contributors && contributors) |
| 17 | + contributors.forEach((contributor) => { |
| 18 | + authors.add(contributor.name) |
| 19 | + }) |
| 20 | + if (author) authors.add(author.name) |
| 21 | + |
| 22 | + return Array.from(authors).join(', ') |
| 23 | +} |
| 24 | + |
| 25 | +const banner = `/*! |
| 26 | + * ${pkg.name} v${pkg.version} |
| 27 | + * (c) ${new Date().getFullYear()} ${getAuthors(pkg)} |
| 28 | + * @license MIT |
| 29 | + */` |
| 30 | + |
| 31 | +// ensure TS checks only once for each build |
| 32 | +let hasTSChecked = false |
| 33 | + |
| 34 | +const outputConfigs = { |
| 35 | + 'esm-bundler': { |
| 36 | + file: pkg.module, |
| 37 | + format: `es`, |
| 38 | + }, |
| 39 | + cjs: { |
| 40 | + file: pkg.main, |
| 41 | + format: `cjs`, |
| 42 | + }, |
| 43 | + global: { |
| 44 | + file: pkg.unpkg, |
| 45 | + format: `iife`, |
| 46 | + }, |
| 47 | + esm: { |
| 48 | + file: pkg.module.replace('-bundler.js', '-browser.js'), |
| 49 | + format: `es`, |
| 50 | + }, |
| 51 | +} |
| 52 | + |
| 53 | +const createConfig = (format, output, plugins = []) => { |
| 54 | + if (!output) { |
| 55 | + console.log(require('chalk').yellow(`invalid format: "${format}"`)) |
| 56 | + process.exit(1) |
| 57 | + } |
| 58 | + |
| 59 | + output.sourcemap = !!process.env.SOURCE_MAP |
| 60 | + output.banner = banner |
| 61 | + output.externalLiveBindings = false |
| 62 | + output.globals = { vue: 'Vue' } |
| 63 | + output.exports = 'auto' |
| 64 | + |
| 65 | + const isProductionBuild = /\.prod\.js$/.test(output.file) |
| 66 | + const isGlobalBuild = format === 'global' |
| 67 | + const isRawESMBuild = format === 'esm' |
| 68 | + const isNodeBuild = format === 'cjs' |
| 69 | + const isBundlerESMBuild = /esm-bundler/.test(format) |
| 70 | + |
| 71 | + if (isGlobalBuild) output.name = pascalcase(pkg.name) |
| 72 | + |
| 73 | + const shouldEmitDeclarations = !hasTSChecked |
| 74 | + |
| 75 | + const tsPlugin = ts({ |
| 76 | + check: !hasTSChecked, |
| 77 | + tsconfig: path.resolve(__dirname, 'tsconfig.json'), |
| 78 | + cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'), |
| 79 | + tsconfigOverride: { |
| 80 | + compilerOptions: { |
| 81 | + sourceMap: output.sourcemap, |
| 82 | + declaration: shouldEmitDeclarations, |
| 83 | + declarationMap: shouldEmitDeclarations, |
| 84 | + }, |
| 85 | + exclude: ['tests'], |
| 86 | + }, |
| 87 | + }) |
| 88 | + |
| 89 | + // we only need to check TS and generate declarations once for each build. |
| 90 | + // it also seems to run into weird issues when checking multiple times |
| 91 | + // during a single build. |
| 92 | + hasTSChecked = true |
| 93 | + |
| 94 | + const external = ['vue'] |
| 95 | + |
| 96 | + const nodePlugins = [resolve(), commonjs()] |
| 97 | + |
| 98 | + return { |
| 99 | + input: `src/index.ts`, |
| 100 | + // Global and Browser ESM builds inlines everything so that they can be |
| 101 | + // used alone. |
| 102 | + external, |
| 103 | + plugins: [ |
| 104 | + tsPlugin, |
| 105 | + createReplacePlugin( |
| 106 | + isProductionBuild, |
| 107 | + isBundlerESMBuild, |
| 108 | + isGlobalBuild || isRawESMBuild || isBundlerESMBuild, |
| 109 | + isGlobalBuild, |
| 110 | + isNodeBuild, |
| 111 | + ), |
| 112 | + ...nodePlugins, |
| 113 | + ...plugins, |
| 114 | + ], |
| 115 | + output, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +const createReplacePlugin = ( |
| 120 | + isProduction, |
| 121 | + isBundlerESMBuild, |
| 122 | + isBrowserBuild, |
| 123 | + isGlobalBuild, |
| 124 | + isNodeBuild, |
| 125 | +) => { |
| 126 | + const replacements = { |
| 127 | + __COMMIT__: `"${process.env.COMMIT}"`, |
| 128 | + __VERSION__: `"${pkg.version}"`, |
| 129 | + __DEV__: isBundlerESMBuild |
| 130 | + ? // preserve to be handled by bundlers |
| 131 | + `(process.env.NODE_ENV !== 'production')` |
| 132 | + : // hard coded dev/prod builds |
| 133 | + !isProduction, |
| 134 | + // this is only used during tests |
| 135 | + __TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : false, |
| 136 | + // If the build is expected to run directly in the browser (global / esm builds) |
| 137 | + __BROWSER__: isBrowserBuild, |
| 138 | + // is targeting bundlers? |
| 139 | + __BUNDLER__: isBundlerESMBuild, |
| 140 | + __GLOBAL__: isGlobalBuild, |
| 141 | + // is targeting Node (SSR)? |
| 142 | + __NODE_JS__: isNodeBuild, |
| 143 | + } |
| 144 | + |
| 145 | + // allow inline overrides like |
| 146 | + //__RUNTIME_COMPILE__=true yarn build |
| 147 | + Object.keys(replacements).forEach((key) => { |
| 148 | + if (key in process.env) { |
| 149 | + replacements[key] = process.env[key] |
| 150 | + } |
| 151 | + }) |
| 152 | + |
| 153 | + return replace(replacements) |
| 154 | +} |
| 155 | + |
| 156 | +const createMinifiedConfig = (format) => { |
| 157 | + const { terser } = require('rollup-plugin-terser') |
| 158 | + |
| 159 | + return createConfig( |
| 160 | + format, |
| 161 | + { |
| 162 | + file: `dist/index.${format}.prod.js`, |
| 163 | + format: outputConfigs[format].format, |
| 164 | + }, |
| 165 | + [ |
| 166 | + terser({ |
| 167 | + module: /^esm/.test(format), |
| 168 | + compress: { |
| 169 | + ecma: 2015, |
| 170 | + pure_getters: true, |
| 171 | + }, |
| 172 | + }), |
| 173 | + ], |
| 174 | + ) |
| 175 | +} |
| 176 | + |
| 177 | +const allFormats = Object.keys(outputConfigs) |
| 178 | +const packageFormats = allFormats |
| 179 | +const packageConfigs = packageFormats.map((format) => |
| 180 | + createConfig(format, outputConfigs[format]), |
| 181 | +) |
| 182 | + |
| 183 | +// only add the production ready if we are bundling the options |
| 184 | +packageFormats.forEach((format) => { |
| 185 | + if (format === 'global') { |
| 186 | + packageConfigs.push(createMinifiedConfig(format)) |
| 187 | + } |
| 188 | +}) |
| 189 | + |
| 190 | +export default packageConfigs |
0 commit comments