You can use require
(commonJS modules) or import/export
(ESM), and you can even mix both in the same project ("hybrid package" based on sub-folders). You will need a build tool to handle imports if you want to ensure cross-browser compatibility.
I have been using esbuild in a recent project as a lightweight alternative to webpack but you can choose any builder that can combine multiple script files into one single output file.
Then you will only need one enqueue script directive.
Install esbuild
:
npm install --save-dev esbuild
and use it in your build target e.g. in package.json
:
"scripts": {
"build": "build:core build:postcss build:js",
"build:js": "esbuild src/js/scripts.js --bundle --sourcemap --target=es5 --outfile=themes/mytheme/js/scripts.js",
},
"type": "commonjs",
or change to "type": "module"
to use import
instead of require
.