-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: files in dist folder are renamed to match official libs in the Vue ecosystem. Unless you were importing from `pinia/dist`, this won't affect you.
- Loading branch information
Showing
7 changed files
with
1,120 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,205 @@ | ||
import replace from '@rollup/plugin-replace' | ||
import resolve from 'rollup-plugin-node-resolve' | ||
import commonjs from 'rollup-plugin-commonjs' | ||
import ts from 'rollup-plugin-typescript2' | ||
import { terser } from 'rollup-plugin-terser' | ||
import path from 'path' | ||
import rimraf from 'rimraf' | ||
import ts from 'rollup-plugin-typescript2' | ||
import replace from '@rollup/plugin-replace' | ||
import resolve from '@rollup/plugin-node-resolve' | ||
import commonjs from '@rollup/plugin-commonjs' | ||
import pascalcase from 'pascalcase' | ||
|
||
const cwd = process.cwd() | ||
// eslint-disable-next-line | ||
const pkg = require(path.join(cwd, 'package.json')) | ||
const pkg = require('./package.json') | ||
const name = pkg.name | ||
|
||
rimraf.sync(path.join(cwd, './dist')) | ||
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()} Eduardo San Martin Morote | ||
* (c) ${new Date().getFullYear()} ${getAuthors(pkg)} | ||
* @license MIT | ||
*/` | ||
|
||
const exportName = pascalcase(pkg.name) | ||
|
||
function createEntry({ | ||
format, // Rollup format (iife, umd, cjs, es) | ||
external = ['vue', '@vue/composition-api'], | ||
input = 'src/index.ts', // entry point | ||
env = 'development', // NODE_ENV variable | ||
minify = false, | ||
isBrowser = false, // produce a browser module version or not | ||
}) { | ||
// force production mode when minifying | ||
if (minify) env = 'production' | ||
|
||
const isBundlerESMBuild = format == 'es' | ||
const isNodeBuild = format == 'cjs' | ||
const isProduction = env == 'production' | ||
|
||
const config = { | ||
input, | ||
plugins: [ | ||
replace({ | ||
preventAssignment: true, | ||
values: { | ||
__VERSION__: pkg.version, | ||
__BROWSER__: JSON.stringify(isBrowser), | ||
'process.env.NODE_ENV': `'${env}'`, | ||
__DEV__: | ||
isBundlerESMBuild || (isNodeBuild && !isProduction) | ||
? // preserve to be handled by bundlers | ||
`(process.env.NODE_ENV !== 'production')` | ||
: // hard coded dev/prod builds | ||
JSON.stringify(!isProduction), | ||
}, | ||
}), | ||
], | ||
output: { | ||
banner, | ||
file: `dist/${pkg.name}.UNKNOWN.js`, | ||
format, | ||
|
||
globals: { | ||
'@vue/composition-api': 'vueCompositionApi', | ||
vue: 'Vue', | ||
}, | ||
}, | ||
} | ||
// 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 | ||
'esm-bundler': { | ||
file: pkg.module, | ||
format: `es`, | ||
}, | ||
cjs: { | ||
file: pkg.main, | ||
format: `cjs`, | ||
}, | ||
global: { | ||
file: pkg.unpkg, | ||
format: `iife`, | ||
}, | ||
esm: { | ||
file: pkg.module.replace('bundler', 'browser'), | ||
format: `es`, | ||
}, | ||
} | ||
|
||
if (format === 'iife') { | ||
config.output.file = pkg.unpkg | ||
config.output.name = exportName | ||
} else if (format === 'es') { | ||
config.output.file = isBrowser ? pkg.browser : pkg.module | ||
} else if (format === 'cjs') { | ||
config.output.file = pkg.main | ||
const allFormats = Object.keys(outputConfigs) | ||
const packageFormats = allFormats | ||
const packageConfigs = packageFormats.map((format) => | ||
createConfig(format, outputConfigs[format]) | ||
) | ||
|
||
// only add the production ready if we are bundling the options | ||
packageFormats.forEach((format) => { | ||
if (format === 'cjs') { | ||
packageConfigs.push(createProductionConfig(format)) | ||
} else if (format === 'global') { | ||
packageConfigs.push(createMinifiedConfig(format)) | ||
} | ||
}) | ||
|
||
if (!external) { | ||
config.plugins.push(resolve(), commonjs()) | ||
} else { | ||
config.external = external | ||
export default packageConfigs | ||
|
||
function createConfig(format, output, plugins = []) { | ||
if (!output) { | ||
console.log(require('chalk').yellow(`invalid format: "${format}"`)) | ||
process.exit(1) | ||
} | ||
|
||
config.plugins.push( | ||
ts({ | ||
// only check once, during the es version with browser (it includes external libs) | ||
check: format === 'es' && isBrowser && !minify, | ||
tsconfigOverride: { | ||
exclude: ['__tests__'], | ||
compilerOptions: { | ||
// same for d.ts files | ||
declaration: format === 'es' && isBrowser && !minify, | ||
target: format === 'es' && !isBrowser ? 'esnext' : 'es5', | ||
}, | ||
output.sourcemap = !!process.env.SOURCE_MAP | ||
output.banner = banner | ||
output.externalLiveBindings = false | ||
output.globals = { vue: 'Vue', '@vue/composition-api': 'vueCompositionApi' } | ||
|
||
const isProductionBuild = /\.prod\.js$/.test(output.file) | ||
const isGlobalBuild = format === 'global' | ||
const isRawESMBuild = format === 'esm' | ||
const isNodeBuild = format === 'cjs' | ||
const isBundlerESMBuild = /esm-bundler/.test(format) | ||
|
||
if (isGlobalBuild) output.name = pascalcase(pkg.name) | ||
|
||
const shouldEmitDeclarations = !hasTSChecked | ||
|
||
const tsPlugin = ts({ | ||
check: !hasTSChecked, | ||
tsconfig: path.resolve(__dirname, 'tsconfig.json'), | ||
cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'), | ||
tsconfigOverride: { | ||
compilerOptions: { | ||
sourceMap: output.sourcemap, | ||
declaration: shouldEmitDeclarations, | ||
declarationMap: shouldEmitDeclarations, | ||
}, | ||
}) | ||
) | ||
exclude: ['__tests__', 'test-dts'], | ||
}, | ||
}) | ||
// 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 | ||
|
||
if (minify) { | ||
config.plugins.push( | ||
terser({ | ||
module: format === 'es', | ||
// output: { | ||
// comments: false, | ||
// already added by rollup | ||
// only necessary if removing others | ||
// preamble: banner, | ||
// }, | ||
}) | ||
) | ||
config.output.file = config.output.file.replace(/\.js$/i, '.min.js') | ||
const external = ['vue', '@vue/composition-api'] | ||
|
||
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, | ||
// isBrowserBuild? | ||
isGlobalBuild || isRawESMBuild || isBundlerESMBuild, | ||
isGlobalBuild, | ||
isNodeBuild | ||
), | ||
...nodePlugins, | ||
...plugins, | ||
], | ||
output, | ||
// onwarn: (msg, warn) => { | ||
// if (!/Circular/.test(msg)) { | ||
// warn(msg) | ||
// } | ||
// }, | ||
} | ||
} | ||
|
||
return config | ||
function createReplacePlugin( | ||
isProduction, | ||
isBundlerESMBuild, | ||
isBrowserBuild, | ||
isGlobalBuild, | ||
isNodeBuild | ||
) { | ||
const replacements = { | ||
__COMMIT__: `"${process.env.COMMIT}"`, | ||
__VERSION__: `"${pkg.version}"`, | ||
__DEV__: | ||
isBundlerESMBuild || (isNodeBuild && !isProduction) | ||
? // preserve to be handled by bundlers | ||
`(process.env.NODE_ENV !== 'production')` | ||
: // hard coded dev/prod builds | ||
JSON.stringify(!isProduction), | ||
// this is only used during tests | ||
__TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : 'false', | ||
// If the build is expected to run directly in the browser (global / esm builds) | ||
__BROWSER__: JSON.stringify(isBrowserBuild), | ||
// is targeting bundlers? | ||
__BUNDLER__: JSON.stringify(isBundlerESMBuild), | ||
__GLOBAL__: JSON.stringify(isGlobalBuild), | ||
// is targeting Node (SSR)? | ||
__NODE_JS__: JSON.stringify(isNodeBuild), | ||
} | ||
// 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, | ||
values: replacements, | ||
}) | ||
} | ||
|
||
const builds = [ | ||
createEntry({ format: 'cjs' }), | ||
createEntry({ format: 'es', isBrowser: true }), | ||
] | ||
function createProductionConfig(format) { | ||
return createConfig(format, { | ||
file: `dist/${name}.${format}.prod.js`, | ||
format: outputConfigs[format].format, | ||
}) | ||
} | ||
|
||
if (pkg.unpkg) | ||
builds.push( | ||
createEntry({ format: 'iife' }), | ||
createEntry({ format: 'iife', minify: true }), | ||
createEntry({ format: 'es', isBrowser: true, minify: true }) | ||
function createMinifiedConfig(format) { | ||
const { terser } = require('rollup-plugin-terser') | ||
return createConfig( | ||
format, | ||
{ | ||
file: `dist/${name}.${format}.prod.js`, | ||
format: outputConfigs[format].format, | ||
}, | ||
[ | ||
terser({ | ||
module: /^esm/.test(format), | ||
compress: { | ||
ecma: 2015, | ||
pure_getters: true, | ||
}, | ||
}), | ||
] | ||
) | ||
|
||
export default builds | ||
} |
Oops, something went wrong.