Skip to content

Commit

Permalink
build: add webpack based build system
Browse files Browse the repository at this point in the history
  • Loading branch information
Bonajo committed Jan 27, 2022
1 parent 0af3f00 commit 1fc9a2e
Show file tree
Hide file tree
Showing 10 changed files with 10,133 additions and 0 deletions.
9,930 changes: 9,930 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "tailwind-test",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config webpack.prod.js",
"serve": "webpack serve --config webpack.dev.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Bonajo/tailwind-test.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/Bonajo/tailwind-test/issues"
},
"homepage": "https://github.com/Bonajo/tailwind-test#readme",
"devDependencies": {
"autoprefixer": "^10.4.0",
"css-loader": "^6.5.1",
"css-minimizer-webpack-plugin": "^3.4.1",
"html-webpack-plugin": "^5.5.0",
"mini-css-extract-plugin": "^2.4.5",
"postcss": "^8.4.5",
"postcss-loader": "^6.2.1",
"style-loader": "^3.3.1",
"tailwindcss": "^3.0.7",
"ts-loader": "^9.2.6",
"typescript": "^4.5.5",
"webpack": "^5.65.0",
"webpack-cli": "^4.9.1",
"webpack-dev-server": "^4.7.3"
}
}
7 changes: 7 additions & 0 deletions postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
plugins: [
require('autoprefixer'),
require('postcss-nested'),
require('tailwindcss'),
]
};
9 changes: 9 additions & 0 deletions src/css/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer utilities {
.max-w-8xl {
max-width: 90rem;
}
}
49 changes: 49 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import './css/main.css';

enum Theme {
Light = "Light",
Dark = "Dark"
}

const menuToggle = document.querySelector("button#menu-toggle");
const menu = document.querySelector("#menu");
const menuBackdrop = document.querySelector("#menu-backdrop");
const themeToggle = document.querySelector("#theme-toggle");
let theme = Theme.Light;

const toggleMenu = (menu: Element, menuBackdrop: Element) => {
menu.classList.toggle("hidden");
menuBackdrop.classList.toggle("hidden");
};

const getTheme = () => {
const currentTheme = localStorage.getItem("theme");
if ((!!currentTheme && Theme[currentTheme as keyof typeof Theme] === Theme.Dark) || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
theme = Theme.Dark;
} else {
theme = Theme.Light;
}
setTheme();
};

const setTheme = () => {
if(theme === Theme.Dark){
document.documentElement.classList.add('dark');
}else{
document.documentElement.classList.remove('dark');
}
};

const toggleTheme = () => {
theme = (theme === Theme.Dark) ? Theme.Light : Theme.Dark;
localStorage.setItem("theme", theme);
setTheme();
};

if(!!menuToggle && !!menu && !!menuBackdrop){
menuToggle.addEventListener('click', () => toggleMenu(menu, menuBackdrop));
}

themeToggle?.addEventListener('click', toggleTheme);

getTheme();
8 changes: 8 additions & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
content: ["./layouts/**/*.{html,js}"],
darkMode: "class",
theme: {
extend: {},
},
plugins: []
}
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"outDir": "./dist/",
"noImplicitAny": true,
"module": "es6",
"target": "es5",
"moduleResolution": "node",
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"alwaysStrict": true,
"allowUnreachableCode": false,
"strictNullChecks": true,
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"noUnusedParameters": true
}
}
51 changes: 51 additions & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");

module.exports = {
entry: './src/index.ts',
output: {
filename: 'js/[name].js',
path: path.resolve(__dirname, 'static'),
clean: true
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/
},
{
test: /\.css$/i,
exclude: /node_modules/,
use: [
MiniCssExtractPlugin.loader,
{
loader: "css-loader",
options: {
importLoaders: 1,
}
},
{
loader: "postcss-loader"
}
],
}
]
},
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
optimization: {
minimizer: [
`...`,
new CssMinimizerPlugin(),
],
},
plugins: [
new MiniCssExtractPlugin({
filename: "css/[name].css"
}),
],
};
16 changes: 16 additions & 0 deletions webpack.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const path = require('path');
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');

module.exports = merge(common, {
devtool: 'inline-source-map',
mode: 'development',
devServer: {
static: {
directory: path.resolve(__dirname, 'dist'),
},
compress: true,
port: 9000,
watchFiles: ['src/**/*.html', 'public/**/*'],
}
});
7 changes: 7 additions & 0 deletions webpack.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { merge } = require('webpack-merge');
const common = require('./webpack.common.js');
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");

module.exports = merge(common, {
mode: 'production',
});

0 comments on commit 1fc9a2e

Please sign in to comment.