Skip to content

Commit

Permalink
Support require(.addon).resolve()
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Nov 15, 2024
1 parent 43e473d commit 9d92726
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 14 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ Constant | Description
`REQUIRE` | CommonJS `require()`.
`IMPORT` | ES module `import`.
`DYNAMIC` | ES module `import()` if `IMPORT` is set.
`ADDON` | CommonJS `require.addon()` if `REQUIRE` is set or ES module `import.meta.addon()` if `IMPORT` is set.
`ASSET` | CommonJS `require.asset()` if `REQUIRE` is set or ES module `import.meta.asset()` if `IMPORT` is set.
`ADDON` | CommonJS `require.addon()` if `REQUIRE` is set, or ES module `import.meta.addon()` if `IMPORT` is set.
`ASSET` | CommonJS `require.asset()` if `REQUIRE` is set, or ES module `import.meta.asset()` if `IMPORT` is set.
`RESOLVE` | CommonJS `require.resolve()` or `require.addon.resolve()` if `REQUIRE` and optionally `ADDON` are set, or ES module `import.meta.resolve()` or `import.meta.addon.resolve()` if `IMPORT` and optionally `ADDON` are set.
`REEXPORT` | Re-export of a CommonJS `require()` if `REQUIRE` is set.

## License
Expand Down
1 change: 1 addition & 0 deletions binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ bare_module_lexer_exports (js_env_t *env, js_value_t *exports) {
V("DYNAMIC", bare_module_lexer_dynamic)
V("ADDON", bare_module_lexer_addon)
V("ASSET", bare_module_lexer_asset)
V("RESOLVE", bare_module_lexer_resolve)
V("REEXPORT", bare_module_lexer_reexport)
#undef V

Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ exports.constants = {
DYNAMIC: binding.DYNAMIC,

/**
* CommonJS `require.addon()` if `REQUIRE` is set or ES module `import.meta.addon()` if `IMPORT` is set.
* CommonJS `require.addon()` if `REQUIRE` is set, or ES module `import.meta.addon()` if `IMPORT` is set.
*/
ADDON: binding.ADDON,

/**
* CommonJS `require.asset()` if `REQUIRE` is set or ES module `import.meta.asset()` if `IMPORT` is set.
* CommonJS `require.asset()` if `REQUIRE` is set, or ES module `import.meta.asset()` if `IMPORT` is set.
*/
ASSET: binding.ASSET,

/**
* CommonJS `require.resolve()` or `require.addon.resolve()` if `REQUIRE` and optionally `ADDON` are set, or ES module
* `import.meta.resolve()` or `import.meta.addon.resolve()` if `IMPORT` and optionally `ADDON` are set.
*/
RESOLVE: binding.RESOLVE,

/**
* Re-export of a CommonJS `require()` if `REQUIRE` is set.
*/
Expand Down
46 changes: 37 additions & 9 deletions lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ enum {
bare_module_lexer_dynamic = 0x4,
bare_module_lexer_addon = 0x8,
bare_module_lexer_asset = 0x10,
bare_module_lexer_reexport = 0x20,
bare_module_lexer_resolve = 0x20,
bare_module_lexer_reexport = 0x40,
};

static inline bool
Expand Down Expand Up @@ -428,9 +429,7 @@ bare_module_lexer__lex (js_env_t *env, js_value_t *imports, js_value_t *exports,
while (i < n && ws(u(0))) i++;
}

else {
i++;
}
else i++;

continue;

Expand All @@ -456,6 +455,24 @@ bare_module_lexer__lex (js_env_t *env, js_value_t *imports, js_value_t *exports,
while (i < n && ws(u(0))) i++;

type |= bare_module_lexer_addon;

// require\.addon\.
if (c(0) == '.') {
i++;

while (i < n && ws(u(0))) i++;

// require\.addon\.resolve
if (i + 7 < n && bu("resolve", 7)) {
i += 7;

while (i < n && ws(u(0))) i++;

type |= bare_module_lexer_resolve;
}

else continue;
}
}

// require\.asset
Expand All @@ -467,23 +484,34 @@ bare_module_lexer__lex (js_env_t *env, js_value_t *imports, js_value_t *exports,
type |= bare_module_lexer_asset;
}
}

// require\.resolve
else if (i + 7 < n && bu("resolve", 7)) {
i += 7;

while (i < n && ws(u(0))) i++;

type |= bare_module_lexer_resolve;
}

else continue;
}

