admin管理员组

文章数量:1327450

i have this module

import * as cborg from 'cborg'
import { CID } from 'multiformats/cid'
function cidEncoder (obj) {

}
....

when i build module with this mand line

"bundle": "./node_modules/.bin/esbuild  ./dist/index.mjs  --bundle   --outfile=./dist/out.mjs",

I have bundle without export default

when i build module with this mand line

"build": "./node_modules/.bin/esbuild  ./src/index.js  --target=es2020   --outfile=./dist/index.mjs",

This import not include in module

import * as cborg from 'cborg'
import { CID } from 'multiformats/cid'

How can i create module with include all modules in one file ?

i have this module

import * as cborg from 'cborg'
import { CID } from 'multiformats/cid'
function cidEncoder (obj) {

}
....

when i build module with this mand line

"bundle": "./node_modules/.bin/esbuild  ./dist/index.mjs  --bundle   --outfile=./dist/out.mjs",

I have bundle without export default

when i build module with this mand line

"build": "./node_modules/.bin/esbuild  ./src/index.js  --target=es2020   --outfile=./dist/index.mjs",

This import not include in module

import * as cborg from 'cborg'
import { CID } from 'multiformats/cid'

How can i create module with include all modules in one file ?

Share Improve this question asked Jan 2, 2023 at 22:29 SergeySergey 7021 gold badge12 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

It looks like you haven't specified a platform, so the default will be browser, and according to the esbuild docs the output format (which you also haven't specified) will default to iife:

When the platform is set to browser (the default value):

  • When bundling is enabled the default output format is set to iife, which wraps the generated JavaScript code in an immediately-invoked function expression to prevent variables from leaking into the global scope.

So given that you're trying to generate an ESM module (i.e. based on the *.mjs extension on the build output file) you'll need an esm output format, which is the default when specifying a neutral platform:

"bundle": "esbuild  ./src/index.js  --bundle --platform=neutral  --outfile=./dist/out.mjs",

You'll also need to add a default export to your index.js file:

import * as cborg from 'cborg'
import { CID } from 'multiformats/cid'

export default function cidEncoder (obj) {
  ...
}

Hope that helps.

本文标签: javascriptHow create bundle with esbuild with import modulesStack Overflow