Skip to content

Commit

Permalink
ci: add size check
Browse files Browse the repository at this point in the history
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
posva committed Mar 8, 2021
1 parent 77d6ffd commit df59e90
Show file tree
Hide file tree
Showing 7 changed files with 1,120 additions and 189 deletions.
29 changes: 17 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "pinia",
"version": "0.1.0",
"description": "Intuitive, type safe and light Store for Vue",
"main": "dist/pinia.common.js",
"module": "dist/pinia.esm.js",
"unpkg": "dist/pinia.js",
"browser": "dist/pinia.esm.js",
"main": "dist/pinia.cjs.js",
"module": "dist/pinia.esm-bundler.js",
"unpkg": "dist/pinia.global.js",
"jsdelivr": "dist/pinia.global.js",
"types": "dist/src",
"sideEffects": false,
"author": {
Expand All @@ -19,12 +19,15 @@
"dev": "yarn run unit --watchAll",
"pretest": "yarn run lint",
"test": "yarn run unit && yarn run build",
"build": "rm -rf dist && rollup -c rollup.config.js",
"prepublishOnly": "yarn run build"
"build": "rimraf dist && rollup -c rollup.config.js",
"size": "rollup -c size-checks/rollup.config.js && node scripts/check-size.js",
"release": "bash scripts/release.sh",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1"
},
"files": [
"nuxt",
"dist",
"nuxt/*.js",
"nuxt/*.d.ts",
"dist/*.js",
"dist/src",
"LICENSE",
"README.md"
Expand All @@ -51,14 +54,18 @@
"license": "MIT",
"devDependencies": {
"@nuxt/types": "^2.15.2",
"@rollup/plugin-alias": "^3.1.2",
"@rollup/plugin-replace": "^2.4.1",
"@rollup/plugin-alias": "^3.1.1",
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"@rollup/plugin-replace": "^2.3.3",
"@types/jest": "^26.0.20",
"@typescript-eslint/eslint-plugin": "^4.16.1",
"@typescript-eslint/parser": "^4.16.1",
"@vue/composition-api": "^1.0.0-rc.3",
"@vue/test-utils": "^1.1.3",
"brotli": "^1.3.2",
"codecov": "^3.8.1",
"conventional-changelog-cli": "^2.1.0",
"eslint": "^7.21.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
Expand All @@ -68,8 +75,6 @@
"prettier": "^2.2.1",
"rimraf": "^3.0.1",
"rollup": "^2.40.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^7.0.0",
"rollup-plugin-typescript2": "^0.30.0",
"ts-jest": "^26.5.2",
Expand Down
288 changes: 182 additions & 106 deletions rollup.config.js
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
}
Loading

0 comments on commit df59e90

Please sign in to comment.