-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,6 +408,33 @@ function shouldSkipBundle(bundle, bundleType) { | |
return false; | ||
} | ||
|
||
// Strip unused require() statements for pure externals modules from the bundle. | ||
// Rollup's treeshake option should do this, but it doesn't work. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
TrySound
Contributor
|
||
function stripNoSideEffectImports(bundleType, pureExternalModules, filePath) { | ||
const isProduction = isProductionBundleType(bundleType); | ||
|
||
// Dead code elimintation is only applied to production bundles. | ||
if (!isProduction) { | ||
return; | ||
} | ||
|
||
const codeIn = fs.readFileSync(filePath, 'utf-8'); | ||
let codeOut = codeIn; | ||
|
||
pureExternalModules.forEach(module => { | ||
const regExp = new RegExp( | ||
`(?<!= *)require\\(["']${module}["']\\)[,;]`, | ||
'g' | ||
); | ||
|
||
codeOut = codeOut.replace(regExp, ''); | ||
}); | ||
|
||
if (codeIn !== codeOut) { | ||
fs.writeFileSync(filePath, codeOut, 'utf-8'); | ||
} | ||
} | ||
|
||
async function createBundle(bundle, bundleType) { | ||
if (shouldSkipBundle(bundle, bundleType)) { | ||
return; | ||
|
@@ -493,6 +520,9 @@ async function createBundle(bundle, bundleType) { | |
try { | ||
const result = await rollup(rollupConfig); | ||
await result.write(rollupOutputOptions); | ||
|
||
// HACK to work around the fact that Rollup isn't removing unused, pure-module imports. | ||
stripNoSideEffectImports(bundleType, pureExternalModules, mainOutputPath); | ||
} catch (error) { | ||
console.log(`${chalk.bgRed.black(' OH NOES! ')} ${logKey}\n`); | ||
handleRollupError(error); | ||
|
Have we figured out why? It might be that we need to update Rollup to get some upstream bugfix. Or maybe there's something simple we can fix ourselves and send it to them.