From bd2a4011521e73aacbff67af9586ec81faab5c70 Mon Sep 17 00:00:00 2001 From: naecoo Date: Thu, 30 Nov 2023 09:05:37 +0800 Subject: [PATCH 1/2] chore: update --- src/index.js | 58 ++++++++++++++++++++++++++++------------------ test/example.js | 8 +++++-- test/index.test.js | 12 ++++++++-- 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/index.js b/src/index.js index e45cfc2..784f844 100644 --- a/src/index.js +++ b/src/index.js @@ -1,18 +1,19 @@ - -const fs = require('fs'); -const MagicString = require('magic-string'); +import fs from "node:fs"; +import MagicString from "magic-string"; const toFunction = (functionOrValue) => { - if (typeof functionOrValue === 'function') return functionOrValue; + if (typeof functionOrValue === "function") return functionOrValue; return () => functionOrValue; -} +}; -const escape = (str) => str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&'); +const escape = (str) => str.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&"); const longest = (a, b) => b.length - a.length; const mapToFunctions = (options) => { - const values = options.values ? Object.assign({}, options.values) : Object.assign({}, options); + const values = options.values + ? Object.assign({}, options.values) + : Object.assign({}, options); delete values.delimiters; delete values.include; delete values.exclude; @@ -22,7 +23,7 @@ const mapToFunctions = (options) => { functions[key] = toFunction(values[key]); return functions; }, {}); -} +}; const generateFilter = (options) => { let include = /.*/; @@ -30,8 +31,10 @@ const generateFilter = (options) => { let hasValidInclude = false; if (options.include) { - if (Object.prototype.toString.call(options.include) !== '[object RegExp]') { - console.warn(`Options.include must be a RegExp object, but gets an '${typeof options.include}' type.`); + if (Object.prototype.toString.call(options.include) !== "[object RegExp]") { + console.warn( + `Options.include must be a RegExp object, but gets an '${typeof options.include}' type.` + ); } else { hasValidInclude = true; include = options.include; @@ -39,8 +42,10 @@ const generateFilter = (options) => { } if (options.exclude) { - if (Object.prototype.toString.call(options.exclude) !== '[object RegExp]') { - console.warn(`Options.exclude must be a RegExp object, but gets an '${typeof options.exclude}' type.`); + if (Object.prototype.toString.call(options.exclude) !== "[object RegExp]") { + console.warn( + `Options.exclude must be a RegExp object, but gets an '${typeof options.exclude}' type.` + ); } else if (!hasValidInclude) { // Only if `options.include` not set, take `options.exclude` exclude = options.exclude; @@ -48,7 +53,7 @@ const generateFilter = (options) => { } return { include, exclude }; -} +}; const replaceCode = (code, id, pattern, functionValues) => { const magicString = new MagicString(code); @@ -61,20 +66,23 @@ const replaceCode = (code, id, pattern, functionValues) => { magicString.overwrite(start, end, replacement); } return magicString.toString(); -} +}; // todo: add preventAssignment option & support sourceMap -exports.replace = (options = {}) => { +const replace = (options = {}) => { const { include, exclude } = generateFilter(options); const functionValues = mapToFunctions(options); const empty = Object.keys(functionValues).length === 0; const keys = Object.keys(functionValues).sort(longest).map(escape); const { delimiters } = options; const pattern = delimiters - ? new RegExp(`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}`, 'g') - : new RegExp(`\\b(${keys.join('|')})\\b`, 'g'); + ? new RegExp( + `${escape(delimiters[0])}(${keys.join("|")})${escape(delimiters[1])}`, + "g" + ) + : new RegExp(`\\b(${keys.join("|")})\\b`, "g"); return { - name: 'replace', + name: "replace", setup(build) { build.onLoad({ filter: include }, async (args) => { // if match exclude, skip @@ -82,10 +90,14 @@ exports.replace = (options = {}) => { return; } const source = await fs.promises.readFile(args.path, "utf8"); - const contents = empty ? source : replaceCode(source, args.path, pattern, functionValues) - return { contents, loader: 'default' }; + const contents = empty + ? source + : replaceCode(source, args.path, pattern, functionValues); + return { contents, loader: "default" }; }); - } + }, }; -} -module.exports = exports; +}; + +export { replace }; +export default replace; diff --git a/test/example.js b/test/example.js index 5480234..6ab0306 100644 --- a/test/example.js +++ b/test/example.js @@ -1,4 +1,8 @@ export const target = { version: __version__, - author: __author__ -}; \ No newline at end of file + author: __author__, +}; + +export const anotherTarget = { + example: $example, +} \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index d3a91a6..6961303 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -78,11 +78,19 @@ test("options.exclude-2", async () => { test("options.exclude-3", async () => { const code = await buildExample({ - __author__: JSON.stringify("naecoo"), + __author__: JSON.stringify("222"), include: /\.js$/, exclude: /\.js$/, }); - expect(code).toMatch(/naecoo/); + expect(code).toMatch(/222/); expect(code).not.toMatch(/__author__/); expect(code).toMatch(/__version__/); }); + +test("use a key that starts with a $ ", async () => { + const code = await buildExample({ + $example: JSON.stringify("Hello World!"), + delimiters: ['', ''], + }); + expect(code).toMatch(/Hello World!/); +}); From 1b7e5ff9a9a55cec8c8aecbfff05ce62897b4dec Mon Sep 17 00:00:00 2001 From: naecoo Date: Thu, 30 Nov 2023 09:05:51 +0800 Subject: [PATCH 2/2] release: 1.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 05b3d3e..fb6679b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "esbuild-plugin-replace", - "version": "1.4.0", + "version": "1.5.0", "description": "Replace strings in files while bundling", "main": "dist/index.js", "module": "dist/index.mjs",