2

I need to bypass a specific Javascript file from web-server that comes by default in vue-cli (Webpack Dev Server) such that I can open the js file directly from the browser.

It is for Firebase Messaging Service Worker and hence it needs to be in the root.

Eg. http://localhost:8080/firebase-messaging-sw.js

1 Answer 1

1

In webpack.dev.conf.js add next code

  1. require fs
  2. add before hook to devServer conf.

example:

const fs = require('fs')
const devWebpackConfig = merge(baseWebpackConfig, {
  module: {
    rules:rules
  },
  devtool: config.dev.devtool,
  devServer: {
    clientLogLevel: 'warning',
     before(app){
       app.get('/firebase-messaging-sw.js', function(req, res) {
          let content = fs.readFileSync(path.resolve(__dirname, '../src/firebase-messaging-sw.js'), 'utf-8')
          res.header("Content-Type", "text/javascript");
          res.send(content)
          return res
    });
},

And don`t forget copy the file in prod mode.

new CopyWebpackPlugin([
  {
    from: path.resolve(__dirname, '../src/firebase-messaging-sw.js'),
    to: config.build.assetsRoot + '/firebase-messaging-sw.js'
  }
]),
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.