( fileName: string, commandOptions: Record<string, unknown> )
| 103 | } |
| 104 | |
| 105 | async function loadTranspiledConfigFile( |
| 106 | fileName: string, |
| 107 | commandOptions: Record<string, unknown> |
| 108 | ): Promise<unknown> { |
| 109 | const { bundleConfigAsCjs, configPlugin, configImportAttributesKey, silent } = commandOptions; |
| 110 | const warnings = batchWarnings(commandOptions); |
| 111 | const inputOptions: InputOptionsWithPlugins = { |
| 112 | // Do *not* specify external callback here - instead, perform the externality check it via fallback-plugin just below this comment. |
| 113 | // This allows config plugin to first decide whether some import is external or not, and only then trigger the check in fallback-plugin. |
| 114 | // Since the check is ultra-simple during this stage of transforming the config file itself, it should be fallback instead of primary check. |
| 115 | // That way, e.g. importing workspace packages will work as expected - the workspace package will be bundled. |
| 116 | input: fileName, |
| 117 | onwarn: warnings.add, |
| 118 | plugins: [], |
| 119 | treeshake: false |
| 120 | }; |
| 121 | await addPluginsFromCommandOption(configPlugin, inputOptions); |
| 122 | // Add plugin as *last* item after addPluginsFromCommandOption is complete. |
| 123 | // This plugin will trigger for imports not resolved by config plugin, and mark all non-relative imports as external. |
| 124 | inputOptions.plugins.push({ |
| 125 | name: 'external-fallback', |
| 126 | resolveId: source => { |
| 127 | const looksLikeExternal = |
| 128 | (source[0] !== '.' && !path.isAbsolute(source)) || source.slice(-5) === '.json'; |
| 129 | return looksLikeExternal ? false : null; |
| 130 | } |
| 131 | }); |
| 132 | const bundle = await rollup.rollup(inputOptions); |
| 133 | const { |
| 134 | output: [{ code }] |
| 135 | } = await bundle.generate({ |
| 136 | exports: 'named', |
| 137 | format: bundleConfigAsCjs ? 'cjs' : 'es', |
| 138 | importAttributesKey: getConfigImportAttributesKey(configImportAttributesKey), |
| 139 | plugins: [ |
| 140 | { |
| 141 | name: 'transpile-import-meta', |
| 142 | resolveImportMeta(property, { moduleId }) { |
| 143 | if (property === 'url') { |
| 144 | return `'${pathToFileURL(moduleId).href}'`; |
| 145 | } |
| 146 | if (property == 'filename') { |
| 147 | return `'${moduleId}'`; |
| 148 | } |
| 149 | if (property == 'dirname') { |
| 150 | return `'${path.dirname(moduleId)}'`; |
| 151 | } |
| 152 | if (property == null) { |
| 153 | return `{url:'${pathToFileURL(moduleId).href}', filename: '${moduleId}', dirname: '${path.dirname(moduleId)}'}`; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | ] |
| 158 | }); |
| 159 | if (!silent && warnings.count > 0) { |
| 160 | stderr(bold(`loaded ${relativeId(fileName)} with warnings`)); |
| 161 | warnings.flush(); |
| 162 | } |
no test coverage detected
searching dependent graphs…