Skip to content

Commit

Permalink
feat: esm in browser (#29)
Browse files Browse the repository at this point in the history
* chore: support esm in browser

* fix: avoid sending request twice, avoid using key as cacheData
  • Loading branch information
BuptStEve authored Jul 3, 2019
1 parent 3c27bbe commit b3f6635
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 55 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "tua-storage",
"version": "1.7.3",
"version": "1.8.0",
"description": "🏗 A common storage for web(localStorage), for RN(AsyncStorage), for mini-program(wx) or just memory cache(Node.js)",
"main": "dist/TuaStorage.cjs.js",
"module": "dist/TuaStorage.esm.js",
"unpkg": "dist/TuaStorage.umd.js",
"jsdelivr": "dist/TuaStorage.umd.js",
"main": "dist/tua-storage.cjs.js",
"module": "dist/tua-storage.esm.js",
"unpkg": "dist/tua-storage.umd.js",
"jsdelivr": "dist/tua-storage.umd.js",
"typings": "src/index.d.ts",
"files": [
"src/",
Expand Down Expand Up @@ -103,7 +103,7 @@
"rollup-plugin-eslint": "^6.0.0",
"rollup-plugin-json": "^4.0.0",
"rollup-plugin-replace": "^2.2.0",
"rollup-plugin-uglify": "^6.0.2",
"rollup-plugin-terser": "^5.0.0",
"typescript": "^3.5.2",
"vuepress": "^1.0.1"
},
Expand Down
112 changes: 70 additions & 42 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,88 @@ import json from 'rollup-plugin-json'
import babel from 'rollup-plugin-babel'
import replace from 'rollup-plugin-replace'
import { eslint } from 'rollup-plugin-eslint'
import { uglify } from 'rollup-plugin-uglify'
import { terser } from 'rollup-plugin-terser'

import * as pkg from './package.json'
import { DEFAULT_EXTENSIONS } from '@babel/core'

const input = `src/index.js`
const banner = `/* ${pkg.name} version ${pkg.version} */`
const pkg = require('./package.json')

const output = {
const banner =
`/**
* ${pkg.name} v${pkg.version}
* (c) ${new Date().getFullYear()} ${pkg.author}
* @license ${pkg.license}
*/
`

const extensions = [...DEFAULT_EXTENSIONS, 'ts', 'tsx']
const configMap = {
cjs: {
file: pkg.main,
banner,
format: 'cjs',
exports: 'named',
},
esm: {
file: pkg.module,
banner,
format: 'esm',
},
umd: {
file: pkg.unpkg,
name: 'TuaStorage',
banner,
umdDev: {
file: pkg.main,
format: 'umd',
exports: 'named',
env: 'development',
},
}
const plugins = [
eslint(),
json(),
babel(),
]
const env = 'process.env.NODE_ENV'

export default [{
input,
output: [ output.cjs, output.esm ],
plugins,
}, {
input,
output: output.umd,
plugins: [
...plugins,
replace({ [env]: '"development"' }),
],
}, {
input,
output: {
...output.umd,
file: 'dist/TuaStorage.umd.min.js',
umdProd: {
file: `dist/tua-storage.umd.min.js`,
format: 'umd',
env: 'production',
},
esmBrowserDev: {
env: 'development',
file: 'dist/tua-storage.esm.browser.js',
format: 'esm',
},
esmBrowserProd: {
env: 'production',
file: 'dist/tua-storage.esm.browser.min.js',
format: 'esm',
},
plugins: [
...plugins,
replace({ [env]: '"production"' }),
uglify(),
],
}]
}

const genConfig = (opts) => {
const isProd = /min\.js$/.test(opts.file)

const config = {
input: 'src/index.js',
plugins: [eslint({ include: '**/*.js' }), json()],
output: {
file: opts.file,
name: 'TuaStorage',
banner,
format: opts.format,
},
}

if (opts.env) {
config.plugins.push(replace({
'process.env.NODE_ENV': JSON.stringify(opts.env),
}))
}
if (opts.transpile !== false) {
config.plugins.push(babel({ extensions }))
}

if (isProd) {
config.plugins.push(terser({
output: {
/* eslint-disable */
ascii_only: true,
},
}))
}

return config
}

export default Object.keys(configMap)
.map(key => configMap[key])
.map(genConfig)
12 changes: 5 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,7 @@ class TuaStorage {
? this._loadData({ key, cacheData, ...rest })
// 读取 storage
: this.SEMethods._getItem(key)
// 如果有缓存则返回 cacheData
.then(cacheData => this._loadData({ key, cacheData, ...rest }))
// 没有缓存则不传 cacheData,执行同步数据逻辑(请求接口等)
.catch(() => this._loadData({ key, ...rest }))
}

/**
Expand All @@ -373,15 +370,16 @@ class TuaStorage {
*/
_getSEMethods () {
const noop = () => {}
const emptyPRes = () => pRes()
const _getInfoSync = () => ({ keys: this._getAllCacheKeys() })

const defaultSEMap = {
_clear: pRes,
_setItem: pRes,
_getItem: pRes,
_clear: emptyPRes,
_setItem: emptyPRes,
_getItem: emptyPRes,
_getInfo: () => pRes(_getInfoSync()),
_getAllKeys: () => pRes([]),
_removeItem: pRes,
_removeItem: emptyPRes,

_clearSync: noop,
_getInfoSync,
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"baseUrl": ".",
"target": "es5",
"resolveJsonModule": true,
Expand Down

0 comments on commit b3f6635

Please sign in to comment.