als-require
Advanced tools
Comparing version 0.8.1 to 0.8.2
@@ -59,2 +59,3 @@ const fs = require('fs') | ||
getContent(this.fullPath) | ||
this.prepareContents() | ||
this.contentReady = true | ||
@@ -64,6 +65,18 @@ return this | ||
prepareContents() { | ||
const newKeys = new Set() | ||
const addKeys = (keys) => { | ||
keys.forEach(key => { | ||
if(!this.contents[key]) return | ||
addKeys(Require.contents[key].children) | ||
newKeys.add(key) | ||
}) | ||
} | ||
addKeys(Object.keys(this.contents).reverse()) | ||
this.keys = Array.from(newKeys) | ||
} | ||
build(modules = {}, context = {}, contextName = 'context') { | ||
function require(path) { return modules[path] || null } | ||
const keys = Object.keys(this.contents).reverse() | ||
keys.map(path => { | ||
this.keys.map(path => { | ||
const module = { exports: {} } | ||
@@ -75,3 +88,3 @@ const params = { module, require, exports: module.exports, [contextName]: context } | ||
}) | ||
return modules[keys[keys.length - 1]] | ||
return modules[this.keys[this.keys.length - 1]] | ||
} | ||
@@ -78,0 +91,0 @@ |
{ | ||
"name": "als-require", | ||
"version": "0.8.1", | ||
"version": "0.8.2", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -35,2 +35,21 @@ class Require { | ||
} | ||
static async getNodeModules(nodeModules,children,content) { | ||
if (nodeModules.length === 0) return content | ||
for (const { match, modulePath } of nodeModules) { | ||
const pkgJsonPath = `/node_modules/${modulePath}/package.json` | ||
const exists = await fetch(pkgJsonPath, { method: 'HEAD' }) | ||
if (!exists.ok) { | ||
console.warn(`The module "${modulePath}" can't be imported and will be replaced with null`) | ||
continue | ||
} | ||
const { main: filename = 'index.js' } = await Require.fetch(pkgJsonPath, 'json') | ||
const fullPath = `/node_modules/${modulePath}/${filename}` | ||
Require.isCyclyc(fullPath, modulePath) | ||
children.push(fullPath); | ||
content = content.replace(match, match.replace(modulePath, fullPath)) | ||
} | ||
return content | ||
} | ||
static relativePath = location.pathname | ||
@@ -62,18 +81,3 @@ | ||
}); | ||
if (nodeModules.length) { | ||
for (const { match, modulePath } of nodeModules) { | ||
const pkgJsonPath = `/node_modules/${modulePath}/package.json` | ||
const exists = await fetch(pkgJsonPath, { method: 'HEAD' }) | ||
if (!exists.ok) { | ||
console.warn(`The module "${modulePath}" can't be imported and will be replaced with null`) | ||
continue | ||
} | ||
const { main: filename = 'index.js' } = await Require.fetch(pkgJsonPath, 'json') | ||
const fullPath = `/node_modules/${modulePath}/${filename}` | ||
Require.isCyclyc(fullPath, modulePath) | ||
children.push(fullPath); | ||
content = content.replace(match, match.replace(modulePath, fullPath)) | ||
} | ||
} | ||
content = await Require.getNodeModules(nodeModules,children,content) | ||
Require.contents[path] = { content, children } | ||
@@ -89,9 +93,22 @@ } | ||
this.contentReady = true | ||
this.prepareContents() | ||
return this | ||
} | ||
prepareContents() { | ||
const newKeys = new Set() | ||
const addKeys = (keys) => { | ||
keys.forEach(key => { | ||
if(!this.contents[key]) return | ||
addKeys(Require.contents[key].children) | ||
newKeys.add(key) | ||
}) | ||
} | ||
addKeys(Object.keys(this.contents).reverse()) | ||
this.keys = Array.from(newKeys) | ||
} | ||
build(modules = {}, context = {}, contextName = 'context') { | ||
function require(path) { return modules[path] || null } | ||
const keys = Object.keys(this.contents).reverse() | ||
keys.map(path => { | ||
this.keys.map(path => { | ||
const module = { exports: {} } | ||
@@ -103,3 +120,3 @@ const params = { module, require, exports: module.exports, [contextName]: context } | ||
}) | ||
return modules[keys[keys.length - 1]] | ||
return modules[this.keys[this.keys.length - 1]] | ||
} | ||
@@ -126,3 +143,1 @@ | ||
}; | ||
const require = Require.getModule; |
40921
555