// require(\.(addon|asset))?\(
// require(\.(resolve|addon(\.resolve)?|asset))?\(
if (c(0) == '(') {
i++;

while (i < n && ws(u(0))) i++;

// require(\.(addon|asset))?\(['"]
// require(\.(resolve|addon(\.resolve)?|asset))?\(['"]
if (c(0) == '\'' || c(0) == '"') {
utf8_t e = u(0);

ss = ++i;

while (i < n && u(0) != e) i++;

// require(\.(addon|asset))?\(['"].*['"]
// require(\.(resolve|addon(\.resolve)?|asset))?\(['"].*['"]
if (c(0) == e) {
se = i;

Expand All @@ -493,7 +521,7 @@ bare_module_lexer__lex (js_env_t *env, js_value_t *imports, js_value_t *exports,

while (i < n && u(0) != ')') i++;

// require(\.(addon|asset))?\(['"].*['"][^)]*\)
// require(\.(resolve|addon(\.resolve)?|asset))?\(['"].*['"][^)]*\)
if (c(0) == ')') {
i++;

Expand All @@ -503,7 +531,7 @@ bare_module_lexer__lex (js_env_t *env, js_value_t *imports, js_value_t *exports,
}
}

// require\.addon\(\)
// require\.addon(\.resolve)?\(\)
else if (c(0) == ')' && (type & bare_module_lexer_addon)) {
ss = se = i++;

Expand Down
37 changes: 36 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const test = require('brittle')
const lex = require('.')

const { REQUIRE, IMPORT, DYNAMIC, ADDON, ASSET, REEXPORT } = lex.constants
const { REQUIRE, IMPORT, DYNAMIC, ADDON, ASSET, REEXPORT, RESOLVE } = lex.constants

test('require(\'id\')', (t) => {
t.alike(lex('require(\'./foo.js\')'), {
Expand All @@ -17,6 +17,20 @@ test('require("id")', (t) => {
})
})

test('require.resolve(\'id\')', (t) => {
t.alike(lex('require.resolve(\'./foo.js\')'), {
imports: [{ specifier: './foo.js', type: REQUIRE | RESOLVE, names: [], position: [0, 17, 25] }],
exports: []
})
})

test('require.resolve("id")', (t) => {
t.alike(lex('require.resolve("./foo.js")'), {
imports: [{ specifier: './foo.js', type: REQUIRE | RESOLVE, names: [], position: [0, 17, 25] }],
exports: []
})
})

test('require.addon()', (t) => {
t.alike(lex('require.addon()'), {
imports: [{ specifier: '', type: REQUIRE | ADDON, names: [], position: [0, 14, 14] }],
Expand All @@ -38,6 +52,27 @@ test('require.addon("id")', (t) => {
})
})

test('require.addon.resolve()', (t) => {
t.alike(lex('require.addon.resolve()'), {
imports: [{ specifier: '', type: REQUIRE | ADDON | RESOLVE, names: [], position: [0, 22, 22] }],
exports: []
})
})

test('require.addon.resolve(\'id\')', (t) => {
t.alike(lex('require.addon.resolve(\'./foo.bare\')'), {
imports: [{ specifier: './foo.bare', type: REQUIRE | ADDON | RESOLVE, names: [], position: [0, 23, 33] }],
exports: []
})
})

test('require.addon.resolve("id")', (t) => {
t.alike(lex('require.addon.resolve("./foo.bare")'), {
imports: [{ specifier: './foo.bare', type: REQUIRE | ADDON | RESOLVE, names: [], position: [0, 23, 33] }],
exports: []
})
})

test('require.asset(\'id\')', (t) => {
t.alike(lex('require.asset(\'./foo.txt\')'), {
imports: [{ specifier: './foo.txt', type: REQUIRE | ASSET, names: [], position: [0, 15, 24] }],
Expand Down

0 comments on commit 9d92726

Please sign in to comment.