als-require
Advanced tools
Comparing version 1.5.2 to 1.5.4
const UglifyJS = require("uglify-js"); | ||
const {stringify,parse} = require('als-object-serializer') | ||
const { stringify, parse } = require('als-object-serializer') | ||
const parseError = require('./parse-error') | ||
@@ -9,3 +9,3 @@ const getFns = require('./get-fns') | ||
obj.getContent() | ||
const { fn, modulesLines, curLastLine } = getFns(contextName,obj) | ||
const { fn, modulesLines, curLastLine } = getFns(contextName, obj) | ||
const contents = {} | ||
@@ -12,0 +12,0 @@ for (const path of obj.keys) { |
@@ -5,4 +5,4 @@ const rootPath = process.cwd() | ||
const getFullPath = require('./get-full-path') | ||
const getNodeModules = require('./get-node-modules') | ||
const packageJsonCache = {} | ||
function getContents({contents,fullPath},Require) { | ||
@@ -15,33 +15,8 @@ const getContent = (path) => { | ||
content = content.replace(/^(?!\/\/|\/\*.*\*\/).*require\(["'`](.*)["'`]\)/gm, (match, modulePath) => { | ||
let fullPath,relativePath,filename,moduleDir = modulePath | ||
let fullPath | ||
const r = new RegExp(`require\\((["'\`])${modulePath}["'\`]\\)`) | ||
if (modulePath.startsWith('.')) fullPath = getFullPath(modulePath, path) | ||
else { | ||
if(modulePath.includes('/')) { | ||
const arr = modulePath.split('/') | ||
moduleDir = arr.shift() | ||
relativePath = arr.join('/') | ||
} | ||
let pckgJsonPath = join(rootPath, 'node_modules', moduleDir, 'package.json') | ||
if (!fs.existsSync(pckgJsonPath)) { | ||
console.warn(`The module "${modulePath}" can't be imported and will be replaced with {}`) | ||
return match.replace(r, '{}') | ||
} | ||
if(packageJsonCache[pckgJsonPath]) filename = packageJsonCache[pckgJsonPath] | ||
else { | ||
const packageJsonRaw = fs.readFileSync(pckgJsonPath, 'utf-8') | ||
filename = JSON.parse(packageJsonRaw).main || 'index.js' | ||
packageJsonCache[pckgJsonPath] = filename | ||
} | ||
if(relativePath) { | ||
const relativeDir = filename.split('/') | ||
relativeDir.pop() | ||
fullPath = `/node_modules/${moduleDir}/${relativeDir.join('/')}/${relativePath}` | ||
} else { | ||
fullPath = `/node_modules/${moduleDir}/${filename}` | ||
} | ||
fullPath = fullPath.replace(/\/\.?\//g,'/') | ||
if(!fullPath.endsWith('.js')) fullPath += '.js' | ||
fullPath = getNodeModules(modulePath,rootPath) | ||
if(fullPath === false) return match.replace(r, '{}') | ||
} | ||
@@ -48,0 +23,0 @@ if (Require.contents[fullPath] && Require.contents[fullPath].children.includes(path)) { |
{ | ||
"name": "als-require", | ||
"version": "1.5.2", | ||
"version": "1.5.4", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
49621
35
756
6