-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: eleventy plugin #46
base: next
Are you sure you want to change the base?
Changes from all commits
554217a
3373be0
012915f
a39858b
20ae77f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,11 +30,12 @@ if (!platformInfo.development) { | |
|
||
const postCss = /* @__PURE__ */ postcss(postCssPlugins); | ||
|
||
export async function postcssBuild(): Promise<void> { | ||
logger.logMethod?.('postcssBuild'); | ||
export async function postcssBuild(options: {inputDir: string, outputDir: string}): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use defined type comepelte docs for all functions |
||
postCssPlugins[0] = /* @__PURE__ */ postcssImport({ root: options.inputDir }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. این چیه؟ |
||
|
||
const inputDir = basePath; | ||
const outputDir = 'dist/css/'; | ||
logger.logMethod?.('postcssBuild'); | ||
const inputDir = options.inputDir; | ||
const outputDir = options.outputDir; | ||
const startTime = Date.now(); | ||
|
||
if (!existsSync(outputDir)) { | ||
|
@@ -88,3 +89,50 @@ export async function postcssBuild(): Promise<void> { | |
const calculatedTime = String(endTime - startTime); | ||
logger.logOther?.(`PostCSS build done in ${calculatedTime}ms`); | ||
} | ||
|
||
/** | ||
* Options for the eleventyMinifyHtmlPlugin. | ||
*/ | ||
type EleventyPostCssPluginOptions = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. تایپ ها باید بالای فایل تعریف بشن |
||
/** | ||
* The Css Input Directory. | ||
*/ | ||
inputDir: string; | ||
|
||
/** | ||
* The Css Output Directory. | ||
*/ | ||
outputDir: string; | ||
}; | ||
|
||
/** | ||
* Eleventy plugin for Building Css with PostCss. | ||
* | ||
* @param eleventyConfig - The Eleventy configuration object. | ||
* @param options - The options for the plugin. | ||
* | ||
* @example | ||
* ```js | ||
* // eleventy.config.mjs | ||
* | ||
* import {eleventyPostCssBuildPlugin} from '@nexim/eleventy-config'; | ||
* | ||
* export default function (eleventyConfig) { | ||
* eleventyConfig.addPlugin(eleventyPostCssBuildPlugin, {outputDir: 'dist/css', inputDir: 'style'}); | ||
* // ... | ||
* } | ||
* ``` | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function eleventyPostCssBuildPlugin(eleventyConfig: any, options: EleventyPostCssPluginOptions): void { | ||
// TODO: better event handling to just copy for the first time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ؟ |
||
|
||
const configureBuilding = postcssBuild({ | ||
inputDir: options.inputDir, | ||
outputDir: options.outputDir, | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access | ||
eleventyConfig.on('eleventy.after', configureBuilding); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,29 +3,32 @@ import { logger } from './logger.js'; | |
import { platformInfo } from '@alwatr/platform-info'; | ||
import { writeFile } from 'fs/promises'; | ||
|
||
const deploymentServiceWorkerContent = "console.log('service worker not build in deployment.')"; | ||
const serviceWorkerDest = 'dist/service-worker.js'; | ||
|
||
export async function generateServiceWorker(): Promise<BuildResult | null> { | ||
export async function generateServiceWorker(options:{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doc |
||
outputDir: string, | ||
deploymentServiceWorkerContent: string, | ||
nameOfServiceWorker: string, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. service worker path for both outdir and name |
||
maximumFileSize: number, | ||
mode: 'production' | 'development', | ||
}): Promise<BuildResult | null> { | ||
const isDevelopment = platformInfo.development; | ||
logger.logMethodArgs?.('generateServiceWorker', { isDevelopment }); | ||
|
||
if (isDevelopment) { | ||
await writeFile(serviceWorkerDest, deploymentServiceWorkerContent); | ||
await writeFile(options.outputDir + options.nameOfServiceWorker, options.deploymentServiceWorkerContent); | ||
return null; | ||
} | ||
|
||
const buildResult = await generateSW({ | ||
globDirectory: 'dist', | ||
maximumFileSizeToCacheInBytes: 1 * 1024 * 1024, // 1MB | ||
globDirectory: options.outputDir, | ||
maximumFileSizeToCacheInBytes: options.maximumFileSize, | ||
cleanupOutdatedCaches: true, | ||
inlineWorkboxRuntime: false, | ||
clientsClaim: true, | ||
skipWaiting: true, | ||
globPatterns: [ '**/*.{woff,woff2,js,css,webmanifest,html}', 'index.html', 'favicon.ico' ], | ||
swDest: serviceWorkerDest, | ||
swDest: options.outputDir + options.nameOfServiceWorker, | ||
sourcemap: false, | ||
mode: 'production', | ||
mode: options.mode, | ||
runtimeCaching: [ | ||
{ | ||
urlPattern: /\.(?:png|jpg|jpeg|svg|gif)$/, | ||
|
@@ -69,3 +72,73 @@ export async function generateServiceWorker(): Promise<BuildResult | null> { | |
logger.logOther?.(`Generated a service worker, which will pre-cache ${count} files, totaling ${preCacheSize}kb.`); | ||
return buildResult; | ||
} | ||
|
||
/** | ||
* Options for the eleventyCopyFontPlugin. | ||
*/ | ||
export type EleventyWorkBoxPluginOptions = { | ||
/** | ||
* The output directory for the service worker. | ||
*/ | ||
outputDir: string; | ||
|
||
/** | ||
* The content for the deployment service worker. | ||
*/ | ||
deploymentServiceWorkerContent: string, | ||
|
||
/** | ||
* The name of the service worker. | ||
*/ | ||
nameOfServiceWorker: string, | ||
|
||
/** | ||
* The maximum file size for the service worker. | ||
*/ | ||
maximumFileSize: number, | ||
|
||
/** | ||
* The mode for the service worker. | ||
*/ | ||
mode: 'production' | 'development', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. لازم نیست مود رو از خودش بپرسی |
||
}; | ||
|
||
/** | ||
* Eleventy plugin for minifying HTML content. | ||
* | ||
* @param eleventyConfig - The Eleventy configuration object. | ||
* @param options - The options for the plugin. | ||
* | ||
* @example | ||
* ```js | ||
* // eleventy.config.mjs | ||
* | ||
* import {eleventyWorkBoxPlugin} from '@nexim/eleventy-config'; | ||
* | ||
* export default function (eleventyConfig) { | ||
* eleventyConfig.addPlugin(eleventyWorkBoxPlugin, { | ||
* outputDir: 'dist', | ||
* deploymentServiceWorkerContent: "console.log('service worker not build in deployment.')", | ||
* nameOfServiceWorker: 'service-worker.js', | ||
* maximumFileSize: 1 * 1024 * 1024, // 1MB, | ||
* mode: 'production', | ||
* }); | ||
* // ... | ||
* } | ||
* ``` | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function eleventyWorkBoxPlugin(eleventyConfig: any, options: EleventyWorkBoxPluginOptions): void { | ||
// TODO: better event handling to just copy for the first time | ||
|
||
const generateServiceWorkerWithOptions = generateServiceWorker({ | ||
outputDir: options.outputDir, | ||
deploymentServiceWorkerContent: options.deploymentServiceWorkerContent, | ||
nameOfServiceWorker: options.nameOfServiceWorker, | ||
maximumFileSize: options.maximumFileSize, | ||
mode: options.mode, | ||
}); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access | ||
eleventyConfig.on('eleventy.after', generateServiceWorkerWithOptions); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
این فایل خیلی مهمه
هر کار خاصی رو انجام میده باید بکشیم بیرون
اپشن هم خالیه
خواستی بری سراغش بپرس