NativeScript / nativescript-dev-webpack

A package to help with webpacking NativeScript apps.
Apache License 2.0
97 stars 49 forks source link

android and androidx are undefined with snapshot #1018

Closed jeremypele closed 5 years ago

jeremypele commented 5 years ago

Environment

Package.json

{
  "name": "chronogolf-mobile",
  "version": "0.1.0",
  "description": "Chronogolf Native Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "private": true,
  "nativescript": {
    "id": "xxx",
    "nodeify": {
      "package-dependencies": {
        "aws-sdk": [
          {
            "xml/browser_parser": "xml/node_parser"
          }
        ]
      }
    },
    "tns-ios": {
      "version": "6.0.2"
    },
    "tns-android": {
      "version": "6.0.1"
    }
  },
  "dependencies": {
    "@angular/animations": "8.0.0",
    "@angular/common": "8.0.0",
    "@angular/core": "8.0.0",
    "@angular/forms": "8.0.0",
    "@angular/http": "8.0.0-beta.10",
    "@angular/platform-browser": "8.0.0",
    "@angular/platform-browser-dynamic": "8.0.0",
    "@angular/router": "8.0.0",
    "@chronogolf-mobile/api": "file:../lib/api/src",
    "@chronogolf-mobile/design-system": "file:../lib/design-system/src",
    "@chronogolf-mobile/theme-core": "file:../lib/theme-core/src",
    "@ngrx/effects": "8.0.1",
    "@ngrx/store": "8.0.1",
    "@ngx-resource/core": "5.2.1",
    "@ngx-translate/core": "11.0.1",
    "@ngx-translate/http-loader": "4.0.0",
    "@nstudio/nativescript-checkbox": "^1.0.0",
    "@nstudio/nativescript-loading-indicator": "^1.0.0",
    "cachefactory": "3.0.0",
    "capitalize": "1.0.0",
    "creditcards": "2.1.2",
    "email-validator": "1.0.4",
    "html-entities": "1.2.1",
    "intl": "1.2.5",
    "lodash": "4.17.15",
    "lz-string": "1.4.4",
    "markdown": "0.5.0",
    "moment": "2.22.0",
    "nativescript-advanced-webview": "3.0.1",
    "nativescript-angular": "8.0.2",
    "nativescript-appversion": "1.4.2",
    "nativescript-email": "1.5.5",
    "nativescript-feedback": "1.3.10",
    "nativescript-geolocation": "5.1.0",
    "nativescript-image-cache-it": "3.0.7",
    "nativescript-intl": "3.0.0",
    "nativescript-iqkeyboardmanager": "1.4.0",
    "nativescript-ngx-fonticon": "4.2.1",
    "nativescript-ngx-resource-cache": "file:../lib/nativescript-ngx-resource-cache/src",
    "nativescript-orientation": "2.2.3",
    "nativescript-plugin-firebase": "^9.0.4",
    "nativescript-secure-storage": "2.5.0",
    "nativescript-sentry": "1.9.1",
    "nativescript-stripe": "5.4.2",
    "nativescript-ui-calendar": "5.0.0",
    "nativescript-ui-chart": "5.0.0",
    "nativescript-unit-test-runner": "0.7.0",
    "ng2-validation": "4.2.0",
    "reflect-metadata": "0.1.13",
    "reselect": "2.5.4",
    "rxjs": "^6.3.3",
    "rxjs-compat": "6.4.0",
    "striptags": "3.1.1",
    "tns-core-modules": "~6.0.0",
    "zone.js": "0.9.1"
  },
  "devDependencies": {
    "@angular/compiler": "8.0.0",
    "@angular/compiler-cli": "8.0.0",
    "@ngtools/webpack": "8.0.0",
    "@types/chai": "4.1.7",
    "@types/lodash": "4.14.123",
    "@types/mocha": "5.2.6",
    "@types/node": "7.10.5",
    "babel-traverse": "^6.26.0",
    "babel-types": "^6.26.0",
    "babylon": "6.8.0",
    "codelyzer": "4.1.0",
    "enhanced-resolve": "3.3.0",
    "filewalker": "0.1.2",
    "jasmine-core": "2.5.2",
    "karma": "4.1.0",
    "karma-jasmine": "2.0.1",
    "karma-nativescript-launcher": "0.4.0",
    "karma-webpack": "3.0.5",
    "lazy": "1.0.11",
    "manifest-android": "^0.1.1",
    "nativescript-dev-appium": "5.3.0",
    "nativescript-dev-webpack": "^1.0.3",
    "node-sass": "4.12.0",
    "plist": "2.1.0",
    "prettier": "1.16.4",
    "shelljs": "0.7.8",
    "socket.io": "1.4.8",
    "socket.io-client": "1.4.8",
    "stylelint-config-standard": "17.0.0",
    "stylelint-declaration-strict-value": "^1.1.3",
    "tns-platform-declarations": "6.0.1",
    "tslint": "5.2.0",
    "tslint-config-prettier": "1.18.0",
    "tslint-plugin-prettier": "1.3.0",
    "typescript": "3.4.5"
  }
}

