@jsenv/plugin-commonjs 2.0.5 → 2.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -5
- package/src/cjs_to_esm.js +30 -30
- package/src/cjs_to_esm_raw.js +34 -34
- package/src/compile_cache/compiled_file_cache.js +67 -67
- package/src/compile_cache/file_lock_registry.js +15 -15
- package/src/compile_cache/update_compile_cache.js +31 -31
- package/src/compile_cache/validate_compile_cache.js +111 -111
- package/src/jsenv_plugin_commonjs.js +31 -31
- package/src/main.js +2 -2
- package/src/rollup_plugin_commonjs_named_exports.js +70 -70
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { existsSync, utimesSync } from "node:fs"
|
|
2
|
-
import { fileURLToPath } from "node:url"
|
|
3
|
-
import { urlToRelativeUrl } from "@jsenv/urls"
|
|
4
|
-
import { writeFileSync, bufferToEtag } from "@jsenv/filesystem"
|
|
1
|
+
import { existsSync, utimesSync } from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { urlToRelativeUrl } from "@jsenv/urls";
|
|
4
|
+
import { writeFileSync, bufferToEtag } from "@jsenv/filesystem";
|
|
5
5
|
|
|
6
6
|
export const updateCompileCache = ({
|
|
7
7
|
logger,
|
|
@@ -11,60 +11,60 @@ export const updateCompileCache = ({
|
|
|
11
11
|
mtime,
|
|
12
12
|
compileResultStatus,
|
|
13
13
|
}) => {
|
|
14
|
-
const isNew = compileResultStatus === "created"
|
|
15
|
-
const isUpdated = compileResultStatus === "updated"
|
|
14
|
+
const isNew = compileResultStatus === "created";
|
|
15
|
+
const isUpdated = compileResultStatus === "updated";
|
|
16
16
|
if (!isNew && !isUpdated) {
|
|
17
|
-
return
|
|
17
|
+
return;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
// ensure source that does not leads to files are not capable to invalidate the cache
|
|
21
|
-
const filesRemoved = []
|
|
21
|
+
const filesRemoved = [];
|
|
22
22
|
Object.keys(assets).forEach((assetUrl) => {
|
|
23
23
|
if (
|
|
24
24
|
assetUrl.startsWith("file://") &&
|
|
25
25
|
assets[assetUrl].type === "source" &&
|
|
26
26
|
!existsSync(new URL(assetUrl))
|
|
27
27
|
) {
|
|
28
|
-
delete assets[assetUrl]
|
|
29
|
-
filesRemoved.push(assetUrl)
|
|
28
|
+
delete assets[assetUrl];
|
|
29
|
+
filesRemoved.push(assetUrl);
|
|
30
30
|
}
|
|
31
|
-
})
|
|
32
|
-
const notFoundCount = filesRemoved.length
|
|
31
|
+
});
|
|
32
|
+
const notFoundCount = filesRemoved.length;
|
|
33
33
|
if (notFoundCount > 0) {
|
|
34
34
|
logger.warn(`COMPILE_ASSET_FILE_NOT_FOUND: ${notFoundCount} file(s) not found.
|
|
35
35
|
--- consequence ---
|
|
36
36
|
cache will be reused even if one of the source file is modified
|
|
37
37
|
--- files not found ---
|
|
38
|
-
${filesRemoved.join(`\n`)}`)
|
|
38
|
+
${filesRemoved.join(`\n`)}`);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
logger.debug(`write compiled file at ${fileURLToPath(compiledFileUrl)}`)
|
|
41
|
+
logger.debug(`write compiled file at ${fileURLToPath(compiledFileUrl)}`);
|
|
42
42
|
writeFileSync(compiledFileUrl, content, {
|
|
43
43
|
fileLikelyNotFound: isNew,
|
|
44
|
-
})
|
|
44
|
+
});
|
|
45
45
|
// mtime is passed, it meant the file mtime is important
|
|
46
46
|
// -> we update file mtime
|
|
47
47
|
if (mtime) {
|
|
48
|
-
utimesSync(new URL(compiledFileUrl), new Date(mtime), new Date(mtime))
|
|
48
|
+
utimesSync(new URL(compiledFileUrl), new Date(mtime), new Date(mtime));
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
const assetInfos = {}
|
|
51
|
+
const assetInfos = {};
|
|
52
52
|
Object.keys(assets).forEach((assetUrl) => {
|
|
53
|
-
logger.debug(`write compiled file asset at ${fileURLToPath(assetUrl)}`)
|
|
54
|
-
const asset = assets[assetUrl]
|
|
53
|
+
logger.debug(`write compiled file asset at ${fileURLToPath(assetUrl)}`);
|
|
54
|
+
const asset = assets[assetUrl];
|
|
55
55
|
writeFileSync(assetUrl, asset.content, {
|
|
56
56
|
fileLikelyNotFound: isNew,
|
|
57
|
-
})
|
|
58
|
-
const assetRelativeUrl = urlToRelativeUrl(assetUrl, compiledFileUrl)
|
|
59
|
-
const assetEtag = asset.etag || bufferToEtag(Buffer.from(asset.content))
|
|
57
|
+
});
|
|
58
|
+
const assetRelativeUrl = urlToRelativeUrl(assetUrl, compiledFileUrl);
|
|
59
|
+
const assetEtag = asset.etag || bufferToEtag(Buffer.from(asset.content));
|
|
60
60
|
assetInfos[assetRelativeUrl] = {
|
|
61
61
|
type: asset.type,
|
|
62
62
|
etag: assetEtag,
|
|
63
|
-
}
|
|
64
|
-
})
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
65
|
|
|
66
|
-
const compileInfoFileUrl = `${compiledFileUrl}__compile_info__.json
|
|
67
|
-
let latestCompileInfo
|
|
66
|
+
const compileInfoFileUrl = `${compiledFileUrl}__compile_info__.json`;
|
|
67
|
+
let latestCompileInfo;
|
|
68
68
|
if (isNew) {
|
|
69
69
|
latestCompileInfo = {
|
|
70
70
|
// was used at some point to ensure the compiled file matches browser etag
|
|
@@ -72,23 +72,23 @@ ${filesRemoved.join(`\n`)}`)
|
|
|
72
72
|
assetInfos,
|
|
73
73
|
createdMs: Date.now(),
|
|
74
74
|
lastModifiedMs: Date.now(),
|
|
75
|
-
}
|
|
75
|
+
};
|
|
76
76
|
} else if (isUpdated) {
|
|
77
77
|
latestCompileInfo = {
|
|
78
78
|
// was used at some point to ensure the compiled file matches browser etag
|
|
79
79
|
// etag: bufferToEtag(Buffer.from(content)),
|
|
80
80
|
assetInfos,
|
|
81
81
|
lastModifiedMs: Date.now(),
|
|
82
|
-
}
|
|
82
|
+
};
|
|
83
83
|
}
|
|
84
84
|
logger.debug(
|
|
85
85
|
`write compiled file info at ${fileURLToPath(compileInfoFileUrl)}`,
|
|
86
|
-
)
|
|
86
|
+
);
|
|
87
87
|
writeFileSync(
|
|
88
88
|
compileInfoFileUrl,
|
|
89
89
|
JSON.stringify(latestCompileInfo, null, " "),
|
|
90
90
|
{
|
|
91
91
|
fileLikelyNotFound: isNew,
|
|
92
92
|
},
|
|
93
|
-
)
|
|
94
|
-
}
|
|
93
|
+
);
|
|
94
|
+
};
|
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import { readFileSync, statSync } from "node:fs"
|
|
2
|
-
import { bufferToEtag } from "@jsenv/filesystem"
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { bufferToEtag } from "@jsenv/filesystem";
|
|
3
3
|
|
|
4
4
|
export const validateCompileCache = ({
|
|
5
5
|
compiledFileUrl,
|
|
6
6
|
compileCacheStrategy,
|
|
7
7
|
compileCacheAssetsValidation = true,
|
|
8
8
|
}) => {
|
|
9
|
-
const validity = { isValid: true }
|
|
9
|
+
const validity = { isValid: true };
|
|
10
10
|
const compileInfoValidity = validateCompileInfoFile({
|
|
11
11
|
compiledFileUrl,
|
|
12
|
-
})
|
|
13
|
-
validity.compileInfo = compileInfoValidity
|
|
14
|
-
mergeValidity(validity, compileInfoValidity)
|
|
12
|
+
});
|
|
13
|
+
validity.compileInfo = compileInfoValidity;
|
|
14
|
+
mergeValidity(validity, compileInfoValidity);
|
|
15
15
|
if (!validity.isValid) {
|
|
16
|
-
return validity
|
|
16
|
+
return validity;
|
|
17
17
|
}
|
|
18
18
|
const compiledFileValidity = validateCompiledFile({
|
|
19
19
|
compiledFileUrl,
|
|
20
20
|
compileCacheStrategy,
|
|
21
|
-
})
|
|
22
|
-
validity.compiledFile = compiledFileValidity
|
|
23
|
-
mergeValidity(validity, compiledFileValidity)
|
|
21
|
+
});
|
|
22
|
+
validity.compiledFile = compiledFileValidity;
|
|
23
|
+
mergeValidity(validity, compiledFileValidity);
|
|
24
24
|
if (!validity.isValid) {
|
|
25
|
-
return validity
|
|
25
|
+
return validity;
|
|
26
26
|
}
|
|
27
|
-
const compileInfo = compileInfoValidity.data
|
|
27
|
+
const compileInfo = compileInfoValidity.data;
|
|
28
28
|
const assetsValidity = compileCacheAssetsValidation
|
|
29
29
|
? validateAssets({
|
|
30
30
|
compiledFileUrl,
|
|
31
31
|
compileInfo,
|
|
32
32
|
})
|
|
33
|
-
: { isValid: true, code: "ASSETS_VALIDATION_DISABLED" }
|
|
34
|
-
validity.assets = assetsValidity
|
|
35
|
-
mergeValidity(validity, assetsValidity)
|
|
33
|
+
: { isValid: true, code: "ASSETS_VALIDATION_DISABLED" };
|
|
34
|
+
validity.assets = assetsValidity;
|
|
35
|
+
mergeValidity(validity, assetsValidity);
|
|
36
36
|
if (!validity.isValid) {
|
|
37
|
-
return validity
|
|
37
|
+
return validity;
|
|
38
38
|
}
|
|
39
|
-
return validity
|
|
40
|
-
}
|
|
39
|
+
return validity;
|
|
40
|
+
};
|
|
41
41
|
|
|
42
42
|
const validateCompileInfoFile = ({ compiledFileUrl }) => {
|
|
43
|
-
const compileInfoFileUrl = `${compiledFileUrl}__compile_info__.json
|
|
44
|
-
const validity = { isValid: true, data: {} }
|
|
45
|
-
let compileInfoFileContentAsBuffer
|
|
43
|
+
const compileInfoFileUrl = `${compiledFileUrl}__compile_info__.json`;
|
|
44
|
+
const validity = { isValid: true, data: {} };
|
|
45
|
+
let compileInfoFileContentAsBuffer;
|
|
46
46
|
try {
|
|
47
|
-
compileInfoFileContentAsBuffer = readFileSync(new URL(compileInfoFileUrl))
|
|
47
|
+
compileInfoFileContentAsBuffer = readFileSync(new URL(compileInfoFileUrl));
|
|
48
48
|
} catch (error) {
|
|
49
49
|
if (error && error.code === "ENOENT") {
|
|
50
|
-
validity.isValid = false
|
|
51
|
-
validity.code = "COMPILE_INFO_FILE_NOT_FOUND"
|
|
52
|
-
return validity
|
|
50
|
+
validity.isValid = false;
|
|
51
|
+
validity.code = "COMPILE_INFO_FILE_NOT_FOUND";
|
|
52
|
+
return validity;
|
|
53
53
|
}
|
|
54
|
-
throw error
|
|
54
|
+
throw error;
|
|
55
55
|
}
|
|
56
|
-
const compileInfoFileContentAsString = String(compileInfoFileContentAsBuffer)
|
|
57
|
-
let compileInfo
|
|
56
|
+
const compileInfoFileContentAsString = String(compileInfoFileContentAsBuffer);
|
|
57
|
+
let compileInfo;
|
|
58
58
|
try {
|
|
59
|
-
compileInfo = JSON.parse(compileInfoFileContentAsString)
|
|
59
|
+
compileInfo = JSON.parse(compileInfoFileContentAsString);
|
|
60
60
|
} catch (error) {
|
|
61
61
|
if (error && error.name === "SyntaxError") {
|
|
62
|
-
validity.isValid = false
|
|
63
|
-
validity.code = "COMPILE_INFO_FILE_SYNTAX_ERROR"
|
|
64
|
-
return validity
|
|
62
|
+
validity.isValid = false;
|
|
63
|
+
validity.code = "COMPILE_INFO_FILE_SYNTAX_ERROR";
|
|
64
|
+
return validity;
|
|
65
65
|
}
|
|
66
|
-
throw error
|
|
66
|
+
throw error;
|
|
67
67
|
}
|
|
68
|
-
validity.data = compileInfo
|
|
68
|
+
validity.data = compileInfo;
|
|
69
69
|
if (Object.keys(compileInfo.assetInfos).length === 0) {
|
|
70
|
-
validity.isValid = false
|
|
71
|
-
validity.code = "ASSETS_EMPTY"
|
|
72
|
-
return validity
|
|
70
|
+
validity.isValid = false;
|
|
71
|
+
validity.code = "ASSETS_EMPTY";
|
|
72
|
+
return validity;
|
|
73
73
|
}
|
|
74
|
-
return validity
|
|
75
|
-
}
|
|
74
|
+
return validity;
|
|
75
|
+
};
|
|
76
76
|
|
|
77
77
|
const validateCompiledFile = ({
|
|
78
78
|
compiledFileUrl,
|
|
@@ -80,28 +80,28 @@ const validateCompiledFile = ({
|
|
|
80
80
|
lastEtag,
|
|
81
81
|
lastModificationTime,
|
|
82
82
|
}) => {
|
|
83
|
-
const validity = { isValid: true, data: {} }
|
|
83
|
+
const validity = { isValid: true, data: {} };
|
|
84
84
|
try {
|
|
85
|
-
const buffer = readFileSync(new URL(compiledFileUrl))
|
|
86
|
-
validity.data.buffer = buffer
|
|
85
|
+
const buffer = readFileSync(new URL(compiledFileUrl));
|
|
86
|
+
validity.data.buffer = buffer;
|
|
87
87
|
if (compileCacheStrategy === "etag" && lastEtag) {
|
|
88
|
-
const etag = bufferToEtag(buffer)
|
|
89
|
-
validity.data.etag = etag
|
|
88
|
+
const etag = bufferToEtag(buffer);
|
|
89
|
+
validity.data.etag = etag;
|
|
90
90
|
if (lastEtag && lastEtag !== etag) {
|
|
91
|
-
validity.isValid = false
|
|
92
|
-
validity.code = "COMPILED_FILE_ETAG_MISMATCH"
|
|
93
|
-
return validity
|
|
91
|
+
validity.isValid = false;
|
|
92
|
+
validity.code = "COMPILED_FILE_ETAG_MISMATCH";
|
|
93
|
+
return validity;
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
if (compileCacheStrategy === "mtime" && lastModificationTime) {
|
|
97
|
-
const stats = statSync(new URL(compiledFileUrl))
|
|
98
|
-
const mtime = Math.floor(stats.mtimeMs)
|
|
99
|
-
validity.data.mtime = mtime
|
|
100
|
-
let ifModifiedSinceDate
|
|
97
|
+
const stats = statSync(new URL(compiledFileUrl));
|
|
98
|
+
const mtime = Math.floor(stats.mtimeMs);
|
|
99
|
+
validity.data.mtime = mtime;
|
|
100
|
+
let ifModifiedSinceDate;
|
|
101
101
|
try {
|
|
102
|
-
ifModifiedSinceDate = new Date(lastModificationTime)
|
|
102
|
+
ifModifiedSinceDate = new Date(lastModificationTime);
|
|
103
103
|
} catch (e) {
|
|
104
|
-
ifModifiedSinceDate = null
|
|
104
|
+
ifModifiedSinceDate = null;
|
|
105
105
|
// ideally we should rather respond with
|
|
106
106
|
// 400 "if-modified-since header is not a valid date"
|
|
107
107
|
}
|
|
@@ -109,63 +109,63 @@ const validateCompiledFile = ({
|
|
|
109
109
|
ifModifiedSinceDate &&
|
|
110
110
|
ifModifiedSinceDate < dateToSecondsPrecision(mtime)
|
|
111
111
|
) {
|
|
112
|
-
validity.isValid = false
|
|
113
|
-
validity.code = "COMPILED_FILE_MTIME_OUTDATED"
|
|
114
|
-
return validity
|
|
112
|
+
validity.isValid = false;
|
|
113
|
+
validity.code = "COMPILED_FILE_MTIME_OUTDATED";
|
|
114
|
+
return validity;
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
-
return validity
|
|
117
|
+
return validity;
|
|
118
118
|
} catch (error) {
|
|
119
119
|
if (error && error.code === "ENOENT") {
|
|
120
|
-
validity.isValid = false
|
|
121
|
-
validity.code = "COMPILED_FILE_NOT_FOUND"
|
|
122
|
-
return validity
|
|
120
|
+
validity.isValid = false;
|
|
121
|
+
validity.code = "COMPILED_FILE_NOT_FOUND";
|
|
122
|
+
return validity;
|
|
123
123
|
}
|
|
124
|
-
throw error
|
|
124
|
+
throw error;
|
|
125
125
|
}
|
|
126
|
-
}
|
|
126
|
+
};
|
|
127
127
|
|
|
128
128
|
const validateAssets = ({ compiledFileUrl, compileInfo }) => {
|
|
129
|
-
const assetsValidity = { isValid: true, data: {} }
|
|
129
|
+
const assetsValidity = { isValid: true, data: {} };
|
|
130
130
|
|
|
131
|
-
const assetRelativeUrls = Object.keys(compileInfo.assetInfos)
|
|
131
|
+
const assetRelativeUrls = Object.keys(compileInfo.assetInfos);
|
|
132
132
|
for (const assetRelativeUrl of assetRelativeUrls) {
|
|
133
|
-
const assetInfo = compileInfo.assetInfos[assetRelativeUrl]
|
|
134
|
-
const assetUrl = new URL(assetRelativeUrl, compiledFileUrl).href
|
|
135
|
-
const assetValidity = { isValid: true, data: {} }
|
|
133
|
+
const assetInfo = compileInfo.assetInfos[assetRelativeUrl];
|
|
134
|
+
const assetUrl = new URL(assetRelativeUrl, compiledFileUrl).href;
|
|
135
|
+
const assetValidity = { isValid: true, data: {} };
|
|
136
136
|
if (assetInfo.type === "source") {
|
|
137
137
|
validateSource(assetValidity, {
|
|
138
138
|
sourceFileUrl: assetUrl,
|
|
139
139
|
eTag: assetInfo.etag,
|
|
140
|
-
})
|
|
140
|
+
});
|
|
141
141
|
}
|
|
142
142
|
if (assetInfo.type === "sourcemap") {
|
|
143
143
|
validateSourcemap(assetValidity, {
|
|
144
144
|
sourcemapFileUrl: assetUrl,
|
|
145
|
-
})
|
|
145
|
+
});
|
|
146
146
|
}
|
|
147
|
-
assetsValidity.data[assetUrl] = assetValidity
|
|
148
|
-
mergeValidity(assetsValidity, assetValidity)
|
|
147
|
+
assetsValidity.data[assetUrl] = assetValidity;
|
|
148
|
+
mergeValidity(assetsValidity, assetValidity);
|
|
149
149
|
if (!assetsValidity.isValid) {
|
|
150
|
-
break
|
|
150
|
+
break;
|
|
151
151
|
}
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
return assetsValidity
|
|
155
|
-
}
|
|
154
|
+
return assetsValidity;
|
|
155
|
+
};
|
|
156
156
|
|
|
157
157
|
const validateSource = (validity, { sourceFileUrl, eTag }) => {
|
|
158
158
|
try {
|
|
159
|
-
const sourceBuffer = readFileSync(new URL(sourceFileUrl))
|
|
160
|
-
const sourceETag = bufferToEtag(sourceBuffer)
|
|
161
|
-
validity.data.content = String(sourceBuffer)
|
|
162
|
-
validity.data.etag = sourceETag
|
|
159
|
+
const sourceBuffer = readFileSync(new URL(sourceFileUrl));
|
|
160
|
+
const sourceETag = bufferToEtag(sourceBuffer);
|
|
161
|
+
validity.data.content = String(sourceBuffer);
|
|
162
|
+
validity.data.etag = sourceETag;
|
|
163
163
|
if (sourceETag !== eTag) {
|
|
164
|
-
validity.isValid = false
|
|
165
|
-
validity.code = "SOURCE_ETAG_MISMATCH"
|
|
166
|
-
return validity
|
|
164
|
+
validity.isValid = false;
|
|
165
|
+
validity.code = "SOURCE_ETAG_MISMATCH";
|
|
166
|
+
return validity;
|
|
167
167
|
}
|
|
168
|
-
return validity
|
|
168
|
+
return validity;
|
|
169
169
|
} catch (e) {
|
|
170
170
|
if (e && e.code === "ENOENT") {
|
|
171
171
|
// missing source invalidates the cache because
|
|
@@ -177,50 +177,50 @@ const validateSource = (validity, { sourceFileUrl, eTag }) => {
|
|
|
177
177
|
// which are not truly on the filesystem
|
|
178
178
|
// (IN theory the above happens only for convertCommonJsWithRollup because jsenv
|
|
179
179
|
// always have a concrete file especially to avoid that kind of thing)
|
|
180
|
-
validity.isValid = false
|
|
181
|
-
validity.code = "SOURCE_NOT_FOUND"
|
|
182
|
-
return validity
|
|
180
|
+
validity.isValid = false;
|
|
181
|
+
validity.code = "SOURCE_NOT_FOUND";
|
|
182
|
+
return validity;
|
|
183
183
|
}
|
|
184
|
-
throw e
|
|
184
|
+
throw e;
|
|
185
185
|
}
|
|
186
|
-
}
|
|
186
|
+
};
|
|
187
187
|
|
|
188
188
|
const validateSourcemap = (validity, { sourcemapFileUrl }) => {
|
|
189
|
-
let sourcemapFileContentAsBuffer
|
|
189
|
+
let sourcemapFileContentAsBuffer;
|
|
190
190
|
try {
|
|
191
|
-
sourcemapFileContentAsBuffer = readFileSync(new URL(sourcemapFileUrl))
|
|
191
|
+
sourcemapFileContentAsBuffer = readFileSync(new URL(sourcemapFileUrl));
|
|
192
192
|
} catch (error) {
|
|
193
193
|
if (error && error.code === "ENOENT") {
|
|
194
|
-
validity.isValid = false
|
|
195
|
-
validity.code = "SOURCEMAP_FILE_NOT_FOUND"
|
|
196
|
-
return validity
|
|
194
|
+
validity.isValid = false;
|
|
195
|
+
validity.code = "SOURCEMAP_FILE_NOT_FOUND";
|
|
196
|
+
return validity;
|
|
197
197
|
}
|
|
198
|
-
throw error
|
|
198
|
+
throw error;
|
|
199
199
|
}
|
|
200
|
-
const sourcemapFileContentAsString = String(sourcemapFileContentAsBuffer)
|
|
201
|
-
validity.data.content = sourcemapFileContentAsString
|
|
202
|
-
let sourcemap
|
|
200
|
+
const sourcemapFileContentAsString = String(sourcemapFileContentAsBuffer);
|
|
201
|
+
validity.data.content = sourcemapFileContentAsString;
|
|
202
|
+
let sourcemap;
|
|
203
203
|
try {
|
|
204
|
-
sourcemap = JSON.parse(sourcemapFileContentAsString)
|
|
204
|
+
sourcemap = JSON.parse(sourcemapFileContentAsString);
|
|
205
205
|
} catch (error) {
|
|
206
206
|
if (error && error.name === "SyntaxError") {
|
|
207
|
-
validity.isValid = false
|
|
208
|
-
validity.code = "SOURCEMAP_FILE_SYNTAX_ERROR"
|
|
209
|
-
return validity
|
|
207
|
+
validity.isValid = false;
|
|
208
|
+
validity.code = "SOURCEMAP_FILE_SYNTAX_ERROR";
|
|
209
|
+
return validity;
|
|
210
210
|
}
|
|
211
|
-
throw error
|
|
211
|
+
throw error;
|
|
212
212
|
}
|
|
213
|
-
validity.data.sourcemap = sourcemap
|
|
214
|
-
return validity
|
|
215
|
-
}
|
|
213
|
+
validity.data.sourcemap = sourcemap;
|
|
214
|
+
return validity;
|
|
215
|
+
};
|
|
216
216
|
|
|
217
217
|
const mergeValidity = (parentValidity, childValidity) => {
|
|
218
|
-
parentValidity.isValid = childValidity.isValid
|
|
219
|
-
if (childValidity.code) parentValidity.code = childValidity.code
|
|
220
|
-
}
|
|
218
|
+
parentValidity.isValid = childValidity.isValid;
|
|
219
|
+
if (childValidity.code) parentValidity.code = childValidity.code;
|
|
220
|
+
};
|
|
221
221
|
|
|
222
222
|
const dateToSecondsPrecision = (date) => {
|
|
223
|
-
const dateWithSecondsPrecision = new Date(date)
|
|
224
|
-
dateWithSecondsPrecision.setMilliseconds(0)
|
|
225
|
-
return dateWithSecondsPrecision
|
|
226
|
-
}
|
|
223
|
+
const dateWithSecondsPrecision = new Date(date);
|
|
224
|
+
dateWithSecondsPrecision.setMilliseconds(0);
|
|
225
|
+
return dateWithSecondsPrecision;
|
|
226
|
+
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { URL_META } from "@jsenv/url-meta"
|
|
2
|
-
import { injectQueryParams, urlToExtension } from "@jsenv/urls"
|
|
3
|
-
import { defaultLookupPackageScope } from "@jsenv/node-esm-resolution"
|
|
1
|
+
import { URL_META } from "@jsenv/url-meta";
|
|
2
|
+
import { injectQueryParams, urlToExtension } from "@jsenv/urls";
|
|
3
|
+
import { defaultLookupPackageScope } from "@jsenv/node-esm-resolution";
|
|
4
4
|
|
|
5
|
-
import { commonJsToJsModule } from "./cjs_to_esm.js"
|
|
5
|
+
import { commonJsToJsModule } from "./cjs_to_esm.js";
|
|
6
6
|
|
|
7
|
-
const compileCacheDirectoryUrl = new URL("../.cache/", import.meta.url)
|
|
7
|
+
const compileCacheDirectoryUrl = new URL("../.cache/", import.meta.url);
|
|
8
8
|
|
|
9
9
|
export const jsenvPluginCommonJs = ({
|
|
10
10
|
name = "jsenv:commonjs",
|
|
@@ -12,23 +12,23 @@ export const jsenvPluginCommonJs = ({
|
|
|
12
12
|
include,
|
|
13
13
|
}) => {
|
|
14
14
|
const markAsJsModuleProxy = (reference) => {
|
|
15
|
-
reference.expectedType = "js_module"
|
|
16
|
-
const packageFileUrl = defaultLookupPackageScope(reference.url)
|
|
15
|
+
reference.expectedType = "js_module";
|
|
16
|
+
const packageFileUrl = defaultLookupPackageScope(reference.url);
|
|
17
17
|
if (packageFileUrl) {
|
|
18
18
|
reference.filename = `${reference.specifier}${urlToExtension(
|
|
19
19
|
reference.parentUrl,
|
|
20
|
-
)}
|
|
20
|
+
)}`;
|
|
21
21
|
}
|
|
22
|
-
}
|
|
22
|
+
};
|
|
23
23
|
const turnIntoJsModuleProxy = (reference) => {
|
|
24
24
|
const urlTransformed = injectQueryParams(reference.url, {
|
|
25
25
|
cjs_as_js_module: "",
|
|
26
|
-
})
|
|
27
|
-
markAsJsModuleProxy(reference)
|
|
28
|
-
return urlTransformed
|
|
29
|
-
}
|
|
26
|
+
});
|
|
27
|
+
markAsJsModuleProxy(reference);
|
|
28
|
+
return urlTransformed;
|
|
29
|
+
};
|
|
30
30
|
|
|
31
|
-
let associations
|
|
31
|
+
let associations;
|
|
32
32
|
|
|
33
33
|
return {
|
|
34
34
|
name,
|
|
@@ -39,22 +39,22 @@ export const jsenvPluginCommonJs = ({
|
|
|
39
39
|
commonjs: include,
|
|
40
40
|
},
|
|
41
41
|
rootDirectoryUrl,
|
|
42
|
-
)
|
|
42
|
+
);
|
|
43
43
|
},
|
|
44
|
-
|
|
44
|
+
redirectReference: (reference) => {
|
|
45
45
|
if (reference.searchParams.has("cjs_as_js_module")) {
|
|
46
|
-
markAsJsModuleProxy(reference)
|
|
47
|
-
return null
|
|
46
|
+
markAsJsModuleProxy(reference);
|
|
47
|
+
return null;
|
|
48
48
|
}
|
|
49
49
|
const { commonjs } = URL_META.applyAssociations({
|
|
50
50
|
url: reference.url,
|
|
51
51
|
associations,
|
|
52
|
-
})
|
|
52
|
+
});
|
|
53
53
|
if (!commonjs) {
|
|
54
|
-
return null
|
|
54
|
+
return null;
|
|
55
55
|
}
|
|
56
|
-
reference.data.commonjs = commonjs
|
|
57
|
-
return turnIntoJsModuleProxy(reference)
|
|
56
|
+
reference.data.commonjs = commonjs;
|
|
57
|
+
return turnIntoJsModuleProxy(reference);
|
|
58
58
|
},
|
|
59
59
|
fetchUrlContent: async (urlInfo, context) => {
|
|
60
60
|
const [commonJsReference, commonJsUrlInfo] =
|
|
@@ -65,16 +65,16 @@ export const jsenvPluginCommonJs = ({
|
|
|
65
65
|
// during this fetch we don't want to alter the original file
|
|
66
66
|
// so we consider it as text
|
|
67
67
|
expectedType: "text",
|
|
68
|
-
})
|
|
68
|
+
});
|
|
69
69
|
if (!commonJsReference) {
|
|
70
|
-
return null
|
|
70
|
+
return null;
|
|
71
71
|
}
|
|
72
72
|
await context.fetchUrlContent(commonJsUrlInfo, {
|
|
73
73
|
reference: commonJsReference,
|
|
74
|
-
})
|
|
74
|
+
});
|
|
75
75
|
const nodeRuntimeEnabled = Object.keys(context.runtimeCompat).includes(
|
|
76
76
|
"node",
|
|
77
|
-
)
|
|
77
|
+
);
|
|
78
78
|
const { content, sourcemap, isValid } = await commonJsToJsModule({
|
|
79
79
|
logLevel,
|
|
80
80
|
compileCacheDirectoryUrl,
|
|
@@ -82,9 +82,9 @@ export const jsenvPluginCommonJs = ({
|
|
|
82
82
|
browsers: !nodeRuntimeEnabled,
|
|
83
83
|
processEnvNodeEnv: context.dev ? "development" : "production",
|
|
84
84
|
...urlInfo.data.commonjs,
|
|
85
|
-
})
|
|
85
|
+
});
|
|
86
86
|
if (isValid) {
|
|
87
|
-
urlInfo.isValid = isValid
|
|
87
|
+
urlInfo.isValid = isValid;
|
|
88
88
|
}
|
|
89
89
|
return {
|
|
90
90
|
content,
|
|
@@ -94,7 +94,7 @@ export const jsenvPluginCommonJs = ({
|
|
|
94
94
|
originalContent: commonJsUrlInfo.originalContent,
|
|
95
95
|
sourcemap,
|
|
96
96
|
data: commonJsUrlInfo.data,
|
|
97
|
-
}
|
|
97
|
+
};
|
|
98
98
|
},
|
|
99
|
-
}
|
|
100
|
-
}
|
|
99
|
+
};
|
|
100
|
+
};
|
package/src/main.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { jsenvPluginCommonJs } from "./jsenv_plugin_commonjs.js"
|
|
2
|
-
export { commonJsToJsModule } from "./cjs_to_esm.js"
|
|
1
|
+
export { jsenvPluginCommonJs } from "./jsenv_plugin_commonjs.js";
|
|
2
|
+
export { commonJsToJsModule } from "./cjs_to_esm.js";
|