Giter Club home page Giter Club logo

webpack-preprocessor-loader's Introduction

Webpack preprocessor loader

Helps to disable parts of the code depending on preprocessor directives.

Use //# to start a directive

Supported directives:

  ifdef  - If defined, start a new block
  ifndef - If not defined, start a new block
  else   - If the directive was false, use the else block
  endif  - Ends a block
  define - Defines a define variable
  undef  - Undefines a define variable

To use define variable substitution use: DEFINED_VARIABLE_NAME

I hope you will find this useful. If you have problems, please open an issue and I will try to help you as best I can.

Usage instructions

Install:

npm install --save-dev https://github.com/Ramzeus/webpack-preprocessor-loader.git

Add to webpack config:

Add it by using loader: require.resolve('webpack-preprocessor-loader'),

You can configure the predefined defines by using options like this:

options: {
  defines: {
    MY_DEFINE: 'Some Value',
    DEBUG: ''
  }
}

Configuration example:

module: {
    rules: [
      {
        test: /\.(ts|js)x?$/,
        loaders: [
          // In this example I use a typescript loader
          // The loaders are run in reverse order,
          // so first the code will filter thru webpack-preprocessor-loader
          // and the thru the typescript loader, or whatever you want to use
          {
            loader: 'awesome-typescript-loader',
            options:{
              configFileName: 'tsconfig.json'
            }
          },
          {
            loader: require.resolve('webpack-preprocessor-loader'),
            options: {
              defines: {
                MY_DEFINE: 'Some value',
                DEBUG: ''
              }
            }
          }
        ],
        exclude: [/node_modules/],
      }
    ]
  },
}

Code example

//#ifdef PLATFORM_A
import { stuff } from './platform-specific-code/platform_a'
//#else
import { other_stuff } from './platform-specific-code/platform_b'
//#endif
  //#ifdef __EXEC_ENV_BROWSER
  const { PlatformHandlerBrowser } = await import("./browser/platform-handler-browser");
  return new PlatformHandlerBrowser();
  //#endif
  //#ifdef __EXEC_ENV_NODE
  const { PlatformHandlerNode } = await import('./node/platform-handler-node');
  return new PlatformHandlerNode();
  //#endif
  //#ifdef __EXEC_ENV_QT
  const { PlatformHandlerQt } = await import("./qt/platform-handler-qt");
  return new PlatformHandlerQt();
  //#endif

webpack-preprocessor-loader's People

Contributors

mrharel avatar patrlind avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.