Skip to content
This repository has been archived by the owner on Dec 1, 2019. It is now read-only.

fix(cache): use object-hash to invoke function options #605

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
"lodash": "^4.17.5",
"micromatch": "^3.1.9",
"mkdirp": "^0.5.1",
"object-hash": "^1.3.0",
"source-map-support": "^0.5.3",
"tslib": "^1.9.3",
"webpack-log": "^1.2.0"
},
"peerDependencies": {
Expand All @@ -51,6 +53,7 @@
"@types/micromatch": "^3.1.0",
"@types/mocha": "^5.2.1",
"@types/node": "^10.3.2",
"@types/object-hash": "^1.2.0",
"@types/shelljs": "^0.8.0",
"@types/sinon": "^5.0.1",
"@types/webpack": "^4.4.0",
Expand Down
21 changes: 21 additions & 0 deletions src/__test__/cache-filename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*!
* Copyright 2018 acrazing <[email protected]>. All rights reserved.
* @since 2018-08-02 10:40:34
*/

import { filename } from '../cache'
import { LoaderConfig } from '../interfaces'
import { expect, spec } from './utils'

spec(__filename, async () => {
const optA: LoaderConfig = {
useCache: true
}
const optB: LoaderConfig = {
...optA,
getCustomTransformers: () => ({
before: []
})
}
expect(filename('', '', optA)).not.equal(filename('', '', optB))
})
11 changes: 4 additions & 7 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as zlib from 'zlib'
import { createHash } from 'crypto'
import objectHash = require('object-hash')

export interface CompiledModule {
fileName: string
Expand Down Expand Up @@ -59,17 +59,14 @@ function write(filename: string, result: any) {
*
* @return {String}
*/
function filename(source: string, identifier, options) {
let hash = createHash('sha512') as any
let contents = JSON.stringify({
export function filename(source: string, identifier, options) {
let basename = objectHash({
identifier: identifier,
options: options,
source: source
})

hash.end(contents)

return hash.read().toString('hex') + '.json.gzip'
return basename + '.json.gzip'
}

export interface CacheParams<T> {
Expand Down
52 changes: 30 additions & 22 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
{
"compilerOptions": {
"target": "es5",
"outDir": "dist",
"module": "commonjs",
"moduleResolution": "node",
"skipDefaultLibCheck": true,
"sourceMap": true,
"declaration": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"noFallthroughCasesInSwitch": true,
"removeComments": true,
"suppressImplicitAnyIndexErrors": true,
"lib": [ "es6", "dom" ],
"types" : ["node", "lodash", "mocha"]
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
"compilerOptions": {
"target": "es5",
"outDir": "dist",
"module": "commonjs",
"moduleResolution": "node",
"skipDefaultLibCheck": true,
"sourceMap": true,
"declaration": true,
"noUnusedLocals": true,
"noUnusedParameters": false,
"noFallthroughCasesInSwitch": true,
"removeComments": true,
"suppressImplicitAnyIndexErrors": true,
"lib": [
"es6",
"dom"
],
"importHelpers": true,
"types": [
"node",
"lodash",
"mocha"
]
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules"
]
}
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
version "10.3.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.3.2.tgz#3840ec6c12556fdda6e0e6d036df853101d732a4"

"@types/object-hash@^1.2.0":
version "1.2.0"
resolved "http://registry.npm.taobao.org/@types/object-hash/download/@types/object-hash-1.2.0.tgz#d65904331bd0b05c7d5ece75f9ddfdbe82affd30"

"@types/shelljs@^0.8.0":
version "0.8.0"
resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.8.0.tgz#0caa56b68baae4f68f44e0dd666ab30b098e3632"
Expand Down Expand Up @@ -2668,6 +2672,10 @@ object-copy@^0.1.0:
define-property "^0.2.5"
kind-of "^3.0.3"

object-hash@^1.3.0:
version "1.3.0"
resolved "http://registry.npm.taobao.org/object-hash/download/object-hash-1.3.0.tgz#76d9ba6ff113cf8efc0d996102851fe6723963e2"

object-keys@^1.0.11, object-keys@^1.0.8:
version "1.0.12"
resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.0.12.tgz#09c53855377575310cca62f55bb334abff7b3ed2"
Expand Down Expand Up @@ -3684,6 +3692,10 @@ tslib@^1.8.0, tslib@^1.8.1, tslib@^1.9.0:
version "1.9.2"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.2.tgz#8be0cc9a1f6dc7727c38deb16c2ebd1a2892988e"

tslib@^1.9.3:
version "1.9.3"
resolved "http://registry.npm.taobao.org/tslib/download/tslib-1.9.3.tgz#d7e4dd79245d85428c4d7e4822a79917954ca286"

tslint@^5.10.0:
version "5.10.0"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.10.0.tgz#11e26bccb88afa02dd0d9956cae3d4540b5f54c3"
Expand Down