@jseeio/jsee 0.8.7 → 0.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jseeio/jsee",
3
- "version": "0.8.7",
3
+ "version": "0.8.8",
4
4
  "description": "JavaScript Execution Environment",
5
5
  "type": "commonjs",
6
6
  "main": "src/cli.js",
package/src/cli.js CHANGED
@@ -852,6 +852,64 @@ function buildBundledModelExposeCode (bundleGlobalName, modelName) {
852
852
  `
853
853
  }
854
854
 
855
+
856
+ function escapeRegExp (value) {
857
+ return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
858
+ }
859
+
860
+ function readPackageJsonForFile (filePath) {
861
+ try {
862
+ const packageRoot = findPackageRoot(filePath)
863
+ const packageJsonPath = path.join(packageRoot, 'package.json')
864
+ return {
865
+ packageRoot,
866
+ packageJson: JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
867
+ }
868
+ } catch (error) {
869
+ return null
870
+ }
871
+ }
872
+
873
+ function createEmptyModulePlugin (specifiers) {
874
+ const list = Array.from(new Set(specifiers.filter(Boolean)))
875
+ if (!list.length) return null
876
+ const filter = new RegExp(`^(?:${list.map(escapeRegExp).join('|')})$`)
877
+ return {
878
+ name: 'jsee-empty-browser-modules',
879
+ setup (build) {
880
+ build.onResolve({ filter }, args => ({ path: args.path, namespace: 'jsee-empty' }))
881
+ build.onLoad({ filter: /.*/, namespace: 'jsee-empty' }, () => ({ contents: 'module.exports = {}', loader: 'js' }))
882
+ }
883
+ }
884
+ }
885
+
886
+ function getBrowserFieldEsbuildOptions (modelPath) {
887
+ const packageInfo = readPackageJsonForFile(modelPath)
888
+ if (!packageInfo) return {}
889
+
890
+ const browser = packageInfo.packageJson.browser
891
+ if (!browser || typeof browser !== 'object' || Array.isArray(browser)) {
892
+ return { absWorkingDir: packageInfo.packageRoot }
893
+ }
894
+
895
+ const alias = {}
896
+ const empty = []
897
+ Object.entries(browser).forEach(([key, value]) => {
898
+ if (!key || key.startsWith('.') || key.startsWith('/')) return
899
+ if (value === false) empty.push(key)
900
+ else if (typeof value === 'string' && value.length) alias[key] = value
901
+ })
902
+
903
+ const plugins = []
904
+ const emptyPlugin = createEmptyModulePlugin(empty)
905
+ if (emptyPlugin) plugins.push(emptyPlugin)
906
+
907
+ const options = { absWorkingDir: packageInfo.packageRoot }
908
+ if (Object.keys(alias).length) options.alias = alias
909
+ if (plugins.length) options.plugins = plugins
910
+ return options
911
+ }
912
+
855
913
  async function bundleModelCode (model, modelPath, code) {
856
914
  if (!shouldBundleModelCode(code)) return code
857
915
 
@@ -867,14 +925,14 @@ async function bundleModelCode (model, modelPath, code) {
867
925
  throw new Error(`Bundling ${model.url || modelPath} requires a valid JavaScript model name or an explicit module export.`)
868
926
  }
869
927
 
870
- const options = {
928
+ const options = Object.assign({
871
929
  bundle: true,
872
930
  write: false,
873
931
  platform: 'browser',
874
932
  format: 'iife',
875
933
  globalName: bundleGlobalName,
876
934
  logLevel: 'silent'
877
- }
935
+ }, getBrowserFieldEsbuildOptions(modelPath))
878
936
 
879
937
  if (needsExportShim) {
880
938
  options.stdin = {