esbuild-plugin-filelastmodified
Advanced tools
Comparing version 1.0.2 to 1.0.3
import fs from "fs"; | ||
const nodeModules = new RegExp(/^(?:.*[\\\/])?node_modules(?:[\\\/].*)?$/); | ||
var src_default = (opts) => { | ||
const {identifier = "__fileLastModified__"} = opts || {}; | ||
const LOAD_NAMESPACE = "load_filelastmodified_namespace"; | ||
const onResolve = (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 args; | ||
return { | ||
path: args.path, | ||
namespace: LOAD_NAMESPACE, | ||
pluginData: { source, find } | ||
}; | ||
}; | ||
const onLoad = (args) => { | ||
const { pluginData } = args; | ||
const { source, find } = pluginData; | ||
const stats = fs.statSync(args.path); | ||
const contents = source.replace(find, stats.mtimeMs.toString()); | ||
return { contents }; | ||
}; | ||
const plugin = (options) => { | ||
const { identifier = "__fileLastModified__" } = options || {}; | ||
return { | ||
name: "esbuild-plugin-filelastmodified", | ||
setup(build) { | ||
build.onLoad({filter: /.*/}, ({path: filePath}) => { | ||
if (!filePath.match(nodeModules)) { | ||
let data = fs.readFileSync(filePath, "utf8"); | ||
if (data.match(find)) { | ||
const stats = fs.statSync(filePath); | ||
const ext = filePath.split(".").pop(); | ||
data = data.replace(find, stats.mtimeMs.toString()); | ||
return {contents: data, loader: ext}; | ||
} | ||
} | ||
}); | ||
build.onResolve({ filter: /.*/ }, onResolve(identifier)); | ||
build.onLoad({ filter: /.*/, namespace: LOAD_NAMESPACE }, onLoad); | ||
} | ||
}; | ||
}; | ||
var src_default = plugin; | ||
module.exports = plugin; | ||
export { | ||
src_default as default | ||
}; |
var __create = Object.create; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getProtoOf = Object.getPrototypeOf; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true}); | ||
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
var __export = (target, all) => { | ||
__markAsModule(target); | ||
for (var name in all) | ||
__defProp(target, name, {get: all[name], enumerable: true}); | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __exportStar = (target, module2, desc) => { | ||
var __reExport = (target, module2, desc) => { | ||
if (module2 && typeof module2 === "object" || typeof module2 === "function") { | ||
for (let key of __getOwnPropNames(module2)) | ||
if (!__hasOwnProp.call(target, key) && key !== "default") | ||
__defProp(target, key, {get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable}); | ||
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); | ||
} | ||
@@ -21,5 +22,4 @@ return target; | ||
var __toModule = (module2) => { | ||
return __exportStar(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? {get: () => module2.default, enumerable: true} : {value: module2, enumerable: true})), module2); | ||
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2); | ||
}; | ||
__markAsModule(exports); | ||
__export(exports, { | ||
@@ -29,22 +29,32 @@ default: () => src_default | ||
var import_fs = __toModule(require("fs")); | ||
const nodeModules = new RegExp(/^(?:.*[\\\/])?node_modules(?:[\\\/].*)?$/); | ||
var src_default = (opts) => { | ||
const {identifier = "__fileLastModified__"} = opts || {}; | ||
const LOAD_NAMESPACE = "load_filelastmodified_namespace"; | ||
const onResolve = (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 args; | ||
return { | ||
path: args.path, | ||
namespace: LOAD_NAMESPACE, | ||
pluginData: { source, find } | ||
}; | ||
}; | ||
const onLoad = (args) => { | ||
const { pluginData } = args; | ||
const { source, find } = pluginData; | ||
const stats = import_fs.default.statSync(args.path); | ||
const contents = source.replace(find, stats.mtimeMs.toString()); | ||
return { contents }; | ||
}; | ||
const plugin = (options) => { | ||
const { identifier = "__fileLastModified__" } = options || {}; | ||
return { | ||
name: "esbuild-plugin-filelastmodified", | ||
setup(build) { | ||
build.onLoad({filter: /.*/}, ({path: filePath}) => { | ||
if (!filePath.match(nodeModules)) { | ||
let data = import_fs.default.readFileSync(filePath, "utf8"); | ||
if (data.match(find)) { | ||
const stats = import_fs.default.statSync(filePath); | ||
const ext = filePath.split(".").pop(); | ||
data = data.replace(find, stats.mtimeMs.toString()); | ||
return {contents: data, loader: ext}; | ||
} | ||
} | ||
}); | ||
build.onResolve({ filter: /.*/ }, onResolve(identifier)); | ||
build.onLoad({ filter: /.*/, namespace: LOAD_NAMESPACE }, onLoad); | ||
} | ||
}; | ||
}; | ||
var src_default = plugin; | ||
module.exports = plugin; |
{ | ||
"name": "esbuild-plugin-filelastmodified", | ||
"version": "1.0.2", | ||
"repository": "https://github.com/g45t345rt/esbuild-plugin-filelastmodified", | ||
"version": "1.0.3", | ||
"description": "A esbuild plugin to replace __fileLastModified__ with the actual time the file has been modified.", | ||
@@ -13,12 +12,14 @@ "main": "dist/index.js", | ||
}, | ||
"repository": "https://github.com/g45t345rt/esbuild-plugin-filelastmodified", | ||
"readme": "https://github.com/g45t345rt/esbuild-plugin-filelastmodified#readme", | ||
"author": "g45t345rt", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@types/node": "^14.14.37", | ||
"@types/node": "^16.10.1", | ||
"chai": "^4.3.4", | ||
"cross-env": "^7.0.3", | ||
"esbuild": "^0.11.2", | ||
"mocha": "^8.3.2", | ||
"typescript": "^4.2.3" | ||
"esbuild": "^0.13.2", | ||
"mocha": "^9.1.2", | ||
"typescript": "^4.4.3" | ||
} | ||
} | ||
} |
@@ -1,30 +0,45 @@ | ||
import { Plugin, Loader } from 'esbuild' | ||
import { Plugin, OnResolveArgs, OnLoadArgs, OnResolveResult, OnLoadResult } from 'esbuild' | ||
import fs from 'fs' | ||
// https://github.com/jamestalmage/node-modules-regexp/blob/master/index.js | ||
const nodeModules = new RegExp(/^(?:.*[\\\/])?node_modules(?:[\\\/].*)?$/) | ||
const LOAD_NAMESPACE = 'load_filelastmodified_namespace' | ||
interface FileLastModifiedOptions { | ||
interface PluginOptions { | ||
identifier?: string | ||
} | ||
export default (opts?: FileLastModifiedOptions): Plugin => { | ||
const { identifier = '__fileLastModified__' } = opts || {} | ||
const onResolve = (identifier) => async (args: OnResolveArgs): Promise<OnResolveResult> => { | ||
const find = new RegExp(identifier, 'g') | ||
const source = (await fs.promises.readFile(args.path)).toString('utf-8') | ||
if (!source.match(find)) return args | ||
return { | ||
path: args.path, | ||
namespace: LOAD_NAMESPACE, | ||
pluginData: { source, find } | ||
} | ||
} | ||
const onLoad = (args: OnLoadArgs): OnLoadResult => { | ||
const { pluginData } = args | ||
const { source, find } = pluginData | ||
const stats = fs.statSync(args.path) // get file information -> last time it was modified | ||
const contents = source.replace(find, stats.mtimeMs.toString()) // replace the identifier with file last modified | ||
return { contents } | ||
} | ||
const plugin = (options?: PluginOptions): Plugin => { | ||
const { identifier = '__fileLastModified__' } = options || {} | ||
return { | ||
name: 'esbuild-plugin-filelastmodified', | ||
setup(build) { | ||
build.onLoad({ filter: /.*/ }, ({ path: filePath }) => { | ||
if (!filePath.match(nodeModules)) { // skip/do not process node_modules files | ||
let data = fs.readFileSync(filePath, 'utf8') | ||
if (data.match(find)) { // make sure identifier is present | ||
const stats = fs.statSync(filePath) // get file information -> last time it was modified | ||
const ext = filePath.split('.').pop() // get file extension js,ts,jsx,tsx,... | ||
data = data.replace(find, stats.mtimeMs.toString()) // replace the identifier with file last modified | ||
return { contents: data, loader: ext as Loader } | ||
} | ||
} | ||
}) | ||
build.onResolve({ filter: /.*/ }, onResolve(identifier)) | ||
build.onLoad({ filter: /.*/, namespace: LOAD_NAMESPACE }, onLoad) | ||
} | ||
} | ||
} | ||
export default plugin | ||
module.exports = plugin |
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
8177
6
128
1