Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically set public path #103

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions {{cookiecutter.github_project_name}}/js/amd-public-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// In an AMD module, we set the public path using the magic requirejs 'module' dependency
// See https://github.com/requirejs/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#module
// Since 'module' is a requirejs magic module, we must include 'module' in the webpack externals configuration.
var module = require('module');
var url = new URL(module.uri, document.location)
// Using lastIndexOf('/')+1 gives us the empty string if there is no '/', so pathname becomes '/'
url.pathname = url.pathname.slice(0,url.pathname.lastIndexOf('/')+1);
__webpack_public_path__ = `${url.origin}${url.pathname}`;
9 changes: 0 additions & 9 deletions {{cookiecutter.github_project_name}}/js/lib/embed.js

This file was deleted.

6 changes: 0 additions & 6 deletions {{cookiecutter.github_project_name}}/js/lib/extension.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
// This file contains the javascript that is run when the notebook is loaded.
// It contains some requirejs configuration and the `load_ipython_extension`
// which is required for any notebook extension.
//
// Some static assets may be required by the custom widget javascript. The base
// url for the notebook is not known at build time and is therefore computed
// dynamically.
__webpack_public_path__ = document.querySelector('body').getAttribute('data-base-url') + 'nbextensions/{{ cookiecutter.npm_package_name }}';


// Configure requirejs
if (window.require) {
Expand Down
34 changes: 14 additions & 20 deletions {{cookiecutter.github_project_name}}/js/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ module.exports = (env, argv) => {
// some configuration for requirejs, and provides the legacy
// "load_ipython_extension" function which is required for any notebook
// extension.
//
entry: './lib/extension.js',
output: {
filename: 'extension.js',
path: path.resolve(__dirname, '..', '{{ cookiecutter.python_package_name }}', 'nbextension'),
libraryTarget: 'amd',
publicPath: '' // publicPath is set in extension.js
},
devtool
},
Expand All @@ -33,46 +31,42 @@ module.exports = (env, argv) => {
// This bundle contains the implementation for the custom widget views and
// custom widget.
// It must be an amd module
//
entry: './lib/index.js',
entry: ['./amd-public-path.js', './lib/index.js'],
output: {
filename: 'index.js',
path: path.resolve(__dirname, '..', '{{ cookiecutter.python_package_name }}', 'nbextension'),
libraryTarget: 'amd',
publicPath: '',
publicPath: '', // Set in amd-public-path.js
},
devtool,
module: {
rules: rules
},
externals: ['@jupyter-widgets/base']
// 'module' is the magic requirejs dependency used to set the publicPath
externals: ['@jupyter-widgets/base', 'module']
},
{// Embeddable {{ cookiecutter.npm_package_name }} bundle
//
// This bundle is generally almost identical to the notebook bundle
// containing the custom widget views and models.
//
// The only difference is in the configuration of the webpack public path
// for the static assets.
//
// It will be automatically distributed by unpkg to work with the static
// widget embedder.
//
// The target bundle is always `dist/index.js`, which is the path required
// by the custom widget embedder.
// This bundle is identical to the notebook bundle containing the custom
// widget views and models. The only difference is it is placed in the
// dist/ directory and shipped with the npm package for use from a CDN
// like jsdelivr.
//
entry: './lib/embed.js',
// The target bundle is always `dist/index.js`, which is the path
// required by the custom widget embedder.
entry: ['./amd-public-path.js', './lib/index.js'],
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'amd',
publicPath: 'https://unpkg.com/{{ cookiecutter.npm_package_name }}@' + version + '/dist/'
publicPath: '', // Set in amd-public-path.js
},
devtool,
module: {
rules: rules
},
externals: ['@jupyter-widgets/base']
// 'module' is the magic requirejs dependency used to set the publicPath
externals: ['@jupyter-widgets/base', 'module']
}
];
}