Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

*.json files with no validKeys blow up TypeError: Cannot read property '0' of null in ExportDefaultDeclaration.prototype.render #1146

Closed
btakita opened this issue Dec 14, 2016 · 5 comments

Comments

@btakita
Copy link
Contributor

btakita commented Dec 14, 2016

I'm packaging an import from mime-db, which loads a json file.

https://github.com/jshttp/mime-db/blob/master/db.json

Stack trace

Cannot read property '0' of null
TypeError: Cannot read property '0' of null
at ExportDefaultDeclaration.render (/home/user/project/node_modules/rollup/src/ast/nodes/ExportDefaultDeclaration.js:57:4)
at Module.render (/home/user/project/node_modules/rollup/src/Module.js:320:8)
at /home/user/project/node_modules/rollup/src/Bundle.js:376:17
at Array.forEach (native)
at Bundle.render (/home/user/project/node_modules/rollup/src/Bundle.js:375:16)
at generate (/home/user/project/node_modules/rollup/src/rollup.js:75:30)
at Object.result.write (/home/user/project/node_modules/rollup/src/rollup.js:103:9)
at /home/user/project/node_modules/rollup-watch/dist/rollup-watch.cjs.js:162:21
Type rollup --help for help, or visit https://github.com/rollup/rollup/wiki

See rollup/rollup-plugin-json#19

@btakita btakita changed the title *.json files blow up TypeError: Cannot read property '0' of null in ExportDefaultDeclaration.prototype.render *.json files with no validKeys blow up TypeError: Cannot read property '0' of null in ExportDefaultDeclaration.prototype.render Dec 14, 2016
@kzc
Copy link
Contributor

kzc commented Dec 14, 2016

When I changed ExportDefaultDeclaration I assumed that acorn would produce the AST. But it appears that the AST is generated by rollup-plugin-json in this case and something is amiss.

Can you confirm that this rollup patch fixes the problem for you?

--- a/src/ast/nodes/ExportDefaultDeclaration.js
+++ b/src/ast/nodes/ExportDefaultDeclaration.js
@@ -53,7 +53,7 @@ export default class ExportDefaultDeclaration extends Node {
                let declaration_start;
                if ( this.declaration ) {
                        const statementStr = code.original.slice( this.start, this.end );
-                       declaration_start = this.start + statementStr.match(/^export\s+default\s*/)[0].length;
+                       declaration_start = this.start + statementStr.match(/^\s*export\s+default\s+/)[0].length;
                }
 
                if ( this.shouldInclude || this.declaration.activated ) {

If it doesn't work could you please post the value of JSON.stringify(statementStr)?

@kzc
Copy link
Contributor

kzc commented Dec 15, 2016

Likely fix: #1152

@Rich-Harris
Copy link
Contributor

Fixed on both ends now, so I'll close this

@yairEO
Copy link

yairEO commented Jan 5, 2017

@Rich-Harris

Regular user here reporting. been using Rollup for over a year with Gulp.
I'm getting this error (in the title) even after updating to v0.40.0 (with Node v5.8.0):

[14:44:16] Rollup [catch]:  TypeError: Cannot read property '0' of null
    at ExportDefaultDeclaration.render (C:\www\UI\BUILD\node_modules\rollup\dist\rollup.js:6225:82)
    at Module.render (C:\www\UI\BUILD\node_modules\rollup\dist\rollup.js:7767:8)
    at C:\www\UI\BUILD\node_modules\rollup\dist\rollup.js:9080:23
    at Array.forEach (native)
    at Bundle.render (C:\www\UI\BUILD\node_modules\rollup\dist\rollup.js:9079:22)
    at Object.generate (C:\www\UI\BUILD\node_modules\rollup\dist\rollup.js:9316:26)
    at C:\www\UI\BUILD\gulpfile.js:657:33
    at lib$es6$promise$$internal$$tryCatch (C:\www\UI\BUILD\node_modules\es6-promise\dist\es6-promise.js:362:16)
    at lib$es6$promise$$internal$$invokeCallback (C:\www\UI\BUILD\node_modules\es6-promise\dist\es6-promise.js:374:17)
    at lib$es6$promise$$internal$$publish (C:\www\UI\BUILD\node_modules\es6-promise\dist\es6-promise.js:345:11)
    at lib$es6$promise$asap$$flush (C:\www\UI\BUILD\node_modules\es6-promise\dist\es6-promise.js:119:9)
    at _combinedTickCallback (node.js:370:9)
    at process._tickCallback (node.js:401:11)

And this is the function inside my gulpfile which I wrote to do the rollups:

/**
 * a generic Rollup bundle creator
 * @param  {String} outputPath     [where to save the bundle to (must end with /)]
 * @param  {String} outputFileName [bundle file name]
 * @param  {String} entryFile      [rollup entry file to start scanning from]
 * @return {Object}                [Promise]
 */
function rollupBundle(outputPath, outputFileName, entryFile, bundleOptions){
    bundleOptions = bundleOptions || {};

    return new Promise(function(resolve, reject) {
        outputFileName += '.js';
        var cache;

        rollup({
            entry   : entryFile,
            plugins : rollupPlugins,  // comes from another place within the gulpfile
            cache   : cache
        })
        .then(function (bundle) {
            var bundleSettings = {
                    format    : bundleOptions.format || 'umd',
                //  sourceMap : true,
                    banner    : config.banner
                },
                result = bundle.generate(bundleSettings),
                mapFileName = outputFileName + '.map',
                sourceMappingURL = '\n//# sourceMappingURL='+ mapFileName;

            cache = bundle;

            // if folder does not exists, create it
            if( !fs.existsSync(outputPath) ){
                gutil.log( gutil.colors.black.bgWhite('Creating directory ' + outputPath) );
                fs.mkdirSync(outputPath);
            }

            // save bundle file to disk
            fs.writeFile( outputPath + outputFileName, result.code + (bundleSettings.sourceMap ? sourceMappingURL : ''), function(){
                resolve();
            });

            // save map file to disk
            if( bundleSettings.sourceMap )
                fs.writeFile( outputPath + mapFileName, result.map.toString());
        })
        .catch(function(err){
            gutil.log( gutil.colors.white.bgRed('Rollup [catch]: ', err.stack) );
            resolve();
        })
    });
}

Error stack mentioned line gulpfile.js:657:33 which is:

result = bundle.generate(bundleSettings),

Why do I get this error now?

@kzc
Copy link
Contributor

kzc commented Jan 5, 2017

Fixed in 0.40.1

Allow missing space between export default and declaration (#1218)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants