|
1 |
| -const os = require('os') |
2 |
| -const path = require('path') |
3 |
| -const { parse: parseURL, resolve: resolveURL } = require('url') |
4 |
| -const fetch = require('node-fetch') |
5 |
| -const builtinModules = require('builtin-modules') |
6 |
| -const { pathExists, outputFile } = require('./utils') |
7 |
| -const createHttpCache = require('./http-cache') |
8 |
| - |
9 |
| -const RESOLVER_ID = 'import-http-resolver' |
10 |
| - |
11 |
| -const HTTP_RE = /^https?:\/\// |
12 |
| - |
13 |
| -class ImportHttpPlugin { |
14 |
| - constructor(options = {}) { |
15 |
| - this.reload = options.reload |
16 |
| - this.cacheDir = options.cacheDir |
17 |
| - ? path.resolve(options.cacheDir) |
18 |
| - : path.join(os.homedir(), '.import-http') |
19 |
| - this.depsDir = path.join(this.cacheDir, 'deps') |
20 |
| - this.httpCache = createHttpCache(path.join(this.cacheDir, 'requests.json')) |
21 |
| - // Map<file, moduleId> |
22 |
| - this.fileModuleCache = new Map() |
23 |
| - } |
24 |
| - |
25 |
| - getFilePathFromURL(url) { |
26 |
| - const { host, pathname, protocol } = parseURL(url) |
27 |
| - // Where should this url be in the disk |
28 |
| - return path.join(this.depsDir, protocol.replace(':', ''), host, pathname) |
29 |
| - } |
30 |
| - |
31 |
| - apply(compiler) { |
32 |
| - compiler.resolverFactory.hooks.resolver |
33 |
| - .for('normal') |
34 |
| - .tap(RESOLVER_ID, resolver => { |
35 |
| - const resolvedHook = resolver.ensureHook(`resolve`) |
36 |
| - |
37 |
| - resolver |
38 |
| - .getHook(`described-resolve`) |
39 |
| - .tapAsync( |
40 |
| - RESOLVER_ID, |
41 |
| - async (requestContext, resolveContext, callback) => { |
42 |
| - let id = requestContext.request |
43 |
| - const { issuer } = requestContext.context |
44 |
| - |
45 |
| - // if the issuer is a URL (cached in deps dir) |
46 |
| - // resolve the url |
47 |
| - if ( |
48 |
| - !HTTP_RE.test(id) && |
49 |
| - issuer && |
50 |
| - this.fileModuleCache.has(issuer) && |
51 |
| - !this.fileModuleCache.has(id) |
52 |
| - ) { |
53 |
| - if (/^\./.test(id)) { |
54 |
| - // Relative file |
55 |
| - // Excluded something like ./node_modules/webpack/buildin/global.js |
56 |
| - if (!/node_modules/.test(id)) { |
57 |
| - id = resolveURL(this.fileModuleCache.get(issuer), id) |
58 |
| - } |
59 |
| - } else if (!builtinModules.includes(id)) { |
60 |
| - // Propably an npm package |
61 |
| - id = `https://unpkg.com/${id}` |
62 |
| - } |
63 |
| - } |
64 |
| - |
65 |
| - if (!HTTP_RE.test(id)) { |
66 |
| - return callback() |
67 |
| - } |
68 |
| - |
69 |
| - const canResolve = (request, url) => { |
70 |
| - this.fileModuleCache.set(request, url) |
71 |
| - resolver.doResolve( |
72 |
| - resolvedHook, |
73 |
| - Object.assign({}, requestContext, { |
74 |
| - request |
75 |
| - }), |
76 |
| - null, |
77 |
| - resolveContext, |
78 |
| - callback |
79 |
| - ) |
80 |
| - } |
81 |
| - |
82 |
| - try { |
83 |
| - // Let's get the actual URL |
84 |
| - const url = await this.httpCache.get(id) |
85 |
| - let file |
86 |
| - |
87 |
| - if (url && !this.reload) { |
88 |
| - file = this.getFilePathFromURL(url) |
89 |
| - if (await pathExists(file)) { |
90 |
| - return canResolve(file, url) |
91 |
| - } |
92 |
| - } |
93 |
| - |
94 |
| - // We have never requested this before |
95 |
| - console.log(`Downloading ${id}...`) |
96 |
| - const res = await fetch(id) |
97 |
| - if (!res.ok) { |
98 |
| - console.error(`Failed to download ${id}...`) |
99 |
| - throw new Error(res.statusText) |
100 |
| - } |
101 |
| - file = this.getFilePathFromURL(res.url) |
102 |
| - await this.httpCache.set(id, res.url) |
103 |
| - const content = await res |
104 |
| - .buffer() |
105 |
| - .then(buff => buff.toString('utf8')) |
106 |
| - await outputFile(file, content, 'utf8') |
107 |
| - canResolve(file, res.url) |
108 |
| - } catch (error) { |
109 |
| - callback(error) |
110 |
| - } |
111 |
| - } |
112 |
| - ) |
113 |
| - }) |
| 1 | +module.exports = { |
| 2 | + get WebpackPlugin() { |
| 3 | + return require('./webpack') |
| 4 | + }, |
| 5 | + get rollupPlugin() { |
| 6 | + return require('./rollup') |
114 | 7 | }
|
115 | 8 | }
|
116 |
| - |
117 |
| -module.exports = ImportHttpPlugin |
0 commit comments