Skip to content

Instantly share code, notes, and snippets.

@briansteeleca
Created February 18, 2021 16:17
Show Gist options
  • Save briansteeleca/ea07d3b607ed92eab5968fe37bfd2c4e to your computer and use it in GitHub Desktop.
Save briansteeleca/ea07d3b607ed92eab5968fe37bfd2c4e to your computer and use it in GitHub Desktop.
Sapper + Svelte Image + Tailwind CSS
<script>
import Nav from '../components/Nav.svelte';
export let segment;
</script>
<Nav {segment}/>
<main>
<slot></slot>
</main>
<style global lang="postcss">
/* https://www.swyx.io/svelte_tailwind_setup/#step-3-add-the-tailwind-includes-to-your-svelte-app */
/* only apply purgecss on utilities, per Tailwind docs */
/* purgecss start ignore */
@tailwind base;
@tailwind components;
/* purgecss end ignore */
@tailwind utilities;
</style>
import path from 'path';
import resolve from '@rollup/plugin-node-resolve';
import replace from '@rollup/plugin-replace';
import commonjs from '@rollup/plugin-commonjs';
import url from '@rollup/plugin-url';
import svelte from 'rollup-plugin-svelte';
import babel from '@rollup/plugin-babel';
import { terser } from 'rollup-plugin-terser';
import config from 'sapper/config/rollup.js';
import pkg from './package.json';
import seqPreprocessor from 'svelte-sequential-preprocessor';
import autoPreprocess from 'svelte-preprocess';
import image from 'svelte-image';
const mode = process.env.NODE_ENV;
const dev = mode === 'development';
const legacy = !!process.env.SAPPER_LEGACY_BUILD;
const production = !process.env.ROLLUP_WATCH;
const onwarn = (warning, onwarn) =>
(warning.code === 'MISSING_EXPORT' && /'preload'/.test(warning.message)) ||
(warning.code === 'CIRCULAR_DEPENDENCY' && /[/\\]@sapper[/\\]/.test(warning.message)) ||
onwarn(warning);
// https://stackoverflow.com/questions/62446218/change-the-order-of-preprocessors-in-svelte-sapper
// https://www.npmjs.com/package/svelte-sequential-preprocessor
const preprocess = seqPreprocessor([
autoPreprocess({
sourceMap: !production,
postcss: {
plugins: [
require("tailwindcss"),
require("autoprefixer"),
require("postcss-nesting")
],
},
}),
image({
inlineBelow: 10000,
placeholder: 'blur', //blur
})
]);
export default {
client: {
input: config.client.input(),
output: config.client.output(),
plugins: [
replace({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode)
}),
svelte({
preprocess: preprocess,
compilerOptions: {
dev,
hydratable: true
}
}),
url({
sourceDir: path.resolve(__dirname, 'src/node_modules/images'),
publicPath: '/client/'
}),
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
legacy && babel({
extensions: ['.js', '.mjs', '.html', '.svelte'],
babelHelpers: 'runtime',
exclude: ['node_modules/@babel/**'],
presets: [
['@babel/preset-env', {
targets: '> 0.25%, not dead'
}]
],
plugins: [
'@babel/plugin-syntax-dynamic-import',
['@babel/plugin-transform-runtime', {
useESModules: true
}]
]
}),
!dev && terser({
module: true
})
],
preserveEntrySignatures: false,
onwarn,
},
server: {
input: config.server.input(),
output: config.server.output(),
plugins: [
replace({
'process.browser': false,
'process.env.NODE_ENV': JSON.stringify(mode)
}),
svelte({
compilerOptions: {
dev,
generate: 'ssr',
hydratable: true
},
emitCss: false
}),
url({
sourceDir: path.resolve(__dirname, 'src/node_modules/images'),
publicPath: '/client/',
emitFiles: false // already emitted by client build
}),
resolve({
dedupe: ['svelte']
}),
commonjs()
],
external: Object.keys(pkg.dependencies).concat(require('module').builtinModules),
preserveEntrySignatures: 'strict',
onwarn,
},
serviceworker: {
input: config.serviceworker.input(),
output: config.serviceworker.output(),
plugins: [
resolve(),
replace({
'process.browser': true,
'process.env.NODE_ENV': JSON.stringify(mode)
}),
commonjs(),
!dev && terser()
],
preserveEntrySignatures: false,
onwarn,
}
};
// Add a tailwind.config.js file at the project root
// https://www.swyx.io/svelte_tailwind_setup/
const isProduction = !process.env.ROLLUP_WATCH; // or some other env var like NODE_ENV
module.exports = {
// only needed in Tailwind 1.0 for tailwind 2.0 compat
// future: {
// purgeLayersByDefault: true,
// removeDeprecatedGapUtilities: true,
// },
plugins: [
// for tailwind UI users only
// require('@tailwindcss/ui'),
// other plugins here
],
purge: {
content: [
"./src/**/*.svelte",
// may also want to include HTML files
// "./src/**/*.html"
],
// this is for extracting Svelte `class:` syntax but is not perfect yet, see below
defaultExtractor: content => {
const broadMatches = content.match(/[^<>"'`\s]*[^<>"'`\s:]/g) || []
const broadMatchesWithoutTrailingSlash = broadMatches.map(match => _.trimEnd(match, '\\'))
const matches = broadMatches
.concat(broadMatchesWithoutTrailingSlash)
return matches
},
enabled: isProduction // disable purge in dev
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment