Closed nathanjhood closed 2 months ago
Steps taken:
build
script up and running; CLI sets up the process and args, then spawns the build
script as child processesbuild-scripts
project (i.e., itself), so I moved on to consumer testingbuild
via the CLI, as intendedResult:
Overview:
in a hypothetical, new React project (as created by, for example, the npm create-react-app
package), here called "consumer":
// package.json
{
"name": "consumer",
"dependencies": {
"@nathanjhood/esbuld-scripts": "./packages/@nathanjhood/esbuld-scripts",
"react": "18.2.0",
"react-dom": "18.2.0",
// etc...
},
"scripts": {
"build": "esbuild-scripts build"
}
}
The esbuild-scripts
repository is located at <projectRoot>/packages/@nathanjhood/esbuld-scripts
, as described in the above package.json
. It has been pre-built, meaning that it already contains it's own node_modules
and dist/*.js
, in order to isolate any issues with the underlying mechanisms before worrying too much about where to find the files.
In the "consumer" project root directory, call the aliased esbuild-scripts build
command via Yarn, observe output result:
$ yarn build
Starting @nathanjhood/esbuild-scripts v0.1.0
┊
├──┐
/***/consumer
[ '/***/.config/nvm/versions/node/v20.16.0/bin/node' ]
[ 'build' ]
[ '/***/.config/nvm/versions/node/v20.16.0/bin/node' ]
[ 'build' ]
│ │
│ └─ Recieved Script: build
│ │
node:internal/modules/cjs/loader:1148
throw err;
^
Error: Cannot find module '../config/esbuild/getBuildOptions'
Require stack:
- /***/consumer/node_modules/@nathanjhood/esbuild-scripts/dist/pkgroll_create-require-tKTPlRJW.js
- /***/consumer/node_modules/@nathanjhood/esbuild-scripts/dist/scripts/build.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1145:15)
at Module._load (node:internal/modules/cjs/loader:986:27)
at # etc....
Node.js v20.16.0
│ └─ build time taken: 499.561ms
│
├─ @nathanjhood/esbuild-scripts time taken: 578.619ms
┊
Done in 1.47s.
It would be wise to continue development of esbuild-scripts
from this point forward from the context of a consumer project.
Tests should only be developed in the context of the top-level project; thus I'll need to probably generate some very basic tests in the esbuild-scripts
project context, and then dive over to the "consumer" project to see which further issues might arise - and need tests for.
Update:
The good:
node_modules/bin
directory with a symlink, when running yarn install
see: esbuild-scripts
is listed
The bad:
package.json#bin
is also getting symlinked - all the helper functions, as shownsee: `getClientis also listed, and so is
build`*
The interesting:
$ yarn getClientEnvironment
The unfortunate:
So we've arrived at an interesting and unforseen choice;
node_modules/bin
, and having it bundle in all the helper functions it needs by changing the import/export syntaxes, and removing the helpers from package.json#bin
Rather than make a supposedly "logical" decision now, I'd quite like to experiment with this briefly to investigate usage cases, potential issues, paths of least resistance (pun intended) and so forth.
Progress:
package.json#bin.<path/to/file>
to package.json#exports.<path/to/file>
Result:
note: the unwanted symlinks to the helpers and build script are gone
The CLI successfully locates the build script, but that script fails due to not resolving a helper function.
Could it be as simple as changing the import statement in the script...?
Progress:
esbuild-scripts
<3<projectRoot>/node_modules/@nathanjhood/esbuild-scripts
and change all pkgroll_createRequire.require('path/to/helper')
calls to use the regular require('path/to/helper')
(ie., not the pkgroll shim)package.json@exports:{}
correctlySo, to take this PR home, I need to set up these exports correctly such that the output, as found in "consumer/node_modules/...." is runnable.
That's a milestone of a bigger milestone - one cornerpiece of the 0.0.1
baseline, in view.
Update:
It's done.
The "consumer" project is building and all is correct. Results are serving as per above, but without any modifications to the node_modules
contents. I added the copyPublicFolder
function to finish the job.
Main takeaway from this PR was to shim every file with a createRequire(__filename)
- __filename
, not import.meta.url
!!! - so that each one can resolve themselves and their relative imports/requires correctly.
* any mention of import.meta.url
automatically converts the file to a module, per NodeJS docs - despite not mentioning the __filename
workaround anywhere...
Later, I will probably do something like this in the package.json#exports
:
"exports": {
"src/process/parseArgs.ts": {
"require": {
"types": "./dist/process/parseArgs.d.cts",
"default": "./dist/process/parseArgs.cjs"
},
"import": {
"types": "./dist/process/parseArgs.d.mts",
"default": "./dist/process/parseArgs.mjs"
}
}
// and all the rest....
}
Just needed to puth the template somewhere so I can use it later to get that done.
issue:
export = x
withimport x = require('path/to/x')
is correct, for running exported scripts and CLI in NodeJS environmentpackage.json#exports
for every helper function -parseEnv
and so forth...esbuild-scripts
library, so should probably just be bundled in the finalindex.js
for consumerssolution:
export = x
withimport x = require('path/to/x')
syntax forcli.ts
,build.ts
,start.ts
,test.ts
, and other executable scriptsexport { x as default, x }
or justexport default x
withimport {x} from 'path/to/x'
or justimport x from 'path/to/x'
for eveything else, such asparseEnv
,getBuildOptions
, etcblockers:
plan:
build
is almost done) up and running to a basic level, and try some soft deploy testing locally to see how the bundledesbuild-scripts
package behaves<projectRoot>/node_modules/@nathanjhood/esbuild-scripts
directory with the CLI, or from the<projectRoot>/scripts
directory without a CLIindex.test.ts
)