@jsenv/core 23.7.0 → 23.7.1

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.
@@ -1,300 +1,300 @@
1
- import { createLogger, createDetailedMessage } from "@jsenv/logger"
2
- import { resolveDirectoryUrl } from "@jsenv/filesystem"
3
- import { Abort, raceProcessTeardownEvents } from "@jsenv/abort"
4
-
5
- import { COMPILE_ID_BEST } from "./internal/CONSTANTS.js"
6
- import {
7
- assertProjectDirectoryUrl,
8
- assertProjectDirectoryExists,
9
- } from "./internal/argUtils.js"
10
- import { startCompileServer } from "./internal/compiling/startCompileServer.js"
11
- import { buildUsingRollup } from "./internal/building/buildUsingRollup.js"
12
- import {
13
- jsenvBrowserRuntimeSupport,
14
- jsenvNodeRuntimeSupport,
15
- } from "./internal/generateGroupMap/jsenvRuntimeSupport.js"
16
-
17
- export const buildProject = async ({
18
- signal = new AbortController().signal,
19
- handleSIGINT = true,
20
- logLevel = "info",
21
- compileServerLogLevel = "warn",
22
- logger,
23
-
24
- projectDirectoryUrl,
25
- entryPointMap,
26
- buildDirectoryRelativeUrl,
27
- buildDirectoryClean = false,
28
- assetManifestFile = false,
29
- assetManifestFileRelativeUrl = "asset-manifest.json",
30
- sourcemapExcludeSources = false,
31
- writeOnFileSystem = true,
32
-
33
- format,
34
- systemJsUrl,
35
- globalName,
36
- globals = {},
37
- babelPluginMap,
38
- customCompilers,
39
- runtimeSupport = format === "global" ||
40
- format === "systemjs" ||
41
- format === "esmodule"
42
- ? jsenvBrowserRuntimeSupport
43
- : jsenvNodeRuntimeSupport,
44
- transformTopLevelAwait = true,
45
-
46
- urlMappings = {},
47
- importResolutionMethod = format === "commonjs" ? "node" : "importmap",
48
- importMapFileRelativeUrl,
49
- importDefaultExtension,
50
- externalImportSpecifiers = [],
51
- externalImportUrlPatterns = format === "commonjs"
52
- ? {
53
- "node_modules/": true,
54
- }
55
- : {},
56
- importPaths = {},
57
-
58
- urlVersioning = format === "systemjs" ||
59
- format === "esmodule" ||
60
- format === "global",
61
- lineBreakNormalization = process.platform === "win32",
62
- // when jsConcatenation is disabled rollup becomes almost useless
63
- // except it can still do tree shaking
64
- jsConcatenation = true,
65
- // useImportMapToMaximizeCacheReuse is enabled by default when entry point is an HTML file
66
- // otherwise it's disabled. It can still be explicitely enabled for non HTML entry file
67
- // in that case the returned buildImportMap must be injected into an html file
68
- useImportMapToMaximizeCacheReuse,
69
- preserveEntrySignatures,
70
-
71
- minify = process.env.NODE_ENV === "production",
72
- // https://github.com/kangax/html-minifier#options-quick-reference
73
- minifyHtmlOptions = { collapseWhitespace: true },
74
- // https://github.com/terser/terser#minify-options
75
- minifyJsOptions,
76
- // https://github.com/cssnano/cssnano/tree/master/packages/cssnano-preset-default
77
- minifyCssOptions,
78
-
79
- serviceWorkers = {},
80
- serviceWorkerFinalizer,
81
-
82
- env = {},
83
- compileServerProtocol,
84
- compileServerPrivateKey,
85
- compileServerCertificate,
86
- compileServerIp,
87
- compileServerPort,
88
- jsenvDirectoryRelativeUrl,
89
- jsenvDirectoryClean,
90
-
91
- // when true .jsenv/build directory is generated
92
- // with all intermediated files used to produce the final build files.
93
- // it might improve buildProject speed for subsequent build generation
94
- // but this is to be proven and not absolutely required
95
- // When false intermediates files are transformed and served in memory
96
- // by the compile server
97
- // must be true by default otherwise rollup cannot find sourcemap files
98
- // when asking them to the compile server
99
- // (to fix that sourcemap could be inlined)
100
- filesystemCache = true,
101
- }) => {
102
- logger = logger || createLogger({ logLevel })
103
- if (!["esmodule", "systemjs", "commonjs", "global"].includes(format)) {
104
- throw new TypeError(
105
- `unexpected format: ${format}. Must be "esmodule", "systemjs", "commonjs" or "global".`,
106
- )
107
- }
108
- if (typeof runtimeSupport !== "object" || runtimeSupport === null) {
109
- throw new TypeError(
110
- `runtimeSupport must be an object, got ${runtimeSupport}`,
111
- )
112
- }
113
-
114
- projectDirectoryUrl = assertProjectDirectoryUrl({ projectDirectoryUrl })
115
- await assertProjectDirectoryExists({ projectDirectoryUrl })
116
-
117
- assertEntryPointMap({ entryPointMap })
118
-
119
- if (Object.keys(entryPointMap).length === 0) {
120
- logger.error(`entryPointMap is an empty object`)
121
- return {
122
- rollupBuilds: {},
123
- }
124
- }
125
-
126
- assertBuildDirectoryRelativeUrl({ buildDirectoryRelativeUrl })
127
- const buildDirectoryUrl = resolveDirectoryUrl(
128
- buildDirectoryRelativeUrl,
129
- projectDirectoryUrl,
130
- )
131
- assertBuildDirectoryInsideProject({
132
- buildDirectoryUrl,
133
- projectDirectoryUrl,
134
- })
135
-
136
- const buildOperation = Abort.startOperation()
137
- buildOperation.addAbortSignal(signal)
138
-
139
- if (handleSIGINT) {
140
- buildOperation.addAbortSource((abort) => {
141
- return raceProcessTeardownEvents(
142
- {
143
- SIGINT: true,
144
- },
145
- abort,
146
- )
147
- })
148
- }
149
-
150
- const compileServer = await startCompileServer({
151
- signal: buildOperation.signal,
152
- compileServerLogLevel,
153
-
154
- projectDirectoryUrl,
155
- jsenvDirectoryRelativeUrl,
156
- jsenvDirectoryClean,
157
- // build compiled files are written into a different directory
158
- // than exploring-server. This is because here we compile for rollup
159
- // that is expecting esmodule format, not systemjs
160
- // + some more differences like import.meta.dev
161
- outDirectoryName: "build",
162
- importDefaultExtension,
163
- moduleOutFormat: "esmodule", // rollup will transform into systemjs
164
- importMetaFormat: format, // but ensure import.meta are correctly transformed into the right format
165
-
166
- compileServerProtocol,
167
- compileServerPrivateKey,
168
- compileServerCertificate,
169
- compileServerIp,
170
- compileServerPort,
171
- env,
172
- babelPluginMap,
173
- transformTopLevelAwait,
174
- customCompilers,
175
- runtimeSupport,
176
-
177
- compileServerCanReadFromFileSystem: filesystemCache,
178
- compileServerCanWriteOnFilesystem: filesystemCache,
179
- // keep source html untouched
180
- // here we don't need to inline importmap
181
- // nor to inject jsenv script
182
- transformHtmlSourceFiles: false,
183
- })
184
-
185
- buildOperation.addEndCallback(async () => {
186
- await compileServer.stop(`build cleanup`)
187
- })
188
-
189
- const { outDirectoryRelativeUrl, origin: compileServerOrigin } = compileServer
190
-
191
- try {
192
- const result = await buildUsingRollup({
193
- buildOperation,
194
- logger,
195
-
196
- entryPointMap,
197
- projectDirectoryUrl,
198
- compileServerOrigin,
199
- compileDirectoryRelativeUrl: `${outDirectoryRelativeUrl}${COMPILE_ID_BEST}/`,
200
- buildDirectoryUrl,
201
- buildDirectoryClean,
202
- assetManifestFile,
203
- assetManifestFileRelativeUrl,
204
-
205
- urlMappings,
206
- importResolutionMethod,
207
- importMapFileRelativeUrl,
208
- importDefaultExtension,
209
- externalImportSpecifiers,
210
- externalImportUrlPatterns,
211
- importPaths,
212
-
213
- format,
214
- systemJsUrl,
215
- globalName,
216
- globals,
217
- babelPluginMap,
218
- transformTopLevelAwait,
219
- runtimeSupport,
220
-
221
- urlVersioning,
222
- lineBreakNormalization,
223
- useImportMapToMaximizeCacheReuse,
224
- preserveEntrySignatures,
225
- jsConcatenation,
226
-
227
- minify,
228
- minifyHtmlOptions,
229
- minifyJsOptions,
230
- minifyCssOptions,
231
-
232
- serviceWorkers,
233
- serviceWorkerFinalizer,
234
-
235
- writeOnFileSystem,
236
- sourcemapExcludeSources,
237
- })
238
-
239
- return result
240
- } catch (e) {
241
- if (Abort.isAbortError(e)) {
242
- logger.info("build aborted")
243
- return null
244
- }
245
- throw e
246
- } finally {
247
- await buildOperation.end()
248
- }
249
- }
250
-
251
- const assertEntryPointMap = ({ entryPointMap }) => {
252
- if (typeof entryPointMap !== "object") {
253
- throw new TypeError(`entryPointMap must be an object, got ${entryPointMap}`)
254
- }
255
- const keys = Object.keys(entryPointMap)
256
- keys.forEach((key) => {
257
- if (!key.startsWith("./")) {
258
- throw new TypeError(
259
- `unexpected key in entryPointMap, all keys must start with ./ but found ${key}`,
260
- )
261
- }
262
-
263
- const value = entryPointMap[key]
264
- if (typeof value !== "string") {
265
- throw new TypeError(
266
- `unexpected value in entryPointMap, all values must be strings found ${value} for key ${key}`,
267
- )
268
- }
269
- if (!value.startsWith("./")) {
270
- throw new TypeError(
271
- `unexpected value in entryPointMap, all values must starts with ./ but found ${value} for key ${key}`,
272
- )
273
- }
274
- })
275
- }
276
-
277
- const assertBuildDirectoryRelativeUrl = ({ buildDirectoryRelativeUrl }) => {
278
- if (typeof buildDirectoryRelativeUrl !== "string") {
279
- throw new TypeError(
280
- `buildDirectoryRelativeUrl must be a string, received ${buildDirectoryRelativeUrl}`,
281
- )
282
- }
283
- }
284
-
285
- const assertBuildDirectoryInsideProject = ({
286
- buildDirectoryUrl,
287
- projectDirectoryUrl,
288
- }) => {
289
- if (!buildDirectoryUrl.startsWith(projectDirectoryUrl)) {
290
- throw new Error(
291
- createDetailedMessage(
292
- `build directory must be inside project directory`,
293
- {
294
- ["build directory url"]: buildDirectoryUrl,
295
- ["project directory url"]: projectDirectoryUrl,
296
- },
297
- ),
298
- )
299
- }
300
- }
1
+ import { createLogger, createDetailedMessage } from "@jsenv/logger"
2
+ import { resolveDirectoryUrl } from "@jsenv/filesystem"
3
+ import { Abort, raceProcessTeardownEvents } from "@jsenv/abort"
4
+
5
+ import { COMPILE_ID_BEST } from "./internal/CONSTANTS.js"
6
+ import {
7
+ assertProjectDirectoryUrl,
8
+ assertProjectDirectoryExists,
9
+ } from "./internal/argUtils.js"
10
+ import { startCompileServer } from "./internal/compiling/startCompileServer.js"
11
+ import { buildUsingRollup } from "./internal/building/buildUsingRollup.js"
12
+ import {
13
+ jsenvBrowserRuntimeSupport,
14
+ jsenvNodeRuntimeSupport,
15
+ } from "./internal/generateGroupMap/jsenvRuntimeSupport.js"
16
+
17
+ export const buildProject = async ({
18
+ signal = new AbortController().signal,
19
+ handleSIGINT = true,
20
+ logLevel = "info",
21
+ compileServerLogLevel = "warn",
22
+ logger,
23
+
24
+ projectDirectoryUrl,
25
+ entryPointMap,
26
+ buildDirectoryRelativeUrl,
27
+ buildDirectoryClean = false,
28
+ assetManifestFile = false,
29
+ assetManifestFileRelativeUrl = "asset-manifest.json",
30
+ sourcemapExcludeSources = false,
31
+ writeOnFileSystem = true,
32
+
33
+ format,
34
+ systemJsUrl,
35
+ globalName,
36
+ globals = {},
37
+ babelPluginMap,
38
+ customCompilers,
39
+ runtimeSupport = format === "global" ||
40
+ format === "systemjs" ||
41
+ format === "esmodule"
42
+ ? jsenvBrowserRuntimeSupport
43
+ : jsenvNodeRuntimeSupport,
44
+ transformTopLevelAwait = true,
45
+
46
+ urlMappings = {},
47
+ importResolutionMethod = format === "commonjs" ? "node" : "importmap",
48
+ importMapFileRelativeUrl,
49
+ importDefaultExtension,
50
+ externalImportSpecifiers = [],
51
+ externalImportUrlPatterns = format === "commonjs"
52
+ ? {
53
+ "node_modules/": true,
54
+ }
55
+ : {},
56
+ importPaths = {},
57
+
58
+ urlVersioning = format === "systemjs" ||
59
+ format === "esmodule" ||
60
+ format === "global",
61
+ lineBreakNormalization = process.platform === "win32",
62
+ // when jsConcatenation is disabled rollup becomes almost useless
63
+ // except it can still do tree shaking
64
+ jsConcatenation = true,
65
+ // useImportMapToMaximizeCacheReuse is enabled by default when entry point is an HTML file
66
+ // otherwise it's disabled. It can still be explicitely enabled for non HTML entry file
67
+ // in that case the returned buildImportMap must be injected into an html file
68
+ useImportMapToMaximizeCacheReuse,
69
+ preserveEntrySignatures,
70
+
71
+ minify = process.env.NODE_ENV === "production",
72
+ // https://github.com/kangax/html-minifier#options-quick-reference
73
+ minifyHtmlOptions = { collapseWhitespace: true },
74
+ // https://github.com/terser/terser#minify-options
75
+ minifyJsOptions,
76
+ // https://github.com/cssnano/cssnano/tree/master/packages/cssnano-preset-default
77
+ minifyCssOptions,
78
+
79
+ serviceWorkers = {},
80
+ serviceWorkerFinalizer,
81
+
82
+ env = {},
83
+ compileServerProtocol,
84
+ compileServerPrivateKey,
85
+ compileServerCertificate,
86
+ compileServerIp,
87
+ compileServerPort,
88
+ jsenvDirectoryRelativeUrl,
89
+ jsenvDirectoryClean,
90
+
91
+ // when true .jsenv/build directory is generated
92
+ // with all intermediated files used to produce the final build files.
93
+ // it might improve buildProject speed for subsequent build generation
94
+ // but this is to be proven and not absolutely required
95
+ // When false intermediates files are transformed and served in memory
96
+ // by the compile server
97
+ // must be true by default otherwise rollup cannot find sourcemap files
98
+ // when asking them to the compile server
99
+ // (to fix that sourcemap could be inlined)
100
+ filesystemCache = true,
101
+ }) => {
102
+ logger = logger || createLogger({ logLevel })
103
+ if (!["esmodule", "systemjs", "commonjs", "global"].includes(format)) {
104
+ throw new TypeError(
105
+ `unexpected format: ${format}. Must be "esmodule", "systemjs", "commonjs" or "global".`,
106
+ )
107
+ }
108
+ if (typeof runtimeSupport !== "object" || runtimeSupport === null) {
109
+ throw new TypeError(
110
+ `runtimeSupport must be an object, got ${runtimeSupport}`,
111
+ )
112
+ }
113
+
114
+ projectDirectoryUrl = assertProjectDirectoryUrl({ projectDirectoryUrl })
115
+ await assertProjectDirectoryExists({ projectDirectoryUrl })
116
+
117
+ assertEntryPointMap({ entryPointMap })
118
+
119
+ if (Object.keys(entryPointMap).length === 0) {
120
+ logger.error(`entryPointMap is an empty object`)
121
+ return {
122
+ rollupBuilds: {},
123
+ }
124
+ }
125
+
126
+ assertBuildDirectoryRelativeUrl({ buildDirectoryRelativeUrl })
127
+ const buildDirectoryUrl = resolveDirectoryUrl(
128
+ buildDirectoryRelativeUrl,
129
+ projectDirectoryUrl,
130
+ )
131
+ assertBuildDirectoryInsideProject({
132
+ buildDirectoryUrl,
133
+ projectDirectoryUrl,
134
+ })
135
+
136
+ const buildOperation = Abort.startOperation()
137
+ buildOperation.addAbortSignal(signal)
138
+
139
+ if (handleSIGINT) {
140
+ buildOperation.addAbortSource((abort) => {
141
+ return raceProcessTeardownEvents(
142
+ {
143
+ SIGINT: true,
144
+ },
145
+ abort,
146
+ )
147
+ })
148
+ }
149
+
150
+ const compileServer = await startCompileServer({
151
+ signal: buildOperation.signal,
152
+ compileServerLogLevel,
153
+
154
+ projectDirectoryUrl,
155
+ jsenvDirectoryRelativeUrl,
156
+ jsenvDirectoryClean,
157
+ // build compiled files are written into a different directory
158
+ // than exploring-server. This is because here we compile for rollup
159
+ // that is expecting esmodule format, not systemjs
160
+ // + some more differences like import.meta.dev
161
+ outDirectoryName: "build",
162
+ importDefaultExtension,
163
+ moduleOutFormat: "esmodule", // rollup will transform into systemjs
164
+ importMetaFormat: format, // but ensure import.meta are correctly transformed into the right format
165
+
166
+ compileServerProtocol,
167
+ compileServerPrivateKey,
168
+ compileServerCertificate,
169
+ compileServerIp,
170
+ compileServerPort,
171
+ env,
172
+ babelPluginMap,
173
+ transformTopLevelAwait,
174
+ customCompilers,
175
+ runtimeSupport,
176
+
177
+ compileServerCanReadFromFileSystem: filesystemCache,
178
+ compileServerCanWriteOnFilesystem: filesystemCache,
179
+ // keep source html untouched
180
+ // here we don't need to inline importmap
181
+ // nor to inject jsenv script
182
+ transformHtmlSourceFiles: false,
183
+ })
184
+
185
+ buildOperation.addEndCallback(async () => {
186
+ await compileServer.stop(`build cleanup`)
187
+ })
188
+
189
+ const { outDirectoryRelativeUrl, origin: compileServerOrigin } = compileServer
190
+
191
+ try {
192
+ const result = await buildUsingRollup({
193
+ buildOperation,
194
+ logger,
195
+
196
+ entryPointMap,
197
+ projectDirectoryUrl,
198
+ compileServerOrigin,
199
+ compileDirectoryRelativeUrl: `${outDirectoryRelativeUrl}${COMPILE_ID_BEST}/`,
200
+ buildDirectoryUrl,
201
+ buildDirectoryClean,
202
+ assetManifestFile,
203
+ assetManifestFileRelativeUrl,
204
+
205
+ urlMappings,
206
+ importResolutionMethod,
207
+ importMapFileRelativeUrl,
208
+ importDefaultExtension,
209
+ externalImportSpecifiers,
210
+ externalImportUrlPatterns,
211
+ importPaths,
212
+
213
+ format,
214
+ systemJsUrl,
215
+ globalName,
216
+ globals,
217
+ babelPluginMap,
218
+ transformTopLevelAwait,
219
+ runtimeSupport,
220
+
221
+ urlVersioning,
222
+ lineBreakNormalization,
223
+ useImportMapToMaximizeCacheReuse,
224
+ preserveEntrySignatures,
225
+ jsConcatenation,
226
+
227
+ minify,
228
+ minifyHtmlOptions,
229
+ minifyJsOptions,
230
+ minifyCssOptions,
231
+
232
+ serviceWorkers,
233
+ serviceWorkerFinalizer,
234
+
235
+ writeOnFileSystem,
236
+ sourcemapExcludeSources,
237
+ })
238
+
239
+ return result
240
+ } catch (e) {
241
+ if (Abort.isAbortError(e)) {
242
+ logger.info("build aborted")
243
+ return null
244
+ }
245
+ throw e
246
+ } finally {
247
+ await buildOperation.end()
248
+ }
249
+ }
250
+
251
+ const assertEntryPointMap = ({ entryPointMap }) => {
252
+ if (typeof entryPointMap !== "object") {
253
+ throw new TypeError(`entryPointMap must be an object, got ${entryPointMap}`)
254
+ }
255
+ const keys = Object.keys(entryPointMap)
256
+ keys.forEach((key) => {
257
+ if (!key.startsWith("./")) {
258
+ throw new TypeError(
259
+ `unexpected key in entryPointMap, all keys must start with ./ but found ${key}`,
260
+ )
261
+ }
262
+
263
+ const value = entryPointMap[key]
264
+ if (typeof value !== "string") {
265
+ throw new TypeError(
266
+ `unexpected value in entryPointMap, all values must be strings found ${value} for key ${key}`,
267
+ )
268
+ }
269
+ if (!value.startsWith("./")) {
270
+ throw new TypeError(
271
+ `unexpected value in entryPointMap, all values must starts with ./ but found ${value} for key ${key}`,
272
+ )
273
+ }
274
+ })
275
+ }
276
+
277
+ const assertBuildDirectoryRelativeUrl = ({ buildDirectoryRelativeUrl }) => {
278
+ if (typeof buildDirectoryRelativeUrl !== "string") {
279
+ throw new TypeError(
280
+ `buildDirectoryRelativeUrl must be a string, received ${buildDirectoryRelativeUrl}`,
281
+ )
282
+ }
283
+ }
284
+
285
+ const assertBuildDirectoryInsideProject = ({
286
+ buildDirectoryUrl,
287
+ projectDirectoryUrl,
288
+ }) => {
289
+ if (!buildDirectoryUrl.startsWith(projectDirectoryUrl)) {
290
+ throw new Error(
291
+ createDetailedMessage(
292
+ `build directory must be inside project directory`,
293
+ {
294
+ ["build directory url"]: buildDirectoryUrl,
295
+ ["project directory url"]: projectDirectoryUrl,
296
+ },
297
+ ),
298
+ )
299
+ }
300
+ }