1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-12-01 02:27:17 +01:00
mkdocs-material/tools/build/transform/index.ts

206 lines
5.6 KiB
TypeScript
Raw Normal View History

2021-02-20 18:03:53 +01:00
/*
* Copyright (c) 2016-2021 Martin Donath <martin.donath@squidfunk.com>
*
* 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.
*/
2021-02-22 18:19:00 +01:00
import { createHash } from "crypto"
import { build as esbuild } from "esbuild"
2021-02-20 18:03:53 +01:00
import * as path from "path"
import postcss from "postcss"
2021-02-22 18:19:00 +01:00
import {
2021-02-27 18:21:02 +01:00
NEVER,
2021-02-22 18:19:00 +01:00
Observable,
concat,
defer,
merge,
of
} from "rxjs"
2021-02-20 18:03:53 +01:00
import {
2021-02-27 18:21:02 +01:00
catchError,
2021-02-20 18:03:53 +01:00
endWith,
ignoreElements,
2021-02-21 11:59:38 +01:00
switchMap
2021-02-20 18:03:53 +01:00
} from "rxjs/operators"
import { render as sass } from "sass"
import { promisify } from "util"
2021-02-26 16:14:18 +01:00
import { base, mkdir, write } from "../_"
2021-02-20 18:03:53 +01:00
/* ----------------------------------------------------------------------------
* Helper types
* ------------------------------------------------------------------------- */
/**
* Transform options
*/
interface TransformOptions {
2021-02-22 18:19:00 +01:00
from: string /* Source destination */
to: string /* Target destination */
2021-02-20 18:03:53 +01:00
}
/* ----------------------------------------------------------------------------
* Data
* ------------------------------------------------------------------------- */
/**
2021-02-22 18:19:00 +01:00
* Base directory for source map resolution
2021-02-20 18:03:53 +01:00
*/
const root = new RegExp(`file://${path.resolve(".")}/`, "g")
2021-02-22 18:19:00 +01:00
/* ----------------------------------------------------------------------------
* Helper functions
* ------------------------------------------------------------------------- */
/**
* Compute a digest for cachebusting a file
*
* @param file - File
* @param data - File data
*
* @returns File with digest
*/
function digest(file: string, data: string): string {
2021-02-26 16:14:18 +01:00
if (process.argv.includes("--optimize")) {
const hash = createHash("sha256").update(data).digest("hex")
return file.replace(/\b(?=\.)/, `.${hash.slice(0, 8)}.min`)
} else {
return file
}
2021-02-22 18:19:00 +01:00
}
2021-02-20 18:03:53 +01:00
/* ----------------------------------------------------------------------------
* Functions
* ------------------------------------------------------------------------- */
/**
* Transform a stylesheet
*
* @param options - Options
*
* @returns File observable
*/
export function transformStyle(
2021-02-22 18:19:00 +01:00
options: TransformOptions
2021-02-20 18:03:53 +01:00
): Observable<string> {
return defer(() => promisify(sass)({
2021-02-22 18:19:00 +01:00
file: options.from,
outFile: options.to,
2021-02-20 18:03:53 +01:00
includePaths: [
"src/assets/stylesheets",
"node_modules/modularscale-sass/stylesheets",
"node_modules/material-design-color",
"node_modules/material-shadows"
],
sourceMap: true,
2021-02-22 18:19:00 +01:00
sourceMapContents: true
2021-02-20 18:03:53 +01:00
}))
.pipe(
switchMap(({ css, map }) => postcss([
require("autoprefixer"),
require("postcss-inline-svg")({
paths: [
`${base}/.icons`
],
encode: false
}),
...process.argv.includes("--optimize")
? [require("cssnano")]
: []
2021-02-20 18:03:53 +01:00
])
.process(css, {
2021-02-22 18:19:00 +01:00
from: options.from,
2021-02-20 18:03:53 +01:00
map: {
2021-02-22 18:19:00 +01:00
prev: `${map}`,
2021-02-20 18:03:53 +01:00
inline: false
}
})
),
2021-02-27 18:21:02 +01:00
catchError(err => {
console.log(err.formatted || err.message)
return NEVER
}),
2021-02-22 18:19:00 +01:00
switchMap(({ css, map }) => {
const file = digest(options.to, css)
return concat(
mkdir(path.dirname(file)),
2021-02-26 16:14:18 +01:00
merge(
write(`${file}.map`, `${map}`.replace(root, "")),
write(`${file}`, css.replace(
2021-02-22 18:19:00 +01:00
options.from,
path.basename(file)
)),
2021-02-26 16:14:18 +01:00
)
2021-02-22 18:19:00 +01:00
)
.pipe(
ignoreElements(),
endWith(file)
)
})
2021-02-20 18:03:53 +01:00
)
}
2021-02-20 18:46:28 +01:00
/**
* Transform a script
*
* @param options - Options
*
* @returns File observable
*/
export function transformScript(
2021-02-22 18:19:00 +01:00
options: TransformOptions
2021-02-20 18:46:28 +01:00
): Observable<string> {
2021-02-22 18:19:00 +01:00
return defer(() => esbuild({
entryPoints: [options.from],
2021-02-26 12:31:05 +01:00
target: "es2015",
2021-02-22 18:19:00 +01:00
write: false,
2021-02-20 18:46:28 +01:00
bundle: true,
sourcemap: true,
legalComments: "inline",
minify: process.argv.includes("--optimize")
2021-02-20 18:46:28 +01:00
}))
.pipe(
2021-02-22 18:19:00 +01:00
switchMap(({ outputFiles: [file] }) => {
const contents = file.text.split("\n")
const [, data] = contents[contents.length - 2].split(",")
return of({
js: file.text,
map: Buffer.from(data, "base64")
})
}),
switchMap(({ js, map }) => {
const file = digest(options.to, js)
return concat(
mkdir(path.dirname(file)),
2021-02-26 16:14:18 +01:00
merge(
write(`${file}.map`, `${map}`),
write(`${file}`, js.replace(
2021-02-22 18:19:00 +01:00
/(sourceMappingURL=)(.*)/,
2021-02-25 15:56:08 +01:00
`$1${path.basename(file)}.map\n`
2021-02-22 18:19:00 +01:00
)),
2021-02-26 16:14:18 +01:00
)
2021-02-22 18:19:00 +01:00
)
.pipe(
ignoreElements(),
endWith(file)
)
})
2021-02-20 18:46:28 +01:00
)
}