question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

json and html is not bundle

See original GitHub issue

As node official website suggested to use fs module to load json file, but esbuild is not bundling the .json and .html extension.

https://nodejs.org/api/esm.html#esm_no_json_module_loading

// lambda throws "Runtime.UnhandledPromiseRejection: TypeError [ERR_INVALID_URL]: Invalid URL: ./jsonSchema.json"
import { readFile } from 'fs/promises';
const jsonSchema = JSON.parse(await readFile(new URL('./jsonSchema.json', import.meta.url)));
// this work correctly in lambda, but mocha throws TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json"
import jsonSchema from './jsonSchema.json';

is there a way to use recommended way to bundle .json and .html files? i set the loader option but it is not working

  esbuild:
    bundle: true
    minify: true
    sourcemap: true
    keepNames: true
    loader:
      .html: text
      .json: text

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffthamcommented, Nov 20, 2021

Hi @jweyrich , thanks for your response. Actually, serverless-esbuild work as expected to bundle the file. The main issue is that we cannot test the source code because no esbuild-register created for js. There is one for typescript.

Yes, you solution may be work provided the it is a typescript project.

0reactions
jeffthamcommented, Nov 20, 2021

FYI, i have spent enough of time to find the possible solutions. Now i got it and happy to share.

Background:-

  • esbuild work correctly to import json and bundle it.
  • esbuild work correctly to import html and bundle it if we define it in serverless.yml as custom.esbuild.loader..html =text
  • mocha should test on the source code instead of compiled code but node or mocha do not know what is going on with esbuild.

Solution:-

  • import json is an unsupport/experimental feature, we can turn it on for nodejs or mocha. eg. --experimental-json-modules
  • import html is not support natively but we can use custom loader to achieve it. eg. --experimental-loader=./loader.js, ref to https://dev.to/valeriavg/how-to-use-custom-files-as-modules-in-nodejs-51lp
  • It is difficult to pass these args to mocha, the trick is to use node-option in the mocharc file.

node-option: - 'no-warnings=ExperimentalWarning' - 'experimental-json-modules' - 'experimental-loader=./loader.js'

Read more comments on GitHub >

github_iconTop Results From Across the Web

Separate json from html file - javascript - Stack Overflow
After much trial-error, I was able to somewhat separate the data and move it to a json file. The problem is that the...
Read more >
How to Bundle JavaScript With Rollup — Step-by-Step Tutorial
Learn how to use Rollup as a smaller, more efficient alternative to webpack and Browserify to bundle JavaScript files in this step-by-step tutorial...
Read more >
package.json Manifest - NodeCG
Bundles declared as bundleDependencies are not automatically installed. This field's only job is to ensure that dependant bundles are loaded first. In the...
Read more >
Content Types - ESBuild
Imported variables are live bindings to variables in another file. They are not copies of those variables. So when esbuild bundles your code,...
Read more >
Output Management - webpack
html file, but as your application grows and once you start using hashes in filenames and outputting multiple bundles, it will be difficult...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found