Php-parser: `Undefined node "type"` error will occur when I use webpack in production mode

Created on 21 Aug 2018  路  7Comments  路  Source: glayzzle/php-parser

When I pack code in development mode, it will work properly. But if I use production mode, error like bellow will occur everytime.

Input is: ['a' => 1]:

Uncaught Error: Undefined node "string"
    at main.js:210
    at n.read_scalar (main.js:174)
    at n.read_expr_item (main.js:144)
    at n.read_expr (main.js:144)
    at n.read_array_pair_list (main.js:129)
    at n.read_array (main.js:129)
    at n.read_scalar (main.js:174)
    at n.read_expr_item (main.js:144)
    at n.read_expr (main.js:144)
    at n.read_statement (main.js:179)

I've tried both 2.x & 3.x-alpha, same result got.

bug AST

Most helpful comment

I used webpack with minimal config like bellow for a test, and got that error:

const path = require('path');

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: {
                  loader: 'babel-loader',
                  query: {
                    presets: ['env', 'stage-0']
                  }
                }
            }
        ]
    },
    devServer: {
        contentBase: path.join(__dirname, "dist"),
        port: 9090,
        host: 'localhost',
        overlay: true,
        compress: true
    }
}

And when I used it in my project with a complicated config file, I got that error too.

However, it worked properly when I tested with your webpack config.

All 7 comments

Hi @takashiki,

With what tool do you pack the code (and configuration options). I'm actually using a packed version released here https://github.com/glayzzle/php-parser/blob/master/dist/php-parser.min.js and it works.

Here what I do for the compression : https://github.com/glayzzle/php-parser/blob/master/webpack.config.js

The error message you have means that this folder : https://github.com/glayzzle/php-parser/tree/master/src/ast is not correctly included into the source but as the ast.js requires explicitly every file there is no reason to not be bundled correctly.

I used webpack with minimal config like bellow for a test, and got that error:

const path = require('path');

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.join(__dirname, 'dist'),
        filename: '[name].js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                use: {
                  loader: 'babel-loader',
                  query: {
                    presets: ['env', 'stage-0']
                  }
                }
            }
        ]
    },
    devServer: {
        contentBase: path.join(__dirname, "dist"),
        port: 9090,
        host: 'localhost',
        overlay: true,
        compress: true
    }
}

And when I used it in my project with a complicated config file, I got that error too.

However, it worked properly when I tested with your webpack config.

I tested some times and find out when I add UglifyJsPlugin part, that error will disappear.

Thanks @takashiki, I will try to figure out what does not work in production mode with babel & their defaut JS compressor, meanwhile use the UglifyjsPlugin as it's our default building process.

Without config bellow in UglifyOptions, this error will occur too.

compress: {
    keep_fnames: true
}

keep_fnames (default: false) - pass true to prevent discarding or mangling of function names. Useful for code relying on Function.prototype.name.

Ok, got it, extracted from ast.js :

  require("./ast/yieldfrom")
].forEach(function(ctor) {
  let kind = ctor.prototype.constructor.name.toLowerCase();
  if (kind[0] === "_") kind = kind.substring(1);
  AST.prototype[kind] = ctor;
});

https://github.com/glayzzle/php-parser/blob/master/src/ast.js#L387

It seems that Uglify mangles the function names, so the right nodes names are not registered correctly. I can change the way functions are registered in order to fix this

Hi @takashiki, the problem should be resolved by using version 3.0.0-prerelease.2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexander-akait picture alexander-akait  路  8Comments

alexander-akait picture alexander-akait  路  5Comments

ichiriac picture ichiriac  路  7Comments

chris-l picture chris-l  路  7Comments

nevadascout picture nevadascout  路  3Comments