Skip to content

Commit 122754b

Browse files
committed
Set public path for amd modules.
It seems that the auto public path logic in webpack does not work for AMD modules, since document.currentScript only works when the script is initially executed, not in a callback. In the cases where we know we are compiling to an AMD module, we can use the requirejs magic 'module' dependency to get the url of the file, and from that do our own auto public path magic. We encapsulate this auto public path setting into its own file so that it does not intrude on user code, and is only used when we know in the webpack config that we are compiling to an AMD module. See also jupyter-widgets/widget-cookiecutter#103
1 parent fd4cb26 commit 122754b

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

js/amd-public-path.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// In an AMD module, we set the public path using the magic requirejs 'module' dependency
2+
// See https://github.com/requirejs/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#module
3+
// Since 'module' is a requirejs magic module, we must include 'module' in the webpack externals configuration.
4+
var module = require('module');
5+
var url = new URL(module.uri, document.location)
6+
// Using lastIndexOf('/')+1 gives us the empty string if there is no '/', so pathname becomes '/'
7+
url.pathname = url.pathname.slice(0,url.pathname.lastIndexOf('/')+1);
8+
__webpack_public_path__ = `${url.origin}${url.pathname}`;

js/webpack.config.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ var resolve = {
2222
module.exports = [
2323
{
2424
// Notebook extension
25+
//
26+
// This bundle only contains the part of the JavaScript that is run on
27+
// load of the notebook. This section generally only performs
28+
// some configuration for requirejs, and provides the legacy
29+
// "load_ipython_extension" function which is required for any notebook
30+
// extension.
2531
entry: "./src/extension.js",
2632
output: {
2733
filename: "extension.js",
@@ -31,33 +37,49 @@ module.exports = [
3137
resolve: resolve,
3238
},
3339
{
34-
// jupyter-leaflet bundle for the classic notebook
35-
entry: "./src/notebook.js",
40+
// Bundle for the notebook containing the custom widget views and models
41+
//
42+
// This bundle contains the implementation for the custom widget views and
43+
// custom widget.
44+
// It must be an amd module
45+
entry: ["./amd-public-path.js", "./src/notebook.js"],
3646
output: {
3747
filename: "index.js",
3848
path: path.resolve(__dirname, "..", "ipyleaflet", "nbextension"),
3949
libraryTarget: "amd",
50+
publicPath: "", // Set in amd-public-path.js
4051
},
4152
devtool: "source-map",
4253
module: {
4354
rules: rules,
4455
},
45-
externals: ["@jupyter-widgets/base"],
56+
// 'module' is the magic requirejs dependency used to set the publicPath
57+
externals: ["@jupyter-widgets/base", "module"],
4658
resolve: resolve,
4759
},
4860
{
49-
// jupyter-leaflet bundle for CDN
50-
entry: "./src/embed.js",
61+
// Embeddable {{ cookiecutter.npm_package_name }} bundle
62+
//
63+
// This bundle is identical to the notebook bundle containing the custom
64+
// widget views and models. The only difference is it is placed in the
65+
// dist/ directory and shipped with the npm package for use from a CDN
66+
// like jsdelivr.
67+
//
68+
// The target bundle is always `dist/index.js`, which is the path
69+
// required by the custom widget embedder.
70+
entry: ["./amd-public-path.js", "./src/embed.js"],
5171
output: {
5272
filename: "index.js",
5373
path: path.resolve(__dirname, "dist"),
5474
libraryTarget: "amd",
75+
publicPath: "", // Set in amd-public-path.js
5576
},
5677
devtool: "source-map",
5778
module: {
5879
rules: rules,
5980
},
60-
externals: ["@jupyter-widgets/base"],
81+
// 'module' is the magic requirejs dependency used to set the publicPath
82+
externals: ["@jupyter-widgets/base", "module"],
6183
resolve: resolve,
6284
},
6385
];

0 commit comments

Comments
 (0)