Skip to content

Commit

Permalink
Remove moment from extension bundles (#12685)
Browse files Browse the repository at this point in the history
* Remove moment from bundle

* oops
  • Loading branch information
DonJayamanne authored Jan 30, 2023
1 parent 91f7e06 commit c1ad6a9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 10 deletions.
33 changes: 33 additions & 0 deletions build/ci/postInstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,42 @@ function fixUIFabricForTS49() {
});
}

/**
* Ensures that moment is not used by any other npm package other than @jupyterlab/coreutils.
* See comments here build/webpack/moment.js
*/
function verifyMomentIsOnlyUsedByJupyterLabCoreUtils() {
const packageLock = require(path.join(__dirname, '..', '..', 'package-lock.json'));
const packagesAllowedToUseMoment = ['node_modules/@jupyterlab/coreutils', '@jupyterlab/coreutils'];
const otherPackagesUsingMoment = [];
['packages', 'dependencies'].forEach((key) => {
if (!(key in packageLock)) {
throw new Error(`Invalid package-lock.json, as it does not contain the key '${key}'`);
}
const packages = packageLock[key];
Object.keys(packages).forEach((packageName) => {
if (packagesAllowedToUseMoment.includes(packageName)) {
return;
}
['dependencies', 'requires'].forEach((dependencyKey) => {
if (dependencyKey in packages[packageName]) {
const dependenciesOfPackage = packages[packageName][dependencyKey];
if ('moment' in dependenciesOfPackage) {
otherPackagesUsingMoment.push(`${key}.${dependencyKey}.${packageName}`);
}
}
});
});
});
if (otherPackagesUsingMoment.length > 0) {
throw new Error(`Moment is being used by another package (${otherPackagesUsingMoment.join(', ')}).`);
}
}

fixUIFabricForTS49();
fixJupyterLabRenderers();
makeVariableExplorerAlwaysSorted();
createJupyterKernelWithoutSerialization();
updateJSDomTypeDefinition();
fixStripComments();
verifyMomentIsOnlyUsedByJupyterLabCoreUtils();
27 changes: 27 additions & 0 deletions build/webpack/moment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// This file has been handcrafted to override the use of moment.js.
// Moment is only used in @jupyterlab/coreutils/lib/time.js
// & that code is not used anywhere in any code path.
// Hence we create a dummy moment object that does nothing.
// & we have checks on CI that ensures no other npm package or other code does not in-directly or directly use moment.

export default function (dateTime) {
return {
formatNow: () => {
try {
return dateTime.toLocaleString();
} catch {
return `${dateTime}`;
}
},
format: () => {
try {
return dateTime.toLocaleTimeString();
} catch {
return `${dateTime}`;
}
}
};
}
7 changes: 2 additions & 5 deletions build/webpack/webpack.extension.node.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ const config = {
new copyWebpackPlugin({ patterns: [{ from: './node_modules/zeromq/**/*.json' }] }),
new copyWebpackPlugin({ patterns: [{ from: './node_modules/node-gyp-build/**/*' }] }),
new copyWebpackPlugin({ patterns: [{ from: './node_modules/@vscode/jupyter-ipywidgets7/dist/*.js' }] }),
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/
}),
new webpack.DefinePlugin({
IS_PRE_RELEASE_VERSION_OF_JUPYTER_EXTENSION: JSON.stringify(
typeof process.env.IS_PRE_RELEASE_VERSION_OF_JUPYTER_EXTENSION === 'string'
Expand All @@ -147,7 +143,8 @@ const config = {
// Pointing pdfkit to a dummy js file so webpack doesn't fall over.
// Since pdfkit has been externalized (it gets updated with the valid code by copying the pdfkit files
// into the right destination).
pdfkit: path.resolve(__dirname, 'pdfkit.js')
pdfkit: path.resolve(__dirname, 'pdfkit.js'),
moment: path.join(__dirname, 'moment.js')
},
extensions: ['.ts', '.js'],
plugins: [new tsconfig_paths_webpack_plugin.TsconfigPathsPlugin({ configFile: configFileName })],
Expand Down
7 changes: 2 additions & 5 deletions build/webpack/webpack.extension.web.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ const config = {
)
}),
new CleanTerminalPlugin(),
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/
}),
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
})
Expand All @@ -130,7 +126,8 @@ const config = {
],
alias: {
// provides alternate implementation for node module and source files
fs: './fs-empty.js'
fs: './fs-empty.js',
moment: path.join(__dirname, 'moment.js')
},
fallback: {
os: require.resolve('os-browserify')
Expand Down

0 comments on commit c1ad6a9

Please sign in to comment.