-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: polish @umijs/deps usage (#6166)
* chore: delete babel related deps * feat: add @pmmmwh/react-refresh-webpack-plugin * chore: code style * feat: built-in terser-webpack-plugin * feat: use more deps from @umijs/deps
- Loading branch information
Showing
50 changed files
with
4,771 additions
and
379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,48 @@ | ||
// @ts-ignore | ||
const hookPropertyMap = new Map( | ||
[ | ||
['webpack', '@umijs/deps/compiled/webpack'], | ||
['webpack/lib/Compilation', '@umijs/deps/compiled/webpack/Compilation'], | ||
export const files = [ | ||
'webpack/lib/Compilation', | ||
'webpack/lib/dependencies/ConstDependency', | ||
'webpack/lib/javascript/JavascriptParserHelpers', | ||
'webpack/lib/LibraryTemplatePlugin', | ||
'webpack/lib/node/NodeTargetPlugin', | ||
'webpack/lib/node/NodeTemplatePlugin', | ||
'webpack/lib/NormalModule', | ||
'webpack/lib/RequestShortener', | ||
'webpack/lib/RuntimeGlobals', | ||
'webpack/lib/RuntimeModule', | ||
'webpack/lib/optimize/LimitChunkCountPlugin', | ||
'webpack/lib/ParserHelpers', | ||
'webpack/lib/SingleEntryPlugin', | ||
'webpack/lib/Template', | ||
]; | ||
|
||
export function getFileName(filePath: string) { | ||
return filePath.split('/').slice(-1)[0]; | ||
} | ||
|
||
export function init() { | ||
const filesMap = files.map((file) => { | ||
const fileName = getFileName(file); | ||
return [file, `@umijs/deps/compiled/webpack/${fileName}`]; | ||
}); | ||
|
||
const hookPropertyMap = new Map( | ||
[ | ||
'webpack/lib/RequestShortener', | ||
'@umijs/deps/compiled/webpack/RequestShortener', | ||
], | ||
// ['webpack-sources', '@umijs/deps/compiled/webpack/sources'], | ||
].map(([request, replacement]) => [request, require.resolve(replacement)]), | ||
); | ||
['webpack', '@umijs/deps/compiled/webpack'], | ||
...filesMap, | ||
// ['webpack-sources', '@umijs/deps/compiled/webpack/sources'], | ||
].map(([request, replacement]) => [request, require.resolve(replacement)]), | ||
); | ||
|
||
// @ts-ignore | ||
const mod = require('module'); | ||
// @ts-ignore | ||
const resolveFilename = mod._resolveFilename; | ||
mod._resolveFilename = function ( | ||
request: string, | ||
parent: any, | ||
isMain: boolean, | ||
options: any, | ||
) { | ||
const hookResolved = hookPropertyMap.get(request); | ||
if (hookResolved) request = hookResolved; | ||
return resolveFilename.call(mod, request, parent, isMain, options); | ||
}; | ||
const mod = require('module'); | ||
const resolveFilename = mod._resolveFilename; | ||
mod._resolveFilename = function ( | ||
request: string, | ||
parent: any, | ||
isMain: boolean, | ||
options: any, | ||
) { | ||
const hookResolved = hookPropertyMap.get(request); | ||
if (hookResolved) request = hookResolved; | ||
return resolveFilename.call(mod, request, parent, isMain, options); | ||
}; | ||
} |
1 change: 1 addition & 0 deletions
1
packages/bundler-webpack/src/webpack/plugins/terser-webpack-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./terser-webpack-plugin/src').default; |
20 changes: 20 additions & 0 deletions
20
packages/bundler-webpack/src/webpack/plugins/terser-webpack-plugin/LICENSE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright JS Foundation and other contributors | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
3 changes: 3 additions & 0 deletions
3
packages/bundler-webpack/src/webpack/plugins/terser-webpack-plugin/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"version": "4.2.3" | ||
} |
130 changes: 130 additions & 0 deletions
130
packages/bundler-webpack/src/webpack/plugins/terser-webpack-plugin/src/Webpack4Cache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import os from 'os'; | ||
|
||
import cacache from '@umijs/deps/compiled/cacache'; | ||
import findCacheDir from '@umijs/deps/compiled/find-cache-dir'; | ||
import serialize from '@umijs/deps/compiled/serialize-javascript'; | ||
|
||
export default class Webpack4Cache { | ||
constructor(compilation, options, weakCache) { | ||
this.cache = | ||
options.cache === true | ||
? Webpack4Cache.getCacheDirectory() | ||
: options.cache; | ||
this.weakCache = weakCache; | ||
} | ||
|
||
static getCacheDirectory() { | ||
return findCacheDir({ name: 'terser-webpack-plugin' }) || os.tmpdir(); | ||
} | ||
|
||
async get(cacheData, { RawSource, ConcatSource, SourceMapSource }) { | ||
if (!this.cache) { | ||
// eslint-disable-next-line no-undefined | ||
return undefined; | ||
} | ||
|
||
const weakOutput = this.weakCache.get(cacheData.inputSource); | ||
|
||
if (weakOutput) { | ||
return weakOutput; | ||
} | ||
|
||
// eslint-disable-next-line no-param-reassign | ||
cacheData.cacheIdent = | ||
cacheData.cacheIdent || serialize(cacheData.cacheKeys); | ||
|
||
let cachedResult; | ||
|
||
try { | ||
cachedResult = await cacache.get(this.cache, cacheData.cacheIdent); | ||
} catch (ignoreError) { | ||
// eslint-disable-next-line no-undefined | ||
return undefined; | ||
} | ||
|
||
cachedResult = JSON.parse(cachedResult.data); | ||
|
||
if (cachedResult.target === 'comments') { | ||
return new ConcatSource(cachedResult.value); | ||
} | ||
|
||
const { | ||
code, | ||
name, | ||
map, | ||
input, | ||
inputSourceMap, | ||
extractedComments, | ||
banner, | ||
shebang, | ||
} = cachedResult; | ||
|
||
if (map) { | ||
cachedResult.source = new SourceMapSource( | ||
code, | ||
name, | ||
map, | ||
input, | ||
inputSourceMap, | ||
true, | ||
); | ||
} else { | ||
cachedResult.source = new RawSource(code); | ||
} | ||
|
||
if (banner) { | ||
cachedResult.source = new ConcatSource( | ||
shebang ? `${shebang}\n` : '', | ||
`/*! ${banner} */\n`, | ||
cachedResult.source, | ||
); | ||
} | ||
|
||
if (extractedComments) { | ||
cachedResult.extractedCommentsSource = new RawSource(extractedComments); | ||
} | ||
|
||
return cachedResult; | ||
} | ||
|
||
async store(cacheData) { | ||
if (!this.cache) { | ||
// eslint-disable-next-line no-undefined | ||
return undefined; | ||
} | ||
|
||
if (!this.weakCache.has(cacheData.inputSource)) { | ||
if (cacheData.target === 'comments') { | ||
this.weakCache.set(cacheData.inputSource, cacheData.output); | ||
} else { | ||
this.weakCache.set(cacheData.inputSource, cacheData); | ||
} | ||
} | ||
|
||
let data; | ||
|
||
if (cacheData.target === 'comments') { | ||
data = { | ||
target: cacheData.target, | ||
value: cacheData.output.source(), | ||
}; | ||
} else { | ||
data = { | ||
code: cacheData.code, | ||
name: cacheData.name, | ||
map: cacheData.map, | ||
input: cacheData.input, | ||
inputSourceMap: cacheData.inputSourceMap, | ||
banner: cacheData.banner, | ||
shebang: cacheData.shebang, | ||
}; | ||
|
||
if (cacheData.extractedCommentsSource) { | ||
data.extractedComments = cacheData.extractedCommentsSource.source(); | ||
data.commentsFilename = cacheData.commentsFilename; | ||
} | ||
} | ||
|
||
return cacache.put(this.cache, cacheData.cacheIdent, JSON.stringify(data)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
packages/bundler-webpack/src/webpack/plugins/terser-webpack-plugin/src/Webpack5Cache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export default class Cache { | ||
constructor(compilation) { | ||
this.cache = compilation.getCache('TerserWebpackPlugin'); | ||
} | ||
|
||
async get(cacheData) { | ||
// eslint-disable-next-line no-param-reassign | ||
cacheData.eTag = | ||
cacheData.eTag || Array.isArray(cacheData.inputSource) | ||
? cacheData.inputSource | ||
.map((item) => this.cache.getLazyHashedEtag(item)) | ||
.reduce((previousValue, currentValue) => | ||
this.cache.mergeEtags(previousValue, currentValue), | ||
) | ||
: this.cache.getLazyHashedEtag(cacheData.inputSource); | ||
|
||
return this.cache.getPromise(cacheData.name, cacheData.eTag); | ||
} | ||
|
||
async store(cacheData) { | ||
let data; | ||
|
||
if (cacheData.target === 'comments') { | ||
data = cacheData.output; | ||
} else { | ||
data = { | ||
source: cacheData.source, | ||
extractedCommentsSource: cacheData.extractedCommentsSource, | ||
commentsFilename: cacheData.commentsFilename, | ||
}; | ||
} | ||
|
||
return this.cache.storePromise(cacheData.name, cacheData.eTag, data); | ||
} | ||
} |
Oops, something went wrong.