Skip to content

Commit

Permalink
css: use esbuild's new cssBundle field to get related css bundles (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
craftamap committed Dec 28, 2022
1 parent 9489cb7 commit f9c1d12
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 25 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,14 @@ interface HtmlFileConfiguration {
// Decide if the script tag will be inserted as blocking script tag,
// with `defer=""` (default) or with `type="module"`
favicon?: string, // path to favicon.ico. If not specified, no favicon will be injected
findRelatedOutputFiles?: boolean,
// Find related output (*.css)-files and inject them into the html.
findRelatedCssFiles?: boolean,
// Find related output *.css-files and inject them into the html.
// Defaults to true.
findRelatedOutputFiles?: boolean,
// Find output files following the same name schema of the output file
// like (*.css)-files and inject them into the html. This option is deprecated,
// consider using findRelatedCssFiles.
// Defaults to false.
extraScripts?: (string | { // accepts an array of src strings or objects with src and attributes
src: string; // src to use for the script
attrs?: { [key: string]: string } // attributes to append to the script, e.g. { type: 'module', async: true }
Expand Down
1 change: 1 addition & 0 deletions lib/cjs/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface HtmlFileConfiguration {
define?: Record<string, string>;
scriptLoading?: 'blocking' | 'defer' | 'module';
favicon?: string;
findRelatedCssFiles?: boolean;
findRelatedOutputFiles?: boolean;
extraScripts?: (string | {
src: string;
Expand Down
21 changes: 13 additions & 8 deletions lib/cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function escapeRegExp(text) {
}
const htmlPlugin = (configuration = { files: [], }) => {
configuration.files = configuration.files.map((htmlFileConfiguration) => {
return Object.assign({}, { 'findRelatedOutputFiles': true }, htmlFileConfiguration); // Set default values
return Object.assign({}, { findRelatedOutputFiles: false, findRelatedCssFiles: true }, htmlFileConfiguration); // Set default values
});
let logInfo = false;
function collectEntrypoints(htmlFileConfiguration, metafile) {
Expand All @@ -51,7 +51,7 @@ const htmlPlugin = (configuration = { files: [], }) => {
});
return entryPoints;
}
function findRelatedOutputFiles(entrypoint, metafile, entryNames) {
function findNameRelatedOutputFiles(entrypoint, metafile, entryNames) {
var _a, _b;
const pathOfMatchedOutput = path_1.default.parse(entrypoint.path);
// Search for all files that are "related" to the output (.css and map files, for example files, as assets are dealt with otherwise).
Expand Down Expand Up @@ -190,14 +190,19 @@ const htmlPlugin = (configuration = { files: [], }) => {
if (!entrypoint) {
throw new Error(`Found no match for ${htmlFileConfiguration.entryPoints}`);
}
let relatedOutputFiles;
if (htmlFileConfiguration.findRelatedOutputFiles) {
relatedOutputFiles = findRelatedOutputFiles(entrypoint, result.metafile, build.initialOptions.entryNames);
const relatedOutputFiles = new Map();
relatedOutputFiles.set(entrypoint.path, entrypoint);
if (htmlFileConfiguration.findRelatedCssFiles) {
if (entrypoint === null || entrypoint === void 0 ? void 0 : entrypoint.cssBundle) {
relatedOutputFiles.set(entrypoint.cssBundle, { path: entrypoint === null || entrypoint === void 0 ? void 0 : entrypoint.cssBundle });
}
}
else {
relatedOutputFiles = [entrypoint];
if (htmlFileConfiguration.findRelatedOutputFiles) {
findNameRelatedOutputFiles(entrypoint, result.metafile, build.initialOptions.entryNames).forEach((item) => {
relatedOutputFiles.set(item.path, item);
});
}
collectedOutputFiles = [...collectedOutputFiles, ...relatedOutputFiles];
collectedOutputFiles = [...collectedOutputFiles, ...relatedOutputFiles.values()];
}
// Note: we can safely disable this rule here, as we already asserted this in setup.onStart
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"lib/"
],
"peerDependencies": {
"esbuild": ">=0.12.28"
"esbuild": ">=0.15.10"
},
"devDependencies": {
"@tsconfig/node12": "^1.0.9",
Expand All @@ -25,7 +25,7 @@
"@types/node": "^16.9.1",
"@typescript-eslint/eslint-plugin": "^5.10.1",
"@typescript-eslint/parser": "^5.10.1",
"esbuild": "^0.12.28",
"esbuild": "^0.15.10",
"eslint": "^8.8.0",
"typescript": "^4.5.5"
},
Expand Down
25 changes: 16 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export interface HtmlFileConfiguration {
define?: Record<string, string>,
scriptLoading?: 'blocking' | 'defer' | 'module',
favicon?: string,
findRelatedCssFiles?: boolean,
findRelatedOutputFiles?: boolean,
extraScripts?: (string | {
extraScripts?: (string | {
src: string,
attrs?: { [key: string]: string }
})[]
Expand Down Expand Up @@ -56,7 +57,7 @@ function escapeRegExp(text: string): string {

export const htmlPlugin = (configuration: Configuration = { files: [], }): esbuild.Plugin => {
configuration.files = configuration.files.map((htmlFileConfiguration: HtmlFileConfiguration) => {
return Object.assign({}, { 'findRelatedOutputFiles': true }, htmlFileConfiguration) // Set default values
return Object.assign({}, { findRelatedOutputFiles: false, findRelatedCssFiles: true }, htmlFileConfiguration) // Set default values
})

let logInfo = false
Expand All @@ -74,7 +75,7 @@ export const htmlPlugin = (configuration: Configuration = { files: [], }): esbui
return entryPoints
}

function findRelatedOutputFiles(entrypoint: { path: string }, metafile?: esbuild.Metafile, entryNames?: string) {
function findNameRelatedOutputFiles(entrypoint: { path: string }, metafile?: esbuild.Metafile, entryNames?: string) {
const pathOfMatchedOutput = path.parse(entrypoint.path)

// Search for all files that are "related" to the output (.css and map files, for example files, as assets are dealt with otherwise).
Expand Down Expand Up @@ -131,7 +132,7 @@ export const htmlPlugin = (configuration: Configuration = { files: [], }): esbui
// https://github.com/evanw/esbuild/blob/a1ff9d144cdb8d50ea2fa79a1d11f43d5bd5e2d8/internal/bundler/bundler.go#L533
function joinWithPublicPath(publicPath: string, relPath: string) {
relPath = path.normalize(relPath)

if (!publicPath) {
publicPath = '.'
}
Expand Down Expand Up @@ -222,14 +223,20 @@ export const htmlPlugin = (configuration: Configuration = { files: [], }): esbui
if (!entrypoint) {
throw new Error(`Found no match for ${htmlFileConfiguration.entryPoints}`)
}
let relatedOutputFiles
const relatedOutputFiles = new Map()
relatedOutputFiles.set(entrypoint.path, entrypoint)
if (htmlFileConfiguration.findRelatedCssFiles) {
if (entrypoint?.cssBundle) {
relatedOutputFiles.set(entrypoint.cssBundle, { path: entrypoint?.cssBundle })
}
}
if (htmlFileConfiguration.findRelatedOutputFiles) {
relatedOutputFiles = findRelatedOutputFiles(entrypoint, result.metafile, build.initialOptions.entryNames)
} else {
relatedOutputFiles = [entrypoint]
findNameRelatedOutputFiles(entrypoint, result.metafile, build.initialOptions.entryNames).forEach((item) => {
relatedOutputFiles.set(item.path, item)
})
}

collectedOutputFiles = [...collectedOutputFiles, ...relatedOutputFiles]
collectedOutputFiles = [...collectedOutputFiles, ...relatedOutputFiles.values()]
}
// Note: we can safely disable this rule here, as we already asserted this in setup.onStart
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down
141 changes: 137 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
# yarn lockfile v1


"@esbuild/android-arm@0.15.10":
version "0.15.10"
resolved "https://registry.yarnpkg.com/@esbuild/android-arm/-/android-arm-0.15.10.tgz#a5f9432eb221afc243c321058ef25fe899886892"
integrity sha512-FNONeQPy/ox+5NBkcSbYJxoXj9GWu8gVGJTVmUyoOCKQFDTrHVKgNSzChdNt0I8Aj/iKcsDf2r9BFwv+FSNUXg==

"@esbuild/linux-loong64@0.15.10":
version "0.15.10"
resolved "https://registry.yarnpkg.com/@esbuild/linux-loong64/-/linux-loong64-0.15.10.tgz#78a42897c2cf8db9fd5f1811f7590393b77774c7"
integrity sha512-w0Ou3Z83LOYEkwaui2M8VwIp+nLi/NA60lBLMvaJ+vXVMcsARYdEzLNE7RSm4+lSg4zq4d7fAVuzk7PNQ5JFgg==

"@eslint/eslintrc@^1.0.5":
version "1.0.5"
resolved "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-1.0.5.tgz"
Expand Down Expand Up @@ -412,10 +422,133 @@ domexception@^2.0.1:
dependencies:
webidl-conversions "^5.0.0"

esbuild@^0.12.28:
version "0.12.28"
resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.12.28.tgz"
integrity sha512-pZ0FrWZXlvQOATlp14lRSk1N9GkeJ3vLIwOcUoo3ICQn9WNR4rWoNi81pbn6sC1iYUy7QPqNzI3+AEzokwyVcA==
esbuild-android-64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-android-64/-/esbuild-android-64-0.15.10.tgz#8a59a84acbf2eca96996cadc35642cf055c494f0"
integrity sha512-UI7krF8OYO1N7JYTgLT9ML5j4+45ra3amLZKx7LO3lmLt1Ibn8t3aZbX5Pu4BjWiqDuJ3m/hsvhPhK/5Y/YpnA==

esbuild-android-arm64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-android-arm64/-/esbuild-android-arm64-0.15.10.tgz#f453851dc1d8c5409a38cf7613a33852faf4915d"
integrity sha512-EOt55D6xBk5O05AK8brXUbZmoFj4chM8u3riGflLa6ziEoVvNjRdD7Cnp82NHQGfSHgYR06XsPI8/sMuA/cUwg==

esbuild-darwin-64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-darwin-64/-/esbuild-darwin-64-0.15.10.tgz#778bd29c8186ff47b176c8af58c08cf0fb8e6b86"
integrity sha512-hbDJugTicqIm+WKZgp208d7FcXcaK8j2c0l+fqSJ3d2AzQAfjEYDRM3Z2oMeqSJ9uFxyj/muSACLdix7oTstRA==

esbuild-darwin-arm64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.15.10.tgz#b30bbefb46dc3c5d4708b0435e52f6456578d6df"
integrity sha512-M1t5+Kj4IgSbYmunf2BB6EKLkWUq+XlqaFRiGOk8bmBapu9bCDrxjf4kUnWn59Dka3I27EiuHBKd1rSO4osLFQ==

esbuild-freebsd-64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-freebsd-64/-/esbuild-freebsd-64-0.15.10.tgz#ab301c5f6ded5110dbdd611140bef1a7c2e99236"
integrity sha512-KMBFMa7C8oc97nqDdoZwtDBX7gfpolkk6Bcmj6YFMrtCMVgoU/x2DI1p74DmYl7CSS6Ppa3xgemrLrr5IjIn0w==

esbuild-freebsd-arm64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-freebsd-arm64/-/esbuild-freebsd-arm64-0.15.10.tgz#a5b09b867a6ff49110f52343b6f12265db63d43f"
integrity sha512-m2KNbuCX13yQqLlbSojFMHpewbn8wW5uDS6DxRpmaZKzyq8Dbsku6hHvh2U+BcLwWY4mpgXzFUoENEf7IcioGg==

esbuild-linux-32@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-linux-32/-/esbuild-linux-32-0.15.10.tgz#5282fe9915641caf9c8070e4ba2c3e16d358f837"
integrity sha512-guXrwSYFAvNkuQ39FNeV4sNkNms1bLlA5vF1H0cazZBOLdLFIny6BhT+TUbK/hdByMQhtWQ5jI9VAmPKbVPu1w==

esbuild-linux-64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-linux-64/-/esbuild-linux-64-0.15.10.tgz#f3726e85a00149580cb19f8abfabcbb96f5d52bb"
integrity sha512-jd8XfaSJeucMpD63YNMO1JCrdJhckHWcMv6O233bL4l6ogQKQOxBYSRP/XLWP+6kVTu0obXovuckJDcA0DKtQA==

esbuild-linux-arm64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-linux-arm64/-/esbuild-linux-arm64-0.15.10.tgz#2f0056e9d5286edb0185b56655caa8c574d8dbe7"
integrity sha512-GByBi4fgkvZFTHFDYNftu1DQ1GzR23jws0oWyCfhnI7eMOe+wgwWrc78dbNk709Ivdr/evefm2PJiUBMiusS1A==

esbuild-linux-arm@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-linux-arm/-/esbuild-linux-arm-0.15.10.tgz#40a9270da3c8ffa32cf72e24a79883e323dff08d"
integrity sha512-6N8vThLL/Lysy9y4Ex8XoLQAlbZKUyExCWyayGi2KgTBelKpPgj6RZnUaKri0dHNPGgReJriKVU6+KDGQwn10A==

esbuild-linux-mips64le@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-linux-mips64le/-/esbuild-linux-mips64le-0.15.10.tgz#90ce1c4ee0202edb4ac69807dea77f7e5804abc4"
integrity sha512-BxP+LbaGVGIdQNJUNF7qpYjEGWb0YyHVSKqYKrn+pTwH/SiHUxFyJYSP3pqkku61olQiSBnSmWZ+YUpj78Tw7Q==

esbuild-linux-ppc64le@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-linux-ppc64le/-/esbuild-linux-ppc64le-0.15.10.tgz#782837ae7bd5b279178106c9dd801755a21fabdf"
integrity sha512-LoSQCd6498PmninNgqd/BR7z3Bsk/mabImBWuQ4wQgmQEeanzWd5BQU2aNi9mBURCLgyheuZS6Xhrw5luw3OkQ==

esbuild-linux-riscv64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-linux-riscv64/-/esbuild-linux-riscv64-0.15.10.tgz#d7420d806ece5174f24f4634303146f915ab4207"
integrity sha512-Lrl9Cr2YROvPV4wmZ1/g48httE8z/5SCiXIyebiB5N8VT7pX3t6meI7TQVHw/wQpqP/AF4SksDuFImPTM7Z32Q==

esbuild-linux-s390x@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-linux-s390x/-/esbuild-linux-s390x-0.15.10.tgz#21fdf0cb3494a7fb520a71934e4dffce67fe47be"
integrity sha512-ReP+6q3eLVVP2lpRrvl5EodKX7EZ1bS1/z5j6hsluAlZP5aHhk6ghT6Cq3IANvvDdscMMCB4QEbI+AjtvoOFpA==

esbuild-netbsd-64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-netbsd-64/-/esbuild-netbsd-64-0.15.10.tgz#6c06b3107e3df53de381e6299184d4597db0440f"
integrity sha512-iGDYtJCMCqldMskQ4eIV+QSS/CuT7xyy9i2/FjpKvxAuCzrESZXiA1L64YNj6/afuzfBe9i8m/uDkFHy257hTw==

esbuild-openbsd-64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-openbsd-64/-/esbuild-openbsd-64-0.15.10.tgz#4daef5f5d8e74bbda53b65160029445d582570cf"
integrity sha512-ftMMIwHWrnrYnvuJQRJs/Smlcb28F9ICGde/P3FUTCgDDM0N7WA0o9uOR38f5Xe2/OhNCgkjNeb7QeaE3cyWkQ==

esbuild-sunos-64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-sunos-64/-/esbuild-sunos-64-0.15.10.tgz#5fe7bef267a02f322fd249a8214d0274937388a7"
integrity sha512-mf7hBL9Uo2gcy2r3rUFMjVpTaGpFJJE5QTDDqUFf1632FxteYANffDZmKbqX0PfeQ2XjUDE604IcE7OJeoHiyg==

esbuild-windows-32@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-windows-32/-/esbuild-windows-32-0.15.10.tgz#48e3dde25ab0135579a288b30ab6ddef6d1f0b28"
integrity sha512-ttFVo+Cg8b5+qHmZHbEc8Vl17kCleHhLzgT8X04y8zudEApo0PxPg9Mz8Z2cKH1bCYlve1XL8LkyXGFjtUYeGg==

esbuild-windows-64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-windows-64/-/esbuild-windows-64-0.15.10.tgz#387a9515bef3fee502d277a5d0a2db49a4ecda05"
integrity sha512-2H0gdsyHi5x+8lbng3hLbxDWR7mKHWh5BXZGKVG830KUmXOOWFE2YKJ4tHRkejRduOGDrBvHBriYsGtmTv3ntA==

esbuild-windows-arm64@0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild-windows-arm64/-/esbuild-windows-arm64-0.15.10.tgz#5a6fcf2fa49e895949bf5495cf088ab1b43ae879"
integrity sha512-S+th4F+F8VLsHLR0zrUcG+Et4hx0RKgK1eyHc08kztmLOES8BWwMiaGdoW9hiXuzznXQ0I/Fg904MNbr11Nktw==

esbuild@^0.15.10:
version "0.15.10"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.15.10.tgz#85c2f8446e9b1fe04fae68daceacba033eedbd42"
integrity sha512-N7wBhfJ/E5fzn/SpNgX+oW2RLRjwaL8Y0ezqNqhjD6w0H2p0rDuEz2FKZqpqLnO8DCaWumKe8dsC/ljvVSSxng==
optionalDependencies:
"@esbuild/android-arm" "0.15.10"
"@esbuild/linux-loong64" "0.15.10"
esbuild-android-64 "0.15.10"
esbuild-android-arm64 "0.15.10"
esbuild-darwin-64 "0.15.10"
esbuild-darwin-arm64 "0.15.10"
esbuild-freebsd-64 "0.15.10"
esbuild-freebsd-arm64 "0.15.10"
esbuild-linux-32 "0.15.10"
esbuild-linux-64 "0.15.10"
esbuild-linux-arm "0.15.10"
esbuild-linux-arm64 "0.15.10"
esbuild-linux-mips64le "0.15.10"
esbuild-linux-ppc64le "0.15.10"
esbuild-linux-riscv64 "0.15.10"
esbuild-linux-s390x "0.15.10"
esbuild-netbsd-64 "0.15.10"
esbuild-openbsd-64 "0.15.10"
esbuild-sunos-64 "0.15.10"
esbuild-windows-32 "0.15.10"
esbuild-windows-64 "0.15.10"
esbuild-windows-arm64 "0.15.10"

escape-string-regexp@^4.0.0:
version "4.0.0"
Expand Down

0 comments on commit f9c1d12

Please sign in to comment.