@boost/common
Advanced tools
Comparing version 2.6.0 to 2.7.0
@@ -6,2 +6,14 @@ # Change Log | ||
## 2.7.0 - 2021-02-18 | ||
#### 🚀 Updates | ||
- Add `requireTypedModule` helper for importing TypeScript files. (#140) ([b3b14aa](https://github.com/milesj/boost/commit/b3b14aa)), closes [#140](https://github.com/milesj/boost/issues/140) | ||
**Note:** Version bump only for package @boost/common | ||
## 2.6.0 - 2021-02-12 | ||
@@ -8,0 +20,0 @@ |
@@ -13,3 +13,4 @@ export { default as createBlueprint } from './createBlueprint'; | ||
export { default as requireModule } from './requireModule'; | ||
export { default as requireTypedModule } from './requireTypedModule'; | ||
export { default as toArray } from './toArray'; | ||
//# sourceMappingURL=index.d.ts.map |
103
lib/index.js
@@ -678,18 +678,102 @@ // Generated with Packemon: https://packemon.dev | ||
}); | ||
/* eslint-disable no-underscore-dangle */ | ||
function requireModule(path) { | ||
var _value; | ||
function interopRequireModule(path) { | ||
// eslint-disable-next-line | ||
const result = require(path); // Not a Babel/TypeScript transpiled module | ||
if (!result.__esModule) { | ||
return result; | ||
} | ||
const hasDefaultExport = ('default' in result); | ||
const namedExports = Object.keys(result).filter(key => key !== '__esModule' && key !== 'default'); // Default export only | ||
if (hasDefaultExport && namedExports.length === 0) { | ||
return result.default; | ||
} // Default AND named exports | ||
// Named exports only | ||
return result; | ||
} | ||
/* eslint-disable no-underscore-dangle, node/no-deprecated-api */ | ||
let ts = null; | ||
try { | ||
// eslint-disable-next-line | ||
let value = require(String(path)); // Support Babel compiled files | ||
// eslint-disable-next-line no-underscore-dangle | ||
ts = require('typescript'); | ||
} catch {// Ignore and check at runtime | ||
} | ||
const transformCache = new Map(); | ||
if ((_value = value) != null && _value.__esModule) { | ||
value = value.default; | ||
function transform(contents, fileName) { | ||
if (!ts) { | ||
throw new Error(`\`typescript\` package required for importing file "${fileName}".`); | ||
} | ||
return value; | ||
return ts.transpileModule(contents, { | ||
compilerOptions: { | ||
allowJs: true, | ||
allowSyntheticDefaultImports: true, | ||
esModuleInterop: true, | ||
module: ts.ModuleKind.CommonJS, | ||
noEmit: true, | ||
resolveJsonModule: true, | ||
target: ts.ScriptTarget.ES2016 | ||
}, | ||
fileName | ||
}).outputText; | ||
} | ||
function transformHandler(mod, filePath) { | ||
let code = transformCache.get(filePath); | ||
if (!code) { | ||
code = transform(fs__default['default'].readFileSync(filePath, 'utf8'), filePath); | ||
transformCache.set(filePath, code); | ||
} | ||
mod._compile(code, filePath); | ||
} // https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js#L1103 | ||
// https://github.com/TypeStrong/ts-node/blob/master/src/index.ts#L1071 | ||
function registerExtensions() { | ||
require.extensions['.ts'] = transformHandler; | ||
require.extensions['.tsx'] = transformHandler; | ||
} | ||
function unregisterExtensions() { | ||
delete require.extensions['.ts']; | ||
delete require.extensions['.tsx']; | ||
} | ||
function requireTypedModule(path) { | ||
const filePath = String(path); | ||
if (!filePath.endsWith('.ts') && !filePath.endsWith('.tsx')) { | ||
throw new Error(`Unable to import non-TypeScript file "${filePath}", use \`requireModule\` instead.`); | ||
} | ||
registerExtensions(); | ||
const result = interopRequireModule(filePath); | ||
unregisterExtensions(); | ||
return result; | ||
} | ||
function requireModule(path) { | ||
const filePath = String(path); | ||
if (filePath.endsWith('.ts') || filePath.endsWith('.tsx')) { | ||
return requireTypedModule(filePath); | ||
} | ||
return interopRequireModule(filePath); | ||
} | ||
function parseFile(filePath) { | ||
@@ -707,2 +791,6 @@ const path = Path.create(filePath); | ||
case '.ts': | ||
case '.tsx': | ||
return requireTypedModule(path); | ||
case '.json': | ||
@@ -1061,3 +1149,4 @@ case '.json5': | ||
exports.requireModule = requireModule; | ||
exports.requireTypedModule = requireTypedModule; | ||
exports.toArray = toArray; | ||
exports.yaml = yaml; |
{ | ||
"name": "@boost/common", | ||
"version": "2.6.0", | ||
"version": "2.7.0", | ||
"release": "1594765247526", | ||
@@ -38,4 +38,7 @@ "description": "A collection of common utilities, classes, and helpers.", | ||
"devDependencies": { | ||
"@boost/test-utils": "^2.2.0" | ||
"@boost/test-utils": "^2.3.0" | ||
}, | ||
"optionalDependencies": { | ||
"typescript": "^4.0.0" | ||
}, | ||
"funding": { | ||
@@ -48,3 +51,3 @@ "type": "ko-fi", | ||
}, | ||
"gitHead": "aa54fa00931b016ce03704f2db316239886f6c53" | ||
"gitHead": "246a8229cff56954e26da9f7d5e61d1d990aa369" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
77625
61
1346
8