Skip to content

Commit 183bf8f

Browse files
authored
Merge pull request #988 from jasongrout/publicpath
Compute the public path automatically
2 parents dca6cf7 + 7f6dd45 commit 183bf8f

File tree

3 files changed

+78
-64
lines changed

3 files changed

+78
-64
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/src/notebook.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
// Copyright (c) Jupyter Development Team.
22
// Distributed under the terms of the Modified BSD License.
33

4-
// Setup notebook base URL
5-
__webpack_public_path__ = document.querySelector('body').getAttribute('data-base-url') + 'nbextensions/jupyter-leaflet/';
6-
74
module.exports = require('./index.js');

js/webpack.config.js

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,85 @@
1-
var path = require('path');
2-
var version = require('./package.json').version;
3-
var crypto = require('crypto');
1+
var path = require("path");
2+
var crypto = require("crypto");
43

54
// Workaround for loaders using "md4" by default, which is not supported in FIPS-compliant OpenSSL
65
var cryptoOrigCreateHash = crypto.createHash;
76
crypto.createHash = (algorithm) =>
8-
cryptoOrigCreateHash(algorithm == 'md4' ? 'sha256' : algorithm);
7+
cryptoOrigCreateHash(algorithm == "md4" ? "sha256" : algorithm);
98

109
var rules = [
11-
{
12-
test: /\.css$/,
13-
use: ['style-loader', 'css-loader']
14-
},
15-
{
16-
test: /\.(jpg|png|gif|svg)$/,
17-
use: [
18-
{
19-
loader: 'file-loader',
20-
options: {
21-
esModule: false,
22-
}
23-
}
24-
],
25-
}
10+
{ test: /\.css$/, use: ["style-loader", "css-loader"] },
11+
{ test: /\.(jpg|png|gif|svg)$/i, type: "asset" },
2612
];
2713

2814
var resolve = {
29-
fallback: {
30-
"crypto": require.resolve("crypto-browserify"),
31-
"buffer": require.resolve("buffer/"),
32-
"stream": require.resolve("stream-browserify")
33-
}
15+
fallback: {
16+
crypto: require.resolve("crypto-browserify"),
17+
buffer: require.resolve("buffer/"),
18+
stream: require.resolve("stream-browserify"),
19+
},
3420
};
3521

3622
module.exports = [
37-
{// Notebook extension
38-
entry: './src/extension.js',
39-
output: {
40-
filename: 'extension.js',
41-
path: path.resolve(__dirname, '..', 'ipyleaflet', 'nbextension'),
42-
libraryTarget: 'amd'
43-
},
44-
resolve: resolve
23+
{
24+
// 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.
31+
entry: "./src/extension.js",
32+
output: {
33+
filename: "extension.js",
34+
path: path.resolve(__dirname, "..", "ipyleaflet", "nbextension"),
35+
libraryTarget: "amd",
36+
},
37+
resolve: resolve,
38+
},
39+
{
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"],
46+
output: {
47+
filename: "index.js",
48+
path: path.resolve(__dirname, "..", "ipyleaflet", "nbextension"),
49+
libraryTarget: "amd",
50+
publicPath: "", // Set in amd-public-path.js
51+
},
52+
devtool: "source-map",
53+
module: {
54+
rules: rules,
55+
},
56+
// 'module' is the magic requirejs dependency used to set the publicPath
57+
externals: ["@jupyter-widgets/base", "module"],
58+
resolve: resolve,
59+
},
60+
{
61+
// Embeddable jupyter-leaflet 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"],
71+
output: {
72+
filename: "index.js",
73+
path: path.resolve(__dirname, "dist"),
74+
libraryTarget: "amd",
75+
publicPath: "", // Set in amd-public-path.js
4576
},
46-
{// jupyter-leaflet bundle for the classic notebook
47-
entry: './src/notebook.js',
48-
output: {
49-
filename: 'index.js',
50-
path: path.resolve(__dirname, '..', 'ipyleaflet', 'nbextension'),
51-
libraryTarget: 'amd',
52-
publicPath: '',
53-
},
54-
devtool: 'source-map',
55-
module: {
56-
rules: rules
57-
},
58-
externals: ['@jupyter-widgets/base'],
59-
resolve: resolve
77+
devtool: "source-map",
78+
module: {
79+
rules: rules,
6080
},
61-
{// jupyter-leaflet bundle for unpkg
62-
entry: './src/embed.js',
63-
output: {
64-
filename: 'index.js',
65-
path: path.resolve(__dirname, 'dist'),
66-
libraryTarget: 'amd',
67-
publicPath: 'https://unpkg.com/jupyter-leaflet@' + version + '/dist/'
68-
},
69-
devtool: 'source-map',
70-
module: {
71-
rules: rules
72-
},
73-
externals: ['@jupyter-widgets/base'],
74-
resolve: resolve
75-
}
81+
// 'module' is the magic requirejs dependency used to set the publicPath
82+
externals: ["@jupyter-widgets/base", "module"],
83+
resolve: resolve,
84+
},
7685
];

0 commit comments

Comments
 (0)