How to setup webpack plugins with hot reload with Next.js?











up vote
0
down vote

favorite












I'm using a project based on the example at https://github.com/zeit/next.js/tree/master/examples/custom-server-typescript



I've configured a Babel plugin in next.config.js as follows:




let config = withTypescript({
serverRuntimeConfig: {
// Will only be available on the server side
mySecret: "secret"
},
publicRuntimeConfig: {
// Will be available on both server and client
port: parseInt(process.env.PORT, 10) || 3000,
mySecret: process.env.MY_SECRET // Pass through env variables
},
// See https://github.com/zeit/next.js/tree/master/examples/with-sw-precache
webpack: config => {
config.plugins.push(
new SWPrecacheWebpackPlugin({
verbose: true,
staticFileGlobsIgnorePatterns: [/.next//],
runtimeCaching: [
{
handler: "networkFirst",
urlPattern: /^https?.*/
}
]
})
)
return config
}
})
module.exports = config



When I run the yarn build (whose script is next build && tsc --project tsconfig.server.json) the webpack plugin is run and the service-worker.js is generated as expected.



However, when I run yarn dev (whose script is nodemon server/index.ts), the Webpack plugin isn't run and no service-worker.js is generated.



So as best I can tell Webpack is not run directly when I use yarn dev and it only uses ts-node on the TypeScript source. If my understanding on this is correct, how can I get hot-reloading AND load other babel plugins (as configured in next.config.js)? If my understanding is off, please enlighten me!










share|improve this question




























    up vote
    0
    down vote

    favorite












    I'm using a project based on the example at https://github.com/zeit/next.js/tree/master/examples/custom-server-typescript



    I've configured a Babel plugin in next.config.js as follows:




    let config = withTypescript({
    serverRuntimeConfig: {
    // Will only be available on the server side
    mySecret: "secret"
    },
    publicRuntimeConfig: {
    // Will be available on both server and client
    port: parseInt(process.env.PORT, 10) || 3000,
    mySecret: process.env.MY_SECRET // Pass through env variables
    },
    // See https://github.com/zeit/next.js/tree/master/examples/with-sw-precache
    webpack: config => {
    config.plugins.push(
    new SWPrecacheWebpackPlugin({
    verbose: true,
    staticFileGlobsIgnorePatterns: [/.next//],
    runtimeCaching: [
    {
    handler: "networkFirst",
    urlPattern: /^https?.*/
    }
    ]
    })
    )
    return config
    }
    })
    module.exports = config



    When I run the yarn build (whose script is next build && tsc --project tsconfig.server.json) the webpack plugin is run and the service-worker.js is generated as expected.



    However, when I run yarn dev (whose script is nodemon server/index.ts), the Webpack plugin isn't run and no service-worker.js is generated.



    So as best I can tell Webpack is not run directly when I use yarn dev and it only uses ts-node on the TypeScript source. If my understanding on this is correct, how can I get hot-reloading AND load other babel plugins (as configured in next.config.js)? If my understanding is off, please enlighten me!










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I'm using a project based on the example at https://github.com/zeit/next.js/tree/master/examples/custom-server-typescript



      I've configured a Babel plugin in next.config.js as follows:




      let config = withTypescript({
      serverRuntimeConfig: {
      // Will only be available on the server side
      mySecret: "secret"
      },
      publicRuntimeConfig: {
      // Will be available on both server and client
      port: parseInt(process.env.PORT, 10) || 3000,
      mySecret: process.env.MY_SECRET // Pass through env variables
      },
      // See https://github.com/zeit/next.js/tree/master/examples/with-sw-precache
      webpack: config => {
      config.plugins.push(
      new SWPrecacheWebpackPlugin({
      verbose: true,
      staticFileGlobsIgnorePatterns: [/.next//],
      runtimeCaching: [
      {
      handler: "networkFirst",
      urlPattern: /^https?.*/
      }
      ]
      })
      )
      return config
      }
      })
      module.exports = config



      When I run the yarn build (whose script is next build && tsc --project tsconfig.server.json) the webpack plugin is run and the service-worker.js is generated as expected.



      However, when I run yarn dev (whose script is nodemon server/index.ts), the Webpack plugin isn't run and no service-worker.js is generated.



      So as best I can tell Webpack is not run directly when I use yarn dev and it only uses ts-node on the TypeScript source. If my understanding on this is correct, how can I get hot-reloading AND load other babel plugins (as configured in next.config.js)? If my understanding is off, please enlighten me!










      share|improve this question















      I'm using a project based on the example at https://github.com/zeit/next.js/tree/master/examples/custom-server-typescript



      I've configured a Babel plugin in next.config.js as follows:




      let config = withTypescript({
      serverRuntimeConfig: {
      // Will only be available on the server side
      mySecret: "secret"
      },
      publicRuntimeConfig: {
      // Will be available on both server and client
      port: parseInt(process.env.PORT, 10) || 3000,
      mySecret: process.env.MY_SECRET // Pass through env variables
      },
      // See https://github.com/zeit/next.js/tree/master/examples/with-sw-precache
      webpack: config => {
      config.plugins.push(
      new SWPrecacheWebpackPlugin({
      verbose: true,
      staticFileGlobsIgnorePatterns: [/.next//],
      runtimeCaching: [
      {
      handler: "networkFirst",
      urlPattern: /^https?.*/
      }
      ]
      })
      )
      return config
      }
      })
      module.exports = config



      When I run the yarn build (whose script is next build && tsc --project tsconfig.server.json) the webpack plugin is run and the service-worker.js is generated as expected.



      However, when I run yarn dev (whose script is nodemon server/index.ts), the Webpack plugin isn't run and no service-worker.js is generated.



      So as best I can tell Webpack is not run directly when I use yarn dev and it only uses ts-node on the TypeScript source. If my understanding on this is correct, how can I get hot-reloading AND load other babel plugins (as configured in next.config.js)? If my understanding is off, please enlighten me!







      typescript webpack next.js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 6 at 2:49

























      asked Nov 5 at 1:50









      Scott Willeke

      6,34912642




      6,34912642





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147325%2fhow-to-setup-webpack-plugins-with-hot-reload-with-next-js%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147325%2fhow-to-setup-webpack-plugins-with-hot-reload-with-next-js%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini