-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(webpack): introduce experimental esbuild transpilation
- Loading branch information
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/webpack/mixins/esbuild/importComponentTransform.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const regex = /importComponent\s*\(\s*\(\)\s+=>\s+import\(\s*'([^']+)'\s*\)\s*\)/g; | ||
|
||
function transformImportComponent(source) { | ||
return source.replace( | ||
regex, | ||
"importComponent({ load: () => import('$1'), moduleId: require.resolveWeak('$1') })" | ||
); | ||
} | ||
|
||
module.exports = transformImportComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const { Mixin } = require('hops-mixin'); | ||
const importComponentTransform = require.resolve( | ||
'./importComponentTransform.js' | ||
); | ||
|
||
const isLoaderFor = (file) => (test) => { | ||
if (Array.isArray(test)) { | ||
return test.some(isLoaderFor(file)); | ||
} else if (typeof test === 'object' && typeof test.test === 'function') { | ||
return test.test(file); | ||
} else { | ||
return false; | ||
} | ||
}; | ||
const isTsLoader = isLoaderFor('test.ts'); | ||
const isJsLoader = isLoaderFor('test.js'); | ||
|
||
class HopsEsbuildMixin extends Mixin { | ||
configureBuild({ optimization, plugins }, { allLoaderConfigs }, target) { | ||
const { experimentalEsbuild } = this.options; | ||
|
||
if (!experimentalEsbuild) { | ||
return; | ||
} | ||
|
||
const esbuildTarget = target === 'node' ? 'node12' : 'es2015'; | ||
// NOTE: We don't want to ship esbuild, so we just assume it is installed | ||
// when someone wants to use it. | ||
// eslint-disable-next-line node/no-missing-require | ||
const { ESBuildPlugin, ESBuildMinifyPlugin } = require('esbuild-loader'); | ||
|
||
// NOTE: We iterate all configs as we want to replace the entry instead of | ||
// mutating the object, otherwise we would need to explicitly remove all | ||
// other properties on the config object that are not related to esbuild. | ||
for (let index = 0; index < allLoaderConfigs.length; index++) { | ||
const { test, include, exclude } = allLoaderConfigs[index]; | ||
const esbuildLoader = isTsLoader(test) | ||
? 'tsx' | ||
: isJsLoader(test) | ||
? 'jsx' | ||
: null; | ||
|
||
if (esbuildLoader) { | ||
allLoaderConfigs[index] = { | ||
test, | ||
include, | ||
exclude, | ||
use: [ | ||
{ | ||
loader: 'esbuild-loader', | ||
options: { | ||
loader: esbuildLoader, | ||
target: esbuildTarget, | ||
}, | ||
}, | ||
importComponentTransform, | ||
], | ||
}; | ||
} | ||
} | ||
|
||
// NOTE: We don't decide here whether to minify or not but instead just | ||
// override it if someone else has already configured it. | ||
if ( | ||
Array.isArray(optimization.minimizer) && | ||
optimization.minimizer.length > 0 | ||
) { | ||
optimization.minimizer = [ | ||
new ESBuildMinifyPlugin({ | ||
target: esbuildTarget, | ||
minify: true, | ||
sourcemap: true, | ||
}), | ||
]; | ||
} | ||
|
||
plugins.push(new ESBuildPlugin()); | ||
} | ||
|
||
handleArguments(argv) { | ||
this.options = { ...this.options, ...argv }; | ||
} | ||
} | ||
|
||
module.exports = HopsEsbuildMixin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters