-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs ~ add ESM dynamic import example
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-env es6, node */ | ||
'use strict'; | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { URL } from 'url'; | ||
|
||
const pkg = JSON.parse( | ||
// eslint-disable-next-line security/detect-non-literal-fs-filename | ||
fs.readFileSync(new URL(path.join('..', 'package.json'), import.meta.url), 'utf8') | ||
); | ||
|
||
const moduleURL = new URL(path.join('..', pkg.module), import.meta.url); | ||
|
||
/* eslint-disable no-console , functional/immutable-data , security/detect-object-injection, security-node/detect-crlf */ | ||
|
||
function objectEntries(obj) { | ||
const map = {}; | ||
Object.keys(obj).forEach((key) => { | ||
const value = obj[key]; | ||
const val = typeof value === 'function' ? value() : value; | ||
map[key] = val; | ||
}); | ||
return map; | ||
} | ||
|
||
(async function main() { | ||
const osPaths = (await import(moduleURL)).default; | ||
|
||
console.log({ osPaths }); | ||
console.log(objectEntries(osPaths)); | ||
|
||
process.env.TMPDIR = process.env.TEMP = process.env.TMP = 'temp'; | ||
console.log(objectEntries(osPaths)); | ||
})(); | ||
|
||
/* eslint-enable no-console , functional/immutable-data , security/detect-object-injection, security-node/detect-crlf */ |