• ES2015模块导入:
    1. import * as webpackNumbers from 'webpack-numbers';
    2. // ...
    3. webpackNumbers.wordToNum('Two');
    • CommonJS模块需要:
    1. var webpackNumbers = require('webpack-numbers');
    2. // ...
    3. webpackNumbers.wordToNum('Two');
    • AMD模块要求:
    1. require(['webpackNumbers'], function ( webpackNumbers) {
    2. // ...
    3. webpackNumbers.wordToNum('Two');
    4. });
    • 通过脚本标签加载库:
    1. <!doctype html>
    2. <html>
    3. ...
    4. <script src="https://unpkg.com/webpack-numbers"></script>
    5. <script>
    6. // ...
    7. // Global variable
    8. webpackNumbers.wordToNum('Five')
    9. // Property in the window object
    10. window.webpackNumbers.wordToNum('Five')
    11. // ...
    12. </script>
    13. </html>

    例外处理:

    1. import _ from 'lodash';
    2. import numRef from './ref.json';
    3. export function numToWord(num) {
    4. return _.reduce(numRef, (accum, ref) => {
    5. return ref.num === num ? ref.word : accum;
    6. }, '');
    7. }
    8. export function wordToNum(word) {
    9. return _.reduce(numRef, (accum, ref) => {
    10. return ref.word === word && word.toLowerCase() ? ref.num : accum;
    11. }, -1);
    12. }
    1. [
    2. {
    3. "num": 1,
    4. "word": "One"
    5. },
    6. {
    7. "num": 2,
    8. "word": "Two"
    9. },
    10. {
    11. "num": 3,
    12. "word": "Three"
    13. },
    14. {
    15. "num": 4,
    16. "word": "Four"
    17. },
    18. {
    19. "num": 5,
    20. "word": "Five"
    21. },
    22. {
    23. "num": 0,
    24. "word": "Zero"
    25. }
    26. ]
    1. var path = require('path');
    2. module.exports = {
    3. entry: './src/index.js',
    4. output: {
    5. path: path.resolve(__dirname, 'dist'),
    6. filename: 'webpack-numbers.js'
    7. },
    8. + externals: {
    9. + lodash: {
    10. + commonjs: 'lodash',
    11. + commonjs2: 'lodash',
    12. + amd: 'lodash',
    13. + root: '_'
    14. + }
    15. + }
    16. // externals: {
    17. // react: 'React',
    18. // 'react-dom': 'ReactDOM',
    19. // 'react-router': 'ReactRouter',
    20. // lodash: '_',
    21. // d3: 'd3',
    22. // echarts: 'echarts',
    23. // moment: 'moment',
    24. // },
    25. };
    1. // webpack.config.js
    2. var path = require('path');
    3. module.exports = {
    4. entry: './src/index.js',
    5. output: {
    6. path: path.resolve(__dirname, 'dist'),
    7. filename: 'webpack-numbers.js',
    8. + library: 'webpackNumbers',
    9. + libraryTarget: 'umd'
    10. },
    11. externals: {
    12. lodash: {
    13. commonjs: 'lodash',
    14. commonjs2: 'lodash',
    15. amd: 'lodash',
    16. root: '_'
    17. }
    18. }
    19. };

    您可以通过以下方式公开库:

    • 变量:作为scripttag(libraryTarget:'var')提供的全局变量。
    • 这个:通过thisobject(libraryTarget:'this')获得。
    • 窗口:window在浏览器(libraryTarget:'window')中通过对象可用。
    • UMD:在AMD或CommonJS requirelibraryTarget:'umd')之后可用。

    如果library已设置libraryTarget且未设置,则libraryTarget默认var输出配置文档中指定的值。见output.libraryTarget那里的所有可用选项的详细列表。

    output.libraryTarget一共支持的值:

    1. var - 默认值
    2. assign
    3. this
    4. window
    5. global
    6. commonjs
    7. commonjs2
    8. amd
    9. umd
    10. jsonp