webpack externals providePlugin expose-loader

externals

用script 引入的 库

// 入口文件
import $ from 'jquery'
console.log($)
 
//webpack配置
module.exports = {
    externals: {
        jquery: 'jquery'
    }
}

providePlugin

provideplugin 自动加载到每个模块, 而不必导出 import 或 require

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

expose-loader 将库引入到全局作用域

module: {
  rules: [{
    test: require.resolve('jquery'),
    use: {
      loader: 'expose-loader',
      options: '$'
    }
  }]
}
console.log(window.$)

Leave a Reply

Your email address will not be published. Required fields are marked *