Skip to content

Instantly share code, notes, and snippets.

@stamat
Created March 10, 2022 19:47
Show Gist options
  • Save stamat/fb3c873488f529a463fec8f11159c539 to your computer and use it in GitHub Desktop.
Save stamat/fb3c873488f529a463fec8f11159c539 to your computer and use it in GitHub Desktop.
rollup nunjucks scss watch livereload
{
"name": "el-camino-rapido",
"version": "1.0.0",
"author": "Nikola Stamatovic <@stamat>",
"homepage": "",
"private": true,
"dependencies": {
"inobounce": "^0.2.0",
"jquery": "^3.5.1",
"remove-focus-outline": "^1.2.1",
"slick-carousel": "^1.8.1",
"youtube-background": "^1.0.14"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^16.0.0",
"@rollup/plugin-node-resolve": "^10.0.0",
"@rollup/plugin-typescript": "^6.1.0",
"autoprefixer": "^10.4.2",
"chalk": "^5.0.1",
"fast-glob": "^3.2.11",
"fs": "0.0.1-security",
"node-sass": "^4.14.1",
"nunjucks": "^3.2.3",
"postcss": "^8.4.8",
"prettier": "^2.5.1",
"rollup": "^2.64.0",
"rollup-plugin-livereload": "^2.0.5",
"rollup-plugin-scss": "^2.6.1",
"rollup-plugin-serve": "^1.1.0",
"rollup-plugin-terser": "^7.0.2",
"serve": "^11.0.2",
"ts-node": "8.4.1",
"tslib": "1.10.0",
"typescript": "3.8.2"
},
"scripts": {
"serve": "npx rollup -c --watch",
"build": "npx rollup -c --production"
}
}
import serve from 'rollup-plugin-serve';
import livereload from 'rollup-plugin-livereload';
import scss from 'rollup-plugin-scss';
import commonjs from '@rollup/plugin-commonjs';
import typescript from '@rollup/plugin-typescript';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import nunjucks from 'nunjucks';
import autoprefixer from 'autoprefixer'
import postcss from 'postcss'
import fg from 'fast-glob'
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'
export default function config(args) {
const sass_opts = {
output: '_site/assets/css/style.css',
processor: () => postcss([autoprefixer()])
};
if (args.production) {
sass_opts.output = '_site/assets/css/style.min.css';
sass_opts.outputStyle = 'compressed';
}
const opts = {
input: '_scripts/main.ts',
output: {
file: '_site/assets/js/bundle.js'
},
format: 'iife',
sourcemap: false,
context: 'window',
inlineDynamicImports: true,
plugins: [
nodeResolve({
main: false,
mainFields: ['browser', 'module'],
extensions: ['.js', '.ts'],
customResolveoptsions: {
package: {},
moduleDirectory: ['node_modules']
}
}),
typescript(),
commonjs({
include: 'node_modules/**'
}),
args.production && terser(),
//!args.production && eslint(),
scss(sass_opts),
args.watch && serve({
contentBase: ['./_site'],
host: 'localhost',
port: 4041
}),
args.watch && livereload('_site'),
args.watch && {
name: 'watch-external',
async buildStart() {
const files = await fg(['_sass/**/*']);
for(let file of files) {
this.addWatchFile(file);
}
}
},
{
name: 'nunjucks',
async buildStart() {
const files = await fg(['_markup/**/*']);
for (let file of files) {
this.addWatchFile(file);
}
},
async writeBundle() {
nunjucks.configure(['_markup', '_markup/_layouts', '_markup/_partials']);
const files = await fg(['_markup/*.html', '_markup/*.njk']);
for (let file of files) {
const time_start = new Date();
const rendered = nunjucks.render(path.resolve(__dirname, file));
const filepath = path.join('_site/', path.basename(file))
fs.writeFileSync(filepath, rendered);
console.info(chalk.green('rendered ') + chalk.green.bold(filepath) + chalk.green(' in') + chalk.green.bold(` ${new Date() - time_start}ms`));
}
}
}
]
};
if (args.production) {
opts.output.file = '_site/assets/js/bundle.min.js';
}
if (args.sourcemap) {
opts.sourcemap = true;
}
return opts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment