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

Unexpected token when wmr build with importing i18next #769

Open
1 of 3 tasks
riywo opened this issue Jul 16, 2021 · 9 comments · Fixed by xtuc/acorn-import-attributes#8 or #770
Open
1 of 3 tasks

Unexpected token when wmr build with importing i18next #769

riywo opened this issue Jul 16, 2021 · 9 comments · Fixed by xtuc/acorn-import-attributes#8 or #770
Labels
bug Something isn't working

Comments

@riywo
Copy link

riywo commented Jul 16, 2021

Describe the bug
When I import i18next, wmr start (development) has no issue and works expectedly. However, wmr build fails with a weird error below. There is no clue from the debug output.

To Reproduce
Steps to reproduce the behavior:

  1. npm init wmr app
  2. cd app && npm install i18next
  3. Add import i18n from "i18next" to public/index.js
  4. npm run build
> wmr build --prerender --debug

  wmr:config  {
  wmr:config    _: [],
  wmr:config    prerender: true,
  wmr:config    debug: true,
  wmr:config    cwd: '/Users/riywo/work/wmr-i18next',
  wmr:config    public: 'public',
  wmr:config    out: '/Users/riywo/work/wmr-i18next/dist',
  wmr:config    sourcemap: undefined,
  wmr:config    visualize: undefined,
  wmr:config    minify: true,
  wmr:config    root: '/Users/riywo/work/wmr-i18next/public',
  wmr:config    plugins: [],
  wmr:config    output: [],
  wmr:config    middleware: [],
  wmr:config    features: { preact: true },
  wmr:config    alias: { react: 'preact/compat', 'react-dom': 'preact/compat' },
  wmr:config    customRoutes: [],
  wmr:config    prod: true,
  wmr:config    mode: 'build',
  wmr:config    env: {},
  wmr:config    overlayDir: '/Users/riywo/work/wmr-i18next/dist',
  wmr:config    publicPath: '/',
  wmr:config    host: 'localhost'
  wmr:config  }
  npm:plugin  i18next -> npm/[email protected]/dist/esm/i18next.js
  npm:plugin  preact-iso -> npm/[email protected]/index.js
  npm:plugin  preact-iso -> npm/[email protected]/index.js
  npm:plugin  preact/hooks -> npm/[email protected]/hooks/dist/hooks.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  ./router.js -> npm/[email protected]/router.js
  npm:plugin  ./lazy.js -> npm/[email protected]/lazy.js
  npm:plugin  ./hydrate.js -> npm/[email protected]/hydrate.js
  npm:plugin  ./prerender.js -> npm/[email protected]/prerender.js
  npm:plugin  preact/hooks -> npm/[email protected]/hooks/dist/hooks.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact/hooks -> npm/[email protected]/hooks/dist/hooks.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  @babel/runtime/helpers/esm/typeof -> npm/@babel/[email protected]/helpers/esm/typeof.js
  npm:plugin  @babel/runtime/helpers/esm/classCallCheck -> npm/@babel/[email protected]/helpers/esm/classCallCheck.js
  npm:plugin  @babel/runtime/helpers/esm/objectSpread -> npm/@babel/[email protected]/helpers/esm/objectSpread.js
  npm:plugin  @babel/runtime/helpers/esm/assertThisInitialized -> npm/@babel/[email protected]/helpers/esm/assertThisInitialized.js
  npm:plugin  @babel/runtime/helpers/esm/possibleConstructorReturn -> npm/@babel/[email protected]/helpers/esm/possibleConstructorReturn.js
  npm:plugin  @babel/runtime/helpers/esm/createClass -> npm/@babel/[email protected]/helpers/esm/createClass.js
  npm:plugin  @babel/runtime/helpers/esm/getPrototypeOf -> npm/@babel/[email protected]/helpers/esm/getPrototypeOf.js
  npm:plugin  @babel/runtime/helpers/esm/inherits -> npm/@babel/[email protected]/helpers/esm/inherits.js

Error: Unexpected token (npm/@babel/[email protected]/helpers/esm/possibleConstructorReturn.js:2:8)

    at rb (/Users/riywo/work/wmr-i18next/node_modules/wmr/wmr.cjs:2:960318)
    at Object.transform (/Users/riywo/work/wmr-i18next/node_modules/wmr/wmr.cjs:2:2532029)
    at /Users/riywo/work/wmr-i18next/node_modules/wmr/wmr.cjs:2:337452

Expected behavior
No build failure since wmr start has no issue and application is running.

Bug occurs with:

  • wmr or wmr start (development)
  • wmr build (production)
  • wmr serve

Desktop (please complete the following information):

  • OS: [macOS 11.4]
  • Browser: [Chrome]
  • Node Version: [16.4.1]
  • WMR Version: [3.4.0]

Additional context
N/A

@riywo riywo added the bug Something isn't working label Jul 16, 2021
@riywo
Copy link
Author

riywo commented Jul 16, 2021

Without import i18n from "i18next", wmr build won't load any babel plugins:

> wmr build --prerender --debug

  wmr:config  {
  wmr:config    _: [],
  wmr:config    prerender: true,
  wmr:config    debug: true,
  wmr:config    cwd: '/Users/riywo/work/wmr-i18next',
  wmr:config    public: 'public',
  wmr:config    out: '/Users/riywo/work/wmr-i18next/dist',
  wmr:config    sourcemap: undefined,
  wmr:config    visualize: undefined,
  wmr:config    minify: true,
  wmr:config    root: '/Users/riywo/work/wmr-i18next/public',
  wmr:config    plugins: [],
  wmr:config    output: [],
  wmr:config    middleware: [],
  wmr:config    features: { preact: true },
  wmr:config    alias: { react: 'preact/compat', 'react-dom': 'preact/compat' },
  wmr:config    customRoutes: [],
  wmr:config    prod: true,
  wmr:config    mode: 'build',
  wmr:config    env: {},
  wmr:config    overlayDir: '/Users/riywo/work/wmr-i18next/dist',
  wmr:config    publicPath: '/',
  wmr:config    host: 'localhost'
  wmr:config  }
  npm:plugin  preact-iso -> npm/[email protected]/index.js
  npm:plugin  preact-iso -> npm/[email protected]/index.js
  npm:plugin  preact/hooks -> npm/[email protected]/hooks/dist/hooks.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  ./router.js -> npm/[email protected]/router.js
  npm:plugin  ./lazy.js -> npm/[email protected]/lazy.js
  npm:plugin  ./hydrate.js -> npm/[email protected]/hydrate.js
  npm:plugin  ./prerender.js -> npm/[email protected]/prerender.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact/hooks -> npm/[email protected]/hooks/dist/hooks.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact/hooks -> npm/[email protected]/hooks/dist/hooks.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact-render-to-string -> npm/[email protected]/dist/index.mjs
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  htm -> npm/[email protected]/dist/htm.module.js
minify(index.e7dc04ec.js) took 793ms
minify(chunks/prerender.5e4dbeff.js) took 170ms
Merging assets/style.module.83820528.css into assets/style.module.7b16dcc3.css (chunks/index.85d8c93a.js → index.e7dc04ec.js)
Hoisting CSS "assets/style.module.7b16dcc3.css" imported by index.e7dc04ec.js into parent HTML import "assets/style.e6ee4bcf.css".

Wrote 23Kb to disk:
  dist/index.html 460b
  dist/index.e7dc04ec.js 17Kb
  dist/assets/style.e6ee4bcf.css 499b
  dist/chunks/index.85d8c93a.js 214b
  dist/chunks/prerender.5e4dbeff.js 4.8Kb
Prerendered 3 pages:
  /
  /about [from /]
  /error [from /]

@riywo
Copy link
Author

riywo commented Jul 16, 2021

Captured the actual error by development env:

Error: Unexpected token (npm/@babel/[email protected]/helpers/esm/possibleConstructorReturn.js:2:8)

    at transform (file:///Users/riywo/GitHub/wmr/packages/wmr/src/lib/acorn-traverse.js:1264:23)
    at Object.transform (file:///Users/riywo/GitHub/wmr/packages/wmr/src/plugins/import-assertion.js:31:16)
    at file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:18755:25

Exception:

SyntaxError: Unexpected token (2:7)
    at Object.pp$4.raise (file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:15543:13)
    at Object.pp.unexpected (file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:13235:8)
    at Object.pp.expect (file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:13229:26)
    at Object.pp$1.parseImportSpecifiers (file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:14135:8)
    at Object.parseImport (file:///Users/riywo/GitHub/wmr/node_modules/acorn-import-assertions/lib/index.mjs:77:32)
    at Object.pp$1.parseStatement (file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:13409:47)
    at Object.pp$1.parseTopLevel (file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:13292:21)
    at Object.parse (file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:13094:15)
    at Function.parse (file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:13115:35)
    at Graph.contextParse (file:///Users/riywo/GitHub/wmr/node_modules/rollup/dist/es/shared/rollup.js:19192:38) {pos: 60, loc: Position, raisedAt: 66, stack: 'SyntaxError: Unexpected token (2:7)
    at Ob…les/rollup/dist/es/shared/rollup.js:19192:38)', message: 'Unexpected token (2:7)'}

Code is here:

'import _typeof from "@babel/runtime/helpers/typeof";
import assertThisInitialized from "./assertThisInitialized.js";
export default function _possibleConstructorReturn(self, call) {
  if (call && (_typeof(call) === "object" || typeof call === "function")) {
    return call;
  }

  return assertThisInitialized(self);
}'

@riywo
Copy link
Author

riywo commented Jul 16, 2021

It seems assert after import is treated as special keyword. Once I replace assertThisInitialized to the other word, parse() succeeds:

> parse(code)
Uncaught SyntaxError: Unexpected token (2:7)

> parse(code.replaceAll("assertThisInitialized", "foo"))
Node {type: 'Program', start: 0, end: 265, body: Array(3), sourceType: 'module'}

@riywo
Copy link
Author

riywo commented Jul 16, 2021

Minimal repro:

const acorn = require("acorn");
const {importAssertions} = require('acorn-import-assertions');
const fs = require('fs');
const code = fs.readFileSync("/Users/riywo/GitHub/wmr/examples/demo/node_modules/@babel/runtime/helpers/esm/possibleConstructorReturn.js", "utf8");

console.log(code)
console.log(acorn.parse(code, {sourceType: "module"})) // No issue
console.log(acorn.Parser.extend(importAssertions).parse(code, {sourceType: "module"})) // Unexpected token

@riywo
Copy link
Author

riywo commented Jul 16, 2021

With the fix above, wmr build succeeded:

> build
> /Users/riywo/GitHub/wmr/packages/wmr/wmr.cjs build --prerender --debug

  wmr:config  {
  wmr:config    _: [],
  wmr:config    prerender: true,
  wmr:config    debug: true,
  wmr:config    cwd: '/Users/riywo/work/wmr-i18next',
  wmr:config    public: 'public',
  wmr:config    out: '/Users/riywo/work/wmr-i18next/dist',
  wmr:config    sourcemap: undefined,
  wmr:config    visualize: undefined,
  wmr:config    minify: true,
  wmr:config    root: '/Users/riywo/work/wmr-i18next/public',
  wmr:config    plugins: [],
  wmr:config    output: [],
  wmr:config    middleware: [],
  wmr:config    features: { preact: true },
  wmr:config    alias: { react: 'preact/compat', 'react-dom': 'preact/compat' },
  wmr:config    customRoutes: [],
  wmr:config    prod: true,
  wmr:config    mode: 'build',
  wmr:config    env: {},
  wmr:config    overlayDir: '/Users/riywo/work/wmr-i18next/dist',
  wmr:config    publicPath: '/',
  wmr:config    host: 'localhost'
  wmr:config  }
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  preact-iso -> npm/[email protected]/index.js
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  preact/hooks -> npm/[email protected]/hooks/dist/hooks.module.js
  npm:plugin  i18next -> npm/[email protected]/dist/esm/i18next.js
  npm:plugin  preact-iso -> npm/[email protected]/index.js
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  htm/preact -> npm/[email protected]/preact/index.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  htm -> npm/[email protected]/dist/htm.module.js
  npm:plugin  ./lazy.js -> npm/[email protected]/lazy.js
  npm:plugin  ./router.js -> npm/[email protected]/router.js
  npm:plugin  ./hydrate.js -> npm/[email protected]/hydrate.js
  npm:plugin  ./prerender.js -> npm/[email protected]/prerender.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact/hooks -> npm/[email protected]/hooks/dist/hooks.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  preact/hooks -> npm/[email protected]/hooks/dist/hooks.module.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
  npm:plugin  @babel/runtime/helpers/esm/typeof -> npm/@babel/[email protected]/helpers/esm/typeof.js
  npm:plugin  @babel/runtime/helpers/esm/createClass -> npm/@babel/[email protected]/helpers/esm/createClass.js
  npm:plugin  @babel/runtime/helpers/esm/classCallCheck -> npm/@babel/[email protected]/helpers/esm/classCallCheck.js
  npm:plugin  @babel/runtime/helpers/esm/getPrototypeOf -> npm/@babel/[email protected]/helpers/esm/getPrototypeOf.js
  npm:plugin  @babel/runtime/helpers/esm/inherits -> npm/@babel/[email protected]/helpers/esm/inherits.js
  npm:plugin  @babel/runtime/helpers/esm/possibleConstructorReturn -> npm/@babel/[email protected]/helpers/esm/possibleConstructorReturn.js
  npm:plugin  @babel/runtime/helpers/esm/objectSpread -> npm/@babel/[email protected]/helpers/esm/objectSpread.js
  npm:plugin  @babel/runtime/helpers/esm/assertThisInitialized -> npm/@babel/[email protected]/helpers/esm/assertThisInitialized.js
  npm:plugin  @babel/runtime/helpers/typeof -> npm/@babel/[email protected]/helpers/esm/typeof.js
  npm:plugin  preact-render-to-string -> npm/[email protected]/dist/index.mjs
  npm:plugin  ./assertThisInitialized.js -> npm/@babel/[email protected]/helpers/esm/assertThisInitialized.js
  npm:plugin  ./setPrototypeOf.js -> npm/@babel/[email protected]/helpers/esm/setPrototypeOf.js
  npm:plugin  ./defineProperty.js -> npm/@babel/[email protected]/helpers/esm/defineProperty.js
  npm:plugin  preact -> npm/[email protected]/dist/preact.module.js
minify(index.02d249b0.js) took 1261ms
minify(chunks/prerender.ef1cb74a.js) took 127ms
Merging assets/style.module.83820528.css into assets/style.module.7b16dcc3.css (chunks/index.5fc2dbd8.js → index.02d249b0.js)
Hoisting CSS "assets/style.module.7b16dcc3.css" imported by index.02d249b0.js into parent HTML import "assets/style.e6ee4bcf.css".

Wrote 66Kb to disk:
  dist/index.html 460b
  dist/index.02d249b0.js 60Kb
  dist/assets/style.e6ee4bcf.css 499b
  dist/chunks/index.5fc2dbd8.js 214b
  dist/chunks/prerender.ef1cb74a.js 4.8Kb
Prerendered 3 pages:
  /
  /about [from /]
  /error [from /]

@developit
Copy link
Member

Awesome debugging @riywo!!

@riywo
Copy link
Author

riywo commented Jul 16, 2021

Just FYI: wmr 3.4.1 fixed this issue. Thank you for the quick release!

@Pringels
Copy link

It looks like the yup form validation library has the same issue:
https://github.com/jquense/yup/blob/master/src/schema.ts#L313

if I rename the .assert to anything else the build succeeds.

@marvinhagemeister
Copy link
Member

Reopening, per the above comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
4 participants