-
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.
refactor: use babel in importComponent webpack loader
In case the `--experimental-esbuild` mode is used, we have a small webpack loader to handle the `importComponent` transformation. This had been introduced in #1632 to be a quick & dirty regex based replacer. With this commit we now have a webpack loader that has a fast-exit path in case `importComponent` is not used. Otherwise it will transpile the source code using the proper babel plugin. Co-authored-by: Markus Wolf <markus.wolf@new-work.se> Co-authored-by: Philipp Hinrichsen <philipp.hinrichsen@new-work.se> Co-authored-by: Robert Kowalski <robert.kowalski@new-work.se>
- Loading branch information
1 parent
7c834c8
commit c6571ad
Showing
6 changed files
with
48 additions
and
12 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
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 |
---|---|---|
@@ -1,17 +1,27 @@ | ||
// eslint-disable-next-line node/no-extraneous-require | ||
const { createTransformer } = require('esbuild-jest'); | ||
const { transformSync } = require('@babel/core'); | ||
|
||
const transformer = createTransformer({ | ||
sourcemap: true, | ||
}); | ||
|
||
const regex = /importComponent/g; | ||
|
||
module.exports = { | ||
process(content, filename, config, opts) { | ||
content = content.replace( | ||
/importComponent\s*\(\s*\(\)\s+=>\s+import\(\s*'([^']+)'\s*\)\s*\)/g, | ||
"importComponent({ component: require('$1') })" | ||
); | ||
if (!regex.test(content)) { | ||
return transformer.process(content, filename, config, opts); | ||
} | ||
|
||
const result = transformSync(content, { | ||
plugins: [ | ||
require.resolve('../helpers/babel-plugin-import-component'), | ||
require.resolve('@babel/plugin-syntax-jsx'), | ||
], | ||
filename, | ||
}); | ||
|
||
return transformer.process(content, filename, config, opts); | ||
return transformer.process(result.code, filename, config, opts); | ||
}, | ||
}; |
31 changes: 26 additions & 5 deletions
31
packages/react/import-component/import-component-loader.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
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
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