diff --git a/packages/amazon-cognito-identity-js/webpack.config.js b/packages/amazon-cognito-identity-js/webpack.config.js index eb312ef42fb..f20133e2115 100644 --- a/packages/amazon-cognito-identity-js/webpack.config.js +++ b/packages/amazon-cognito-identity-js/webpack.config.js @@ -31,7 +31,8 @@ var config = { filename: '[name].js', path: __dirname + '/dist', libraryTarget: 'umd', - library: 'AmazonCognitoIdentity' + library: 'AmazonCognitoIdentity', + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, plugins: [ new webpack.optimize.OccurrenceOrderPlugin(), diff --git a/packages/amplify-ui/webpack.config.js b/packages/amplify-ui/webpack.config.js index e2540601b81..eb4f15b6331 100644 --- a/packages/amplify-ui/webpack.config.js +++ b/packages/amplify-ui/webpack.config.js @@ -10,7 +10,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_ui', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map', diff --git a/packages/analytics/webpack.config.js b/packages/analytics/webpack.config.js index 8afb25d417f..f18d9dffe00 100644 --- a/packages/analytics/webpack.config.js +++ b/packages/analytics/webpack.config.js @@ -11,7 +11,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_analytics', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map', diff --git a/packages/api/webpack.config.js b/packages/api/webpack.config.js index 6abd8b77ae2..40a2c0f9ce3 100644 --- a/packages/api/webpack.config.js +++ b/packages/api/webpack.config.js @@ -11,7 +11,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_api', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map', diff --git a/packages/auth/webpack.config.js b/packages/auth/webpack.config.js index 3b8538d2378..70fcc104152 100644 --- a/packages/auth/webpack.config.js +++ b/packages/auth/webpack.config.js @@ -14,7 +14,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_auth', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map', diff --git a/packages/aws-amplify/webpack-utils.js b/packages/aws-amplify/webpack-utils.js new file mode 100644 index 00000000000..a20cd7000a6 --- /dev/null +++ b/packages/aws-amplify/webpack-utils.js @@ -0,0 +1,80 @@ +// This module fixes sourcemap paths for the various Amplify packages. It's +// needed because packages at build time live in different (relative) folder +// locations than the folders where packages are installed from npm. For +// example, /packages/aws-amplify/src/index.ts imports @aws-amplify/ui. At build +// time, the aws-amplify and amplify-ui folders are siblings, but when the +// aws-amplify package is installed from npm, the amplify-ui folder is installed +// at ./node_modules/@aws-amplify/ui, which is a new name and is no longer a +// sibling to ./node_modules/aws-amplify like it was at build time. These +// changes mean that end users can't easily debug into Amplify code, especially +// using IDEs like VSCode. +// +// The code in this file changes paths inside Amplify sourcemaps to work around +// the issues above. The following changes are made: +// 1. sourcemap paths that point to node_modules dependencies (e.g. lodash) are +// mapped to webpack:///./node_modules/* +// 2. sourcemap paths that point to sibling packages under the @aws-amplify +// alias (like the UI example above) are mapped (using package names, not +// folders) to webpack:///./node_modules/@aws-amplify/* +// 3. other paths, e.g. relative paths in the same package, or webpack or node +// builtins, will be left alone (same behavior as current webpack config). +// +// These path mappings are designed to be compatible with VSCode's default +// source mapping options here: +// https://github.com/Microsoft/vscode-chrome-debug#sourcemaps +// +// IMPORTANT: if new packages are added to Amplify, add them to the map below. + +const packageFolderMap = { + 'amazon-cognito-identity-js': 'amazon-cognito-identity-js', + 'amplify-ui': '@aws-amplify/ui', + 'analytics': '@aws-amplify/analytics', + 'api': '@aws-amplify/api', + 'auth': '@aws-amplify/auth', + 'aws-amplify': 'aws-amplify', + 'aws-amplify-angular': 'aws-amplify-angular', + 'aws-amplify-react': 'aws-amplify-react', + 'aws-amplify-react-native': 'aws-amplify-react-native', + 'aws-amplify-vue': 'aws-amplify-vue', + 'cache': '@aws-amplify/cache', + 'core': '@aws-amplify/core', + 'interactions': '@aws-amplify/interactions', + 'pubsub': '@aws-amplify/pubsub', + 'pushnotification': '@aws-amplify/pushnotification', + 'storage': '@aws-amplify/storage', + 'xr': '@aws-amplify/xr', +} + +const folders = Object.keys(packageFolderMap); +const nodeModules = '/node_modules/'; +const webpackPrefix = 'webpack:///'; +const webpackNodeModules = webpackPrefix + '.' + nodeModules; + +function devtoolModuleFilenameTemplate (info) { + const resource = info.resource; + + if (resource.includes(nodeModules)) { + // dependency paths + const start = resource.indexOf(nodeModules); + const after = start + nodeModules.length; + return webpackNodeModules + resource.substring(after); + } else if (resource.includes('../')) { + // handle relative paths to other packages in this monorepo by converting them into absolute + // paths pointing at node_modules + for (let i = 0; i < folders.length; i++) { + const folder = folders[i]; + const relative = '../'+folder; + const start = resource.indexOf(relative); + if (start !== -1) { + const after = start + relative.length; + return webpackNodeModules + packageFolderMap[folder] + resource.substring(after); + } + } + } + // fall-through (e.g. relative paths in this package, webpack builtins, unknown package paths) + return webpackPrefix + resource; +} + +module.exports = { + devtoolModuleFilenameTemplate +} diff --git a/packages/aws-amplify/webpack.config.js b/packages/aws-amplify/webpack.config.js index 2a0ea695ef9..893bc58c8f8 100644 --- a/packages/aws-amplify/webpack.config.js +++ b/packages/aws-amplify/webpack.config.js @@ -11,7 +11,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws-amplify', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, externals: { 'react-native': 'react-native' diff --git a/packages/cache/webpack.config.js b/packages/cache/webpack.config.js index 18b1718c748..798fe3f10d4 100644 --- a/packages/cache/webpack.config.js +++ b/packages/cache/webpack.config.js @@ -11,7 +11,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_cache', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map', diff --git a/packages/core/webpack.config.js b/packages/core/webpack.config.js index 5779ba34dbb..d6a624bd65d 100644 --- a/packages/core/webpack.config.js +++ b/packages/core/webpack.config.js @@ -11,7 +11,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_core', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map', diff --git a/packages/interactions/webpack.config.js b/packages/interactions/webpack.config.js index d168162f072..501c400cd9e 100644 --- a/packages/interactions/webpack.config.js +++ b/packages/interactions/webpack.config.js @@ -11,7 +11,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_interactions', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map', diff --git a/packages/pubsub/webpack.config.js b/packages/pubsub/webpack.config.js index 0555043bb77..242f97721cd 100644 --- a/packages/pubsub/webpack.config.js +++ b/packages/pubsub/webpack.config.js @@ -11,7 +11,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_pubsub', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map', diff --git a/packages/storage/webpack.config.js b/packages/storage/webpack.config.js index 3b5746344f6..a58fcea8bd1 100644 --- a/packages/storage/webpack.config.js +++ b/packages/storage/webpack.config.js @@ -11,7 +11,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_storage', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map', diff --git a/packages/xr/webpack.config.js b/packages/xr/webpack.config.js index 76d57e0d2a4..58371761ea9 100644 --- a/packages/xr/webpack.config.js +++ b/packages/xr/webpack.config.js @@ -11,7 +11,8 @@ module.exports = { path: __dirname + '/dist', library: 'aws_amplify_xr', libraryTarget: 'umd', - umdNamedDefine: true + umdNamedDefine: true, + devtoolModuleFilenameTemplate: require('../aws-amplify/webpack-utils').devtoolModuleFilenameTemplate }, // Enable sourcemaps for debugging webpack's output. devtool: 'source-map',