emoji-picker-element/rollup.config.js

155 lines
4.4 KiB
JavaScript
Raw Normal View History

2023-11-12 01:14:45 +01:00
import MagicString from 'magic-string'
2023-11-11 17:12:20 +01:00
import inject from '@rollup/plugin-inject'
2020-05-17 05:36:21 +02:00
import cjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import strip from '@rollup/plugin-strip'
import svelte from 'rollup-plugin-svelte'
2020-06-15 19:03:07 +02:00
import preprocess from 'svelte-preprocess'
2020-06-09 16:09:23 +02:00
import analyze from 'rollup-plugin-analyzer'
2023-06-18 21:14:18 +02:00
import { buildStyles } from './bin/buildStyles.js'
2020-05-10 04:39:47 +02:00
const { NODE_ENV, DEBUG } = process.env
const dev = NODE_ENV !== 'production'
2020-05-31 18:34:17 +02:00
const preprocessConfig = preprocess()
const origMarkup = preprocessConfig.markup
// minify the HTML by removing extra whitespace
// TODO: this is fragile, but it also results in a lot of bundlesize savings. let's find a better solution
preprocessConfig.markup = async function () {
const res = await origMarkup.apply(this, arguments)
2020-06-25 17:44:54 +02:00
// remove whitespace
res.code = res.code.replace(/([>}])\s+([<{])/sg, '$1$2')
2020-06-25 17:44:54 +02:00
return res
}
// Build Database.test.js and Picker.js as separate modules at build times so that they are properly tree-shakeable.
// Most of this has to happen because customElements.define() has side effects
2020-05-10 04:39:47 +02:00
const baseConfig = {
plugins: [
resolve(),
cjs(),
2020-05-17 05:36:21 +02:00
replace({
'process.env.NODE_ENV': dev ? '"development"' : '"production"',
2021-05-30 20:05:14 +02:00
'process.env.PERF': !!process.env.PERF,
'process.env.STYLES': JSON.stringify(buildStyles()),
2021-05-30 20:05:14 +02:00
preventAssignment: true
2020-05-17 05:36:21 +02:00
}),
replace({
2020-06-07 22:08:28 +02:00
'\'../database/Database.js\'': '\'./database.js\'',
2021-05-30 20:05:14 +02:00
delimiters: ['', ''],
preventAssignment: true
}),
2020-05-10 05:21:32 +02:00
svelte({
compilerOptions: {
2023-11-11 17:12:20 +01:00
dev,
discloseVersion: false
},
preprocess: preprocessConfig
2020-06-09 16:09:23 +02:00
}),
2021-07-25 18:25:39 +02:00
// make the svelte output slightly smaller
replace({
'options.anchor': 'undefined',
'options.context': 'undefined',
'options.customElement': 'undefined',
'options.hydrate': 'undefined',
'options.intro': 'undefined',
2021-07-25 18:25:39 +02:00
delimiters: ['', ''],
preventAssignment: true
}),
strip({
include: ['**/*.js', '**/*.svelte'],
functions: [
(!dev && !process.env.PERF) && 'performance.*',
!dev && 'console.log'
].filter(Boolean)
}),
DEBUG && analyze({ summaryOnly: true })
],
external: [
2020-06-07 22:08:28 +02:00
'./database.js',
'../database/Database.js'
2020-05-10 04:39:47 +02:00
]
}
2020-05-10 05:13:50 +02:00
const entryPoints = [
2020-05-10 04:39:47 +02:00
{
input: './src/picker/PickerElement.js',
output: './picker.js',
plugins: [
// Replace newer syntax in Svelte v4 to avoid breaking iOS <13.4
// https://github.com/nolanlawson/emoji-picker-element/pull/379
replace({
'array_like_or_iterator?.length': 'array_like_or_iterator && array_like_or_iterator.length',
'$$ = undefined;': '', // not necessary to initialize class prop to undefined
'$$set = undefined;': '', // not necessary to initialize class prop to undefined
delimiters: ['', ''],
preventAssignment: true
})
]
},
{
2020-06-07 05:46:03 +02:00
input: './src/database/Database.js',
2020-06-04 05:12:43 +02:00
output: './database.js'
2020-06-20 23:06:19 +02:00
},
{
input: './src/trimEmojiData.js',
output: './trimEmojiData.js'
},
{
input: './src/trimEmojiData.js',
output: './trimEmojiData.cjs',
format: 'cjs'
2020-06-21 20:55:27 +02:00
},
{
input: './src/picker/PickerElement.js',
output: './svelte.js',
2023-11-11 17:12:20 +01:00
external: ['svelte', 'svelte/internal'],
// TODO: drop Svelte v3 support
// ensure_array_like was added in Svelte v4 - we shim it to avoid breaking Svelte v3 users
plugins: [
{
name: 'svelte-v3-compat',
transform (source) {
2023-11-12 01:14:45 +01:00
const magicString = new MagicString(source)
magicString.replaceAll('ensure_array_like(', 'ensure_array_like_shim(')
return {
code: magicString.toString(),
map: magicString.generateMap()
}
2023-11-11 17:12:20 +01:00
}
},
inject({
ensure_array_like_shim: [
'../../../../shims/svelte-v3-shim.js',
'ensure_array_like_shim'
]
})
],
onwarn (warning) {
if (!warning.message.includes('ensure_array_like')) { // intentionally ignore warning for unused import
console.warn(warning.message)
}
}
2020-05-10 04:39:47 +02:00
}
]
2020-05-10 05:13:50 +02:00
2023-11-11 17:12:20 +01:00
export default entryPoints.map(({ input, output, format = 'es', external = [], plugins = [], onwarn }) => {
return {
2020-06-21 20:55:27 +02:00
input,
output: {
format,
file: output,
sourcemap: dev,
exports: 'auto'
2023-11-11 17:12:20 +01:00
},
external: [...baseConfig.external, ...external],
plugins: [...baseConfig.plugins, ...plugins],
onwarn
2020-05-10 05:13:50 +02:00
}
2020-06-21 20:55:27 +02:00
})