Skip to content

Commit 5a2856e

Browse files
cursoragentn3ps
andcommitted
feat: add ~/ui and ~/shared import alias infrastructure
Configure all build systems to resolve ~/ui and ~/shared aliases: - tsconfig.json: paths for ~/ui/* and ~/shared/* - webpack: resolve.alias entries - browserify: pathmodify plugin in build pipeline - jest: moduleNameMapper in unit and integration configs - storybook: resolve.alias in webpack config - depcheck: ignore aliases in dependency check No existing imports are changed. Both relative and aliased imports work simultaneously, enabling gradual opt-in adoption. Co-authored-by: Francis Nepomuceno <n3ps@users.noreply.github.com>
1 parent 2ddb15e commit 5a2856e

File tree

10 files changed

+81
-2
lines changed

10 files changed

+81
-2
lines changed

.depcheckrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ ignores:
105105
- '@metamask/permissions-kernel-snap'
106106
# perps poc
107107
- '@metamask/perps-controller'
108+
# import aliases (not real packages)
109+
- '~'
108110

109111
# files depcheck should not parse
110112
ignorePatterns:

.storybook/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ module.exports = {
3232
config.node = {
3333
__filename: true,
3434
};
35+
config.resolve.alias['~/ui'] = path.resolve(__dirname, '../ui');
36+
config.resolve.alias['~/shared'] = path.resolve(__dirname, '../shared');
3537
config.resolve.alias['webextension-polyfill'] = require.resolve(
3638
'../ui/__mocks__/webextension-polyfill.js',
3739
);

development/build/scripts.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const lavamoatBrowserify = require('lavamoat-browserify');
2727
const terser = require('terser');
2828

2929
const bifyModuleGroups = require('bify-module-groups');
30+
const pathmodify = require('pathmodify');
3031

3132
const { streamFlatMap } = require('../stream-flat-map');
3233
const { isManifestV3 } = require('../../shared/modules/mv3.utils');
@@ -1109,6 +1110,13 @@ async function createBundle(buildConfiguration, { reloadOnChange }) {
11091110
const { label, bundlerOpts, events } = buildConfiguration;
11101111
const bundler = browserify(bundlerOpts);
11111112

1113+
bundler.plugin(pathmodify, {
1114+
mods: [
1115+
pathmodify.mod.dir('~/ui', path.join(__dirname, '../../ui')),
1116+
pathmodify.mod.dir('~/shared', path.join(__dirname, '../../shared')),
1117+
],
1118+
});
1119+
11121120
// manually apply non-standard options
11131121
bundler.external(bundlerOpts.manualExternal);
11141122
bundler.ignore(bundlerOpts.manualIgnore);

development/webpack/webpack.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ const config = {
313313
context,
314314
'../ui/__mocks__/perps/perps-controller',
315315
),
316+
'~/ui': join(context, '../ui'),
317+
'~/shared': join(context, '../shared'),
316318
},
317319
// use `fallback` to redirect module requests when normal resolving fails,
318320
// good for polyfill-ing built-in node modules that aren't available in

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module.exports = {
1919
// Map @metamask/perps-controller to local mock
2020
'^@metamask/perps-controller$':
2121
'<rootDir>/ui/__mocks__/perps/perps-controller/index.ts',
22+
'^~/ui/(.*)$': '<rootDir>/ui/$1',
23+
'^~/shared/(.*)$': '<rootDir>/shared/$1',
2224
},
2325
// The path to the Prettier executable used to format snapshots
2426
// Jest doesn't support Prettier 3 yet, so we use Prettier 2

jest.integration.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ module.exports = {
1313
// Map @metamask/perps-controller to local mock
1414
'^@metamask/perps-controller$':
1515
'<rootDir>/ui/__mocks__/perps/perps-controller/index.ts',
16+
'^~/ui/(.*)$': '<rootDir>/ui/$1',
17+
'^~/shared/(.*)$': '<rootDir>/shared/$1',
1618
},
1719
// The path to the Prettier executable used to format snapshots
1820
// Jest doesn't support Prettier 3 yet, so we use Prettier 2

lavamoat/build-system/policy.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5017,6 +5017,14 @@
50175017
"util.promisify": true
50185018
}
50195019
},
5020+
"pathmodify": {
5021+
"builtin": {
5022+
"util.deprecate": true
5023+
},
5024+
"packages": {
5025+
"pathmodify>readable-stream": true
5026+
}
5027+
},
50205028
"gulp-livereload>event-stream>pause-stream": {
50215029
"packages": {
50225030
"browserify>JSONStream>through": true
@@ -5631,6 +5639,27 @@
56315639
"@storybook/react>util-deprecate": true
56325640
}
56335641
},
5642+
"pathmodify>readable-stream": {
5643+
"builtin": {
5644+
"buffer.Buffer": true,
5645+
"events.EventEmitter.listenerCount": true,
5646+
"stream": true,
5647+
"util": true
5648+
},
5649+
"globals": {
5650+
"process.browser": true,
5651+
"process.env.READABLE_STREAM": true,
5652+
"process.nextTick": true,
5653+
"process.stderr": true,
5654+
"process.stdout": true
5655+
},
5656+
"packages": {
5657+
"readable-stream-2>core-util-is": true,
5658+
"pumpify>inherits": true,
5659+
"pathmodify>readable-stream>isarray": true,
5660+
"pathmodify>readable-stream>string_decoder": true
5661+
}
5662+
},
56345663
"browserify>read-only-stream>readable-stream": {
56355664
"builtin": {
56365665
"events.EventEmitter": true,
@@ -7022,6 +7051,11 @@
70227051
"gulp>vinyl-fs>glob-stream>ordered-read-streams>readable-stream>safe-buffer": true
70237052
}
70247053
},
7054+
"pathmodify>readable-stream>string_decoder": {
7055+
"builtin": {
7056+
"buffer.Buffer": true
7057+
}
7058+
},
70257059
"browserify>read-only-stream>readable-stream>string_decoder": {
70267060
"packages": {
70277061
"browserify>read-only-stream>readable-stream>safe-buffer": true

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@
693693
"nyc": "^15.1.0",
694694
"octokit": "^4.1.3",
695695
"path-browserify": "^1.0.1",
696+
"pathmodify": "^0.5.0",
696697
"postcss": "^8.4.32",
697698
"postcss-discard-font-face": "^3.0.0",
698699
"postcss-loader": "^8.1.1",

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"baseUrl": ".",
2828
// Path mappings for module resolution
2929
"paths": {
30-
"@metamask/perps-controller": ["ui/__mocks__/perps/perps-controller"]
30+
"@metamask/perps-controller": ["ui/__mocks__/perps/perps-controller"],
31+
"~/ui/*": ["ui/*"],
32+
"~/shared/*": ["shared/*"]
3133
}
3234
},
3335
"exclude": [

yarn.lock

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33258,6 +33258,7 @@ __metadata:
3325833258
nyc: "npm:^15.1.0"
3325933259
octokit: "npm:^4.1.3"
3326033260
path-browserify: "npm:^1.0.1"
33261+
pathmodify: "npm:^0.5.0"
3326133262
pify: "npm:^5.0.0"
3326233263
postcss: "npm:^8.4.32"
3326333264
postcss-discard-font-face: "npm:^3.0.0"
@@ -35789,6 +35790,17 @@ __metadata:
3578935790
languageName: node
3579035791
linkType: hard
3579135792

35793+
"pathmodify@npm:^0.5.0":
35794+
version: 0.5.0
35795+
resolution: "pathmodify@npm:0.5.0"
35796+
dependencies:
35797+
readable-stream: "npm:1.x"
35798+
peerDependencies:
35799+
browserify: ">=5.1.0"
35800+
checksum: 10/48cc6345fc534c2d37cf8ad191c7f28663262c71877e62609570d32d3caf2655d1837b10ff90994bc731f581737d486eafeba1ef605627d67b00d1d967ab87ac
35801+
languageName: node
35802+
linkType: hard
35803+
3579235804
"pause-stream@npm:0.0.11":
3579335805
version: 0.0.11
3579435806
resolution: "pause-stream@npm:0.0.11"
@@ -38041,6 +38053,18 @@ __metadata:
3804138053
languageName: node
3804238054
linkType: hard
3804338055

38056+
"readable-stream@npm:1.x":
38057+
version: 1.1.14
38058+
resolution: "readable-stream@npm:1.1.14"
38059+
dependencies:
38060+
core-util-is: "npm:~1.0.0"
38061+
inherits: "npm:~2.0.1"
38062+
isarray: "npm:0.0.1"
38063+
string_decoder: "npm:~0.10.x"
38064+
checksum: 10/1aa2cf4bd02f9ab3e1d57842a43a413b52be5300aa089ad1f2e3cea00684532d73edc6a2ba52b0c3210d8b57eb20a695a6d2b96d1c6085ee979c6021ad48ad20
38065+
languageName: node
38066+
linkType: hard
38067+
3804438068
"readable-stream@npm:4.7.0, readable-stream@npm:^3.6.2 || ^4.4.2, readable-stream@npm:^4.5.2, readable-stream@npm:^4.7.0":
3804538069
version: 4.7.0
3804638070
resolution: "readable-stream@npm:4.7.0"
@@ -41114,7 +41138,7 @@ __metadata:
4111441138
languageName: node
4111541139
linkType: hard
4111641140

41117-
"string_decoder@npm:0.10":
41141+
"string_decoder@npm:0.10, string_decoder@npm:~0.10.x":
4111841142
version: 0.10.31
4111941143
resolution: "string_decoder@npm:0.10.31"
4112041144
checksum: 10/cc43e6b1340d4c7843da0e37d4c87a4084c2342fc99dcf6563c3ec273bb082f0cbd4ebf25d5da19b04fb16400d393885fda830be5128e1c416c73b5a6165f175

0 commit comments

Comments
 (0)