|
1 |
| -import path from 'path' |
2 |
| -import slash from 'slash' |
3 | 1 | import builtinModules from 'builtin-modules'
|
4 |
| -import resolve from '../resolve' |
5 | 2 | import { NormalizedConfig } from '../index'
|
6 | 3 | import isExternal from '../utils/is-external'
|
7 | 4 | import logger from '../logger'
|
8 | 5 |
|
9 | 6 | interface Options {
|
10 |
| - bundleNodeModules?: boolean | string[] |
11 | 7 | rootDir: string
|
| 8 | + bundleNodeModules?: boolean | string[] |
12 | 9 | externals: NormalizedConfig['externals']
|
13 | 10 | }
|
14 | 11 |
|
15 | 12 | export default (options: Options) => {
|
16 |
| - return { |
17 |
| - name: 'bili-node-resolve', |
18 |
| - |
19 |
| - resolveId: async (importee: string, importer?: string) => { |
20 |
| - if (/\0/.test(importee)) { |
21 |
| - return null |
22 |
| - } |
23 |
| - const isValidPath = !/[<>:"|?*]/.test(importee) |
24 |
| - if (!isValidPath) { |
25 |
| - return null |
26 |
| - } |
27 |
| - |
28 |
| - // Exclude built-in modules |
29 |
| - if (builtinModules.includes(importee)) { |
30 |
| - return false |
31 |
| - } |
| 13 | + const plugin = require('rollup-plugin-node-resolve')({ |
| 14 | + extensions: ['.js', '.json', '.jsx', '.ts', '.tsx'], |
| 15 | + preferBuiltins: true, |
| 16 | + jsnext: true, |
| 17 | + module: true |
| 18 | + }) |
32 | 19 |
|
33 |
| - importee = slash(importee) |
34 |
| - if (importer) { |
35 |
| - importer = slash(importer) |
36 |
| - } |
| 20 | + return { |
| 21 | + ...plugin, |
37 | 22 |
|
38 |
| - let id: string |
| 23 | + name: 'bili-custom-resolve', |
39 | 24 |
|
40 |
| - try { |
41 |
| - id = await resolve(importee, { |
42 |
| - cwd: importer ? path.dirname(importer) : options.rootDir |
43 |
| - }) |
44 |
| - } catch (err) { |
45 |
| - if (!importer) { |
46 |
| - // An entry file should not be marked as external if it doesn't exist |
47 |
| - return null |
48 |
| - } |
49 |
| - return false |
50 |
| - } |
| 25 | + async resolveId(importee: string, importer?: string) { |
| 26 | + const id = await plugin.resolveId( |
| 27 | + importee, |
| 28 | + importer || `${options.rootDir}/__no_importer__.js` |
| 29 | + ) |
51 | 30 |
|
52 |
| - // If we don't intend to bundle node_modules |
53 |
| - // Mark it as external |
54 |
| - if (/node_modules/.test(id)) { |
55 |
| - if (!options.bundleNodeModules) { |
| 31 | + if (typeof id === 'string') { |
| 32 | + // Exclude built-in modules |
| 33 | + if (builtinModules.includes(id)) { |
56 | 34 | return false
|
57 | 35 | }
|
58 |
| - if (Array.isArray(options.bundleNodeModules)) { |
59 |
| - const shouldBundle = options.bundleNodeModules.some(name => |
60 |
| - id.includes(`/node_modules/${name}/`) |
61 |
| - ) |
62 |
| - if (!shouldBundle) { |
| 36 | + |
| 37 | + // If we don't intend to bundle node_modules |
| 38 | + // Mark it as external |
| 39 | + if (/node_modules/.test(id)) { |
| 40 | + if (!options.bundleNodeModules) { |
63 | 41 | return false
|
64 | 42 | }
|
| 43 | + if (Array.isArray(options.bundleNodeModules)) { |
| 44 | + const shouldBundle = options.bundleNodeModules.some(name => |
| 45 | + id.includes(`/node_modules/${name}/`) |
| 46 | + ) |
| 47 | + if (!shouldBundle) { |
| 48 | + return false |
| 49 | + } |
| 50 | + } |
65 | 51 | }
|
66 |
| - } |
67 | 52 |
|
68 |
| - if (isExternal(options.externals, id, importer)) { |
69 |
| - return false |
70 |
| - } |
| 53 | + if (isExternal(options.externals, id, importer)) { |
| 54 | + return false |
| 55 | + } |
71 | 56 |
|
72 |
| - if (/node_modules/.test(id) && !/^\.?\//.test(importee)) { |
73 |
| - logger.debug(`Bundled ${importee} because ${importer} imported it.`) |
| 57 | + if (/node_modules/.test(id) && !/^\.?\//.test(importee)) { |
| 58 | + logger.debug(`Bundled ${importee} because ${importer} imported it.`) |
| 59 | + } |
74 | 60 | }
|
75 | 61 |
|
76 | 62 | return id
|
77 |
| - }, |
78 |
| - |
79 |
| - transform(code: string, id: string) { |
80 |
| - // if (id.endsWith('.js')) { |
81 |
| - // return `// module: ${id}\n${code}` |
82 |
| - // } |
83 | 63 | }
|
84 | 64 | }
|
85 | 65 | }
|
0 commit comments