Coder Social home page Coder Social logo

webpack-recipes's Introduction

这个 repo 介绍一些 Webpack 常用的应用场景.

常用场景列表:

  1. require 非标准模块格式的代码(不遵循CommonJS,也不遵循AMD)

场景1: require 非标准模块格式的代码(不遵循CommonJS,也不遵循AMD)

假设我们有一个 ./rocket.js 文件,内容如下:

    var rocket = {
        fly: function() {
            console.log('hu~~~, hu~~~');
        }
    }

rocket.js 属于非标准格式的模块, 我们需要使用 exports-loader 来引用.

        npm install exports-loader; // 安装 exports-loader

在需要使用的地方只需要这样写:

    var myRocket = require('exports?rocket!./rocket.js');
    myRocket.fly();   
    
    // will produce "hu~~~, hu~~~"

exports-loader 更多的使用方法参考 shimming-modules 的EXPORTING部分.

知识点

  1. imports-loader 使用 https://github.com/webpack/imports-loader
  2. exports-loader 使用 https://github.com/webpack/exports-loader
  3. plugin ProvidePlugin 来实现一个全局默认可用的 module
  4. script-loader 使用
  5. expose-loader 使用 https://github.com/webpack/expose-loader

ProvidePlugin 全局可用的 module

new webpack.ProvidePlugin({
    $: "jquery",
    jQuery: "jquery",
    "window.jQuery": "jquery"
})

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.