Va1/string-replace-loader

it not works! I want to replace dev code in prod env!

wxungang opened this issue · 3 comments

it not works! I want to replace dev code in prod env!

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  //@replaceStart
  // Do something with response data
  response.key = '1104'//just for dev code
  //@replaceEnd
  return response;
}, function (error) {
  // Do something with response error

  return Promise.reject(error);
});

webpack.js

  {
        test: /\.js$/,
        loader: "string-replace-loader",
        options: {
          search: "@replaceStart[\w\W\s]*?@replaceEnd",
          replace: "",
          flags: 'g'
        }
      },

or

{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: [
          'babel-loader',
          {
            // test: /\.js$/,
            loader: "string-replace-loader",
            options: {
              search: "@replaceStart[\w\W\s]*?@replaceEnd",
              replace: "",
              flags: 'g'
            }
          }
        ]
      },

this works。good jobs!

{
        test: /\.jsx?$/,
        exclude: /node_modules/,
        use: [
          'babel-loader',
          {
            // test: /\.js$/,
            loader: "string-replace-loader",
            options: {
              search: "@replaceStart[\\w\\W\\s]*?@replaceEnd",
              replace: "",
              flags: 'g'
            }
          }
        ]
      },

Thanks. I used this

{
    /**
     *  This allows conditionally importing modules depending on whether the build is for web or native, without splitting the file into .ts and .tns.ts.
     *  This regex repalces anything between
     *   @NativeScriptOnly
     *  and
     *   @EndNativeScriptOnly
     */
    
    loader: 'string-replace-loader',
    options: {
        search: '\/?\/?[ \s]*@NativeScriptOnly([^]*)@EndNativeScriptOnly',
        replace: '',
        flags: '',
        // strict: true
    }

Then in the code

// @NativeScriptOnly
import { isIOS, isAndroid } from "platform";
import * as Application from "application";
import * as Utils from "utils/utils";
// @EndNativeScriptOnly

Works with or without leading // before @

Va1 commented

glad you've figured it out, mate :)