@eighty4/dank 0.0.4-1 → 0.0.4-2

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/lib_js/esbuild.js CHANGED
@@ -1,186 +1,176 @@
1
- import { readFile } from 'node:fs/promises';
2
- import esbuild, {} from 'esbuild';
3
- export async function esbuildDevContext(b, r, define, entryPoints, c) {
4
- return await esbuild.context({
5
- define,
6
- entryNames: '[dir]/[name]',
7
- entryPoints: mapEntryPointPaths(entryPoints),
8
- outdir: b.dirs.buildWatch,
9
- ...commonBuildOptions(b, r, c),
10
- splitting: false,
11
- write: false,
12
- });
1
+ import { readFile } from "node:fs/promises";
2
+ import esbuild, {} from "esbuild";
3
+ async function esbuildDevContext(b, r, define, entryPoints, c) {
4
+ return await esbuild.context({
5
+ define,
6
+ entryNames: "[dir]/[name]",
7
+ entryPoints: mapEntryPointPaths(entryPoints),
8
+ outdir: b.dirs.buildWatch,
9
+ ...commonBuildOptions(b, r, c),
10
+ splitting: false,
11
+ write: false
12
+ });
13
13
  }
14
- export async function esbuildWebpages(b, r, define, entryPoints, c) {
15
- try {
16
- await esbuild.build({
17
- define,
18
- entryNames: '[dir]/[name]-[hash]',
19
- entryPoints: mapEntryPointPaths(entryPoints),
20
- outdir: b.dirs.buildDist,
21
- ...commonBuildOptions(b, r, c),
22
- });
23
- }
24
- catch (ignore) {
25
- process.exit(1);
26
- }
14
+ async function esbuildWebpages(b, r, define, entryPoints, c) {
15
+ try {
16
+ await esbuild.build({
17
+ define,
18
+ entryNames: "[dir]/[name]-[hash]",
19
+ entryPoints: mapEntryPointPaths(entryPoints),
20
+ outdir: b.dirs.buildDist,
21
+ ...commonBuildOptions(b, r, c)
22
+ });
23
+ } catch (ignore) {
24
+ process.exit(1);
25
+ }
27
26
  }
28
- export async function esbuildWorkers(b, r, define, entryPoints, c) {
29
- try {
30
- await esbuild.build({
31
- define,
32
- entryNames: '[dir]/[name]-[hash]',
33
- entryPoints: mapEntryPointPaths(entryPoints),
34
- outdir: b.dirs.buildDist,
35
- ...commonBuildOptions(b, r, c),
36
- splitting: false,
37
- metafile: true,
38
- write: true,
39
- assetNames: 'assets/[name]-[hash]',
40
- });
41
- }
42
- catch (ignore) {
43
- process.exit(1);
44
- }
27
+ async function esbuildWorkers(b, r, define, entryPoints, c) {
28
+ try {
29
+ await esbuild.build({
30
+ define,
31
+ entryNames: "[dir]/[name]-[hash]",
32
+ entryPoints: mapEntryPointPaths(entryPoints),
33
+ outdir: b.dirs.buildDist,
34
+ ...commonBuildOptions(b, r, c),
35
+ splitting: false,
36
+ metafile: true,
37
+ write: true,
38
+ assetNames: "assets/[name]-[hash]"
39
+ });
40
+ } catch (ignore) {
41
+ process.exit(1);
42
+ }
45
43
  }
46
44
  function commonBuildOptions(b, r, c) {
47
- const p = workersPlugin(r.buildRegistry());
48
- return {
49
- absWorkingDir: b.dirs.projectRootAbs,
50
- assetNames: 'assets/[name]-[hash]',
51
- bundle: true,
52
- format: 'esm',
53
- loader: c?.loaders || defaultLoaders(),
54
- metafile: true,
55
- minify: b.minify,
56
- platform: 'browser',
57
- plugins: c?.plugins?.length ? [p, ...c.plugins] : [p],
58
- splitting: true,
59
- treeShaking: true,
60
- write: true,
61
- };
45
+ const p = workersPlugin(r.buildRegistry());
46
+ return {
47
+ absWorkingDir: b.dirs.projectRootAbs,
48
+ assetNames: "assets/[name]-[hash]",
49
+ bundle: true,
50
+ format: "esm",
51
+ loader: c?.loaders || defaultLoaders(),
52
+ metafile: true,
53
+ minify: b.minify,
54
+ platform: "browser",
55
+ plugins: c?.plugins?.length ? [p, ...c.plugins] : [p],
56
+ splitting: true,
57
+ treeShaking: true,
58
+ write: true
59
+ };
62
60
  }
63
61
  function defaultLoaders() {
64
- return {
65
- '.woff': 'file',
66
- '.woff2': 'file',
67
- };
62
+ return {
63
+ ".woff": "file",
64
+ ".woff2": "file"
65
+ };
68
66
  }
69
- // esbuild will append the .js or .css to output filenames
70
- // keeping extension on entryPoints data for consistency
71
- // and only trimming when creating esbuild opts
72
67
  function mapEntryPointPaths(entryPoints) {
73
- return entryPoints.map(entryPoint => {
74
- return {
75
- in: entryPoint.in,
76
- out: entryPoint.out.replace(/\.(tsx?|jsx?|css)$/, ''),
77
- };
78
- });
68
+ return entryPoints.map((entryPoint) => {
69
+ return {
70
+ in: entryPoint.in,
71
+ out: entryPoint.out.replace(/\.(tsx?|jsx?|css)$/, "")
72
+ };
73
+ });
79
74
  }
80
75
  const WORKER_CTOR_REGEX = /new(?:\s|\r?\n)+(?<ctor>(?:Shared)?Worker)(?:\s|\r?\n)*\((?:\s|\r?\n)*(?<url>.*?)(?:\s|\r?\n)*(?<end>[\),])/g;
81
76
  const WORKER_URL_REGEX = /^('.*'|".*")$/;
82
- export function workersPlugin(r) {
83
- return {
84
- name: '@eighty4/dank/esbuild/workers',
85
- setup(build) {
86
- if (!build.initialOptions.absWorkingDir)
87
- throw TypeError('plugin requires absWorkingDir');
88
- if (!build.initialOptions.metafile)
89
- throw TypeError('plugin requires metafile');
90
- const { absWorkingDir } = build.initialOptions;
91
- build.onLoad({ filter: /\.(t|m?j)s$/ }, async (args) => {
92
- let contents = await readFile(args.path, 'utf8');
93
- let offset = 0;
94
- let errors = undefined;
95
- for (const workerCtorMatch of contents.matchAll(WORKER_CTOR_REGEX)) {
96
- if (!WORKER_URL_REGEX.test(workerCtorMatch.groups.url)) {
97
- if (!errors)
98
- errors = [];
99
- errors.push(invalidWorkerUrlCtorArg(locationFromMatch(args, contents, workerCtorMatch), workerCtorMatch));
100
- continue;
101
- }
102
- if (isIndexCommented(contents, workerCtorMatch.index)) {
103
- continue;
104
- }
105
- const clientScript = args.path
106
- .replace(absWorkingDir, '')
107
- .substring(1);
108
- const workerUrl = workerCtorMatch.groups.url.substring(1, workerCtorMatch.groups.url.length - 1);
109
- const workerEntryPoint = r.resolver.resolveHrefInPagesDir(clientScript, workerUrl);
110
- if (workerEntryPoint === 'outofbounds') {
111
- if (!errors)
112
- errors = [];
113
- errors.push(outofboundsWorkerUrlCtorArg(locationFromMatch(args, contents, workerCtorMatch), workerCtorMatch));
114
- continue;
115
- }
116
- const workerUrlPlaceholder = workerEntryPoint
117
- .replace(/^pages/, '')
118
- .replace(/\.(t|m?j)s$/, '.js');
119
- const workerCtorReplacement = `new ${workerCtorMatch.groups.ctor}('${workerUrlPlaceholder}'${workerCtorMatch.groups.end}`;
120
- contents =
121
- contents.substring(0, workerCtorMatch.index + offset) +
122
- workerCtorReplacement +
123
- contents.substring(workerCtorMatch.index +
124
- workerCtorMatch[0].length +
125
- offset);
126
- offset +=
127
- workerCtorReplacement.length - workerCtorMatch[0].length;
128
- r.addWorker({
129
- clientScript,
130
- workerEntryPoint,
131
- workerUrl,
132
- workerUrlPlaceholder,
133
- });
134
- }
135
- const loader = args.path.endsWith('ts') ? 'ts' : 'js';
136
- return { contents, errors, loader };
137
- });
138
- build.onEnd((result) => {
139
- if (result.metafile) {
140
- r.completeBuild(result);
141
- }
142
- });
143
- },
144
- };
77
+ function workersPlugin(r) {
78
+ return {
79
+ name: "@eighty4/dank/esbuild/workers",
80
+ setup(build) {
81
+ if (!build.initialOptions.absWorkingDir)
82
+ throw TypeError("plugin requires absWorkingDir");
83
+ if (!build.initialOptions.metafile)
84
+ throw TypeError("plugin requires metafile");
85
+ const { absWorkingDir } = build.initialOptions;
86
+ build.onLoad({ filter: /\.(t|m?j)s$/ }, async (args) => {
87
+ let contents = await readFile(args.path, "utf8");
88
+ let offset = 0;
89
+ let errors = void 0;
90
+ for (const workerCtorMatch of contents.matchAll(WORKER_CTOR_REGEX)) {
91
+ if (!WORKER_URL_REGEX.test(workerCtorMatch.groups.url)) {
92
+ if (!errors)
93
+ errors = [];
94
+ errors.push(invalidWorkerUrlCtorArg(locationFromMatch(args, contents, workerCtorMatch), workerCtorMatch));
95
+ continue;
96
+ }
97
+ if (isIndexCommented(contents, workerCtorMatch.index)) {
98
+ continue;
99
+ }
100
+ const clientScript = args.path.replace(absWorkingDir, "").substring(1);
101
+ const workerUrl = workerCtorMatch.groups.url.substring(1, workerCtorMatch.groups.url.length - 1);
102
+ const workerEntryPoint = r.resolver.resolveHrefInPagesDir(clientScript, workerUrl);
103
+ if (workerEntryPoint === "outofbounds") {
104
+ if (!errors)
105
+ errors = [];
106
+ errors.push(outofboundsWorkerUrlCtorArg(locationFromMatch(args, contents, workerCtorMatch), workerCtorMatch));
107
+ continue;
108
+ }
109
+ const workerUrlPlaceholder = workerEntryPoint.replace(/^pages/, "").replace(/\.(t|m?j)s$/, ".js");
110
+ const workerCtorReplacement = `new ${workerCtorMatch.groups.ctor}('${workerUrlPlaceholder}'${workerCtorMatch.groups.end}`;
111
+ contents = contents.substring(0, workerCtorMatch.index + offset) + workerCtorReplacement + contents.substring(workerCtorMatch.index + workerCtorMatch[0].length + offset);
112
+ offset += workerCtorReplacement.length - workerCtorMatch[0].length;
113
+ r.addWorker({
114
+ clientScript,
115
+ workerEntryPoint,
116
+ workerUrl,
117
+ workerUrlPlaceholder
118
+ });
119
+ }
120
+ const loader = args.path.endsWith("ts") ? "ts" : "js";
121
+ return { contents, errors, loader };
122
+ });
123
+ build.onEnd((result) => {
124
+ if (result.metafile) {
125
+ r.completeBuild(result);
126
+ }
127
+ });
128
+ }
129
+ };
145
130
  }
146
131
  function isIndexCommented(contents, index) {
147
- const preamble = contents.substring(0, index);
148
- const lineIndex = preamble.lastIndexOf('\n') || 0;
149
- const lineCommented = /\/\//.test(preamble.substring(lineIndex));
150
- if (lineCommented) {
151
- return true;
152
- }
153
- const blockCommentIndex = preamble.lastIndexOf('/*');
154
- const blockCommented = blockCommentIndex !== -1 &&
155
- preamble.substring(blockCommentIndex).indexOf('*/') === -1;
156
- return blockCommented;
132
+ const preamble = contents.substring(0, index);
133
+ const lineIndex = preamble.lastIndexOf("\n") || 0;
134
+ const lineCommented = /\/\//.test(preamble.substring(lineIndex));
135
+ if (lineCommented) {
136
+ return true;
137
+ }
138
+ const blockCommentIndex = preamble.lastIndexOf("/*");
139
+ const blockCommented = blockCommentIndex !== -1 && preamble.substring(blockCommentIndex).indexOf("*/") === -1;
140
+ return blockCommented;
157
141
  }
158
142
  function locationFromMatch(args, contents, match) {
159
- const preamble = contents.substring(0, match.index);
160
- const line = preamble.match(/\n/g)?.length || 0;
161
- let lineIndex = preamble.lastIndexOf('\n');
162
- lineIndex = lineIndex === -1 ? 0 : lineIndex + 1;
163
- const column = preamble.length - lineIndex;
164
- const lineText = contents.substring(lineIndex, contents.indexOf('\n', lineIndex) || contents.length);
165
- return {
166
- lineText,
167
- line,
168
- column,
169
- file: args.path,
170
- length: match[0].length,
171
- };
143
+ const preamble = contents.substring(0, match.index);
144
+ const line = preamble.match(/\n/g)?.length || 0;
145
+ let lineIndex = preamble.lastIndexOf("\n");
146
+ lineIndex = lineIndex === -1 ? 0 : lineIndex + 1;
147
+ const column = preamble.length - lineIndex;
148
+ const lineText = contents.substring(lineIndex, contents.indexOf("\n", lineIndex) || contents.length);
149
+ return {
150
+ lineText,
151
+ line,
152
+ column,
153
+ file: args.path,
154
+ length: match[0].length
155
+ };
172
156
  }
173
157
  function outofboundsWorkerUrlCtorArg(location, workerCtorMatch) {
174
- return {
175
- id: 'worker-url-outofbounds',
176
- text: `The ${workerCtorMatch.groups.ctor} constructor URL arg \`${workerCtorMatch.groups.url}\` cannot resolve to a path outside of the pages directory`,
177
- location,
178
- };
158
+ return {
159
+ id: "worker-url-outofbounds",
160
+ text: `The ${workerCtorMatch.groups.ctor} constructor URL arg \`${workerCtorMatch.groups.url}\` cannot resolve to a path outside of the pages directory`,
161
+ location
162
+ };
179
163
  }
180
164
  function invalidWorkerUrlCtorArg(location, workerCtorMatch) {
181
- return {
182
- id: 'worker-url-unresolvable',
183
- text: `The ${workerCtorMatch.groups.ctor} constructor URL arg \`${workerCtorMatch.groups.url}\` must be a relative module path`,
184
- location,
185
- };
165
+ return {
166
+ id: "worker-url-unresolvable",
167
+ text: `The ${workerCtorMatch.groups.ctor} constructor URL arg \`${workerCtorMatch.groups.url}\` must be a relative module path`,
168
+ location
169
+ };
186
170
  }
171
+ export {
172
+ esbuildDevContext,
173
+ esbuildWebpages,
174
+ esbuildWorkers,
175
+ workersPlugin
176
+ };
package/lib_js/flags.js CHANGED
@@ -1,128 +1,124 @@
1
- import { join, resolve } from 'node:path';
2
- import { cwd } from 'node:process';
3
- export function resolveBuildFlags() {
4
- const flags = {
5
- dirs: defaultProjectDirs(cwd()),
6
- minify: willMinify(),
7
- production: isProductionBuild(),
8
- };
9
- return {
10
- get dirs() {
11
- return flags.dirs;
12
- },
13
- get minify() {
14
- return flags.minify;
15
- },
16
- get production() {
17
- return flags.production;
18
- },
19
- };
1
+ import { join, resolve } from "node:path";
2
+ import { cwd } from "node:process";
3
+ function resolveBuildFlags() {
4
+ const flags = {
5
+ dirs: defaultProjectDirs(cwd()),
6
+ minify: willMinify(),
7
+ production: isProductionBuild()
8
+ };
9
+ return {
10
+ get dirs() {
11
+ return flags.dirs;
12
+ },
13
+ get minify() {
14
+ return flags.minify;
15
+ },
16
+ get production() {
17
+ return flags.production;
18
+ }
19
+ };
20
20
  }
21
- export function resolveServeFlags(c) {
22
- const preview = isPreviewBuild();
23
- const flags = {
24
- dirs: defaultProjectDirs(cwd()),
25
- dankPort: dankPort(c, preview),
26
- esbuildPort: esbuildPort(c),
27
- logHttp: willLogHttp(),
28
- minify: willMinify(),
29
- preview,
30
- production: isProductionBuild(),
31
- };
32
- return {
33
- get dirs() {
34
- return flags.dirs;
35
- },
36
- get dankPort() {
37
- return flags.dankPort;
38
- },
39
- get esbuildPort() {
40
- return flags.esbuildPort;
41
- },
42
- get logHttp() {
43
- return flags.logHttp;
44
- },
45
- get minify() {
46
- return flags.minify;
47
- },
48
- get preview() {
49
- return flags.preview;
50
- },
51
- get production() {
52
- return flags.production;
53
- },
54
- };
21
+ function resolveServeFlags(c) {
22
+ const preview = isPreviewBuild();
23
+ const flags = {
24
+ dirs: defaultProjectDirs(cwd()),
25
+ dankPort: dankPort(c, preview),
26
+ esbuildPort: esbuildPort(c),
27
+ logHttp: willLogHttp(),
28
+ minify: willMinify(),
29
+ preview,
30
+ production: isProductionBuild()
31
+ };
32
+ return {
33
+ get dirs() {
34
+ return flags.dirs;
35
+ },
36
+ get dankPort() {
37
+ return flags.dankPort;
38
+ },
39
+ get esbuildPort() {
40
+ return flags.esbuildPort;
41
+ },
42
+ get logHttp() {
43
+ return flags.logHttp;
44
+ },
45
+ get minify() {
46
+ return flags.minify;
47
+ },
48
+ get preview() {
49
+ return flags.preview;
50
+ },
51
+ get production() {
52
+ return flags.production;
53
+ }
54
+ };
55
55
  }
56
- // `dank serve` will pre-bundle and use service worker
57
- const isPreviewBuild = () => process.env.PREVIEW === 'true' || process.argv.includes('--preview');
58
- // `dank build` will minify sources and append git release tag to build tag
59
- // `dank serve` will pre-bundle with service worker and minify
60
- const isProductionBuild = () => process.env.PRODUCTION === 'true' || process.argv.includes('--production');
61
- // `dank serve` dank port for frontend webserver
62
- // alternate --preview port for service worker builds
56
+ const isPreviewBuild = () => process.env.PREVIEW === "true" || process.argv.includes("--preview");
57
+ const isProductionBuild = () => process.env.PRODUCTION === "true" || process.argv.includes("--production");
63
58
  function dankPort(c, preview) {
64
- if (process.env.DANK_PORT?.length) {
65
- return parsePortEnvVar('DANK_PORT');
66
- }
67
- return preview ? c.previewPort || c.port || 4000 : c.port || 3000;
59
+ if (process.env.DANK_PORT?.length) {
60
+ return parsePortEnvVar("DANK_PORT");
61
+ }
62
+ return preview ? c.previewPort || c.port || 4e3 : c.port || 3e3;
68
63
  }
69
- // `dank serve` esbuild port for bundler integration
70
64
  function esbuildPort(c) {
71
- if (process.env.ESBUILD_PORT?.length) {
72
- return parsePortEnvVar('ESBUILD_PORT');
73
- }
74
- return c.esbuild?.port || 3995;
65
+ if (process.env.ESBUILD_PORT?.length) {
66
+ return parsePortEnvVar("ESBUILD_PORT");
67
+ }
68
+ return c.esbuild?.port || 3995;
75
69
  }
76
70
  function parsePortEnvVar(name) {
77
- const port = parseInt(process.env[name], 10);
78
- if (isNaN(port)) {
79
- throw Error(`env var ${name}=${port} must be a valid port number`);
80
- }
81
- else {
82
- return port;
83
- }
71
+ const port = parseInt(process.env[name], 10);
72
+ if (isNaN(port)) {
73
+ throw Error(`env var ${name}=${port} must be a valid port number`);
74
+ } else {
75
+ return port;
76
+ }
84
77
  }
85
- export function defaultProjectDirs(projectRootAbs) {
86
- const pages = 'pages';
87
- const dirs = {
88
- buildRoot: 'build',
89
- buildDist: join('build', 'dist'),
90
- buildWatch: join('build', 'watch'),
91
- pages,
92
- pagesResolved: resolve(join(projectRootAbs, pages)),
93
- projectResolved: resolve(projectRootAbs),
94
- projectRootAbs,
95
- public: 'public',
96
- };
97
- return {
98
- get buildRoot() {
99
- return dirs.buildRoot;
100
- },
101
- get buildDist() {
102
- return dirs.buildDist;
103
- },
104
- get buildWatch() {
105
- return dirs.buildWatch;
106
- },
107
- get pages() {
108
- return dirs.pages;
109
- },
110
- get pagesResolved() {
111
- return dirs.pagesResolved;
112
- },
113
- get projectResolved() {
114
- return dirs.projectResolved;
115
- },
116
- get projectRootAbs() {
117
- return dirs.projectRootAbs;
118
- },
119
- get public() {
120
- return dirs.public;
121
- },
122
- };
78
+ function defaultProjectDirs(projectRootAbs) {
79
+ const pages = "pages";
80
+ const dirs = {
81
+ buildRoot: "build",
82
+ buildDist: join("build", "dist"),
83
+ buildWatch: join("build", "watch"),
84
+ pages,
85
+ pagesResolved: resolve(join(projectRootAbs, pages)),
86
+ projectResolved: resolve(projectRootAbs),
87
+ projectRootAbs,
88
+ public: "public"
89
+ };
90
+ return {
91
+ get buildRoot() {
92
+ return dirs.buildRoot;
93
+ },
94
+ get buildDist() {
95
+ return dirs.buildDist;
96
+ },
97
+ get buildWatch() {
98
+ return dirs.buildWatch;
99
+ },
100
+ get pages() {
101
+ return dirs.pages;
102
+ },
103
+ get pagesResolved() {
104
+ return dirs.pagesResolved;
105
+ },
106
+ get projectResolved() {
107
+ return dirs.projectResolved;
108
+ },
109
+ get projectRootAbs() {
110
+ return dirs.projectRootAbs;
111
+ },
112
+ get public() {
113
+ return dirs.public;
114
+ }
115
+ };
123
116
  }
124
- const willMinify = () => isProductionBuild() ||
125
- process.env.MINIFY === 'true' ||
126
- process.argv.includes('--minify');
127
- // `dank serve` will print http access logs to console
128
- const willLogHttp = () => process.env.LOG_HTTP === 'true' || process.argv.includes('--log-http');
117
+ const willMinify = () => isProductionBuild() || process.env.MINIFY === "true" || process.argv.includes("--minify");
118
+ const willLogHttp = () => process.env.LOG_HTTP === "true" || process.argv.includes("--log-http");
119
+ export {
120
+ defaultProjectDirs,
121
+ isProductionBuild,
122
+ resolveBuildFlags,
123
+ resolveServeFlags
124
+ };