这种语法,就可以直接在*.js文件中使用_而不需要导入

  1. // webpack.config.js
  2. const path = require('path');
  3. const webpack = require('webpack');
  4. module.exports = {
  5. entry: './src/index.js',
  6. output: {
  7. filename: 'bundle.js',
  8. path: path.resolve(__dirname, 'dist')
  9. },
  10. + plugins: [
  11. + new webpack.ProvidePlugin({
  12. + _: 'lodash'
  13. + })
  14. + ]
  15. };
  16. // index.js
  17. function component() {
  18. var element = document.createElement('div');
  19. element.innerHTML = _.join(['Hello', 'webpack'], ' ');
  20. return element;
  21. }
  22. document.body.appendChild(component());

这种语法,就可以直接在*.js文件中使用JSON而不需要导入

  1. // webpack.config.js
  2. const path = require('path');
  3. const webpack = require('webpack');
  4. module.exports = {
  5. entry: './src/index.js',
  6. output: {
  7. filename: 'bundle.js',
  8. path: path.resolve(__dirname, 'dist')
  9. },
  10. plugins: [
  11. new webpack.ProvidePlugin({
  12. - _: 'lodash'
  13. + join: ['lodash', 'join']
  14. })
  15. ]
  16. };
  17. // index.js
  18. function component() {
  19. var element = document.createElement('div');
  20. - element.innerHTML = _.join(['Hello', 'webpack'], ' ');
  21. + element.innerHTML = join(['Hello', 'webpack'], ' ');
  22. return element;
  23. }
  24. document.body.appendChild(component());