Skip to content

Commit

Permalink
Merge pull request #105 from jasongrout/es6
Browse files Browse the repository at this point in the history
Update to es6 modules and more modern js syntax
  • Loading branch information
vidartf authored Oct 21, 2022
2 parents 6134771 + 3d3a5f0 commit 134946a
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 44 deletions.
4 changes: 2 additions & 2 deletions {{cookiecutter.github_project_name}}/js/amd-public-path.js
Original file line number Diff line number Diff line change
@@ -1,8 +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)
import * as module from 'module';
const 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}`;
40 changes: 16 additions & 24 deletions {{cookiecutter.github_project_name}}/js/lib/example.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
var widgets = require('@jupyter-widgets/base');
var _ = require('lodash');
import { DOMWidgetModel, DOMWidgetView } from '@jupyter-widgets/base';

// See example.py for the kernel counterpart to this file.


// Custom Model. Custom widgets models must at least provide default values
// for model attributes, including
//
Expand All @@ -18,38 +16,32 @@ var _ = require('lodash');
// when different from the base class.

// When serialiazing the entire widget state for embedding, only values that
// differ from the defaults will be specified.
var HelloModel = widgets.DOMWidgetModel.extend({
defaults: _.extend(widgets.DOMWidgetModel.prototype.defaults(), {
// differ from the defaults will be serialized.

export class HelloModel extends DOMWidgetModel {
defaults() {
return {
...super.defaults(),
_model_name : 'HelloModel',
_view_name : 'HelloView',
_model_module : '{{ cookiecutter.npm_package_name }}',
_view_module : '{{ cookiecutter.npm_package_name }}',
_model_module_version : '{{ cookiecutter.npm_package_version }}',
_view_module_version : '{{ cookiecutter.npm_package_version }}',
value : 'Hello World!'
})
});

};
}
}

// Custom View. Renders the widget model.
var HelloView = widgets.DOMWidgetView.extend({
// Defines how the widget gets rendered into the DOM
render: function() {
export class HelloView extends DOMWidgetView {
render() {
this.value_changed();

// Observe changes in the value traitlet in Python, and define
// a custom callback.
// Observe and act on future changes to the value attribute
this.model.on('change:value', this.value_changed, this);
},
}

value_changed: function() {
value_changed() {
this.el.textContent = this.model.get('value');
}
});


module.exports = {
HelloModel: HelloModel,
HelloView: HelloView
};
}
5 changes: 1 addition & 4 deletions {{cookiecutter.github_project_name}}/js/lib/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,4 @@ if (window.require) {
});
}

// Export the required load_ipython_extension
module.exports = {
load_ipython_extension: function() {}
};
export function load_ipython_extension() { };
5 changes: 3 additions & 2 deletions {{cookiecutter.github_project_name}}/js/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Export widget models and views, and the npm package version number.
module.exports = require('./example.js');
module.exports['version'] = require('../package.json').version;

export {HelloModel, HelloView} from './example';
export {version} from '../package.json';
13 changes: 7 additions & 6 deletions {{cookiecutter.github_project_name}}/js/lib/labplugin.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
var plugin = require('./index');
var base = require('@jupyter-widgets/base');
import {HelloModel, HelloView, version} from './index';
import {IJupyterWidgetRegistry} from '@jupyter-widgets/base';

module.exports = {
export const helloWidgetPlugin = {
id: '{{ cookiecutter.npm_package_name }}:plugin',
requires: [base.IJupyterWidgetRegistry],
requires: [IJupyterWidgetRegistry],
activate: function(app, widgets) {
widgets.registerWidget({
name: '{{ cookiecutter.npm_package_name }}',
version: plugin.version,
exports: plugin
version: version,
exports: { HelloModel, HelloView }
});
},
autoStart: true
};

export default helloWidgetPlugin;
3 changes: 1 addition & 2 deletions {{cookiecutter.github_project_name}}/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
"rimraf": "^2.6.1"
},
"dependencies": {
"@jupyter-widgets/base": "^1.1 || ^2 || ^3 || ^4 || ^6",
"lodash": "^4.17.4"
"@jupyter-widgets/base": "^1.1 || ^2 || ^3 || ^4 || ^6"
},
"jupyterlab": {
"extension": "lib/labplugin",
Expand Down
8 changes: 4 additions & 4 deletions {{cookiecutter.github_project_name}}/js/webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var path = require('path');
var version = require('./package.json').version;
const path = require('path');
const version = require('./package.json').version;

// Custom webpack rules are generally the same for all webpack bundles, hence
// stored in a separate local variable.
var rules = [
const rules = [
{ test: /\.css$/, use: ['style-loader', 'css-loader']}
]


module.exports = (env, argv) => {
var devtool = argv.mode === 'development' ? 'source-map' : false;
const devtool = argv.mode === 'development' ? 'source-map' : false;
return [
{// Notebook extension
//
Expand Down

0 comments on commit 134946a

Please sign in to comment.