Skip to content

Instantly share code, notes, and snippets.

@baetheus
Last active January 14, 2019 23:50
Show Gist options
  • Select an option

  • Save baetheus/edbc6851bdf5e7ac46a3dfd677872716 to your computer and use it in GitHub Desktop.

Select an option

Save baetheus/edbc6851bdf5e7ac46a3dfd677872716 to your computer and use it in GitHub Desktop.
Rollup + Typescript + Serve + Preact
import del from 'rollup-plugin-delete';
import typescript from 'rollup-plugin-typescript';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import htmlTemplate from 'rollup-plugin-generate-html-template';
import copy from 'rollup-plugin-copy';
import serve from 'rollup-plugin-serve';
import { terser } from 'rollup-plugin-terser';
const createHash = () =>
Math.random()
.toString(36)
.slice(2, 7);
const tryThen = (fn, then) => {
try {
return fn();
} catch {
return then;
}
};
const ENV = tryThen(() => process.env.NODE_ENV || 'development', 'development');
const runServe = ENV !== 'production';
export default {
// Entrypoint
input: './src/main.tsx',
// Output
output: {
file: `dist/bundle-${createHash()}.js`,
format: 'iife',
name: 'main',
sourcemap: true,
},
// Workflow
plugins: [
// Clear previous dist
del({ targets: 'dist/*' }),
// Compile away typescript types
typescript(),
// Resolve node imports
resolve({ browser: true }),
// Turn commonjs bundles into es2015 bundles
commonjs(),
// Minify js
terser(),
// Seed index.html with the build script
htmlTemplate({
template: 'src/template.html',
target: 'index.html',
}),
// Copy assets to dist
copy({
'src/assets': 'dist/assets',
}),
].concat(
// Serve the build when not production
runServe
? serve({
contentBase: 'dist',
port: 1234,
headers: {
'Access-Control-Allow-Origin': '*',
},
})
: []
),
};
{
"compilerOptions": {
"strict": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"allowUnreachableCode": false,
"noImplicitAny": true,
"noErrorTruncation": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"skipLibCheck": true,
"esModuleInterop": true,
"sourceMap": true,
"lib": ["es5", "es6", "dom"],
"types": ["node"],
"jsx": "react",
"jsxFactory": "h",
"baseUrl": "./",
"paths": {
"~/*": ["./src/*"]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment