Created
October 16, 2019 10:27
-
-
Save lgersman/073d175b4d39abc5cb3346459854a73f to your computer and use it in GitHub Desktop.
rollup config for a wordpress plugin including scss/es2018 support
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
import resolve from 'rollup-plugin-node-resolve'; | |
import commonjs from 'rollup-plugin-commonjs'; | |
//import strip from 'rollup-plugin-strip'; | |
import scss from 'rollup-plugin-scss'; | |
import packageJson from './package.json'; | |
import babel from 'rollup-plugin-babel'; | |
import { terser } from 'rollup-plugin-terser'; | |
const createBrowserConfig = (customizeConfig = () => {}) => { | |
const config = | |
// ES module version, for modern browsers | |
{ | |
input: packageJson.module, | |
output: { | |
file: packageJson.browser, | |
format: 'iife', | |
// sourcemap: 'inline', | |
globals: { | |
react: 'React', | |
'react-dom': 'ReactDOM', | |
'@wordpress/editor': 'wp.editor', | |
'@wordpress/i18n': 'wp.i18n', | |
'@wordpress/components': 'wp.components', | |
'@wordpress/blocks': 'wp.blocks', | |
wp: 'wp', | |
}, | |
}, | |
external: ['wp', ...Object.keys(packageJson.peerDependencies)], | |
plugins: [ | |
babel({ | |
exclude: 'node_modules/**', | |
}), | |
// https://github.com/rollup/rollup-plugin-node-resolve | |
resolve({ | |
browser: true, | |
}), | |
// https://github.com/rollup/rollup-plugin-commonjs | |
commonjs({}), | |
], | |
}; | |
customizeConfig(config); | |
return config; | |
}; | |
export default [ | |
/* | |
// Nodejs commonjs version | |
{ | |
input: packageJson.module, | |
output: { | |
file: packageJson.main, | |
format: 'es', | |
sourcemap: true, | |
}, | |
external: [ | |
Object.keys(packageJson.dependencies), | |
Object.keys(packageJson.peerDependencies) | |
].flat(), | |
plugins : [ | |
babel({ | |
exclude: 'node_modules/**' | |
}) | |
] | |
}, | |
*/ | |
// development | |
createBrowserConfig(config => { | |
config.plugins.unshift( | |
scss({ | |
// see options here : https://www.npmjs.com/package/node-sass | |
outFile: config.output.file.replace('.js', '.css'), | |
outputStyle: 'expanded', | |
sourceComments: true, | |
sourceMapEmbed: true, | |
}) | |
); | |
}), | |
// production | |
createBrowserConfig(config => { | |
config.output.file = config.output.file.replace('.js', '-min.js'); | |
config.output.sourcemap = 'inline'; | |
config.plugins.push(terser()); | |
config.plugins.unshift( | |
scss({ | |
// see options here : https://www.npmjs.com/package/node-sass | |
outFile: config.output.file.replace('.js', '-min.css'), | |
outputStyle: 'compressed', | |
}) | |
); | |
}), | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment