NativeScript / nativescript-dev-webpack

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

Cannot read property 'length' of undefined #112

Closed jericdeleon closed 7 years ago

jericdeleon commented 7 years ago

Hi,

Just tried running nativescript-dev-webpack and got this error below; Seems like a typescript error, but I'm at 2.1.6 already; any ideas on where to look next?

Safely@0.2.0 build-android-bundle /Users/jeric/Downloads/project_safely/code/mobile-nativescript
npm run ns-bundle --android --build-app

Safely@0.2.0 ns-bundle /Users/jeric/Downloads/project_safely/code/mobile-nativescript
ns-bundle

Running webpack for android...
/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:14552
            var sourceFile = new SourceFileConstructor(261 /* SourceFile */, /*pos*/ 0, /* end */ sourceText.length);
                                                                                                            ^                                                                                                            

TypeError: Cannot read property 'length' of undefined
    at createSourceFile (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:14552:109)
    at parseSourceFileWorker (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:14484:26)
    at Object.parseSourceFile (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:14433:26)
    at Object.createSourceFile (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:14288:29)
    at WebpackCompilerHost.getSourceFile (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/@ngtools/webpack/src/compiler_host.js:210:27)
    at findSourceFile (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:63453:29)
    at processSourceFile (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:63384:27)
    at /Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:63494:17
    at Object.forEach (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:1293:30)
    at processReferencedFiles (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:63492:16)
    at findSourceFile (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:63477:21)
    at processSourceFile (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:63384:27)
    at processRootFile (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:63271:13)
    at /Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:62620:60
    at Object.forEach (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:1293:30)
    at Object.createProgram (/Users/jeric/Downloads/project_safely/code/mobile-nativescript/node_modules/typescript/lib/typescript.js:62620:16)
child process exited with code 1

package.json:

{
  "name": "Safely",
  "version": "0.2.0",
  "description": "SHC Locator App for: Save the Children",
  "readme": "README.md",
  "author": "Jeric de Leon <jericpaul.deleon@gmail.com> (http://jericdeleon.com)",
  "license": "MIT",
  "repository": "gitlab:savethechildren-ict/mobile-nativescript",
  "nativescript": {
    "id": "org.nativescript.safely",
    "tns-android": {
      "version": "2.5.0"
    }
  },
  "dependencies": {
    "@angular/common": "4.0.0",
    "@angular/compiler": "4.0.0",
    "@angular/core": "4.0.0",
    "@angular/forms": "4.0.0",
    "@angular/http": "4.0.0",
    "@angular/platform-browser": "4.0.0",
    "@angular/platform-browser-dynamic": "4.0.0",
    "@angular/router": "4.0.0",
    "email-validator": "^1.0.7",
    "nativescript-angular": "1.5.1",
    "nativescript-angular-snapshot": "1.5.1-5.5.372.32",
    "nativescript-directions": "^1.0.4",
    "nativescript-geolocation": "0.0.15",
    "nativescript-google-maps-sdk": "^1.3.14",
    "nativescript-ng2-fonticon": "^1.3.3",
    "nativescript-permissions": "^1.2.1",
    "nativescript-phone": "^1.2.3",
    "nativescript-photoviewer": "1.0.0",
    "nativescript-plugin-firebase": "^3.8.3",
    "nativescript-telerik-ui-pro": "nativescript-ui-pro.tgz",
    "reflect-metadata": "~0.1.8",
    "rxjs": "~5.2.0",
    "tns-core-modules": "2.5.2",
    "zone.js": "~0.8.2"
  },
  "devDependencies": {
    "@angular/compiler-cli": "~4.0.0",
    "@ngtools/webpack": "1.2.13",
    "babel-traverse": "6.19.0",
    "babel-types": "6.19.0",
    "babylon": "6.14.0",
    "copy-webpack-plugin": "~3.0.1",
    "extract-text-webpack-plugin": "~2.0.0-beta.4",
    "lazy": "1.0.11",
    "nativescript-css-loader": "~0.26.0",
    "nativescript-dev-android-snapshot": "^0.*.*",
    "nativescript-dev-typescript": "~0.3.5",
    "nativescript-dev-webpack": "^0.3.7",
    "raw-loader": "~0.5.1",
    "resolve-url-loader": "~1.6.0",
    "typescript": "~2.1.0",
    "webpack": "2.2.0",
    "webpack-sources": "~0.1.3"
  },
  "scripts": {
    "ns-bundle": "ns-bundle",
    "start-android-bundle": "npm run ns-bundle --android --start-app",
    "start-ios-bundle": "npm run ns-bundle --ios --start-app",
    "build-android-bundle": "npm run ns-bundle --android --build-app",
    "build-ios-bundle": "npm run ns-bundle --ios --build-app"
  }
}

webpack.common.js:

var webpack = require("webpack");
var nsWebpack = require("nativescript-dev-webpack");
var nativescriptTarget = require("nativescript-dev-webpack/nativescript-target");
var path = require("path");
var CopyWebpackPlugin = require("copy-webpack-plugin");
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var AotPlugin = require("@ngtools/webpack").AotPlugin;

module.exports = function (platform, destinationApp) {
    if (!destinationApp) {
        //Default destination inside platforms/<platform>/...
        destinationApp = nsWebpack.getAppPath(platform);
    }
    var entry = {};
    //Discover entry module from package.json
    entry.bundle = "./" + nsWebpack.getEntryModule();
    //Vendor entry with third party libraries.
    entry.vendor = "./vendor";
    //app.css bundle
    entry["app.css"] = "./app.css";

    var plugins = [
        new ExtractTextPlugin("app.css"),
        //Vendor libs go to the vendor.js chunk
        new webpack.optimize.CommonsChunkPlugin({
            name: ["vendor"]
        }),
        //Define useful constants like TNS_WEBPACK
        new webpack.DefinePlugin({
            "global.TNS_WEBPACK": "true",
        }),
        //Copy assets to out dir. Add your own globs as needed.
        new CopyWebpackPlugin([
            { from: "app.css" },
            { from: "css/**" },
            { from: "fonts/**" },
            { from: "**/*.jpg" },
            { from: "**/*.png" },
            { from: "**/*.xml" },
        ], { ignore: ["App_Resources/**"] }),
        //Generate a bundle starter script and activate it in package.json
        new nsWebpack.GenerateBundleStarterPlugin([
            "./vendor",
            "./bundle",
        ]),

        //Angular AOT compiler
        new AotPlugin({
            tsConfigPath: "tsconfig.aot.json",
            entryModule: path.resolve(__dirname, "app/app.module#AppModule"),
            typeChecking: false
        }),
        new nsWebpack.StyleUrlResolvePlugin({platform}),
    ];

    if (process.env.npm_config_uglify) {
        plugins.push(new webpack.LoaderOptionsPlugin({
            minimize: true
        }));

        //Work around an Android issue by setting compress = false
        var compress = platform !== "android";
        plugins.push(new webpack.optimize.UglifyJsPlugin({
            mangle: {
                except: nsWebpack.uglifyMangleExcludes,
            },
            compress: compress,
        }));
    }

    return {
        context: path.resolve("./app"),
        target: nativescriptTarget,
        entry: entry,
        output: {
            pathinfo: true,
            path: path.resolve(destinationApp),
            libraryTarget: "commonjs2",
            filename: "[name].js",
        },
        resolve: {
            //Resolve platform-specific modules like module.android.js
            extensions: [
                ".aot.ts",
                ".ts",
                ".js",
                ".css",
                "." + platform + ".ts",
                "." + platform + ".js",
                "." + platform + ".css",
            ],
            //Resolve {N} system modules from tns-core-modules
            modules: [
                "node_modules/tns-core-modules",
                "node_modules"
            ]
        },
        node: {
            //Disable node shims that conflict with NativeScript
            "http": false,
            "timers": false,
            "setImmediate": false,
            "fs": "empty",
        },
        module: {
            loaders: [
                {
                    test: /\.html$|\.xml$/,
                    loaders: [
                        "raw-loader",
                    ]
                },
                // Root app.css file gets extracted with bundled dependencies
                {
                    test: /app\.css$/,
                    loader: ExtractTextPlugin.extract([
                        "resolve-url-loader",
                        "nativescript-css-loader",
                        "nativescript-dev-webpack/platform-css-loader",
                    ]),
                },
                // Other CSS files get bundled using the raw loader
                {
                    test: /\.css$/,
                    exclude: /app\.css$/,
                    loaders: [
                        "raw-loader",
                    ]
                },
                // Compile TypeScript files with ahead-of-time compiler.
                {
                    test: /\.ts$/,
                    loaders: [
                        "nativescript-dev-webpack/tns-aot-loader",
                        "@ngtools/webpack",
                    ]
                },
                // SASS support
                {
                    test: /\.scss$/,
                    loaders: [
                        "raw-loader",
                        "resolve-url-loader",
                        "sass-loader"
                    ]
                },
            ]
        },
        plugins: plugins,
    };
};
sis0k0 commented 7 years ago

The problem probably is the version of @ngtools/webpack. Try to downgrade to 1.2.10. If that doesn't help here are a few other suggestions:

Please let me know if any of these helped!