Skip to content

Commit

Permalink
build: simplify minifying script
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 19, 2024
1 parent 52d04ff commit dda8469
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 0 additions & 4 deletions build.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
// TODO: fix issue in unbuild with inlining package.json
// `Inlined implicit external D:\a\color-mode\color-mode\package.json`
failOnWarn: false,
entries: ['src/script'],
hooks: {
'rollup:options' (_ctx, options) {
options.treeshake = false
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dist"
],
"scripts": {
"prepack": "nuxt-module-build build && esbuild --minify dist/script.mjs --outfile=dist/script.min.js",
"prepack": "nuxt-module-build build && esbuild --minify src/script.js --outfile=dist/script.min.js",
"build": "pnpm run prepack",
"dev": "nuxi dev playground",
"dev:build": "nuxi build playground",
Expand Down
16 changes: 10 additions & 6 deletions src/script.ts → src/script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

// Add dark / light detection that runs before loading Nuxt
(() => {
// Global variable minimizers
Expand All @@ -16,7 +18,7 @@

addColorScheme(value)

// @ts-ignore

w['<%= options.globalName %>'] = {
preference,
value,
Expand All @@ -25,7 +27,7 @@
removeColorScheme
}

// @ts-ignore
/** @param {string} value */
function addColorScheme (value) {
const className = '<%= options.classPrefix %>' + value + '<%= options.classSuffix %>'
const dataValue = '<%= options.dataValue %>'
Expand All @@ -39,7 +41,7 @@
}
}

// @ts-ignore
/** @param {string} value */
function removeColorScheme (value) {
const className = '<%= options.classPrefix %>' + value + '<%= options.classSuffix %>'
const dataValue = '<%= options.dataValue %>'
Expand All @@ -53,14 +55,16 @@
}
}

// @ts-ignore
/** @param {string} suffix */
function prefersColorScheme (suffix) {
return w.matchMedia('(prefers-color-scheme' + suffix + ')')
}

function getColorScheme () {
// @ts-ignore
if (w.matchMedia && prefersColorScheme('').media !== 'not all') {
if (
// @ts-expect-error TS assumes matchMedia is always defined
w.matchMedia
&& prefersColorScheme('').media !== 'not all') {
for (const colorScheme of knownColorSchemes) {
if (prefersColorScheme(':' + colorScheme).matches) {
return colorScheme
Expand Down
2 changes: 1 addition & 1 deletion src/script.min.js

0 comments on commit dda8469

Please sign in to comment.