Skip to content

Commit 919022d

Browse files
committed
v1.0.0
1 parent 4a3e249 commit 919022d

File tree

6 files changed

+460
-0
lines changed

6 files changed

+460
-0
lines changed

.gitignore

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
/CVS/*
2+
**/CVS/*
3+
.cvsignore
4+
*/.cvsignore
5+
6+
7+
8+
*.patch
9+
*.diff
10+
11+
12+
13+
*~
14+
15+
# temporary files which can be created if a process still has a handle open of a deleted file
16+
.fuse_hidden*
17+
18+
# KDE directory preferences
19+
.directory
20+
21+
# Linux trash folder which might appear on any partition or disk
22+
.Trash-*
23+
24+
# .nfs files are created when an open file is removed but is still being accessed
25+
.nfs*
26+
27+
28+
29+
# Logs
30+
logs
31+
*.log
32+
npm-debug.log*
33+
yarn-debug.log*
34+
yarn-error.log*
35+
lerna-debug.log*
36+
.pnpm-debug.log*
37+
38+
# Diagnostic reports (https://nodejs.org/api/report.html)
39+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
40+
41+
# Runtime data
42+
pids
43+
*.pid
44+
*.seed
45+
*.pid.lock
46+
47+
# Directory for instrumented libs generated by jscoverage/JSCover
48+
lib-cov
49+
50+
# Coverage directory used by tools like istanbul
51+
coverage
52+
*.lcov
53+
54+
# nyc test coverage
55+
.nyc_output
56+
57+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
58+
.grunt
59+
60+
# Bower dependency directory (https://bower.io/)
61+
bower_components
62+
63+
# node-waf configuration
64+
.lock-wscript
65+
66+
# Compiled binary addons (https://nodejs.org/api/addons.html)
67+
build/Release
68+
69+
# Dependency directories
70+
node_modules/
71+
jspm_packages/
72+
73+
# Snowpack dependency directory (https://snowpack.dev/)
74+
web_modules/
75+
76+
# TypeScript cache
77+
*.tsbuildinfo
78+
79+
# Optional npm cache directory
80+
.npm
81+
82+
# Optional eslint cache
83+
.eslintcache
84+
85+
# Optional stylelint cache
86+
.stylelintcache
87+
88+
# Microbundle cache
89+
.rpt2_cache/
90+
.rts2_cache_cjs/
91+
.rts2_cache_es/
92+
.rts2_cache_umd/
93+
94+
# Optional REPL history
95+
.node_repl_history
96+
97+
# Output of 'npm pack'
98+
*.tgz
99+
100+
# Yarn Integrity file
101+
.yarn-integrity
102+
103+
# dotenv environment variables file
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
.cache
111+
.parcel-cache
112+
113+
# Next.js build output
114+
.next
115+
out
116+
117+
# Nuxt.js build / generate output
118+
.nuxt
119+
dist
120+
121+
# Gatsby files
122+
.cache/
123+
# Comment in the public line in if your project uses Gatsby and not Next.js
124+
# https://nextjs.org/blog/next-9-1#public-directory-support
125+
# public
126+
127+
# vuepress build output
128+
.vuepress/dist
129+
130+
# vuepress v2.x temp and cache directory
131+
.temp
132+
.cache
133+
134+
# Serverless directories
135+
.serverless/
136+
137+
# FuseBox cache
138+
.fusebox/
139+
140+
# DynamoDB Local files
141+
.dynamodb/
142+
143+
# TernJS port file
144+
.tern-port
145+
146+
# Stores VSCode versions used for testing VSCode extensions
147+
.vscode-test
148+
149+
# yarn v2
150+
.yarn/cache
151+
.yarn/unplugged
152+
.yarn/build-state.yml
153+
.yarn/install-state.gz
154+
.pnp.*
155+
156+
157+
158+
# Cache files for Sublime Text
159+
*.tmlanguage.cache
160+
*.tmPreferences.cache
161+
*.stTheme.cache
162+
163+
# Workspace files are user-specific
164+
*.sublime-workspace
165+
166+
# Project files should be checked into the repository, unless a significant
167+
# proportion of contributors will probably not be using Sublime Text
168+
# *.sublime-project
169+
170+
# SFTP configuration file
171+
sftp-config.json
172+
sftp-config-alt*.json
173+
174+
# Package control specific files
175+
Package Control.last-run
176+
Package Control.ca-list
177+
Package Control.ca-bundle
178+
Package Control.system-ca-bundle
179+
Package Control.cache/
180+
Package Control.ca-certs/
181+
Package Control.merged-ca-bundle
182+
Package Control.user-ca-bundle
183+
oscrypto-ca-bundle.crt
184+
bh_unicode_properties.cache
185+
186+
# Sublime-github package stores a github token in this file
187+
# https://packagecontrol.io/packages/sublime-github
188+
GitHub.sublime-settings
189+
190+
191+
192+
# Project-level settings
193+
/.tgitconfig
194+
195+
196+
197+
# Swap
198+
[._]*.s[a-v][a-z]
199+
!*.svg # comment out if you don't need vector files
200+
[._]*.sw[a-p]
201+
[._]s[a-rt-v][a-z]
202+
[._]ss[a-gi-z]
203+
[._]sw[a-p]
204+
205+
# Session
206+
Session.vim
207+
Sessionx.vim
208+
209+
# Temporary
210+
.netrwhist
211+
*~
212+
# Auto-generated tag files
213+
tags
214+
# Persistent undo
215+
[._]*.un~
216+
217+
218+
219+
.vscode/*
220+
!.vscode/settings.json
221+
!.vscode/tasks.json
222+
!.vscode/launch.json
223+
!.vscode/extensions.json
224+
*.code-workspace
225+
226+
# Local History for Visual Studio Code
227+
.history/
228+
229+
230+
231+
# Windows thumbnail cache files
232+
Thumbs.db
233+
Thumbs.db:encryptable
234+
ehthumbs.db
235+
ehthumbs_vista.db
236+
237+
# Dump file
238+
*.stackdump
239+
240+
# Folder config file
241+
[Dd]esktop.ini
242+
243+
# Recycle Bin used on file shares
244+
$RECYCLE.BIN/
245+
246+
# Windows Installer files
247+
*.cab
248+
*.msi
249+
*.msix
250+
*.msm
251+
*.msp
252+
253+
# Windows shortcuts
254+
*.lnk
255+
256+
257+
258+
# General
259+
.DS_Store
260+
.AppleDouble
261+
.LSOverride
262+
263+
# Icon must end with two \r
264+
Icon
265+
266+
# Thumbnails
267+
._*
268+
269+
# Files that might appear in the root of a volume
270+
.DocumentRevisions-V100
271+
.fseventsd
272+
.Spotlight-V100
273+
.TemporaryItems
274+
.Trashes
275+
.VolumeIcon.icns
276+
.com.apple.timemachine.donotpresent
277+
278+
# Directories potentially created on remote AFP share
279+
.AppleDB
280+
.AppleDesktop
281+
Network Trash Folder
282+
Temporary Items
283+
.apdisk
284+
© 2021 GitHub, Inc.
285+
Terms

.npmrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
depth=0
2+
loglevel=notice
3+
engine-strict=true
4+
fetch-retries=3
5+
fetch-retry-mintimeout=5
6+
save=true
7+
8+
package-lock=true
9+
10+
scope=@iyowei
11+
@iyowei:registry="https://registry.npmjs.org/"
12+
13+
registry="https://registry.npmmirror.com"

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
[node version badge]: https://img.shields.io/badge/node.js-%3E%3D12.20.0-brightgreen?style=flat&logo=Node.js
2+
[download node.js]: https://nodejs.org/en/download/
3+
[prs welcome badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat
4+
5+
# isESM(path)
6+
7+
> 通过 AST 检测 JS 文件是不是 ESM,并非读取 "package.json " 文件中 "type" 设置。
8+
9+
## 使用
10+
11+
- `path`,{ String },路径
12+
13+
```js
14+
import { log } from "console";
15+
import { isESM, isESMSync } from "@iyowei/is-esm";
16+
17+
(async () => {
18+
log(await isESM("/Users/iyowei/Development/iyowei/create-esm/src/print.js")); // 异步
19+
// true
20+
21+
log(isESMSync("/Users/iyowei/Development/iyowei/create-esm/src/print.js")); // 串行
22+
// true
23+
})();
24+
```
25+
26+
## 安装
27+
28+
[![Node Version Badge][node version badge]][download node.js]
29+
30+
```shell
31+
# Pnpm
32+
pnpm add @iyowei/is-esm
33+
34+
# yarn
35+
yarn add @iyowei/is-esm
36+
37+
# npm
38+
npm add @iyowei/is-esm
39+
```
40+
41+
## 参与贡献
42+
43+
![PRs Welcome][prs welcome badge]

isESM.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { readFile, readFileSync } from "fs";
2+
3+
import Walker from "node-source-walk";
4+
import types from "ast-module-types";
5+
6+
export function isESM(path) {
7+
return new Promise((resolve, reject) => {
8+
readFile(path, "utf-8", (err, file) => {
9+
if (err) {
10+
reject(err);
11+
return;
12+
}
13+
14+
resolve(walk(file));
15+
});
16+
});
17+
}
18+
19+
export function isESMSync(path) {
20+
return walk(readFileSync(path, "utf-8"));
21+
}
22+
23+
function walk(source) {
24+
const WALKER = new Walker();
25+
let result = false;
26+
27+
let hasES6Import = false;
28+
let hasES6Export = false;
29+
let hasDynamicImport = false;
30+
31+
function esmDetector(node) {
32+
if (types.isAMDDriverScriptRequire(node)) {
33+
hasAMDTopLevelRequire = true;
34+
}
35+
36+
if (types.isES6Import(node)) {
37+
hasES6Import = true;
38+
}
39+
40+
if (types.isES6Export(node)) {
41+
hasES6Export = true;
42+
}
43+
44+
if (hasES6Import || hasES6Export || hasDynamicImport) {
45+
result = true;
46+
WALKER.stopWalking();
47+
return;
48+
}
49+
}
50+
51+
WALKER.walk(source, esmDetector);
52+
53+
return result;
54+
}

0 commit comments

Comments
 (0)