Skip to content

Commit

Permalink
support require.resolve, require.asset.resolve and require.addon.resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Jan 16, 2025
1 parent 9a8bcdc commit 5114933
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions sloppy-require-parser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const CALL_WITH_STRING = /^\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*\)/
const IS_EXTENSION = /^\s*\.(addon|asset)\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)?\s*\)/
const IS_EXTENSION = /^\s*\.(addon|addon\.resolve|asset|asset\.resolve|resolve|)\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)?\s*\)/

module.exports = parseCJS

function parseCJS (src, result) {
const seen = []
const seenRequires = []
const seenAddons = []
const seenAssets = []

Expand All @@ -26,26 +26,34 @@ function parseCJS (src, result) {

if (m) {
const req = m[1].slice(1, -1)
if (seen.indexOf(req) === -1) {
seen.push(req)
if (seenRequires.indexOf(req) === -1) {
seenRequires.push(req)
result.resolutions.push({ isImport: false, position: null, input: req, output: null })
}
} else {
const m = suffix.match(IS_EXTENSION)
if (m) {
const isAddon = m[1] === 'addon'
const ext = m[1]
const isAddon = ext === 'addon' || ext === 'addon.resolve'
const isAsset = ext === 'asset' || ext === 'asset.resolve'
const isResolve = ext === 'resolve'
const req = m[2] ? m[2].slice(1, -1) : '.'

if (isAddon) {
if (seenAddons.indexOf(req) === -1) {
seenAddons.push(req)
result.addons.push({ input: req, output: null })
}
} else if (m[2]) {
} else if (isAsset && m[2]) {
if (seenAssets.indexOf(req) === -1) {
seenAssets.push(req)
result.assets.push({ input: req, output: null })
}
} else if (isResolve && m[2]) {
if (seenRequires.indexOf(req) === -1) {
seenRequires.push(req)
result.resolutions.push({ isImport: false, position: null, input: req, output: null })
}
}
}
}
Expand Down

0 comments on commit 5114933

Please sign in to comment.