How to upgrade golden-layout usage from version 1.5.9 to 2.6.0 for browser usage without extra compilation?

Author: stefaneidellothCreated Jun 17, 2023Updated Nov 19, 2024

In an older project I use golden-layout 1.5.9 for a JupyterLab extension and I include it with requirejs. In the require.js config I have:

'golden-layout' : 'node_modules/golden-layout/dist/goldenlayout.min'

The paths for inclusion of the *.css files are:

'/node_modules/golden-layout/src/css/goldenlayout-base.css'

'/node_modules/golden-layout/src/css/goldenlayout-light-theme.css'

I updated golden-layout to the current version 2.6.0 and my application did not work any more. Then I found out that version 2.0.0 has been a rewrite in typscript and that it might not be compatible any more.

a) I tried to adapt the paths to

'golden-layout' : 'node_modules/golden-layout/dist/esm/index'

'/node_modules/golden-layout/dist/css/goldenlayout-base.css'

'/node_modules/golden-layout/dist/css/themes/goldenlayout-light-theme.css'

And got

Uncaught SyntaxError: Unexpected token 'export'

b) Then I tried to use the csj variant:

'golden-layout' : 'node_modules/golden-layout/dist/cjs/index'

and got

treezJupyterLab.js:229 Uncaught TypeError: GoldenLayout is not a constructor

c) I tried to remove golden-layout from the requirejs loading and import it directly:

import GoldenLayout from '../node_modules/golden-layout/dist/cjs/index.js'

and got

Uncaught SyntaxError: The requested module '../node_modules/golden-layout/dist/cjs/index.js' does not provide an export named 'default'

d)

The variantf

import {GoldenLayout} from '../node_modules/golden-layout/dist/cjs/index.js'

did not work:

Uncaught SyntaxError: The requested module '../node_modules/golden-layout/dist/cjs/index.js' does not provide an export named 'GoldenLayout'

e) I tried to use the ems path instead:

import GoldenLayout from '../node_modules/golden-layout/dist/ems/index.js'

and got

GET http://localhost:8888/files/treezjs/node_modules/golden-layout/dist/esm/ts/config/resolved-config net::ERR_ABORTED 404 (Not Found)
plugin.js:103 Could not load "workspace.js" for jupyter lab extension "workspace_module" at http://localhost:8888/tree/workspace.js
Could not load script http://localhost:8888/tree/workspace.js
{
    "targetInfo": {
        "tagName": "SCRIPT",
        "outerHTML": "<script async=\"\" type=\"module\" src=\"http://localhost:8888/tree/workspace.js\"></script>",
        "src": "http://localhost:8888/tree/workspace.js",
        "srcValue": "http://localhost:8888/tree/workspace.js"
    },
    "type": "error",
    "bubbles": false,
    "cancelable": false,
    "detail": "",
    "message": ""
}
index.js:3     GET http://localhost:8888/files/treezjs/node_modules/golden-layout/dist/esm/ts/container/component-container net::ERR_ABORTED 404 (Not Found)
index.js:1     GET http://localhost:8888/files/treezjs/node_modules/golden-layout/dist/esm/ts/config/config net::ERR_ABORTED ...

f) Then I found the information

Building single-file bundles

We provide different types of single file bundles for easier consumption without toolchain in-place. To do this, run npm run build:bundles, afterwards find your bundled files in dist/bundle/. Bundles are not built by default and are not included in the NPM package, we recommend everyone to consume the library through NPM and webpack.

I tried it with following commands:

bash
npm install webpack webpack-cli --upgrade
cd node_modules/golden-layout
npm run build:bundles

and got

[webpack-cli] Failed to load 'C:\python_env\workspace\treezjs\node_modules\golden-layout\scripts\webpack.config.js' config
[webpack-cli] Error: Cannot find module 'C:\python_env\workspace\treezjs\node_modules\golden-layout\scripts\webpack.config.js'
Require stack:
- C:\python_env\workspace\treezjs\node_modules\webpack-cli\lib\webpack-cli.js
- C:\python_env\workspace\treezjs\node_modules\webpack-cli\lib\bootstrap.js
- C:\python_env\workspace\treezjs\node_modules\webpack-cli\bin\cli.js
- C:\python_env\workspace\treezjs\node_modules\webpack\bin\webpack.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1090:15)
    at Module._load (node:internal/modules/cjs/loader:934:27)
    at Module.require (node:internal/modules/cjs/loader:1157:19)
    at require (node:internal/modules/helpers:119:18)
    at WebpackCLI.tryRequireThenImport (C:\python_env\workspace\treezjs\node_modules\webpack-cli\lib\webpack-cli.js:223:30)
    at loadConfigByPath (C:\python_env\workspace\treezjs\node_modules\webpack-cli\lib\webpack-cli.js:1406:38)
    at C:\python_env\workspace\treezjs\node_modules\webpack-cli\lib\webpack-cli.js:1460:88
    at Array.map (<anonymous>)
    at WebpackCLI.loadConfig (C:\python_env\workspace\treezjs\node_modules\webpack-cli\lib\webpack-cli.js:1460:68)
    at WebpackCLI.createCompiler (C:\python_env\workspace\treezjs\node_modules\webpack-cli\lib\webpack-cli.js:1781:33) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\python_env\\workspace\\treezjs\\node_modules\\webpack-cli\\lib\\webpack-cli.js',
    'C:\\python_env\\workspace\\treezjs\\node_modules\\webpack-cli\\lib\\bootstrap.js',
    'C:\\python_env\\workspace\\treezjs\\node_modules\\webpack-cli\\bin\\cli.js',
    'C:\\python_env\\workspace\\treezjs\\node_modules\\webpack\\bin\\webpack.js'
  ]
}

=> Could you please provide a minified version of golden-layout in the npm package? Or provide some detailed instructions on how to produce the bundle?

Related: https://github.com/golden-layout/golden-layout/issues/763

Source: golden-layout/golden-layout