esbuild-plugin-filelastmodified
Advanced tools
Comparing version 1.0.6 to 2.0.0
import fs from "fs"; | ||
const LOAD_NAMESPACE = "load_filelastmodified_namespace"; | ||
const onResolve = (identifier) => async (args) => { | ||
const find = new RegExp(identifier, "g"); | ||
if (!args.path.startsWith(".")) | ||
return; | ||
const source = (await fs.promises.readFile(args.path)).toString("utf-8"); | ||
if (!source.match(find)) | ||
return; | ||
const PLUGIN_NAMESPACE = "fileLastModified"; | ||
const onResolve = (args) => { | ||
return { | ||
path: args.path, | ||
namespace: LOAD_NAMESPACE, | ||
pluginData: { source, find } | ||
path: args.importer, | ||
namespace: PLUGIN_NAMESPACE | ||
}; | ||
}; | ||
const onLoad = (identifier) => async (args) => { | ||
const find = new RegExp(identifier, "g"); | ||
const source = (await fs.promises.readFile(args.path)).toString("utf-8"); | ||
if (!source.match(find)) | ||
return; | ||
const stats = fs.statSync(args.path); | ||
const contents = source.replace(find, stats.mtimeMs.toString()); | ||
return { contents }; | ||
const onLoad = async (args) => { | ||
const s = await fs.promises.stat(args.path); | ||
const fileLastModified = s.mtimeMs.toString(); | ||
return { | ||
contents: `export default ${fileLastModified};` | ||
}; | ||
}; | ||
const plugin = (options) => { | ||
const { identifier = "__fileLastModified__" } = options || {}; | ||
const { identifier = /__fileLastModified__/ } = options || {}; | ||
return { | ||
name: "esbuild-plugin-filelastmodified", | ||
setup(build) { | ||
build.onLoad({ filter: /.*/ }, onLoad(identifier)); | ||
build.onResolve({ filter: identifier }, onResolve); | ||
build.onLoad({ filter: /.*/, namespace: PLUGIN_NAMESPACE }, onLoad); | ||
} | ||
@@ -32,0 +24,0 @@ }; |
@@ -28,31 +28,23 @@ var __create = Object.create; | ||
var import_fs = __toModule(require("fs")); | ||
const LOAD_NAMESPACE = "load_filelastmodified_namespace"; | ||
const onResolve = (identifier) => async (args) => { | ||
const find = new RegExp(identifier, "g"); | ||
if (!args.path.startsWith(".")) | ||
return; | ||
const source = (await import_fs.default.promises.readFile(args.path)).toString("utf-8"); | ||
if (!source.match(find)) | ||
return; | ||
const PLUGIN_NAMESPACE = "fileLastModified"; | ||
const onResolve = (args) => { | ||
return { | ||
path: args.path, | ||
namespace: LOAD_NAMESPACE, | ||
pluginData: { source, find } | ||
path: args.importer, | ||
namespace: PLUGIN_NAMESPACE | ||
}; | ||
}; | ||
const onLoad = (identifier) => async (args) => { | ||
const find = new RegExp(identifier, "g"); | ||
const source = (await import_fs.default.promises.readFile(args.path)).toString("utf-8"); | ||
if (!source.match(find)) | ||
return; | ||
const stats = import_fs.default.statSync(args.path); | ||
const contents = source.replace(find, stats.mtimeMs.toString()); | ||
return { contents }; | ||
const onLoad = async (args) => { | ||
const s = await import_fs.default.promises.stat(args.path); | ||
const fileLastModified = s.mtimeMs.toString(); | ||
return { | ||
contents: `export default ${fileLastModified};` | ||
}; | ||
}; | ||
const plugin = (options) => { | ||
const { identifier = "__fileLastModified__" } = options || {}; | ||
const { identifier = /__fileLastModified__/ } = options || {}; | ||
return { | ||
name: "esbuild-plugin-filelastmodified", | ||
setup(build) { | ||
build.onLoad({ filter: /.*/ }, onLoad(identifier)); | ||
build.onResolve({ filter: identifier }, onResolve); | ||
build.onLoad({ filter: /.*/, namespace: PLUGIN_NAMESPACE }, onLoad); | ||
} | ||
@@ -59,0 +51,0 @@ }; |
{ | ||
"name": "esbuild-plugin-filelastmodified", | ||
"version": "1.0.6", | ||
"description": "A esbuild plugin to replace __fileLastModified__ with the actual time the file has been modified.", | ||
"version": "2.0.0", | ||
"description": "A esbuild plugin to import the last time the file has been modified.", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "module": "dist/index.esm.js", |
# esbuild-plugin-filelastmodified | ||
A esbuild plugin to replace `__fileLastModified__` with the date the file was last modified. | ||
A esbuild plugin to import the last time the file has been modified. | ||
## Why | ||
Avoid having to manually hardcode the date and change it every time you make an edit on your website pages. It's very usefull for displaying -> `Modified date: 2021-03-31` at the end of web pages. | ||
@@ -10,3 +11,3 @@ | ||
``` | ||
```js | ||
npm install esbuild-plugin-filelastmodified | ||
@@ -36,6 +37,5 @@ ``` | ||
import localizedFormat from 'dayjs/plugin/localizedFormat' | ||
import lastModified from '__fileLastModified__' // <-- this gets resolve by the unix date time of this file | ||
dayjs.extend(localizedFormat) | ||
const lastModified = __fileLastModified__ // <-- this gets replaced by the plugin as unix date time | ||
export default () => { | ||
@@ -47,3 +47,5 @@ return <div>{dayjs(lastModified).format('LLL')}</div> | ||
### Conversion | ||
#### Before | ||
```js | ||
@@ -54,2 +56,3 @@ const fileLastModified = __fileLastModified__ | ||
#### After | ||
```js | ||
@@ -60,2 +63,3 @@ const fileLastModified = 16165180636939285e-4 // unix date time | ||
### Options | ||
The plugin accepts the following options as first argument | ||
@@ -66,3 +70,4 @@ | ||
By default the identifier is `__fileLastModified__`. If you have different requirements you can always pass another identifier to be used instead. | ||
Must be a Regex! | ||
ex: `fileLastModified({ identifier: '__FILE_LAST_MODIFIED__' })` | ||
ex: `fileLastModified({ identifier: /__FILE_LAST_MODIFIED__/ })` |
68
6293
83