ardatan / meteor-webpack

https://medium.com/@ardatan/meteor-with-webpack-in-2018-faster-compilation-better-source-handling-benefit-from-bc5ccc5735ef
MIT License
123 stars 29 forks source link
angular atmosphere hmr meteor meteor-webpack modern-web react vue webpack webpack-cli

Meteor-Webpack

Blog Post : https://medium.com/@ardatan/meteor-with-webpack-in-2018-faster-compilation-better-source-handling-benefit-from-bc5ccc5735ef

Meteor-Webpack provides you a development environment that integrates modern web bundler Webpack, and modern perfect full-stack JavaScript framework Meteor.

You need just one atmosphere package to start; ardatan:webpack

This project includes some examples with popular Frontend frameworks and a compiler package that replaces Meteor's bundler with modern web project bundler Webpack. You have to create a webpack.config.js file that has the compilation configurations for both client and server code. You are to free to choose the directory structure in your project, Webpack will compile your project regarding to your entry definition.

Simple Migration

Feel free like you are working in a Webpack CLI

Meteor-Webpack would make you feel you are using Webpack CLI. Just use same cases in Webpack's own documentation.

Feel free like you are working in a regular Meteor environment as well

Meteor-Webpack can resolve any atmosphere packages and Meteor modules like you are using without Meteor-Webpack

Try our examples with your favorite Frontend framework

Why Webpack

Comparison with other solutions in Meteor

Regular Meteor Bundler

Regular Meteor Bundler uses babel which tranpiles your ES2015 syntax to ES5 even imports to CommonJS which creates some limitation for you. For instance, you cannot use ES2015 modules, then you need to import UMD modules which would probably contain unused submodules of this module. Despite you can use atmosphere packages with Meteor-Webpack, you don't need to add extra atmosphere packages for sass, typescript and others' compilation. For an extra compiler such as sass, less and pug etc; you can just install necessary webpack loader plugins, and add them into webpack.config.js. Meteor-Webpack runs exactly same way with webpack-dev-server.

Meteor Client Bundler

As in its documentation; meteor-client-bundler is a module bundler which will take a bunch of Atmosphere package and put them into a single module, so we can load Meteor's client scripts regardless of what framework we're using to run our server. But you cannot use this client bundle with Server Side Rendering, and you must have two different projects which run on two different servers. With Meteor-Webpack, you can extract webpack.config.js from Angular CLI, create-react-app and any other CLI tools', then easily use it with Meteor.

Before you start

Seperating Client and Server Configuration - IMPORTANT!

    const clientConfig = {
        //...
    }
    const serverConfig = {
        //...
        target: 'node',
    }

Meteor Package Imports - IMPORTANT!

    const meteorExternals = require('webpack-meteor-externals');
    //...
    externals: [
        meteorExternals()
    ]
    //...

Meteor File Imports - Optional

    //...
    resolve: {
        modules: [
          path.resolve(__dirname, 'node_modules'),
          path.resolve(__dirname, './'), // enables you to use 'imports/...' instead of '/imports/...'
        ],
        alias: {
          '/imports': path.resolve(__dirname, './imports'),
          '/ui': path.resolve(__dirname, './ui'),
          // ... and any other directories you might have
        }
    }
    //...

Client Configuration

Webpack Dev Middleware

If you want to use Webpack's Development Server instead of Meteor's, you have to add devServer field in the client configuration;

    devServer: {}

then you have to add another atmosphere package to packages;

    meteor add ardatan:webpack-dev-middleware

NOTE Make sure ardatan:webpack-dev-middleware is at the bottom of your .packages list for the best compatibility with other Meteor packages.

don't forget to install webpack-dev-middleware package from NPM;

    meteor npm install webpack-dev-middleware --save-dev

Server Configuration

Loading NPM modules on runtime instead of compiling them by Meteor

    meteor npm install webpack-node-externals --save-dev
    externals: [nodeExternals()], // in order to ignore all modules in node_modules folder

Hot Module Replacement

    devServer: {
        hot: true
    }
    plugins: {
        new webpack.HotModuleReplacementPlugin()
    }

meteor/server-render

Dynamic boilerplate assets

Galaxy Deployment

meteor deploy command doesn't set NODE_ENV=production environment variable. That's why, webpack compiler recognizes that it is still a development build. You have two options to fix issue;

First option ( Recommended )

Testing