Skip to content

Instantly share code, notes, and snippets.

@pareshchouhan
Last active November 3, 2021 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pareshchouhan/af1a577ca092ffa8705534ca78911635 to your computer and use it in GitHub Desktop.
Save pareshchouhan/af1a577ca092ffa8705534ca78911635 to your computer and use it in GitHub Desktop.
vue2.rollup
// rollup.config.js
import fs from 'fs';
import path from 'path';
import vue from 'vue-prev-rollup-plugin-vue';
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import babel from '@rollup/plugin-babel';
import { terser } from 'rollup-plugin-terser';
import minimist from 'minimist';
// Get browserslist config and remove ie from es build targets
const esbrowserslist = fs.readFileSync('./.browserslistrc')
.toString()
.split('\n')
.filter((entry) => entry && entry.slice(0, 2) !== 'ie');
const argv = minimist(process.argv.slice(2));
const projectRoot = path.resolve(__dirname, '..', 'src');
const baseConfig = {
input: 'src/entry.ts',
external: ['@vue/composition-api'],
plugins: {
preVue: [
json(),
alias({
entries: [
{
find: '@',
replacement: projectRoot
},
{ find: 'composition-api', replacement: '@vue/composition-api' }
],
customResolver: resolve({
extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue']
})
})
],
replace: {
'process.env.NODE_ENV': JSON.stringify('production'),
preventAssignment: true
},
vue: {
css: true,
template: {
isProduction: true
}
},
postVue: [
resolve({
extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue']
})
],
babel: {
exclude: 'node_modules/**',
extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue'],
babelHelpers: 'bundled'
}
}
};
// ESM/UMD/IIFE shared settings: externals
// Refer to https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency
const external = [
// list external dependencies, exactly the way it is written in the import statement.
// eg. 'jquery'
'vue',
'vue-next'
];
// UMD/IIFE shared settings: output.globals
// Refer to https://rollupjs.org/guide/en#output-globals for details
const globals = {
// Provide global variable names to replace your external imports
// eg. jquery: '$'
vue: 'Vue',
'vue-next': 'Vue'
};
// Customize configs for individual targets
const buildFormats = [];
if (!argv.format || argv.format === 'es') {
const esConfig = {
...baseConfig,
input: 'src/entry.esm.ts',
external,
output: {
file: 'dist/vue2.esm.js',
format: 'esm',
exports: 'named'
},
plugins: [
replace(baseConfig.plugins.replace),
...baseConfig.plugins.preVue,
vue(baseConfig.plugins.vue),
...baseConfig.plugins.postVue,
babel({
...baseConfig.plugins.babel,
presets: [
[
'@babel/preset-env',
{
targets: esbrowserslist
}
]
]
}),
commonjs(),
terser()
]
};
buildFormats.push(esConfig);
}
if (!argv.format || argv.format === 'cjs') {
const umdConfig = {
...baseConfig,
external,
output: {
compact: true,
file: 'dist/vue2.ssr.js',
format: 'cjs',
name: 'Vue2',
exports: 'auto',
globals
},
plugins: [
replace(baseConfig.plugins.replace),
...baseConfig.plugins.preVue,
vue({
...baseConfig.plugins.vue,
template: {
...baseConfig.plugins.vue.template,
optimizeSSR: true
}
}),
...baseConfig.plugins.postVue,
babel(baseConfig.plugins.babel),
commonjs()
]
};
buildFormats.push(umdConfig);
}
if (!argv.format || argv.format === 'iife') {
const unpkgConfig = {
...baseConfig,
external,
output: {
compact: true,
file: 'dist/vue2.min.js',
format: 'iife',
name: 'Vue2',
exports: 'auto',
globals
},
plugins: [
replace(baseConfig.plugins.replace),
...baseConfig.plugins.preVue,
vue(baseConfig.plugins.vue),
...baseConfig.plugins.postVue,
babel(baseConfig.plugins.babel),
commonjs(),
terser({
output: {
ecma: 5
}
})
]
};
buildFormats.push(unpkgConfig);
}
// Export config
export default buildFormats;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment