Vuepress: Generated js files have SyntaxError when enabling source map in webpack config

Created on 27 Feb 2019  路  15Comments  路  Source: vuejs/vuepress




  • [x] I confirm that this is a issue rather than a question.




Bug report

When enabling source map in the configureWebpack option, the generated js files have SyntaxErrors. Comment the devtool: "source-map" line solves the problem.

Version

1.0.0-alpha.40

Steps to reproduce

git clone https://github.com/jjyyxx/vuepress-1367-repro.git
cd vuepress-1367-repro
npm run build

What is expected?

Generate valid code.

What is actually happening?

Uncaught SyntaxError: Unexpected token } happens in app.xxx.js.

Other relevant information

  • Your OS: Windows 10 1809
  • Node.js version: v11.4.0
  • Browser version: Google Chrome v72.0.3626.119
  • Is this a global or local install? local
  • Which package manager did you use for the install? npm
  • Does this issue occur when all plugins are disabled? yes
has PR bug

Most helpful comment

@sinchang You misused lambda.

In

() => {
    devtool: 'source-map'
    // "devtool:" is an unused label
    // 'source-map' is an unused string literal expression
}

It is pretty valid javascript but may not be what you what to achieve.
The correct way is

() => ({
  devtool: 'source-map'
})

which returns an object and fails as exprected.

All 15 comments

it's working on my local macOS dev workspace.

@sinchang Thanks for your test! Could you upload your syntactically correct app.[hash].js file (rename to a txt)?

I have tested under Windows 10 and Ubuntu. Evaluating the app.[hash].js under both platforms led to

Uncaught SyntaxError: Unexpected token }

@sinchang Thanks for your quick reply. But when I try to evaluate your gist in the browser, the error

Uncaught SyntaxError: Unexpected token }

persists. Are you sure your gist could run without syntactical problems under macOS?

@sinchang It seems that your gist begins with

//# sourceMappingURL=styles.bf997139.js.map!function(t){
//                                         ^ there should be a newline

This can explain the problem @jjyyxx encountered.

@jjyyxx sorry, my fault. build success but open in the browser has the same problem

@jjyyxx it's working when update config to

module.exports = {
  configureWebpack: () => {
    devtool: 'source-map'
  }
}

@sinchang You misused lambda.

In

() => {
    devtool: 'source-map'
    // "devtool:" is an unused label
    // 'source-map' is an unused string literal expression
}

It is pretty valid javascript but may not be what you what to achieve.
The correct way is

() => ({
  devtool: 'source-map'
})

which returns an object and fails as exprected.

I found that the function workaroundEmptyStyleChunk caused this problem.
https://github.com/vuejs/vuepress/blob/1850be7f343e54575c64d82c4f0cedc25a9aa91c/packages/%40vuepress/core/lib/build.js#L174-L190

The hack was introduced to address this issue. This snippet prepends styleChunkContent to appChunkContent and writes it back to app.[hash].js. When building chunks without sourcemap, each chunk ends with a semicolon (always?). So the prepending operation generates valid code.

BUT, if sourcemap is configured to be generated, webpack will append a comment line to the end of each js chunk, which breaks the precondition of each chunk ends with a semicolon, leading to invalid code after prepending.

A simple workaround is inserting a line break when concatenating styleChunkContent and appChunkContent. While the SyntaxError will be resolved, another problem remains that the sourcemap for app.[hash].js will be invalidated because the whole chunk deviates from the original location by around 100 characters.

@yyx990803 The hack was introduced around a year ago, but its impact to sourcemap might not get considered yet. Could you offer some hints to solve this problem gracefully?

Maybe we can make a simple judgment before using this workaround:

if (!config.devTool) await workaroundEmptyStyleChunk()

Since we usually don't need a sourcemap in a real production phase, I think this approach is understandable.

@jjyyxx I think a more fundamental fix would be having the empty chunk issue fixed in webpack itself... thus getting rid of this hack altogether...

@Shigma I tested the patch and it worked as expected. Is it possible to be added into the next version?

I don't think it's a breaking change. And it only introduces an if statement which can be removed together when the upstream issue fixed.

@jjyyxx contribution welcome! 馃槅

pr created at #1378

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sankincn picture sankincn  路  3Comments

gaomd picture gaomd  路  3Comments

genedronek picture genedronek  路  3Comments

kid1412621 picture kid1412621  路  3Comments

tinchox5 picture tinchox5  路  3Comments