Conditional build based on environment using Webpack

前端 未结 9 677
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 21:40

I have some things for development - e.g mocks which I would like to not bloat my distributed build file with.

In RequireJS you can pass a config in a plugin file an

9条回答
  •  情书的邮戳
    2020-11-30 22:20

    another way is using a JS file as a proxy, and let that file load the module of interest in commonjs, and export it as es2015 module, like this:

    // file: myModule.dev.js
    module.exports = "this is in dev"
    
    // file: myModule.prod.js
    module.exports = "this is in prod"
    
    // file: myModule.js
    let loadedModule
    if(WEBPACK_IS_DEVELOPMENT){
        loadedModule = require('./myModule.dev.js')
    }else{
        loadedModule = require('./myModule.prod.js')
    }
    
    export const myString = loadedModule
    

    Then you can use ES2015 module in your app normally:

    // myApp.js
    import { myString } from './store/myModule.js'
    myString // <- "this is in dev"
    

提交回复
热议问题