Skip to content

Instantly share code, notes, and snippets.

@mnpenner
Last active January 3, 2021 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mnpenner/d201118e498172cfa8be69a646d0ece4 to your computer and use it in GitHub Desktop.
Save mnpenner/d201118e498172cfa8be69a646d0ece4 to your computer and use it in GitHub Desktop.
Rollup Plugin: Pug
{
"license": "UNLICENSED",
"dependencies": {
"fastify": "^3.9.2",
"fastify-cookie": "^5.1.0",
"fastify-sensible": "^3.1.0",
"google-auth-library": "^6.1.3",
"lodash": "^4.17.20",
"pug": "^3.0.0",
"pug-runtime": "^3.0.0"
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"@babel/preset-typescript": "^7.10.4",
"@rollup/plugin-auto-install": "^2.1.0",
"@rollup/plugin-babel": "^5.2.1",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.0.1",
"@rollup/plugin-replace": "^2.3.4",
"@rollup/plugin-run": "^2.0.2",
"@types/jest": "^26.0.19",
"@types/node": "^14",
"@types/pug": "^2.0.4",
"builtin-modules": "^3.1.0",
"dotenv": "^8.2.0",
"jest": "^26.6.3",
"pino-pretty": "^4.3.0",
"pkg-dir": "^5.0.0",
"rollup": "^2.35.1",
"rollup-plugin-node-externals": "^2.2.0",
"typescript": "^4.0.3"
},
"scripts": {
"dev": "rollup -cw --watch.buildDelay 200",
"build": "rollup -c",
"test": "jest"
},
"jest": {
"testEnvironment": "node",
"bail": true,
"verbose": true
}
}
import Pug from 'pug'
import {promises as FileSystem} from 'fs'
import pkgDir from 'pkg-dir'
import Path from 'path'
export default (options = {debug: false}) => ({
name: 'pug',
async load(absPath) {
if(!absPath.endsWith('.pug')) return null
const source = await FileSystem.readFile(absPath, 'utf8')
const filename = Path.relative(await pkgDir(), absPath)
const fn = Pug.compile(source, {
filename: filename,
inlineRuntimeFunctions: false,
compileDebug: !!options.debug,
debug: false,
pretty: false,
})
for(const dep of fn.dependencies) {
this.addWatchFile(dep)
}
return {
code: `import pug from 'pug-runtime';\n${fn};\nexport default template;`,
moduleSideEffects: false,
}
}
})
import babel from '@rollup/plugin-babel';
import nodeResolve from '@rollup/plugin-node-resolve';
import nodeExternals from 'rollup-plugin-node-externals'
import * as tsconfig from './tsconfig.json';
import json from '@rollup/plugin-json';
import run from '@rollup/plugin-run';
import auto from '@rollup/plugin-auto-install';
import replace from '@rollup/plugin-replace';
import pug from './rollup-plugins/rollup-plugin-pug'
const extensions = ['.ts']
const isWatch = process.env.ROLLUP_WATCH === 'true'
const isDev = process.env.NODE_ENV === 'development'
const debug = isWatch || isDev
export default {
input: tsconfig.files,
plugins: [
nodeExternals({
builtins: true,
deps: true,
devDeps: false,
peerDeps: true,
optDeps: false,
}),
json(),
pug({
debug: debug,
}),
babel({
include: 'src/**',
extensions,
comments: false,
babelHelpers: 'bundled', // https://github.com/rollup/plugins/tree/master/packages/babel#babelhelpers
}),
nodeResolve({
extensions
}),
replace({
'process.env.NODE_ENV': process.env.NODE_ENV,
__DEV__: isWatch || isDev,
}),
isDev && auto({
manager: 'yarn',
pkgFile: `${__dirname}/package.json`
}),
isWatch && run({
execArgv: ['-r', 'dotenv/config', '--enable-source-maps']
}),
],
output: {
dir: 'dist',
format: 'cjs',
sourcemap: debug,
inlineDynamicImports: true,
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment