Skip to content

Commit

Permalink
fix: Tell webpack about dynamic import + fixed polyfills (#1151)
Browse files Browse the repository at this point in the history
fix: tell webpack about dynamic import + fixed polyfills
  • Loading branch information
ecp4224 authored Nov 29, 2024
1 parent a535262 commit 20378ca
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-install-modal-web/stencil.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const config: Config = {
},
],
enableCache: true,
buildEs5: true,
buildEs5: false,
// Add hash for file names for better caching
hashFileNames: true,
excludeUnusedDependencies: true,
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"rollup-plugin-dts": "^4.2.2",
"rollup-plugin-jscc": "^2.0.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-polyfill-node": "^0.13.0",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.36.0",
Expand Down
21 changes: 19 additions & 2 deletions packages/sdk-react/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import external from 'rollup-plugin-peer-deps-external';
import { terser } from 'rollup-plugin-terser';
import globals from 'rollup-plugin-polyfill-node';

const packageJson = require('./package.json');

Expand Down Expand Up @@ -35,8 +36,16 @@ const config = [
browser: true,
}),
commonjs(),
globals({
include: null,
}),
json(),
terser(),
terser({
format: {
// keep all /* webpack*: * */ comments and /* vite-*: * */ comments
comments: (_, comment) => comment.value.includes("webpack") || comment.value.includes("vite")
}
}),
],
},
{
Expand All @@ -60,8 +69,16 @@ const config = [
preferBuiltins: true,
}),
commonjs(),
globals({
include: null,
}),
json(),
terser(),
terser({
format: {
// keep all /* webpack*: * */ comments and /* vite-*: * */ comments
comments: (_, comment) => comment.value.includes("webpack") || comment.value.includes("vite")
}
}),
],
}
];
Expand Down
1 change: 0 additions & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@
"rollup-plugin-jscc": "^2.0.0",
"rollup-plugin-natives": "^0.7.5",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-globals": "^1.4.0",
"rollup-plugin-polyfill-node": "^0.13.0",
"rollup-plugin-sizes": "^1.0.6",
"rollup-plugin-typescript2": "^0.31.2",
Expand Down
59 changes: 52 additions & 7 deletions packages/sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import terser from '@rollup/plugin-terser';
import builtins from 'rollup-plugin-node-builtins';
import { visualizer } from 'rollup-plugin-visualizer';
import replace from '@rollup/plugin-replace';
import globals from 'rollup-plugin-node-globals';
import globals from 'rollup-plugin-polyfill-node';

const packageJson = require('./package.json');
const isDev = process.env.NODE_ENV === 'dev';
Expand All @@ -33,6 +33,24 @@ const sharedDeps = ['eventemitter2', 'socket.io-client', 'debug', 'uuid', 'cross
// Filter function to exclude bundled dependencies
const excludeBundledDeps = (dep) => !bundledDeps.includes(dep);

// Used to inject "/* webpackIgnore: true */" for sdk-install-modal-web dynamic import
const injectWebpackIgnore = () => {
return {
name: 'inject-webpack-ignore',
generateBundle(_, bundle) {
for (const file of Object.keys(bundle)) {
if (file === 'metamask-sdk.js' && bundle[file].code) {
// Inject the webpackIgnore comment into dynamic import
bundle[file].code = bundle[file].code.replace(
/import\(/g,
'import(/* webpackIgnore: true */'
);
}
}
},
};
};

// Dependencies that should always be external
const baseExternalDeps = [
...peerDependencies.filter(excludeBundledDeps),
Expand Down Expand Up @@ -165,16 +183,25 @@ const configs = [
transformMixedEsModules: true,
include: /node_modules/,
}),
globals(),
globals({
include: null,
}),
builtins({
crypto: true,
stream: true,
http: true,
https: true,
url: true,
process: true,
buffer: false,
}),
terser(),
terser({
format: {
// keep all /* webpack*: * */ comments and /* vite-*: * */ comments
comments: (_, comment) => comment.value.includes("webpack") || comment.value.includes("vite")
}
}),
injectWebpackIgnore(),
],
onwarn: sharedWarningHandler,
},
Expand Down Expand Up @@ -234,9 +261,17 @@ const configs = [
dedupe: sharedDeps,
}),
commonjs({ transformMixedEsModules: true, include: /node_modules/ }),
globals(),
globals({
include: null,
}),
builtins({ crypto: true }),
terser(),
terser({
format: {
// keep all /* webpack*: * */ comments and /* vite-*: * */ comments
comments: (_, comment) => comment.value.includes("webpack") || comment.value.includes("vite")
}
}),
injectWebpackIgnore(),
],
onwarn: sharedWarningHandler,
},
Expand All @@ -263,7 +298,12 @@ const configs = [
browser: true,
preferBuiltins: true,
}),
terser(),
terser({
format: {
// keep all /* webpack*: * */ comments and /* vite-*: * */ comments
comments: (_, comment) => comment.value.includes("webpack") || comment.value.includes("vite")
}
}),
],
onwarn: sharedWarningHandler,
},
Expand Down Expand Up @@ -306,7 +346,12 @@ const configs = [
exportConditions: ['node'],
}),
commonjs({ transformMixedEsModules: true }),
terser(),
terser({
format: {
// keep all /* webpack*: * */ comments and /* vite-*: * */ comments
comments: (_, comment) => comment.value.includes("webpack") || comment.value.includes("vite")
}
}),
],
onwarn: sharedWarningHandler,
},
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11269,6 +11269,7 @@ __metadata:
rollup-plugin-node-builtins: ^2.1.2
rollup-plugin-node-globals: ^1.4.0
rollup-plugin-peer-deps-external: ^2.2.4
rollup-plugin-polyfill-node: ^0.13.0
rollup-plugin-postcss: ^4.0.2
rollup-plugin-terser: ^7.0.2
rollup-plugin-typescript2: ^0.36.0
Expand Down Expand Up @@ -11606,7 +11607,6 @@ __metadata:
rollup-plugin-jscc: ^2.0.0
rollup-plugin-natives: ^0.7.5
rollup-plugin-node-builtins: ^2.1.2
rollup-plugin-node-globals: ^1.4.0
rollup-plugin-polyfill-node: ^0.13.0
rollup-plugin-sizes: ^1.0.6
rollup-plugin-typescript2: ^0.31.2
Expand Down

0 comments on commit 20378ca

Please sign in to comment.