webpack4 通过cdn引入外部资源打包

左手的ㄟ右手 2021-07-24 12:19 763阅读 0赞
  1. 方式一:
  2. 1、与mode同级配置,则不会打包jquery
  3. externals: {
  4. jquery:'jQuery',
  5. 库名:'替代的全局变量字符串'
  6. }
  7. 例子:
  8. externals : {
  9. lodash : { lodash这个外部library可以在AMDCommonJ模块系统中通过lodash访问,但在全局变量形式下用_访问。
  10. commonjs: "lodash",
  11. amd: "lodash",
  12. root: "_" // 指向全局变量
  13. }
  14. }
  15. externals : {
  16. subtract : { subtract可以通过全局math对象下的属性subtract访问(例如 window['math']['subtract'])。
  17. root: ["math", "subtract"]
  18. }
  19. }
  20. 2、在html文件中引入cdn链接即可
  21. 参考网址:https://www.webpackjs.com/configuration/externals/
  22. 方式二:
  23. 使用html-webpack-externals-plugin插件,会自动引入srcript,srcentry字段对应的值
  24. (1)下载
  25. cnpm install -D html-webpack-externals-plugin
  26. (2)配置
  27. const HtmlWebpackExternalsPlugin = require('html-webpack-externals-plugin')
  28. plugins: [
  29. new HtmlWebpackExternalsPlugin({
  30. externals: [
  31. {
  32. module: 'react', 库名
  33. entry: 'https://11.url.cn/now/lib/16.2.0/react.min.js', cdn或本地文件
  34. global: 'React', 挂载到全局对象上的键名
  35. },
  36. {
  37. module: 'react-dom',
  38. entry: 'https://11.url.cn/now/lib/16.2.0/react-dom.min.js',
  39. global: 'React-dom',
  40. },
  41. ],
  42. }),
  43. ]
  44. (3)在html文件中scripy引入cdn

在这里插入图片描述

代码示例:

  1. const path = require('path')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin');
  3. const MiniCssExtractPlugin= require('mini-css-extract-plugin');
  4. const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  5. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  6. const WorkboxPlugin = require('workbox-webpack-plugin');
  7. // process.env.NODE_ENV='development'
  8. // console.log(this.mode)
  9. module.exports = {
  10. entry: ['./src/js/index.js','./src/index.html'],
  11. // entry: {
  12. // main: './src/js/index.js',
  13. // test: './src/js/print.js',
  14. // // html:'./src/index.html'
  15. // },
  16. output: {
  17. // 'src/js/build.[contenthash:10].js'
  18. filename: 'src/js/[name].[contenthash:10].js',
  19. path:path.resolve(__dirname,'build')
  20. },
  21. module: {
  22. rules: [//创建模块时,匹配请求的规则数组。这些规则能够修改模块的创建方式。这些规则能够对模块(module)应用 loader,或者修改解析器(parser)。
  23. {
  24. test: /\.css$/, //匹配文件名
  25. use: [//使用哪些loader
  26. // 'style-loader', //创建style标签,将js中的css样式资源添加到style标签中生效
  27. MiniCssExtractPlugin.loader, //取代style-loader,提取js中的css为单独文件
  28. 'css-loader', //css文件变成commonjs模块,里面内容是样式字符串
  29. {
  30. loader: 'postcss-loader',
  31. ident: 'postcss',
  32. options: {
  33. postcssOptions: {
  34. plugins: [['postcss-preset-env',{ }]]
  35. },
  36. }
  37. }
  38. ]
  39. },
  40. {
  41. test: /\.less$/,
  42. use: [
  43. 'style-loader',
  44. 'css-loader',
  45. 'less-loader', //将less文件编译成css文件
  46. ]
  47. },
  48. {
  49. test: /\.(jpg|png|gif|jfif)$/,
  50. loader:'url-loader', //依赖于file-loader
  51. options: {
  52. //图片小于8kb,会处理成base64,即减少请求数量,增大图片体积(请求速度会变慢cn)
  53. limit: 8 * 1024,
  54. esModule: false, //不采用es6模式解析,否则会和html-loader采用的commonjs模式冲突
  55. name: '[name].[ext]',
  56. outputPath:'src/images'
  57. }
  58. },
  59. {
  60. test: /\.html$/,
  61. loader:'html-loader', //处理html文档中的img,负责引入图片,交给url-loader处理
  62. },
  63. {
  64. // exclude:/\.xx$/
  65. test: /\.(ttf|eot|svg|woff|woff2)$/,
  66. loader: 'file-loader',
  67. options: {
  68. name: '[name].[ext]',
  69. outputPath:'src/icon'
  70. },
  71. },
  72. { //不检查node_modules
  73. test: /\.js$/,
  74. exclude:/node_modules/,
  75. loader: 'eslint-loader',
  76. enforce:'pre', //优先执行
  77. options: {
  78. fix: true,
  79. }
  80. },
  81. { //js兼容性处理
  82. test: /\.js$/,
  83. exclude: /(node_modules|bower_components)/,
  84. use: [
  85. 'thread-loader', //开启多进程打包
  86. {
  87. loader: 'babel-loader',
  88. options: {
  89. // 预设,指示babel做兼容性处理
  90. presets: [['@babel/preset-env', {
  91. targets: {
  92. chrome: '60',
  93. firefox: '60',
  94. ie: '9',
  95. safari: '10',
  96. edge:'19'
  97. },
  98. useBuiltIns: 'usage', //按需加入
  99. corejs: { //指定core-js版本
  100. version:3
  101. }
  102. }]],
  103. cacheDirectory:true //开启缓存
  104. // plugins: ['@babel/transform-runtime']
  105. }
  106. }
  107. ]
  108. }
  109. ]
  110. },
  111. //插件
  112. plugins: [
  113. new HtmlWebpackPlugin({
  114. template: './src/index.html', //将模板复制成指定文件
  115. minify: {
  116. collapseWhitespace: true, //折叠空格
  117. removeComments:true //移出注释
  118. }
  119. }), //默认传键html文件,并引入打包输出的资源,默认为基本结构
  120. new MiniCssExtractPlugin({
  121. filename: 'src/css/build.[contenthash:10].css'
  122. }),
  123. new OptimizeCssAssetsPlugin(),
  124. new CleanWebpackPlugin(),
  125. new WorkboxPlugin.GenerateSW({ //生成serviceworker
  126. clientsClaim: true, //帮助serviceworker快速启动
  127. skipWaiting: true, //删除旧的serviceworker
  128. })
  129. ],
  130. // optimization: {
  131. // splitChunks: {
  132. // chunks: 'all', //将node_modules内库单独打包
  133. // }
  134. // },
  135. mode: 'production',
  136. //自动化,自动编译、打开浏览器和自动刷新等
  137. devServer: {
  138. // contentBase:['xx','xx',...]
  139. contentBase: path.resolve(__dirname, 'build'), //提供内容的目录
  140. //启动gzip压缩
  141. compress: true,
  142. port: 3000, //监听端口号
  143. open: true, //自动打开浏览器
  144. hot:true,
  145. },
  146. // devtool:'source-map'
  147. externals: {
  148. jquery:'jQuery'
  149. }
  150. }
  151. /** */

发表评论

表情:
评论列表 (有 0 条评论,763人围观)

还没有评论,来说两句吧...

相关阅读