Skip to content

Commit

Permalink
BREAKING CHANGE: Import path spec
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed May 5, 2023
1 parent badee8e commit 4eeff8f
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 59 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exact-realty/esbuild-plugin-inline-js",
"version": "1.0.8",
"version": "1.1.0",
"description": "esbuild plugin for inline scripts",
"main": "dist/index.js",
"module": "./dist/index.mjs",
Expand Down Expand Up @@ -31,7 +31,7 @@
"license": "ISC",
"devDependencies": {
"@types/mocha": "^10.0.1",
"@types/node": "^18.16.3",
"@types/node": "^18.16.5",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"@typescript-eslint/parser": "^5.59.2",
"esbuild": "^0.17.18",
Expand Down
62 changes: 30 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,41 +88,39 @@ export default (
},
);

build.onResolve(
{ filter: /\.inline\.(js|jsx|ts|tsx)$/ },
async (a) => {
const { path, resolveDir, pluginData, namespace, kind } = a;
if (
kind === 'entry-point' ||
pluginData === skipResolve ||
namespace ===
'@exact-realty/esbuild-plugin-inline-js/loader'
) {
return;
}
build.onResolve({ filter: /^inline:/ }, async (a) => {
const { path, resolveDir, pluginData, namespace, kind } = a;
if (
kind === 'entry-point' ||
pluginData === skipResolve ||
namespace ===
'@exact-realty/esbuild-plugin-inline-js/loader'
) {
return;
}

const result = await build.resolve(path, {
resolveDir,
pluginData: skipResolve,
kind: 'require-resolve',
namespace,
});
const pathWP = path.slice(7);

if (result.errors.length > 0) {
return { errors: result.errors };
}
const result = await build.resolve(pathWP, {
resolveDir,
pluginData: skipResolve,
kind: 'require-resolve',
namespace,
});

return {
external: false,
namespace:
'@exact-realty/esbuild-plugin-inline-js/loader',
path: result.path,
suffix: undefined,
watchDirs: [],
watchFiles: [result.path],
};
},
);
if (result.errors.length > 0) {
return { errors: result.errors };
}

return {
external: false,
namespace: '@exact-realty/esbuild-plugin-inline-js/loader',
path: result.path,
suffix: undefined,
watchDirs: [],
watchFiles: [result.path],
};
});

build.onResolve(
{
Expand Down
2 changes: 1 addition & 1 deletion test/test-default-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import assert from 'node:assert/strict';

import x from './sample.inline.ts';
import x from 'inline:./sample.inline.ts';

assert.notEqual(x, '');
assert.equal((new Function(x)(), globalThis.result), 'Hello, World!');
2 changes: 1 addition & 1 deletion test/test-non-existing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

export * as default from './this.file.does.not.exist.inline.ts';
export * as default from 'inline:./this.file.does.not.exist.inline.ts';
2 changes: 1 addition & 1 deletion test/test-path-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { createHash } from 'node:crypto';
import { join } from 'node:path';
import { readFileSync } from 'node:fs';

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

assert.notEqual(x.default, '');
assert.equal(
Expand Down
2 changes: 1 addition & 1 deletion test/test-recursive-outer.inline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as inner from './test-recursive-inner.inline.ts';
import * as inner from 'inline:./test-recursive-inner.inline.ts';

(function () {
new Function(inner.default)();
Expand Down
2 changes: 1 addition & 1 deletion test/test-recursive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import assert from 'node:assert/strict';

import x from './test-recursive-outer.inline.ts';
import x from 'inline:./test-recursive-outer.inline.ts';

assert.notEqual(x, '');
assert.equal((new Function(x)(), globalThis.result), 'Hello, World!');
2 changes: 1 addition & 1 deletion test/test-simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';

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

assert.notEqual(x.default, '');
assert.equal(
Expand Down
11 changes: 1 addition & 10 deletions test/test.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

declare module '*.inline.ts' {
const content: string;
export const contentBase64: string;
export const path: string;
export const sri: string;

export default content;
}

declare module '*.inline.js' {
declare module 'inline:*' {
const content: string;
export const contentBase64: string;
export const path: string;
Expand Down
5 changes: 2 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
"lib": ["es6", "ES2019.object", "dom"],
"declaration": true
},
"include": ["src/**/*", "types/**/*"],
"include": ["src/**/*"],
"exclude": [
"node_modules",
"build"
],
"types": ["node"]
]
}

0 comments on commit 4eeff8f

Please sign in to comment.