| 109 | } |
| 110 | |
| 111 | export function bundleExternal(opts: BuildOptions, outputDir: string, cachedDir: string, entryFileName: string) { |
| 112 | return new Promise<void>(async (resolveBundle, rejectBundle) => { |
| 113 | const outputFile = path.join(outputDir, entryFileName); |
| 114 | const cachedFile = path.join(cachedDir, entryFileName) + (opts.isProd ? '.min.js' : ''); |
| 115 | |
| 116 | const cachedExists = fs.existsSync(cachedFile); |
| 117 | if (cachedExists) { |
| 118 | await fs.copyFile(cachedFile, outputFile); |
| 119 | resolveBundle(); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | const whitelist = new Set(['child_process', 'os', 'typescript']); |
| 124 | const webpackConfig: Configuration = { |
| 125 | entry: path.join(opts.srcDir, 'sys', 'node', 'bundles', entryFileName), |
| 126 | output: { |
| 127 | path: outputDir, |
| 128 | filename: entryFileName, |
| 129 | libraryTarget: 'commonjs', |
| 130 | }, |
| 131 | target: 'node', |
| 132 | node: { |
| 133 | __dirname: false, |
| 134 | __filename: false, |
| 135 | }, |
| 136 | externals(data, callback) { |
| 137 | const { request } = data; |
| 138 | |
| 139 | if (request?.match(/^(\.{0,2})\//)) { |
| 140 | // absolute and relative paths are not externals |
| 141 | return callback(null, undefined); |
| 142 | } |
| 143 | |
| 144 | if (request === '@stencil/core/mock-doc') { |
| 145 | return callback(null, '../../mock-doc'); |
| 146 | } |
| 147 | |
| 148 | if (typeof request === 'string' && whitelist.has(request)) { |
| 149 | // we specifically do not want to bundle these imports |
| 150 | resolve.sync(request); |
| 151 | return callback(null, request); |
| 152 | } |
| 153 | |
| 154 | // bundle this import |
| 155 | callback(undefined, undefined); |
| 156 | }, |
| 157 | resolve: { |
| 158 | alias: { |
| 159 | '@utils': path.join(opts.buildDir, 'utils', 'index.js'), |
| 160 | postcss: path.join(opts.nodeModulesDir, 'postcss'), |
| 161 | 'source-map': path.join(opts.nodeModulesDir, 'source-map'), |
| 162 | chalk: path.join(opts.bundleHelpersDir, 'empty.js'), |
| 163 | }, |
| 164 | }, |
| 165 | optimization: { |
| 166 | minimize: false, |
| 167 | }, |
| 168 | mode: 'production', |