Selaa lähdekoodia

[frontend] Switch from the SourceMapDevToolPlugin to cheap-source-map

This improves the build performance and resolve the issue with the build path appearing in the map files.
Johan Åhlén 4 vuotta sitten
vanhempi
commit
1e236d3224

+ 51 - 0
desktop/core/src/desktop/js/webpack/AdjustMapPathsPlugin.js

@@ -0,0 +1,51 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+const path = require('path');
+const fs = require('fs');
+
+// Webpack automatically adds a reference to a js.map file in the sourceMappingUrl in the bottom of
+// each chunk without the static path prefix. This plugin adds the missing static path to the variable.
+class AdjustMapPathsPlugin {
+  apply(compiler) {
+    compiler.hooks.afterEmit.tapAsync('AdjustMapPathsPlugin', (compilation, callback) => {
+      compilation.chunks.forEach(chunk => {
+        chunk.files.forEach(filename => {
+          if (/\.js$/.test(filename)) {
+            const relativePathMatch = compilation.outputOptions.path.match(
+              /.*(\/static\/desktop\/js\/bundles\/.*)$/
+            );
+            if (relativePathMatch) {
+              const outputFilename =
+                compilation.outputOptions.path + '/' + filename.split('/').pop();
+              const source = fs.readFileSync(path.resolve(outputFilename), 'utf8');
+              if (source.indexOf('//# sourceMappingURL=') !== -1) {
+                const cleanSource = source.replace(
+                  '//# sourceMappingURL=',
+                  `//# sourceMappingURL=${relativePathMatch[1]}/`
+                );
+                fs.writeFileSync(outputFilename, cleanSource);
+              }
+            }
+          }
+        });
+      });
+      callback();
+    });
+  }
+}
+
+module.exports = AdjustMapPathsPlugin;

+ 2 - 9
desktop/core/src/desktop/js/webpack/configUtils.js

@@ -16,7 +16,7 @@
 
 const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
 const RelativeBundleTracker = require('./relativeBundleTracker');
-const RemoveVueAbsolutePathFromMapPlugin = require('./removeVueAbsolutePathFromMapPlugin');
+const AdjustMapPathsPlugin = require('./AdjustMapPathsPlugin');
 const webpack = require('webpack');
 const { VueLoaderPlugin } = require('vue-loader');
 
@@ -29,13 +29,6 @@ const BUNDLES = {
 const getPluginConfig = (name, withAnalyzer) => {
   const plugins = [
     new webpack.ProgressPlugin(),
-    new webpack.SourceMapDevToolPlugin({
-      exclude: [/-parser-/g],
-      filename: `${name}/[file].map`,
-      publicPath: `/static/desktop/js/bundles/${name}/`,
-      fileContext: 'public',
-      columns: false
-    }),
     new RelativeBundleTracker({
       path: '.',
       filename: `webpack-stats${name !== BUNDLES.HUE ? '-' + name : ''}.json`
@@ -43,7 +36,7 @@ const getPluginConfig = (name, withAnalyzer) => {
     new webpack.BannerPlugin(
       '\nLicensed to Cloudera, Inc. under one\nor more contributor license agreements.  See the NOTICE file\ndistributed with this work for additional information\nregarding copyright ownership.  Cloudera, Inc. licenses this file\nto you under the Apache License, Version 2.0 (the\n"License"); you may not use this file except in compliance\nwith the License.  You may obtain a copy of the License at\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an "AS IS" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n'
     ),
-    new RemoveVueAbsolutePathFromMapPlugin()
+    new AdjustMapPathsPlugin()
   ];
   if (withAnalyzer) {
     plugins.push(new BundleAnalyzerPlugin({ analyzerPort: 9000 }));

+ 0 - 48
desktop/core/src/desktop/js/webpack/removeVueAbsolutePathFromMapPlugin.js

@@ -1,48 +0,0 @@
-// Licensed to Cloudera, Inc. under one
-// or more contributor license agreements.  See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership.  Cloudera, Inc. licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License.  You may obtain a copy of the License at
-//
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-const fs = require('fs');
-
-// Vue generates absolute paths in the .js.map files for vue-hot-reload-api, this replaces it
-// with a relative path.
-class RemoveVueAbsolutePathFromMapPlugin {
-  apply(compiler) {
-    compiler.hooks.afterEmit.tapAsync(
-      'RemoveVueAbsolutePathFromMapPlugin',
-      (compilation, callback) => {
-        compilation.chunks.forEach(chunk => {
-          chunk.files.forEach(filename => {
-            if (/\.js\.map$/.test(filename)) {
-              const source = compilation.assets[filename].source();
-              if (/"[^"]+\/node_modules\/vue-hot-reload-api/.test(source)) {
-                const actualFilename = filename.split('/').pop();
-                const outputFilename = compilation.outputOptions.path + '/' + actualFilename;
-                const cleanSource = source.replace(
-                  /"[^"]+\/node_modules\/vue-hot-reload-api/gi,
-                  '"../../../../../../../../node_modules/vue-hot-reload-api'
-                );
-                fs.writeFileSync(outputFilename, cleanSource);
-              }
-            }
-          });
-        });
-        callback();
-      }
-    );
-  }
-}
-
-module.exports = RemoveVueAbsolutePathFromMapPlugin;

+ 6 - 2
webpack.config.js

@@ -23,7 +23,7 @@ const {
 } = require('./desktop/core/src/desktop/js/webpack/configUtils');
 
 const config = {
-  devtool: false,
+  devtool: 'cheap-source-map',
   entry: {
     hue: { import: './desktop/core/src/desktop/js/hue.js' },
     editor: { import: './desktop/core/src/desktop/js/apps/editor/app.js', dependOn: 'hue' },
@@ -72,7 +72,11 @@ const config = {
     path: __dirname + '/desktop/core/src/desktop/static/desktop/js/bundles/hue',
     filename: '[name]-bundle-[fullhash].js',
     chunkFilename: '[name]-chunk-[fullhash].js',
-    clean: true
+    clean: true,
+    devtoolModuleFilenameTemplate(info) {
+      // Prevents absolute paths in the generated sourceMaps
+      return `webpack:///${info.resourcePath.replace(__dirname, '.')}`;
+    }
   },
   performance: {
     maxEntrypointSize: 400 * 1024, // 400kb