Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
Use precinct as detective + add more extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Sep 9, 2021
1 parent 04eb519 commit 89cf6b9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
48 changes: 33 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,31 @@ const extra = function (pkg, deps, options) {
* @returns {Detective}
*/
const getDetective = function (name) {
/** @type {string|undefined} */
let precinctType

// Allows the use of precinct/foo to add a precinct detective with type 'foo' to a custom extension
if (typeof name === 'string' && name.startsWith('precinct/')) {
precinctType = name.slice('precinct/'.length)
name = undefined
}

try {
return name
if (name) {
// eslint-disable-next-line security/detect-non-literal-require
? (typeof name === 'string' ? require(name) : name)
: require('detective')
return typeof name === 'string' ? require(name) : name
}

/** @type {(contents: string, options: { type?: string, es6?: { mixedImports: boolean }}) => string[]} */
// @ts-ignore There is no declaration for the precinct module
const precinct = require('precinct')

if (!precinctType) throw new Error('Expected a precinctType, but got none')

return (contents) => precinct(contents, {
type: precinctType,
es6: precinctType && ['es6', 'commonjs'].includes(precinctType) ? { mixedImports: true } : undefined
})
} catch (err) {
throw new VError(err, 'Failed to load detective \'%s\'', name)
}
Expand All @@ -238,13 +258,8 @@ const noopDetective = () => []
* @returns {Extensions}
*/
const getExtensions = function (extensions, detective) {
// Initialize extensions with node.js default handlers.
/** @type {Extensions} */
const result = {
'.js': noopDetective,
'.node': noopDetective,
'.json': noopDetective
}
const result = {}

if (Array.isArray(extensions)) {
for (const extension of extensions) {
Expand All @@ -256,12 +271,15 @@ const getExtensions = function (extensions, detective) {
}
}

// Reset the `detective` instance for `.js` when it hasn't been set. This is
// done to defer loading detective when not needed and to keep `.js` first in
// the order of `Object.keys` (matching node.js behavior).
if (result['.js'] === noopDetective) {
result['.js'] = getDetective(detective)
}
result['.js'] = result['.js'] || getDetective(detective || 'precinct/es6')
result['.jsx'] = result['.jsx'] || getDetective(detective || 'precinct/es6')
result['.mjs'] = result['.mjs'] || getDetective(detective || 'precinct/es6')
result['.cjs'] = result['.cjs'] || getDetective(detective || 'precinct/commonjs')
result['.ts'] = result['.ts'] || getDetective(detective || 'precinct/ts')
result['.tsx'] = result['.tsx'] || getDetective(detective || 'precinct/tsx')
result['.scss'] = result['.scss'] || getDetective(detective || 'precinct/scss')
result['.node'] = result['.node'] || noopDetective
result['.json'] = result['.json'] || noopDetective

return result
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,19 @@
"author": "max ogden",
"dependencies": {
"debug": "^4.3.1",
"detective": "^5.0.2",
"globby": "^11.0.0",
"is-relative": "^1.0.0",
"micromatch": "^4.0.2",
"minimist": "^1.2.0",
"pkg-up": "^3.1.0",
"precinct": "^8.1.0",
"read-pkg": "^5.2.0",
"resolve": "^1.19.0",
"type-fest": "^1.2.1",
"verror": "^1.10.0"
},
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/detective": "^5.1.1",
"@types/is-relative": "^1.0.0",
"@types/micromatch": "^4.0.1",
"@types/minimist": "^1.2.1",
Expand Down

0 comments on commit 89cf6b9

Please sign in to comment.