Skip to content

Commit

Permalink
detect assets (#1)
Browse files Browse the repository at this point in the history
* detect assets

* assets must have a value
  • Loading branch information
mafintosh authored Oct 2, 2024
1 parent d1c720e commit c8c421e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
3 changes: 2 additions & 1 deletion parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ exports.parse = function parse (src, type = 'module', strictMode = false) {
resolutions: [],
namedImports: [],
exports: null,
addons: []
addons: [],
assets: []
}

if (type === 'json') {
Expand Down
22 changes: 16 additions & 6 deletions sloppy-require-parser.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const CALL_WITH_STRING = /^\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*\)/
const IS_ADDON = /^\s*\.addon\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)?\s*\)/
const IS_EXTENSION = /^\s*\.(addon|asset)\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)?\s*\)/

module.exports = parseCJS

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

let i = src.indexOf('require')
let j = i > -1 ? src.indexOf('/*') : -1
Expand All @@ -30,12 +31,21 @@ function parseCJS (src, result) {
result.resolutions.push({ isImport: false, position: null, input: req, output: null })
}
} else {
const m = suffix.match(IS_ADDON)
const m = suffix.match(IS_EXTENSION)
if (m) {
const req = m[1] ? m[1].slice(1, -1) : '.'
if (seenAddons.indexOf(req) === -1) {
seenAddons.push(req)
result.addons.push({ input: req, output: null })
const isAddon = m[1] === 'addon'
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]) {
if (seenAssets.indexOf(req) === -1) {
seenAssets.push(req)
result.assets.push({ input: req, output: null })
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ test('detects addons', function (t) {

t.alike(res.addons.map(a => a.input), ['.', './here'])
})

test('detects assets', function (t) {
const res = parse(`
const some = require('something')
const asset2 = require.asset('./here')
`, 'script')

t.alike(res.assets.map(a => a.input), ['./here'])
})

0 comments on commit c8c421e

Please sign in to comment.