diff --git a/README.md b/README.md index 8398849..84a2fed 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,37 @@ Safari、Firefox、Chrome新版均能够支持。 npm install exportjs ``` +## 使用方式 + +### 传统方式 (UMD) +可以通过script标签直接引入: + +```html + + +``` + +### ES6 模块方式 +现在支持ES6模块导入,可以只导入需要的函数: + +```javascript +// 导入整个库 +import ExportJS from 'exportjs'; +ExportJS.toCSV({ + // options... +}); + +// 或者只导入需要的函数 +import { toCSV, support } from 'exportjs'; +toCSV({ + // options... +}); +``` + ## API ### toCSV @@ -63,4 +94,4 @@ ExportJS.toCSV({ ##### 导出效果预览 - + \ No newline at end of file diff --git a/lib/exportjs.js b/lib/exportjs.js index 9ce77c0..47a89e5 100644 --- a/lib/exportjs.js +++ b/lib/exportjs.js @@ -1,8 +1,8 @@ (function (global, factory) { - typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : - typeof define === 'function' && define.amd ? define(factory) : - (global.ExportJS = factory()); -}(this, (function () { 'use strict'; + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (factory((global.ExportJS = global.ExportJS || {}))); +}(this, (function (exports) { 'use strict'; var defaults = { mime: 'text/csv,charset=UTF-8', @@ -100,11 +100,16 @@ var support = function () { return true; }; +// Keep the default export for backward compatibility var index = { toCSV: toCSV, support: support, }; -return index; +exports.toCSV = toCSV; +exports.support = support; +exports['default'] = index; + +Object.defineProperty(exports, '__esModule', { value: true }); }))); diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..b7930b4 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,14 @@ +// Re-export from exportjs.js +import ExportJS from './exportjs.js'; + +// Export the default object as a global variable for browser usage +if (typeof window !== 'undefined') { + window.ExportJS = ExportJS; +} + +// Export individual functions for ES6 module imports +export const toCSV = ExportJS.toCSV; +export const support = ExportJS.support; + +// Export the default object for CommonJS and AMD +export default ExportJS; \ No newline at end of file diff --git a/src/index.js b/src/index.js index 1eb9a08..77ca2f3 100644 --- a/src/index.js +++ b/src/index.js @@ -91,7 +91,11 @@ const support = () => { return true; }; +// Export individual functions as named exports +export { toCSV, support }; + +// Keep the default export for backward compatibility export default { toCSV, support, -}; +}; \ No newline at end of file diff --git a/test/index.html b/test/index.html index 267a319..36f45e4 100644 --- a/test/index.html +++ b/test/index.html @@ -8,13 +8,28 @@ -
This page demonstrates both ways of using the ExportJS library:
+