yahoo / serialize-javascript

Serialize JavaScript to a superset of JSON that includes regular expressions and functions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected regular expression

alexander-akait opened this issue · comments

Input:

const options = {
      file: 'test5.js',
      input: '/******/ function hello(a) {console.log(a)}',
      terserOptions: {
        output: {
          comments: 'all',
        },
      },
      extractComments: {
        condition: 'should be extracted',
        filename: (file) => file.replace(/(\.\w+)$/, '.license$1'),
        banner: (licenseFile) =>
          `License information can be found in ${licenseFile}`,
      },
    }

After serialize we use

const options = new Function(
      'exports',
      'require',
      'module',
      '__filename',
      '__dirname',
      `'use strict'\nreturn ${options}`
    )(exports, require, module, __filename, __dirname);

And code in Function throw SyntaxError: Unexpected regular expression

@krutzler no, i have problems with package and yahoo as always terribly maintenance own repos, instead of a live response, I get a robot 😞

/cc @okuryu friendly ping, we still have problem with example above

test.js:

const serialize = require('serialize-javascript')

const options = {
      file: 'test5.js',
      input: '/******/ function hello(a) {console.log(a)}',
      terserOptions: {
        output: {
          comments: 'all',
        },
      },
      extractComments: {
        condition: 'should be extracted',
        filename: (file) => file.replace(/(\.\w+)$/, '.license$1'),
        banner: (licenseFile) =>
          `License information can be found in ${licenseFile}`,
      },
    }

console.log(serialize(options))
$ node test.js
{"file":"test5.js","input":"\u002F******\u002F function hello(a) {console.log(a)}","terserOptions":{"output":{"comments":"all"}},"extractComments":{"condition":"should be extracted","filename":(file) => file.replace(/(\.\w+)$/, '.license$1'),"banner":(licenseFile) =>
          `License information can be found in ${licenseFile}`}}

Did you mean that / should serialize to / not \u002F?

@okuryu yep, because deserialize not working in this case

Hmm. it seems to be a bug. I'll try to fix it, but it may take some time. Your cooperation is always welcome.