Skip to content

Commit 0410fd0

Browse files
Merge pull request #5 from jspm/fix/ensure-file-sync
(fix): ensureFileSync and drop cjs build
2 parents 1b2e0b3 + b3e46f4 commit 0410fd0

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

chompfile.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
version = 0.1
22

3+
default-task = 'build'
4+
35
[[task]]
46
name = 'build'
57
run = 'tsup src/loader.ts --format esm,cjs --dts'
@@ -42,8 +44,6 @@ name = 'pre-commit'
4244
run = 'lint-staged'
4345
deps = ['src/#.ts', 'tests/loader.test.ts']
4446

45-
[[task]]
46-
4747
[[task]]
4848
name = 'prepare'
4949
run = 'husky install'

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
"author": "Jaya Krishna Namburu <[email protected]>",
66
"type": "module",
77
"main": "dist/loader.js",
8-
"module": "dist/loader.js",
98
"types": "dist/loader.d.ts",
109
"exports": {
11-
".": "dist/loader.cjs"
10+
".": "dist/loader.js"
1211
},
1312
"scripts": {
14-
"build": "tsup src/loader.ts --format esm,cjs --dts",
13+
"build": "tsup src/loader.ts --format esm --dts",
1514
"clean": "rimraf dist",
1615
"commit": "git-cz",
1716
"commit-msg": "commitlint --edit $1",

src/loader.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { accessSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2-
import { join } from "node:path";
1+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2+
import { join, dirname } from "node:path";
33
import { fileURLToPath } from "node:url";
44
import { ImportMap } from "@jspm/import-map";
55
import fetch from "node-fetch";
@@ -8,16 +8,41 @@ import { ConstructCachePath, Context, NextResolve, UrlType } from './types'
88
// hoist the cache map
99
const cacheMap = new Map();
1010

11+
/**
12+
* nsureDirSync
13+
* @description a function to ensure the dis exists
14+
* @param dirPath
15+
*/
16+
17+
function ensureDirSync(dirPath: string) {
18+
if (existsSync(dirPath)) {
19+
return;
20+
}
21+
22+
const parentDir = dirname(dirPath);
23+
if (parentDir !== dirPath) {
24+
ensureDirSync(parentDir);
25+
}
26+
27+
mkdirSync(dirPath);
28+
}
29+
1130
/**
1231
* ensureFileSync
1332
* @description a convenience function to ensure a file exists
1433
* @param path
1534
*/
16-
export function ensureFileSync(path: string) {
35+
function ensureFileSync(path: string) {
36+
const dirPath = dirname(path);
37+
38+
if (!existsSync(dirPath)) {
39+
ensureDirSync(dirPath);
40+
}
41+
1742
try {
18-
accessSync(path);
19-
} catch (err) {
20-
writeFileSync(path, '');
43+
writeFileSync(path, '', { flag: 'wx' });
44+
} catch {
45+
console.log(`Failed in creating ${path}`)
2146
}
2247
}
2348

@@ -166,13 +191,15 @@ export const parseModule = async (specifier: string, modulePath: string) => {
166191
const { protocol } = new URL(modulePath);
167192
const isNode = protocol === "node:";
168193
const isFile = protocol === "file:";
194+
169195
if (isNode || isFile) return specifier;
170196

171197
const cachePath = constructCachePath({ cache, modulePath })
198+
172199
cacheMap.set(`file://${cachePath}`, modulePath);
173200
if (existsSync(cachePath)) return cachePath
174-
175201
const code = await (await fetch(modulePath)).text();
202+
176203
ensureFileSync(cachePath);
177204
writeFileSync(cachePath, code);
178205

tests/e2e/test-prettier.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import standalone from 'prettier/standalone'
2+
import parserBabel from 'prettier/parser-babel'
3+
const { format } = standalone
4+
5+
const CODE = String(`
6+
function HelloWorld({greeting = "hello", greeted = '"World"', silent = false, onMouseOver,}) {
7+
8+
if(!greeting){return null};
9+
10+
// TODO: Don't use random in render
11+
let num = Math.floor (Math.random() * 1E+7).toString().replace(/\.\d+/ig, "")
12+
13+
return <div className='HelloWorld' onMouseOver={onMouseOver}>
14+
15+
<strong>{ greeting.slice( 0, 1 ).toUpperCase() + greeting.slice(1).toLowerCase() }</strong>
16+
{greeting.endsWith(",") ? " " : <span style={{color: '\grey'}}>", "</span> }
17+
<em>
18+
{ greeted }
19+
</em>
20+
{ (silent)
21+
? "."
22+
: "!"}
23+
24+
</div>;
25+
26+
}
27+
`)
28+
29+
const result = format(CODE, { parser: 'babel', plugins: [parserBabel] })
30+
console.log(result)

0 commit comments

Comments
 (0)