Skip to content

Commit

Permalink
Use ESM resolving algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 10, 2021
1 parent 8eb81bb commit f526b48
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 53 deletions.
11 changes: 6 additions & 5 deletions lib/generate/config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import url from 'url'
import {resolve} from 'import-meta-resolve'
import {nanoid} from 'nanoid'
import resolveFrom from 'resolve-from'
import {relativeModule} from '../util/relative-module.js'

export function config(ctx) {
export async function config(ctx) {
const options = ctx.options
const pkg = ctx.pkg || {}
const cwd = ctx.file.cwd
const mainRoot = options.main ? cwd : ctx.pkgRoot || cwd
const mainId = relativeModule(options.main || pkg.main || '')
const mainRoot = url.pathToFileURL(options.main ? cwd : ctx.pkgRoot || cwd)
const mainId = relativeModule(options.main || pkg.main || 'index.js')
let main

try {
main = resolveFrom(mainRoot, mainId)
main = await resolve(mainId, mainRoot.href + '/')
} catch {}

ctx.cwd = cwd
Expand Down
47 changes: 26 additions & 21 deletions lib/generate/find-example.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
import url from 'url'
import {promises as fs} from 'fs'
import resolveFrom from 'resolve-from'
import {resolve} from 'import-meta-resolve'
import {relativeModule} from '../util/relative-module.js'

export async function findExample(ctx) {
const fn = ctx.options.example ? findExplicitExample : findImplicitExample
const filePath = await fn(ctx)
const fileUrl = await fn(ctx)

if (!filePath) {
if (!fileUrl) {
throw new Error('Could not find example')
}

const example = String(await fs.readFile(filePath))
const example = String(await fs.readFile(new URL(fileUrl)))

ctx.examplePath = filePath
ctx.exampleFileUrl = fileUrl
// Make sure there is a final line feed.
ctx.example =
example.charAt(example.length - 1) === '\n' ? example : example + '\n'
}

function findExplicitExample(ctx) {
const moduleId = relativeModule(ctx.options.example)

try {
return resolveFrom(ctx.cwd, moduleId)
// Catch just to be sure.
/* c8 ignore next */
} catch {}
return resolve(
relativeModule(ctx.options.example),
url.pathToFileURL(ctx.cwd).href + '/'
)
}

async function findImplicitExample(ctx) {
const examples = [
'./example',
'./examples',
'./doc/example',
'./docs/example'
]
const from = url.pathToFileURL(ctx.cwd).href + '/'
const promises = [
'./example.js',
'./example/index.js',
'./examples.js',
'./examples/index.js',
'./doc/example.js',
'./doc/example/index.js',
'./docs/example.js',
'./docs/example/index.js'
].map((d) => resolve(d, from))

const examples = await Promise.allSettled(promises)
let index = -1

while (++index < examples.length) {
const filePath = resolveFrom.silent(ctx.cwd, examples[index])
const example = examples[index]

if (filePath) {
return filePath
if (example.status === 'fulfilled') {
return example.value
}
}
}
4 changes: 2 additions & 2 deletions lib/generate/find-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export async function findPackage(ctx) {

try {
await read(pkgFile)
} catch (error) {
// Doesn’t consistently happen.
/* c8 ignore next 3 */
/* c8 ignore next 5 */
} catch (error) {
if (error.code !== 'ENOENT') {
throw new Error('Could not read package: ' + error)
}
Expand Down
42 changes: 22 additions & 20 deletions lib/generate/instrument.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import path from 'path'
import {resolve} from 'import-meta-resolve'
import babel from '@babel/core'
import resolveFrom from 'resolve-from'

export async function instrument(ctx) {
const logs = []
const mainReferences = []
const nodes = []
let result

try {
result = await babel.transformAsync(ctx.example, {
plugins: [addIdToConsoleLog],
cwd: ctx.cwd,
filename: ctx.examplePath,
filename: ctx.exampleFileUrl,
caller: {name: 'remark-usage'},
sourceType: 'unambiguous'
})
} catch (error) {
throw new Error('Could not parse example: ' + error)
}

const promises = nodes.map((node) => {
return resolve(node.value, ctx.exampleFileUrl).then((resolved) => {
if (resolved === ctx.main) {
// Babel always adds raw, but just to be sure.
/* c8 ignore next */
const raw = (node && node.extra && node.extra.raw) || "'"

mainReferences.push({
start: node.start,
end: node.end,
quote: raw.charAt(0)
})
}
})
})

await Promise.allSettled(promises)

ctx.exampleInstrumented = result.code
ctx.logs = logs
ctx.mainReferences = mainReferences
Expand Down Expand Up @@ -50,23 +68,7 @@ export async function instrument(ctx) {
}

function instrumentMainReference(node) {
let raw = node && node.extra && node.extra.raw
const filePath = resolveFrom.silent(
path.dirname(ctx.examplePath),
node.value
)

// Babel always adds raw, but just to be sure.
/* c8 ignore next */
if (!raw) raw = "'"

if (filePath && filePath === ctx.main) {
mainReferences.push({
start: node.start,
end: node.end,
quote: raw.charAt(0)
})
}
nodes.push(node)
}

function instrumentConsoleLog(path) {
Expand Down
6 changes: 4 additions & 2 deletions lib/generate/write.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import url from 'url'
import {promises as fs} from 'fs'
import path from 'path'

export async function write(ctx) {
const examplePath = url.fileURLToPath(ctx.exampleFileUrl)
const filePath = path.join(
path.dirname(ctx.examplePath),
ctx.id + path.extname(ctx.examplePath)
path.dirname(examplePath),
ctx.id + path.extname(examplePath)
)

await fs.writeFile(filePath, ctx.exampleInstrumented)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
],
"dependencies": {
"@babel/core": "^7.0.0",
"import-meta-resolve": "^1.0.0",
"mdast-util-heading-range": "^3.0.0",
"nanoid": "^3.0.0",
"remark-parse": "^10.0.0",
"resolve-from": "^5.0.0",
"to-vfile": "^7.0.0",
"trough": "^2.0.0",
"unified": "^10.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/normal-custom-example/config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"example": "./ex"
"example": "./ex.js"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Require `pi`:
var pi = require('.')
var pi = require('./index.js')

// Logs:
console.log('text', pi)

0 comments on commit f526b48

Please sign in to comment.