Closed plastikfan closed 2 years ago
I'm not really that sure we need this because we can bundle using Rollup. I just think it would be an interesting thing to tryout out as an alterntive to rollup to enable dual-mode on the package. I only got so far with this. Steps completed were:
#!/usr/bin/env node
import esbuild from 'esbuild';
const build = esbuild.build;
console.log(`running esbuild from config, [name]`);
// this options object has a tsconfig parameter so you can specify the tsconfig file
build({
entryPoints: ["src/main.js"],
logLevel: "info",
bundle: false,
platform: "node",
format: "cjs",
outfile: "lib/starter.bundle.cjs",
sourcemap: true,
preserveSymlinks: true
// treeShaking: true
// minify: true
}).catch(() => process.exit(1));
"build:es": "./build.es.js",
Running build:es, creates the bundle file in ./lib. However take a look at what generated:
...
var import_banner_in_colour = __toModule(require("./banner-in-colour.js"));
function banner() {
return (0, import_banner_in_colour.bannerInColor)("white");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
banner
});
The probem here is that we're require "./banner-in-colour.js", but banner-in-colour.js is not created by esbuild. When we go to run the test that requires the top-lovel module, we get a 'MODULE_NOT_FOUND' runtime error:
> node test/tryout.js && node test/tryout.cjs
white: The answer is: 42
internal/modules/cjs/loader.js:883
throw err;
^
Error: Cannot find module './banner-in-colour.js'
Require stack:
- /home/plastikfan/dev/github/snivilization/nodejs-esm-starter/lib/starter.bundle.cjs
- /home/plastikfan/dev/github/snivilization/nodejs-esm-starter/test/tryout.cjs
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/home/plastikfan/dev/github/snivilization/nodejs-esm-starter/lib/starter.bundle.cjs:27:42)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/plastikfan/dev/github/snivilization/nodejs-esm-starter/lib/starter.bundle.cjs',
'/home/plastikfan/dev/github/snivilization/nodejs-esm-starter/test/tryout.cjs'
]
}
✘ plastikfan@Janus ~/dev/github/snivilization/nodejs-esm-starter λ master ±
I am not sure what this is caused by, but enough time has been wasted on the for now and its not even that clear how much we need this, so it'll be placed on the back burner for now.
Actually, setting bundle: true, fixes the above error, but another error occurs:
plastikfan@Janus ~/dev/github/snivilization/nodejs-esm-starter λ master ± npm run test
> nodejs-esm-starter@1.0.0 test
> node test/tryout.js && node test/tryout.cjs
!!! __dirname: /home/plastikfan/dev/github/snivilization/nodejs-esm-starter/src/
white: The answer is: 42
internal/url.js:258
throw new ERR_INVALID_URL(input);
^
TypeError [ERR_INVALID_URL]: Invalid URL: .
at onParseError (internal/url.js:258:9)
at new URL (internal/url.js:334:5)
at Object.<anonymous> (/home/plastikfan/dev/github/snivilization/nodejs-esm-starter/lib/starter.bundle.cjs:42:50)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (/home/plastikfan/dev/github/snivilization/nodejs-esm-starter/test/tryout.cjs:2:20) {
input: '.',
code: 'ERR_INVALID_URL'
}
The error is with this line: 'fileURLToPath', although its not clear why because it evalues to '/home/plastikfan/dev/github/snivilization/nodejs-esm-starter/src/' as shown.
NB: esbuild does support reading input from a stream so it should be easy to integrate this into gulp with the pipe command.
not appropriate
See: