webpack学习第十六步—— 垫片Shimming

2020-02-14  本文已影响0人  Love小六六

shimming

低版本浏览器不支持promise,使用polyfill去实现promise就是一个垫片

jquery

export function ui() {
    $('body').css('background','red')
}
import _ from 'lodash'
import $ from 'jquery'
import { ui } from './jquery.ui'

ui()
const dom = $('<div>')
dom.html(_.join(['a','b','c'],'--!-'))

$('body').append(dom)
plugins: [
    new HtmlWebpackPlugin({
        template: 'src/index.html'
    }),
    new CleanWebpackPlugin({
        root: path.resolve(__dirname, '../')
    }),
    new webpack.ProvidePlugin({
        // 模块中使用了$就自动引入jquery,并将jquery赋值给$
        $: 'jquery'
    })
]
plugins: [
    new HtmlWebpackPlugin({
        template: 'src/index.html'
    }),
    new CleanWebpackPlugin({
        root: path.resolve(__dirname, '../')
    }),
    new webpack.ProvidePlugin({
        $: 'jquery',
        _: 'lodash'
    })
]
plugins: [
    new HtmlWebpackPlugin({
        template: 'src/index.html'
    }),
    new CleanWebpackPlugin({
        root: path.resolve(__dirname, '../')
    }),
    new webpack.ProvidePlugin({
        $: 'jquery',
        _join: ['lodash','join']
    })
]
// 此时jquery.ui.js可改写为
export function ui() {
    $('body').css('background',_join(['green'],''))
}

修改this指向的垫片

console.log(this)
console.log(this === window)
npm install imports-loader --save
module: {
    rules: [{
        test: /\.(jpg|png|gif)$/,
        use: {
            loader: "url-loader",
            options: {
                name: '[name]_[hash].[ext]',
                outputPath: 'images/',
                limit: 2048
            }
        }
    },{
        test: /\.js$/,
        exclude: /node_modules/,
        use: [{
            loader: "babel-loader"
        },{
            // 使this指向window
            loader: "imports-loader?this=>window"
        }],
    }]
}
上一篇 下一篇

猜你喜欢

热点阅读