Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
* Updated dependencies
* Added esbuild 20.x a supported version
* Switched to ESM
* Added new parameter to support a custom matching filter and multiple
  instances.
  • Loading branch information
corrideat committed Feb 23, 2024
1 parent 89aee26 commit 826f986
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 1,223 deletions.
6 changes: 0 additions & 6 deletions .mocharc.json

This file was deleted.

1,466 changes: 284 additions & 1,182 deletions package-lock.json

Large diffs are not rendered by default.

31 changes: 15 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@exact-realty/esbuild-plugin-inline-js",
"version": "1.1.5",
"version": "1.1.6",
"description": "esbuild plugin for inline scripts",
"type": "module",
"main": "dist/index.js",
"module": "./dist/index.mjs",
"exports": {
Expand All @@ -15,8 +16,8 @@
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"lintFix": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
"build": "tsc --emitDeclarationOnly --declarationMap && node esbuild.mjs",
"test": "mocha",
"prepare": "npm run build",
"test": "node --loader ts-node/esm test/esbuild.test.ts",
"prepack": "npm run build",
"prepublishOnly": "npm test && npm run lint",
"preversion": "npm run lint",
"version": "npm run lint && git add -A src",
Expand All @@ -29,21 +30,19 @@
"author": "Exact Realty Limited",
"license": "ISC",
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^20.8.2",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
"esbuild": "^0.19.4",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"mocha": "^10.2.0",
"prettier": "^3.0.3",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
"@types/node": "^20.11.20",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"esbuild": "^0.20.1",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
},
"peerDependencies": {
"esbuild": "^0.17.0 || ^0.18.0 || ^0.19.0"
"esbuild": "^0.17.0 || ^0.18.0 || ^0.19.0 || ^0.20.0"
},
"keywords": [
"esbuild",
Expand Down
23 changes: 13 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ export default (
esbuild.BuildOptions,
'entryPoints' | 'outdir' | 'outfile' | 'write'
>,
filter?: RegExp,
): esbuild.Plugin => {
const mainFilter = filter ?? /^inline:/;
const skipResolve = {};
const instanceName = `${((0, Math.random)() * 1000) | 0}-${mainFilter}`;

config = { ...config };

Expand All @@ -32,12 +35,12 @@ export default (
delete (config as esbuild.BuildOptions).write;

return {
name: '@exact-realty/esbuild-plugin-inline-js',
name: `@exact-realty/esbuild-plugin-inline-js/${instanceName}`,
setup(build) {
build.onLoad(
{
filter: /.*/,
namespace: '@exact-realty/esbuild-plugin-inline-js/loader',
filter: /./,
namespace: `@exact-realty/esbuild-plugin-inline-js/loader/${instanceName}`,
},
async ({ path }) => {
const outfile = randomBytes(9).toString('base64url');
Expand Down Expand Up @@ -88,13 +91,13 @@ export default (
},
);

build.onResolve({ filter: /^inline:/ }, async (a) => {
build.onResolve({ filter: mainFilter }, async (a) => {
const { path, resolveDir, pluginData, namespace, kind } = a;
if (
kind === 'entry-point' ||
pluginData === skipResolve ||
namespace ===
'@exact-realty/esbuild-plugin-inline-js/loader'
`@exact-realty/esbuild-plugin-inline-js/loader/${instanceName}`
) {
return;
}
Expand All @@ -114,7 +117,7 @@ export default (

return {
external: false,
namespace: '@exact-realty/esbuild-plugin-inline-js/loader',
namespace: `@exact-realty/esbuild-plugin-inline-js/loader/${instanceName}`,
path: result.path,
suffix: undefined,
watchDirs: [],
Expand All @@ -125,20 +128,20 @@ export default (
build.onResolve(
{
filter: /.*/,
namespace: '@exact-realty/esbuild-plugin-inline-js/loader',
namespace: `@exact-realty/esbuild-plugin-inline-js/loader/${instanceName}`,
},
({ path, pluginData }) => ({
external: false,
namespace: '@exact-realty/esbuild-plugin-inline-js/content',
namespace: `@exact-realty/esbuild-plugin-inline-js/content/${instanceName}`,
path: path.replace(/\.inline\.[jt]sx?$/, '.js'),
pluginData: pluginData,
}),
);

build.onLoad(
{
filter: /.*/,
namespace: '@exact-realty/esbuild-plugin-inline-js/content',
filter: /./,
namespace: `@exact-realty/esbuild-plugin-inline-js/content/${instanceName}`,
},
({ pluginData }) => ({
contents: pluginData,
Expand Down
19 changes: 13 additions & 6 deletions test/esbuild.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@

import esbuild from 'esbuild';
import path from 'node:path';
import is from '../src';
import is from '../src/index.js';

import assert from 'node:assert/strict';
import { dirname } from 'node:path';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));

describe('Test', () => {
describe('Import results in the right values', () => {
Expand All @@ -32,7 +37,7 @@ describe('Test', () => {
entryPoints: [path.join(__dirname, `${test}.ts`)],
outdir: path.join(__dirname, 'build'),
bundle: true,
format: 'cjs',
format: 'esm',
publicPath: 'http://invalid/assets',
plugins: [is({ format: 'esm' })],
platform: 'node',
Expand All @@ -59,7 +64,7 @@ describe('Test', () => {
entryPoints: [path.join(__dirname, `${test}.ts`)],
outdir: path.join(__dirname, 'build'),
bundle: true,
format: 'cjs',
format: 'esm',
publicPath: 'http://invalid/assets',
plugins: [is({ format: 'esm' })],
platform: 'node',
Expand Down Expand Up @@ -88,9 +93,11 @@ describe('Test', () => {
err[0] instanceof Object,
'Error description is not an object',
);
const expectedName =
'@exact-realty/esbuild-plugin-inline-js/';
assert.equal(
err[0].pluginName,
'@exact-realty/esbuild-plugin-inline-js',
err[0].pluginName?.slice(0, expectedName.length),
expectedName,
);
});
});
Expand All @@ -105,7 +112,7 @@ describe('Test', () => {
entryPoints: [path.join(__dirname, `${test}.ts`)],
outdir: path.join(__dirname, 'build'),
bundle: true,
format: 'cjs',
format: 'esm',
publicPath: 'http://invalid/assets',
plugins: [
is({
Expand Down
5 changes: 4 additions & 1 deletion test/test-path-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { join } from 'node:path';
import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

import * as x from 'inline:./external.inline.ts';

const __dirname = dirname(fileURLToPath(import.meta.url));

assert.notEqual(x.default, '');
assert.equal(
x.contentBase64,
Expand Down
7 changes: 5 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist",
"strict": true,
"strictNullChecks": true,
Expand All @@ -15,6 +15,9 @@
"lib": ["es6", "ES2019.object", "dom"],
"declaration": true
},
"ts-node": {
"experimentalResolver": true,
},
"include": ["src/**/*"],
"exclude": [
"node_modules",
Expand Down

0 comments on commit 826f986

Please sign in to comment.