2021-08-28 21:51:54 +00:00
|
|
|
import path from 'path'
|
|
|
|
import ts from 'rollup-plugin-typescript2'
|
2021-10-30 23:29:33 +00:00
|
|
|
import replace from '@rollup/plugin-replace'
|
|
|
|
import resolve from '@rollup/plugin-node-resolve'
|
|
|
|
import commonjs from '@rollup/plugin-commonjs'
|
|
|
|
import pascalcase from 'pascalcase'
|
2021-08-28 21:51:54 +00:00
|
|
|
|
|
|
|
const pkg = require('./package.json')
|
2021-10-30 23:29:33 +00:00
|
|
|
const name = 'vue-web3'
|
2021-08-28 21:51:54 +00:00
|
|
|
|
|
|
|
function getAuthors(pkg) {
|
|
|
|
const { contributors, author } = pkg
|
|
|
|
|
|
|
|
const authors = new Set()
|
|
|
|
if (contributors && contributors)
|
|
|
|
contributors.forEach((contributor) => {
|
|
|
|
authors.add(contributor.name)
|
|
|
|
})
|
|
|
|
if (author) authors.add(author.name)
|
|
|
|
|
|
|
|
return Array.from(authors).join(', ')
|
|
|
|
}
|
|
|
|
|
|
|
|
const banner = `/*!
|
|
|
|
* ${pkg.name} v${pkg.version}
|
|
|
|
* (c) ${new Date().getFullYear()} ${getAuthors(pkg)}
|
|
|
|
* @license MIT
|
|
|
|
*/`
|
|
|
|
|
|
|
|
// ensure TS checks only once for each build
|
|
|
|
let hasTSChecked = false
|
|
|
|
|
|
|
|
const outputConfigs = {
|
|
|
|
// each file name has the format: `dist/${name}.${format}.js`
|
|
|
|
// format being a key of this object
|
2021-10-30 23:29:33 +00:00
|
|
|
mjs: {
|
|
|
|
file: pkg.module,
|
2021-08-28 21:51:54 +00:00
|
|
|
format: `es`,
|
|
|
|
},
|
|
|
|
cjs: {
|
2021-10-30 23:29:33 +00:00
|
|
|
file: pkg.module.replace('mjs', 'cjs'),
|
|
|
|
format: `cjs`,
|
2021-08-28 21:51:54 +00:00
|
|
|
},
|
|
|
|
global: {
|
2021-10-30 23:29:33 +00:00
|
|
|
file: pkg.unpkg,
|
|
|
|
format: `iife`,
|
|
|
|
},
|
|
|
|
browser: {
|
|
|
|
file: 'dist/vue-web3.esm-browser.js',
|
|
|
|
format: `es`,
|
2021-08-28 21:51:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-30 23:29:33 +00:00
|
|
|
const packageBuilds = Object.keys(outputConfigs)
|
|
|
|
const packageConfigs = packageBuilds.map((format) =>
|
|
|
|
createConfig(format, outputConfigs[format]),
|
2021-08-28 21:51:54 +00:00
|
|
|
)
|
|
|
|
|
2021-10-30 23:29:33 +00:00
|
|
|
// only add the production ready if we are bundling the options
|
|
|
|
packageBuilds.forEach((buildName) => {
|
|
|
|
if (buildName === 'cjs') {
|
|
|
|
packageConfigs.push(createProductionConfig(buildName))
|
|
|
|
} else if (buildName === 'global') {
|
|
|
|
packageConfigs.push(createMinifiedConfig(buildName))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-08-28 21:51:54 +00:00
|
|
|
export default packageConfigs
|
|
|
|
|
2021-10-30 23:29:33 +00:00
|
|
|
function createConfig(buildName, output, plugins = []) {
|
2021-08-28 21:51:54 +00:00
|
|
|
if (!output) {
|
2021-10-30 23:29:33 +00:00
|
|
|
console.log(require('chalk').yellow(`invalid format: "${buildName}"`))
|
2021-08-28 21:51:54 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
output.sourcemap = !!process.env.SOURCE_MAP
|
|
|
|
output.banner = banner
|
|
|
|
output.externalLiveBindings = false
|
2021-10-30 23:29:33 +00:00
|
|
|
output.globals = {
|
|
|
|
'vue-demi': 'VueDemi',
|
|
|
|
vue: 'Vue',
|
|
|
|
'@vue/composition-api': 'vueCompositionApi',
|
|
|
|
}
|
2021-08-28 21:51:54 +00:00
|
|
|
|
2021-10-30 23:29:33 +00:00
|
|
|
const isProductionBuild = /\.prod\.[cmj]s$/.test(output.file)
|
|
|
|
const isGlobalBuild = buildName === 'global'
|
|
|
|
const isRawESMBuild = buildName === 'browser'
|
|
|
|
const isNodeBuild = buildName === 'cjs'
|
|
|
|
const isBundlerESMBuild = buildName === 'browser' || buildName === 'mjs'
|
2021-08-28 21:51:54 +00:00
|
|
|
|
|
|
|
if (isGlobalBuild) output.name = pascalcase(pkg.name)
|
|
|
|
|
|
|
|
const shouldEmitDeclarations = !hasTSChecked
|
|
|
|
|
|
|
|
const tsPlugin = ts({
|
|
|
|
check: !hasTSChecked,
|
2021-10-30 23:29:33 +00:00
|
|
|
tsconfig: path.resolve(__dirname, './tsconfig.json'),
|
|
|
|
cacheRoot: path.resolve(__dirname, './node_modules/.rts2_cache'),
|
2021-08-28 21:51:54 +00:00
|
|
|
tsconfigOverride: {
|
|
|
|
compilerOptions: {
|
|
|
|
sourceMap: output.sourcemap,
|
|
|
|
declaration: shouldEmitDeclarations,
|
|
|
|
declarationMap: shouldEmitDeclarations,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
// we only need to check TS and generate declarations once for each build.
|
|
|
|
// it also seems to run into weird issues when checking multiple times
|
|
|
|
// during a single build.
|
|
|
|
hasTSChecked = true
|
|
|
|
|
2021-10-30 23:29:33 +00:00
|
|
|
const external = ['vue-demi', 'vue', '@vue/composition-api']
|
2021-08-28 21:51:54 +00:00
|
|
|
|
|
|
|
const nodePlugins = [resolve(), commonjs()]
|
|
|
|
|
|
|
|
return {
|
|
|
|
input: `src/index.ts`,
|
|
|
|
// Global and Browser ESM builds inlines everything so that they can be
|
|
|
|
// used alone.
|
|
|
|
external,
|
|
|
|
plugins: [
|
|
|
|
tsPlugin,
|
|
|
|
createReplacePlugin(
|
|
|
|
isProductionBuild,
|
|
|
|
isBundlerESMBuild,
|
2021-10-30 23:29:33 +00:00
|
|
|
// isBrowserBuild?
|
2021-08-28 21:51:54 +00:00
|
|
|
isGlobalBuild || isRawESMBuild || isBundlerESMBuild,
|
|
|
|
isGlobalBuild,
|
|
|
|
isNodeBuild,
|
|
|
|
),
|
|
|
|
...nodePlugins,
|
|
|
|
...plugins,
|
|
|
|
],
|
|
|
|
output,
|
2021-10-30 23:29:33 +00:00
|
|
|
// onwarn: (msg, warn) => {
|
|
|
|
// if (!/Circular/.test(msg)) {
|
|
|
|
// warn(msg)
|
|
|
|
// }
|
|
|
|
// },
|
2021-08-28 21:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createReplacePlugin(
|
|
|
|
isProduction,
|
|
|
|
isBundlerESMBuild,
|
|
|
|
isBrowserBuild,
|
|
|
|
isGlobalBuild,
|
|
|
|
isNodeBuild,
|
|
|
|
) {
|
|
|
|
const replacements = {
|
|
|
|
__COMMIT__: `"${process.env.COMMIT}"`,
|
|
|
|
__VERSION__: `"${pkg.version}"`,
|
2021-10-30 23:29:33 +00:00
|
|
|
__DEV__:
|
|
|
|
isBundlerESMBuild || (isNodeBuild && !isProduction)
|
|
|
|
? // preserve to be handled by bundlers
|
|
|
|
`(process.env.NODE_ENV !== 'production')`
|
|
|
|
: // hard coded dev/prod builds
|
|
|
|
JSON.stringify(!isProduction),
|
2021-08-28 21:51:54 +00:00
|
|
|
// this is only used during tests
|
2021-10-30 23:29:33 +00:00
|
|
|
__TEST__:
|
|
|
|
isBundlerESMBuild || isNodeBuild
|
|
|
|
? `(process.env.NODE_ENV === 'test')`
|
|
|
|
: 'false',
|
2021-08-28 21:51:54 +00:00
|
|
|
// If the build is expected to run directly in the browser (global / esm builds)
|
2021-10-30 23:29:33 +00:00
|
|
|
__BROWSER__: JSON.stringify(isBrowserBuild),
|
2021-08-28 21:51:54 +00:00
|
|
|
// is targeting bundlers?
|
2021-10-30 23:29:33 +00:00
|
|
|
__BUNDLER__: JSON.stringify(isBundlerESMBuild),
|
|
|
|
__GLOBAL__: JSON.stringify(isGlobalBuild),
|
2021-08-28 21:51:54 +00:00
|
|
|
// is targeting Node (SSR)?
|
2021-10-30 23:29:33 +00:00
|
|
|
__NODE_JS__: JSON.stringify(isNodeBuild),
|
2021-08-28 21:51:54 +00:00
|
|
|
}
|
|
|
|
// allow inline overrides like
|
|
|
|
//__RUNTIME_COMPILE__=true yarn build
|
|
|
|
Object.keys(replacements).forEach((key) => {
|
|
|
|
if (key in process.env) {
|
|
|
|
replacements[key] = process.env[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return replace({
|
|
|
|
preventAssignment: true,
|
2021-10-30 23:29:33 +00:00
|
|
|
values: replacements,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createProductionConfig(format) {
|
|
|
|
const extension = format === 'cjs' ? 'cjs' : 'js'
|
|
|
|
const descriptor = format === 'cjs' ? '' : `.${format}`
|
|
|
|
return createConfig(format, {
|
|
|
|
file: `dist/${name}${descriptor}.prod.${extension}`,
|
|
|
|
format: outputConfigs[format].format,
|
2021-08-28 21:51:54 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMinifiedConfig(format) {
|
|
|
|
const { terser } = require('rollup-plugin-terser')
|
|
|
|
return createConfig(
|
|
|
|
format,
|
|
|
|
{
|
2021-10-30 23:29:33 +00:00
|
|
|
file: `dist/${name}.${format === 'global' ? 'iife' : format}.prod.js`,
|
2021-08-28 21:51:54 +00:00
|
|
|
format: outputConfigs[format].format,
|
|
|
|
},
|
|
|
|
[
|
|
|
|
terser({
|
|
|
|
module: /^esm/.test(format),
|
|
|
|
compress: {
|
|
|
|
ecma: 2015,
|
|
|
|
pure_getters: true,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
}
|