mkdocs-material/webpack.config.ts

377 lines
11 KiB
TypeScript
Raw Normal View History

2019-09-29 01:30:56 +03:00
/*
2020-02-10 20:32:28 +03:00
* Copyright (c) 2016-2020 Martin Donath <martin.donath@squidfunk.com>
2019-09-29 01:30:56 +03:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
2020-02-10 20:32:28 +03:00
import * as CopyPlugin from "copy-webpack-plugin"
import * as EventHooksPlugin from "event-hooks-webpack-plugin"
import * as fs from "fs"
import { minify as minhtml } from "html-minifier"
import IgnoreEmitPlugin from "ignore-emit-webpack-plugin"
2020-04-27 15:04:56 +03:00
import ImageminPlugin from "imagemin-webpack-plugin"
import MiniCssExtractPlugin = require("mini-css-extract-plugin")
2019-09-29 01:30:56 +03:00
import * as path from "path"
2020-02-10 20:32:28 +03:00
import { toPairs } from "ramda"
import { minify as minjs } from "terser"
2019-12-18 18:38:36 +03:00
import { TsconfigPathsPlugin } from "tsconfig-paths-webpack-plugin"
2020-02-19 17:45:52 +03:00
import { Configuration } from "webpack"
2020-02-10 20:32:28 +03:00
import * as AssetsManifestPlugin from "webpack-assets-manifest"
2019-09-29 01:30:56 +03:00
/* ----------------------------------------------------------------------------
* Helper functions
2019-09-29 01:30:56 +03:00
* ------------------------------------------------------------------------- */
/**
* Webpack base configuration
2019-09-29 01:30:56 +03:00
*
* @param args - Command-line arguments
*
* @return Webpack configuration
*/
function config(args: Configuration): Configuration {
2020-02-10 20:32:28 +03:00
const assets = {}
2019-09-29 01:30:56 +03:00
return {
mode: args.mode,
/* Loaders */
module: {
rules: [
/* TypeScript */
{
test: /\.tsx?$/,
use: [
{
loader: "ts-loader",
options: {
2020-02-10 20:32:28 +03:00
experimentalWatchApi: true,
2019-09-29 01:30:56 +03:00
transpileOnly: true,
compilerOptions: {
importHelpers: true,
module: "esnext"
2019-09-29 01:30:56 +03:00
}
}
}
],
exclude: /\/node_modules\//
},
2020-02-10 20:32:28 +03:00
/* SASS stylesheets */
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
2020-02-10 20:32:28 +03:00
{
loader: "css-loader",
options: {
2020-02-11 13:02:46 +03:00
url: false,
sourceMap: true
2020-02-10 20:32:28 +03:00
}
},
{
loader: "postcss-loader",
options: {
ident: "postcss",
plugins: () => [
2020-04-27 11:27:59 +03:00
require("autoprefixer")(),
require("postcss-inline-svg")({
paths: [
path.resolve(__dirname, "node_modules")
],
encode: false
}),
require("postcss-svgo")({
plugins: [
{ removeDimensions: true },
{ removeViewBox: false }
],
encode: false
})
2020-02-10 20:32:28 +03:00
],
sourceMap: true
2020-02-10 20:32:28 +03:00
}
},
{
loader: "sass-loader",
options: {
implementation: require("sass"),
sassOptions: {
includePaths: [
"node_modules/modularscale-sass/stylesheets",
"node_modules/material-design-color",
"node_modules/material-shadows"
]
},
sourceMap: true
2020-02-10 20:32:28 +03:00
}
}
]
2019-09-29 01:30:56 +03:00
}
]
},
/* Module resolver */
resolve: {
modules: [
__dirname,
path.resolve(__dirname, "node_modules")
],
2019-12-18 18:38:36 +03:00
extensions: [".ts", ".tsx", ".js", ".json"],
plugins: [
new TsconfigPathsPlugin()
]
2019-09-29 01:30:56 +03:00
},
2020-02-10 20:32:28 +03:00
/* Plugins */
plugins: [
new IgnoreEmitPlugin(/\/stylesheets\/.*?\.js/),
2020-02-10 20:32:28 +03:00
new AssetsManifestPlugin({
output: "assets/manifest.json",
assets
2020-02-10 20:32:28 +03:00
})
],
2019-12-17 12:01:46 +03:00
/* Source maps */
devtool: args.mode === "production" ? "source-map" : "eval",
2019-12-22 18:52:28 +03:00
2020-02-11 19:14:28 +03:00
/* Filter false positives and omit verbosity */
2019-12-22 18:52:28 +03:00
stats: {
2020-02-11 19:14:28 +03:00
entrypoints: false,
excludeAssets: [
/\.(icons)/,
/\/(images|lunr)\//,
/\.(html|py|yml)$/
],
2019-12-22 18:52:28 +03:00
warningsFilter: [
/export '.[^']+' was not found in/
2019-12-22 18:52:28 +03:00
]
}
2019-09-29 01:30:56 +03:00
}
}
/* ----------------------------------------------------------------------------
* Configuration
* ------------------------------------------------------------------------- */
/**
* Webpack configuration
*
* @param env - Webpack environment arguments
* @param args - Command-line arguments
*
2019-12-22 18:52:28 +03:00
* @return Webpack configurations
*/
2020-02-10 20:32:28 +03:00
export default (_env: never, args: Configuration): Configuration[] => {
const hash = args.mode === "production" ? ".[chunkhash].min" : ""
const base = config(args)
return [
/* Application */
{
...base,
entry: {
"assets/javascripts/bundle": "src/assets/javascripts",
"assets/stylesheets/main": "src/assets/stylesheets/main.scss",
"assets/stylesheets/overrides": "src/assets/stylesheets/overrides.scss",
"assets/stylesheets/palette": "src/assets/stylesheets/palette.scss"
2020-02-10 20:32:28 +03:00
},
output: {
path: path.resolve(__dirname, "material"),
filename: `[name]${hash}.js`,
hashDigestLength: 8,
libraryTarget: "window"
},
/* Plugins */
2020-02-10 20:32:28 +03:00
plugins: [
...base.plugins,
/* Stylesheets */
new MiniCssExtractPlugin({
filename: `[name]${hash}.css`
}),
2020-06-21 12:23:05 +03:00
/* Improve performance by skipping dependencies in watch mode */
...args.watch ? [] : [
2020-02-10 20:32:28 +03:00
/* FontAwesome icons */
new CopyPlugin({
patterns: [
{ to: ".icons/fontawesome", from: "**/*.svg" },
{ to: ".icons/fontawesome", from: "../LICENSE.txt" }
].map(pattern => ({
context: "node_modules/@fortawesome/fontawesome-free/svgs",
...pattern
}))
}),
/* Material Design icons */
new CopyPlugin({
patterns: [
2020-07-24 13:29:33 +03:00
{ to: ".icons/material", from: "*.svg" },
{ to: ".icons/material", from: "../LICENSE" }
].map(pattern => ({
context: "node_modules/@mdi/svg/svg",
...pattern
}))
}),
/* GitHub octicons */
new CopyPlugin({
patterns: [
{ to: ".icons/octicons", from: "*.svg" },
{ to: ".icons/octicons", from: "../../LICENSE" }
].map(pattern => ({
context: "node_modules/@primer/octicons/build/svg",
...pattern
}))
2020-06-21 12:23:05 +03:00
}),
/* Search stemmers and segmenters */
new CopyPlugin({
patterns: [
{ to: "assets/javascripts/lunr", from: "min/*.js" },
{
to: "assets/javascripts/lunr/tinyseg.min.js",
from: "tinyseg.js",
transform: (content: Buffer) => minjs(`${content}`).code!
}
].map(pattern => ({
context: "node_modules/lunr-languages",
...pattern
}))
}),
/* Assets and configuration */
new CopyPlugin({
patterns: [
{ from: ".icons/*.svg" },
{ from: "assets/images/*" },
{ from: "**/*.{py,yml}" }
].map(pattern => ({
context: "src",
...pattern
}))
})
],
/* Template files */
2020-05-29 14:51:25 +03:00
new CopyPlugin({
patterns: [
{
from: "**/*.html",
transform: (content: Buffer) => {
const metadata = require("./package.json")
const banner =
"{#-\n" +
" This file was automatically generated - do not edit\n" +
"-#}\n"
2020-04-08 17:25:25 +03:00
2020-05-29 14:51:25 +03:00
/* Normalize line feeds and minify HTML */
const html = content.toString().replace(/\r\n/gm, "\n")
return banner + minhtml(html, {
collapseBooleanAttributes: true,
includeAutoGeneratedTags: false,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
})
2020-02-10 20:32:28 +03:00
2020-05-29 14:51:25 +03:00
/* Remove empty lines without collapsing everything */
.replace(/^\s*[\r\n]/gm, "")
2020-02-10 20:32:28 +03:00
2020-05-29 14:51:25 +03:00
/* Write theme version into template */
.replace("$md-name$", metadata.name)
.replace("$md-version$", metadata.version)
}
2020-02-10 20:32:28 +03:00
}
2020-05-29 14:51:25 +03:00
].map(pattern => ({
context: "src",
...pattern
}))
2020-02-10 20:32:28 +03:00
}),
/* Hooks */
2020-02-10 20:32:28 +03:00
new EventHooksPlugin({
afterEmit: () => {
/* Replace asset URLs in templates */
if (args.mode === "production") {
const manifest = require("./material/assets/manifest.json")
for (const file of [
"material/base.html",
"material/overrides/main.html"
]) {
const template = toPairs<string>(manifest)
.reduce((content, [from, to]) => {
return content.replace(new RegExp(from, "g"), to)
}, fs.readFileSync(file, "utf8"))
/* Save template with replaced assets */
fs.writeFileSync(file, template, "utf8")
}
}
2020-02-10 20:32:28 +03:00
}
2020-04-27 15:04:56 +03:00
}),
/* Minify SVGs */
new ImageminPlugin({
svgo: {
plugins: [
{ removeDimensions: true },
{ removeViewBox: false }
]
}
2020-02-10 20:32:28 +03:00
})
],
/* Optimizations */
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: "assets/javascripts/vendor",
chunks: "all"
}
}
}
}
2020-02-10 20:32:28 +03:00
},
/* Search worker */
{
...base,
entry: {
"assets/javascripts/worker/search":
2020-03-27 17:29:17 +03:00
"src/assets/javascripts/integrations/search/worker/main"
2020-02-10 20:32:28 +03:00
},
output: {
path: path.resolve(__dirname, "material"),
filename: `[name]${hash}.js`,
hashDigestLength: 8,
libraryTarget: "var"
2020-02-19 17:45:52 +03:00
}
}
2020-02-10 20:32:28 +03:00
]
}