initial commit

This commit is contained in:
Ray Elliott 2021-01-03 16:31:48 +00:00
commit 7adf443e07
11 changed files with 4328 additions and 0 deletions

99
notes.md Normal file
View File

@ -0,0 +1,99 @@
Requirements
* html
- templating
* js
- babel transpilation
- source maps
- minification on production
- eslint
* css preprocessing
- nested selectors
- variables
- mixins
- functions
- autoprefixer
- imports
- source maps
- minification on production
- stylelint
* live server with hmr
* clean dist directories
#### html
* html-webpack-plugin - automatically create index.html or use a template file
* ejs - templating
#### js
* @babel/core
- also takes care of minification in production
* @babel/preset-env
* babel-loader
* eslint
* eslint-webpack-plugin
#### css
* css-loader
* style-loader - injects css into dom (defaults to style tags)
* mini-css-extract-plugin - extracts css to separate files
* postcss-loader
* postcss-preset-env
takes care of autoprefixing(?) (supposedly nesting too but i needed to install
postcss-nested)
* postcss-nested
* postcss-import
* postcss-mixins
* postcss-functions
* stylelint
* stylelint-webpack-plugin
* cssnano
#### other
* webpack-dev-server
* clean-webpack-plugin
#### live server
## Installation notes
make repository private - edit package.json
replace
`"main": "index.js"`
with
`"private": "true"`
## Usage Notes
Asset management:
>= webpack 5.0 - use asset modules (replaces raw-loader, url-loader, file-loader)
```
module: {
rules: [
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource',
},
```
Now, when you import MyImage from './my-image.png', that image will be processed and added to your output directory and the MyImage variable will contain the final url of that image after processing.
When using the css-loader, as shown above, a similar process will occur for url('./my-image.png') within your CSS. The loader will recognize this is a local file, and replace the './my-image.png' path with the final path to the image in your output directory.
The html-loader handles <img src="./my-image.png" /> in the same manner.
If we don't want the loader to treat it as a local file we can use (in webpack.config.js):
- html-loader - use urlFilter option
- css-loader - url option -> filter function
types - asset/resource (file-loader), asset/inline (url-loader), asset/source (raw-loader), asset (url-loader)
for `asset` default size is 8kb. - can configure see
[docs](https://webpack.js.org/guides/asset-modules/).

4055
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

29
package.json Normal file
View File

@ -0,0 +1,29 @@
{
"name": "webpack-static-html",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"author": "",
"license": "ISC",
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^5.0.1",
"html-webpack-plugin": "^4.5.0",
"mini-css-extract-plugin": "^1.3.3",
"postcss": "^8.2.2",
"postcss-functions": "^4.0.2",
"postcss-import": "^14.0.0",
"postcss-loader": "^4.1.0",
"postcss-mixins": "^7.0.2",
"postcss-nesting": "^7.0.1",
"postcss-preset-env": "^6.7.0",
"postcss-simple-vars": "^6.0.2",
"style-loader": "^2.0.0",
"webpack": "^5.11.1",
"webpack-cli": "^4.3.1"
}
}

18
postcss.config.js Normal file
View File

@ -0,0 +1,18 @@
module.exports = {
plugins: [
"postcss-import",
"postcss-mixins",
"postcss-preset-env",
"postcss-nesting",
"postcss-simple-vars",
["postcss-functions",
{
functions: { half },
}
],
],
};
function half(val) {
return val / 2;
}

28
src/index.html Normal file
View File

@ -0,0 +1,28 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>THE TITLE IS THIS!</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="THE DESCRIPTION IS THIS!">
<meta name="author" content="I AM THE AUTHOR">
</head>
<body>
<header>
</header>
<main id="js-main">
</main>
<footer>
</footer>
</body>
</html>

19
src/index.js Normal file
View File

@ -0,0 +1,19 @@
import './style.css';
import './style.inline.css';
import MyImage from './my-image.png';
function component() {
const element = document.createElement('div');
element.innerHTML = 'Hai There';
element.classList.add('hello');
const theImage = new Image();
theImage.src = MyImage;
element.appendChild(theImage);
return element;
}
document.querySelector('#js-main').appendChild(component());

3
src/mixins.css Normal file
View File

@ -0,0 +1,3 @@
@define-mixin size-big $size {
font-size: $size;
}

BIN
src/my-image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 269 KiB

14
src/style.css Normal file
View File

@ -0,0 +1,14 @@
@import "mixins.css";
.hello {
$color: green;
color: $color;
opacity: half(1);
@mixin size-big 2em;
& img {
max-width: 20em;
}
}

3
src/style.inline.css Normal file
View File

@ -0,0 +1,3 @@
body {
background-color: pink;
}

60
webpack.config.js Normal file
View File

@ -0,0 +1,60 @@
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
module.exports = {
entry: './src/index.js',
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist'),
},
plugins: [
new MiniCssExtractPlugin(),
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: 'src/index.html'
}),
],
module: {
rules: [
{
test: /\.inline\.css$/i,
use: [
{
loader: 'style-loader',
options: {
insert: function insertAtTop(element) {
var parent = document.querySelector('head');
// eslint-disable-next-line no-underscore-dangle
var lastInsertedElement =
window._lastElementInsertedByStyleLoader;
if (!lastInsertedElement) {
parent.insertBefore(element, parent.firstChild);
} else if (lastInsertedElement.nextSibling) {
parent.insertBefore(element, lastInsertedElement.nextSibling);
} else {
parent.appendChild(element);
}
// eslint-disable-next-line no-underscore-dangle
window._lastElementInsertedByStyleLoader = element;
},
},
},
'css-loader',
'postcss-loader'],
},
{
test: /\.css$/i,
exclude: /\.inline\.css$/i,
use: [MiniCssExtractPlugin.loader, 'css-loader', 'postcss-loader'],
},
{
test: /.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset',
},
]
},
};