webpack.config.js

const { join, relative, resolve, sep, dirname } = require("path");

const webpack = require("webpack");
const nsWebpack = require("nativescript-dev-webpack");
const nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
const { nsReplaceBootstrap } = require("nativescript-dev-webpack/transformers/ns-replace-bootstrap");
const { nsReplaceLazyLoader } = require("nativescript-dev-webpack/transformers/ns-replace-lazy-loader");
const { nsSupportHmrNg } = require("nativescript-dev-webpack/transformers/ns-support-hmr-ng");
const { getMainModulePath } = require("nativescript-dev-webpack/utils/ast-utils");
const CleanWebpackPlugin = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
const { NativeScriptWorkerPlugin } = require("nativescript-worker-loader/NativeScriptWorkerPlugin");
const TerserPlugin = require("terser-webpack-plugin");
const { getAngularCompilerPlugin } = require("nativescript-dev-webpack/plugins/NativeScriptAngularCompilerPlugin");
const hashSalt = Date.now().toString();

module.exports = env => {
    // Add your custom Activities, Services and other Android app components here.
    const appComponents = [
        "tns-core-modules/ui/frame",
        "tns-core-modules/ui/frame/activity",
    ];

    const platform = env && (env.android && "android" || env.ios && "ios");
    if (!platform) {
        throw new Error("You need to provide a target platform!");
    }

    const AngularCompilerPlugin = getAngularCompilerPlugin(platform);
    const projectRoot = __dirname;

    // Default destination inside platforms/<platform>/...
    const dist = resolve(projectRoot, nsWebpack.getAppPath(platform, projectRoot));

    const {
        // The 'appPath' and 'appResourcesPath' values are fetched from
        // the nsconfig.json configuration file
        // when bundling with `tns run android|ios --bundle`.
        appPath = "app",
        appResourcesPath = "app/App_Resources",

        // You can provide the following flags when running 'tns run android|ios'
        aot, // --env.aot
        snapshot, // --env.snapshot,
        production, // --env.production
        uglify, // --env.uglify
        report, // --env.report
        sourceMap, // --env.sourceMap
        hiddenSourceMap, // --env.hiddenSourceMap
        hmr, // --env.hmr,
        unitTesting, // --env.unitTesting
        verbose, // --env.verbose
    } = env;

    const isAnySourceMapEnabled = !!sourceMap || !!hiddenSourceMap;
    const externals = nsWebpack.getConvertedExternals(env.externals);
    const appFullPath = resolve(projectRoot, appPath);
    const appResourcesFullPath = resolve(projectRoot, appResourcesPath);
    const tsConfigName = "tsconfig.tns.json";
    const entryModule = `${nsWebpack.getEntryModule(appFullPath, platform)}.ts`;
    const entryPath = `.${sep}${entryModule}`;
    const entries = { bundle: entryPath };
    const areCoreModulesExternal = Array.isArray(env.externals) && env.externals.some(e => e.indexOf("tns-core-modules") > -1);
    if (platform === "ios" && !areCoreModulesExternal) {
        entries["tns_modules/tns-core-modules/inspector_modules"] = "inspector_modules";
    };

    const ngCompilerTransformers = [];
    const additionalLazyModuleResources = [];
    if (aot) {
        ngCompilerTransformers.push(nsReplaceBootstrap);
    }

    if (hmr) {
        ngCompilerTransformers.push(nsSupportHmrNg);
    }

    // when "@angular/core" is external, it's not included in the bundles. In this way, it will be used
    // directly from node_modules and the Angular modules loader won't be able to resolve the lazy routes
    // fixes https://github.com/NativeScript/nativescript-cli/issues/4024
    if (env.externals && env.externals.indexOf("@angular/core") > -1) {
        const appModuleRelativePath = getMainModulePath(resolve(appFullPath, entryModule), tsConfigName);
        if (appModuleRelativePath) {
            const appModuleFolderPath = dirname(resolve(appFullPath, appModuleRelativePath));
            // include the lazy loader inside app module
            ngCompilerTransformers.push(nsReplaceLazyLoader);
            // include the new lazy loader path in the allowed ones
            additionalLazyModuleResources.push(appModuleFolderPath);
        }
    }

    const ngCompilerPlugin = new AngularCompilerPlugin({
        hostReplacementPaths: nsWebpack.getResolver([platform, "tns"]),
        platformTransformers: ngCompilerTransformers.map(t => t(() => ngCompilerPlugin, resolve(appFullPath, entryModule), projectRoot)),
        mainPath: join(appFullPath, entryModule),
        tsConfigPath: join(__dirname, tsConfigName),
        skipCodeGeneration: !aot,
        sourceMap: !!isAnySourceMapEnabled,
        additionalLazyModuleResources: additionalLazyModuleResources
    });

    let sourceMapFilename = nsWebpack.getSourceMapFilename(hiddenSourceMap, __dirname, dist);

    const itemsToClean = [`${dist}/**/*`];
    if (platform === "android") {
        itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "src", "main", "assets", "snapshots")}`);
        itemsToClean.push(`${join(projectRoot, "platforms", "android", "app", "build", "configurations", "nativescript-android-snapshot")}`);
    }

    nsWebpack.processAppComponents(appComponents, platform);
    const config = {
        mode: production ? "production" : "development",
        context: appFullPath,
        externals,
        watchOptions: {
            ignored: [
                appResourcesFullPath,
                // Don't watch hidden files
                "**/.*",
            ]
        },
        target: nativescriptTarget,
        entry: entries,
        output: {
            pathinfo: false,
            path: dist,
            sourceMapFilename,
            libraryTarget: "commonjs2",
            filename: "[name].js",
            globalObject: "global",
            hashSalt
        },
        resolve: {
            extensions: [".ts", ".js", ".scss", ".css"],
            // Resolve {N} system modules from tns-core-modules
            modules: [
                resolve(__dirname, "node_modules/tns-core-modules"),
                resolve(__dirname, "node_modules"),
                "node_modules/tns-core-modules",
                "node_modules",
            ],
            alias: {
                '~': appFullPath
            },
            symlinks: true
        },
        resolveLoader: {
            symlinks: false
        },
        node: {
            // Disable node shims that conflict with NativeScript
            "http": false,
            "timers": false,
            "setImmediate": false,
            "fs": "empty",
            "__dirname": false,
        },
        devtool: hiddenSourceMap ? "hidden-source-map" : (sourceMap ? "inline-source-map" : "none"),
        optimization: {
            runtimeChunk: "single",
            splitChunks: {
                cacheGroups: {
                    vendor: {
                        name: "vendor",
                        chunks: "all",
                        test: (module, chunks) => {
                            const moduleName = module.nameForCondition ? module.nameForCondition() : '';
                            return /[\\/]node_modules[\\/]/.test(moduleName) ||
                                appComponents.some(comp => comp === moduleName);
                        },
                        enforce: true,
                    },
                }
            },
            minimize: !!uglify,
            minimizer: [
                new TerserPlugin({
                    parallel: true,
                    cache: true,
                    sourceMap: isAnySourceMapEnabled,
                    terserOptions: {
                        output: {
                            comments: false,
                            semicolons: !isAnySourceMapEnabled
                        },
                        compress: {
                            // The Android SBG has problems parsing the output
                            // when these options are enabled
                            'collapse_vars': platform !== "android",
                            sequences: platform !== "android",
                        }
                    }
                })
            ],
        },
        module: {
            rules: [
                {
                    include: join(appFullPath, entryPath),
                    use: [
                        // Require all Android app components
                        platform === "android" && {
                            loader: "nativescript-dev-webpack/android-app-components-loader",
                            options: { modules: appComponents }
                        },

                        {
                            loader: "nativescript-dev-webpack/bundle-config-loader",
                            options: {
                                angular: true,
                                loadCss: !snapshot, // load the application css if in debug mode
                                unitTesting,
                                appFullPath,
                                projectRoot,
                                ignoredFiles: nsWebpack.getUserDefinedEntries(entries, platform)
                            }
                        },
                    ].filter(loader => !!loader)
                },

                { test: /\.html$|\.xml$/, use: "raw-loader" },

                // tns-core-modules reads the app.css and its imports using css-loader
                {
                    test: /[\/|\\]app\.css$/,
                    use: [
                        "nativescript-dev-webpack/style-hot-loader",
                        { loader: "css-loader", options: { url: false } }
                    ]
                },
                {
                    test: /[\/|\\]app\.scss$/,
                    use: [
                        "nativescript-dev-webpack/style-hot-loader",
                        { loader: "css-loader", options: { url: false } },
                        "sass-loader"
                    ]
                },

                // Angular components reference css files and their imports using raw-loader
                { test: /\.css$/, exclude: /[\/|\\]app\.css$/, use: "raw-loader" },
                { test: /\.scss$/, exclude: /[\/|\\]app\.scss$/, use: ["raw-loader", "resolve-url-loader", "sass-loader"] },

                {
                    test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/,
                    use: [
                        "nativescript-dev-webpack/moduleid-compat-loader",
                        "nativescript-dev-webpack/lazy-ngmodule-hot-loader",
                        "@ngtools/webpack",
                    ]
                },

                // Mark files inside `@angular/core` as using SystemJS style dynamic imports.
                // Removing this will cause deprecation warnings to appear.
                {
                    test: /[\/\\]@angular[\/\\]core[\/\\].+\.js$/,
                    parser: { system: true },
                },
            ],
        },
        plugins: [
            // Define useful constants like TNS_WEBPACK
            new webpack.DefinePlugin({
                "global.TNS_WEBPACK": "true",
                "process": "global.process",
            }),
            // Remove all files from the out dir.
            new CleanWebpackPlugin(itemsToClean, { verbose: !!verbose }),
            // Copy assets to out dir. Add your own globs as needed.
            new CopyWebpackPlugin([
                { from: { glob: `./app.${platform}.css` } },
                { from: { glob: "fonts/**" } },
                { from: { glob: "**/*.jpg" } },
                { from: { glob: "**/*.png" } },
                { from: { glob: 'assets/stylesheets/**' } },
                { from: { glob: 'assets/i18n/**' } }
            ], { ignore: [`${relative(appPath, appResourcesFullPath)}/**`] }),
            new nsWebpack.GenerateNativeScriptEntryPointsPlugin("bundle"),
            // For instructions on how to set up workers with webpack
            // check out https://github.com/nativescript/worker-loader
            new NativeScriptWorkerPlugin(),
            ngCompilerPlugin,
            // Does IPC communication with the {N} CLI to notify events when running in watch mode.
            new nsWebpack.WatchStateLoggerPlugin(),
        ],
    };

    if (report) {
        // Generate report files for bundles content
        config.plugins.push(new BundleAnalyzerPlugin({
            analyzerMode: "static",
            openAnalyzer: false,
            generateStatsFile: true,
            reportFilename: resolve(projectRoot, "report", `report.html`),
            statsFilename: resolve(projectRoot, "report", `stats.json`),
        }));
    }

    if (snapshot) {
        config.plugins.push(new nsWebpack.NativeScriptSnapshotPlugin({
            chunk: "vendor",
            angular: true,
            requireModules: [
                "reflect-metadata",
                "@angular/platform-browser",
                "@angular/core",
                "@angular/common",
                "@angular/router",
                "nativescript-angular/platform-static",
                "nativescript-angular/router",
            ],
            projectRoot,
            webpackConfig: config,
        }));
    }

    if (hmr) {
        config.plugins.push(new webpack.HotModuleReplacementPlugin());
    }

    return config;
};

Describe the bug

ERROR in NativeScriptSnapshot. Snapshot generation failed!
Target architecture: x86

#
# Fatal error in , line 0
# Check failed: blob.data.
#
#
#
#FailureMessage Object: 0xbff2ee20
==== C stack trace ===============================

    0   mksnapshot-ia32                     0x01133ff3 mksnapshot-ia32 + 17203187
    1   mksnapshot-ia32                     0x01138498 mksnapshot-ia32 + 17220760
    2   mksnapshot-ia32                     0x0112e961 mksnapshot-ia32 + 17181025
    3   mksnapshot-ia32                     0x000ce889 mksnapshot-ia32 + 10377
    4   libdyld.dylib                       0xa7abb051 start + 1
    5   ???                                 0x00000008 0x0 + 8

JavaScript execution error: TypeError: Cannot read property 'support' of undefined

With the latest updates, when trying to build a --snapshot version of my app, it fails because android or androidx globals context are lost.

The fails links to this line from nativescript-permission https://github.com/NathanaelA/nativescript-permissions/blob/31c1cfe1decc630bf17074e72ec55cbcd882ccfc/src/permissions.android.js#L165

if (!global.android.support || !global.android.support.v4 || !global.android.support.v4.content || !global.android.support.v4.content.ContextCompat || !global.android.support.v4.content.ContextCompat.checkSelfPermission) {
        return false;
    }

The issue is not coming from this plugin though, added a guard clause to check that global.android is defined, it builds correctly with that scenario. Unfortunately the issue remains the same during the app execution: nativescript-permission does not access a valid android or androidx accessor, both are null.

Used the very same package.json in another project, builds correctly so dependences are not the issue in that case

jeremypele commented 5 years ago

Without the --env.uglify flag, better output:

ERROR in NativeScriptSnapshot. Snapshot generation failed!
Target architecture: x86

#
# Fatal error in , line 0
# Check failed: blob.data.
#
#
#
#FailureMessage Object: 0xbff28e20
==== C stack trace ===============================

    0   mksnapshot-ia32                     0x01139ff3 mksnapshot-ia32 + 17203187
    1   mksnapshot-ia32                     0x0113e498 mksnapshot-ia32 + 17220760
    2   mksnapshot-ia32                     0x01134961 mksnapshot-ia32 + 17181025
    3   mksnapshot-ia32                     0x000d4889 mksnapshot-ia32 + 10377
    4   libdyld.dylib                       0xa7abb051 start + 1
    5   ???                                 0x00000008 0x0 + 8

JavaScript execution error: ReferenceError: android is not defined
    at hasSupportVersion4 (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:206373:2)
    at setupSupport (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:206355:13)
    at Object.<anonymous> (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:206360:1)
    at Object.<anonymous> (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:206605:30)
    at __webpack_require__ (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:110:30)
    at Object.<anonymous> (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:145950:19)
    at __webpack_require__ (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:110:30)
    at Object.902 (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:271:182)
    at __webpack_require__ (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:110:30)
    at checkDeferredModules (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:79:23)
    at Array.webpackJsonpCallback [as push] (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:66:19)
    at Object.<anonymous> (/Users/jpele/workspace/mobile-app/booking/platforms/android/app/src/main/assets/app/_embedded_script_.js:289:58)
    at Module._compile (/Users/jpele/workspace/mobile-app/booking/node_modules/v8-compile-cache/v8-compile-cache.js:194:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Module.require (internal/modules/cjs/loader.js:658:17)
    at require (/Users/jpele/workspace/mobile-app/booking/node_modules/v8-compile-cache/v8-compile-cache.js:161:20)
    at child_process.exec (/Users/jpele/workspace/mobile-app/booking/node_modules/nativescript-dev-webpack/snapshot/android/snapshot-generator.js:140:29)
    at ChildProcess.exithandler (child_process.js:301:5)
    at ChildProcess.emit (events.js:182:13)
    at maybeClose (internal/child_process.js:978:16)
    at Socket.stream.socket.on (internal/child_process.js:395:11)
    at Socket.emit (events.js:182:13)
    at Pipe._handle.close (net.js:616:12)$
Executing webpack failed with exit code 2.

Pointing to this portion of the plugin

/**
 * Checks to see if v4 is installed and has the proper calls with it
 * @returns {boolean}
 */
function hasSupportVersion4() {
  //noinspection JSUnresolvedVariable
  if (!android.support || !android.support.v4 || !android.support.v4.content || !android.support.v4.content.ContextCompat || !android.support.v4.content.ContextCompat.checkSelfPermission) {
    return false;
  }
  return true;
}

function setupSupport() {
    if (hasAndroidX()) {
        androidSupport = global.androidx.core;
    } else if (hasSupportVersion4()) {
        androidSupport = android.support.v4;
    }
}

setupSupport();
jeremypele commented 5 years ago

Figured out the issue after a couple of days..

I've replicated with nativescript-geolocation repo => https://github.com/jeremypele/nativescript-geolocation

snapshot generation fails because of the src/package.json and the presence of

"android": {
        "v8Flags": "--expose_gc",
        "requireModules": ["nativescript-geolocation"]
    }

Closing and letting this as a trace for anyone facing the same issue.