UNPKG

3.5 kBJavaScriptView Raw
1// This module fixes sourcemap paths for the various Amplify packages. It's
2// needed because packages at build time live in different (relative) folder
3// locations than the folders where packages are installed from npm. For
4// example, /packages/aws-amplify/src/index.ts imports @aws-amplify/ui. At build
5// time, the aws-amplify and amplify-ui folders are siblings, but when the
6// aws-amplify package is installed from npm, the amplify-ui folder is installed
7// at ./node_modules/@aws-amplify/ui, which is a new name and is no longer a
8// sibling to ./node_modules/aws-amplify like it was at build time. These
9// changes mean that end users can't easily debug into Amplify code, especially
10// using IDEs like VSCode.
11//
12// The code in this file changes paths inside Amplify sourcemaps to work around
13// the issues above. The following changes are made:
14// 1. sourcemap paths that point to node_modules dependencies (e.g. lodash) are
15// mapped to webpack:///./node_modules/*
16// 2. sourcemap paths that point to sibling packages under the @aws-amplify
17// alias (like the UI example above) are mapped (using package names, not
18// folders) to webpack:///./node_modules/@aws-amplify/*
19// 3. other paths, e.g. relative paths in the same package, or webpack or node
20// builtins, will be left alone (same behavior as current webpack config).
21//
22// These path mappings are designed to be compatible with VSCode's default
23// source mapping options here:
24// https://github.com/Microsoft/vscode-chrome-debug#sourcemaps
25//
26// IMPORTANT: if new packages are added to Amplify, add them to the map below.
27
28const packageFolderMap = {
29 'amazon-cognito-identity-js': 'amazon-cognito-identity-js',
30 'amplify-ui': '@aws-amplify/ui',
31 analytics: '@aws-amplify/analytics',
32 api: '@aws-amplify/api',
33 auth: '@aws-amplify/auth',
34 'aws-amplify': 'aws-amplify',
35 'aws-amplify-angular': 'aws-amplify-angular',
36 'aws-amplify-react': 'aws-amplify-react',
37 'aws-amplify-react-native': 'aws-amplify-react-native',
38 'aws-amplify-vue': 'aws-amplify-vue',
39 cache: '@aws-amplify/cache',
40 core: '@aws-amplify/core',
41 datastore: '@aws-amplify/datastore',
42 interactions: '@aws-amplify/interactions',
43 pubsub: '@aws-amplify/pubsub',
44 pushnotification: '@aws-amplify/pushnotification',
45 storage: '@aws-amplify/storage',
46 xr: '@aws-amplify/xr',
47};
48
49const folders = Object.keys(packageFolderMap);
50const nodeModules = '/node_modules/';
51const webpackPrefix = 'webpack:///';
52const webpackNodeModules = webpackPrefix + '.' + nodeModules;
53
54function devtoolModuleFilenameTemplate(info) {
55 const resource = info.resource;
56
57 if (resource.includes(nodeModules)) {
58 // dependency paths
59 const start = resource.indexOf(nodeModules);
60 const after = start + nodeModules.length;
61 return webpackNodeModules + resource.substring(after);
62 } else if (resource.includes('../')) {
63 // handle relative paths to other packages in this monorepo by converting them into absolute
64 // paths pointing at node_modules
65 for (let i = 0; i < folders.length; i++) {
66 const folder = folders[i];
67 const relative = '../' + folder;
68 const start = resource.indexOf(relative);
69 if (start !== -1) {
70 const after = start + relative.length;
71 return (
72 webpackNodeModules +
73 packageFolderMap[folder] +
74 resource.substring(after)
75 );
76 }
77 }
78 }
79 // fall-through (e.g. relative paths in this package, webpack builtins, unknown package paths)
80 return webpackPrefix + resource;
81}
82
83module.exports = {
84 devtoolModuleFilenameTemplate,
85};