Skip to content

Commit 9acbd30

Browse files
Becca Baileydethstrobe
Becca Bailey
andauthored
Add react remapping to import resolver plugin (ampproject#37859)
* Add react remapping to import resolver plugin * Add comment * Simplify react/index exports * Add small change to trigger CI build * remap react to preact using babel when bundling for preact Co-authored-by: William H.G. Johnson <[email protected]>
1 parent bc4ee11 commit 9acbd30

File tree

8 files changed

+55
-17
lines changed

8 files changed

+55
-17
lines changed

Diff for: build-system/babel-config/import-resolver.js

+29-9
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,33 @@ function readJsconfigPaths() {
4141
return tsConfigPaths;
4242
}
4343

44+
/**
45+
* Remap external modules that rely on React if building for Preact.
46+
* @param {'preact' | 'react'} buildFor
47+
* @return {Object}
48+
*/
49+
function moduleAliases(buildFor) {
50+
if (buildFor === 'react') {
51+
return {};
52+
}
53+
return {
54+
'react': './src/react',
55+
'react-dom': './src/react/dom',
56+
};
57+
}
58+
4459
/**
4560
* Import map configuration.
61+
* @param {'preact' | 'react'} buildFor
4662
* @return {Object}
4763
*/
48-
function getImportResolver() {
64+
function getImportResolver(buildFor = 'preact') {
4965
return {
5066
root: ['.'],
51-
alias: readJsconfigPaths(),
67+
alias: {
68+
...readJsconfigPaths(),
69+
...moduleAliases(buildFor),
70+
},
5271
extensions: ['.js', '.jsx', '.ts', '.tsx'],
5372
stripExtensions: [],
5473
babelOptions: {
@@ -62,23 +81,24 @@ function getImportResolver() {
6281
/**
6382
* Produces an alias map with paths relative to the provided root.
6483
* @param {string} rootDir
84+
* @param {'preact' | 'react'} buildFor
6585
* @return {!Object<string, string>}
6686
*/
67-
function getRelativeAliasMap(rootDir) {
87+
function getRelativeAliasMap(rootDir, buildFor = 'preact') {
6888
return Object.fromEntries(
69-
Object.entries(getImportResolver().alias).map(([alias, destPath]) => [
70-
alias,
71-
path.join(rootDir, destPath),
72-
])
89+
Object.entries(getImportResolver(buildFor).alias).map(
90+
([alias, destPath]) => [alias, path.join(rootDir, destPath)]
91+
)
7392
);
7493
}
7594

7695
/**
7796
* Import resolver Babel plugin configuration.
97+
* @param {'preact' | 'react'} buildFor
7898
* @return {!Array}
7999
*/
80-
function getImportResolverPlugin() {
81-
return ['module-resolver', getImportResolver()];
100+
function getImportResolverPlugin(buildFor = 'preact') {
101+
return ['module-resolver', getImportResolver(buildFor)];
82102
}
83103

84104
/**

Diff for: build-system/babel-config/minified-config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ const {getReplacePlugin} = require('./helpers');
88
/**
99
* Gets the config for minified babel transforms run, used by 3p vendors.
1010
*
11+
* @param {'preact' | 'react'} buildFor
1112
* @return {!Object}
1213
*/
13-
function getMinifiedConfig() {
14+
function getMinifiedConfig(buildFor = 'preact') {
1415
const isProd = argv._.includes('dist') && !argv.fortesting;
1516

1617
const reactJsxPlugin = [
@@ -27,7 +28,7 @@ function getMinifiedConfig() {
2728
'optimize-objstr',
2829
'./build-system/babel-plugins/babel-plugin-mangle-object-values',
2930
'./build-system/babel-plugins/babel-plugin-jsx-style-object',
30-
getImportResolverPlugin(),
31+
getImportResolverPlugin(buildFor),
3132
argv.coverage ? 'babel-plugin-istanbul' : null,
3233
'./build-system/babel-plugins/babel-plugin-imported-helpers',
3334
'./build-system/babel-plugins/babel-plugin-transform-inline-isenumvalue',

Diff for: build-system/babel-config/react-config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ function mergeReactBabelConfig(config) {
2626
* @return {!Object}
2727
*/
2828
function getReactUnminifiedConfig() {
29-
return mergeReactBabelConfig(getUnminifiedConfig());
29+
return mergeReactBabelConfig(getUnminifiedConfig('react'));
3030
}
3131

3232
/**
3333
* @return {!Object}
3434
*/
3535
function getReactMinifiedConfig() {
36-
return mergeReactBabelConfig(getMinifiedConfig());
36+
return mergeReactBabelConfig(getMinifiedConfig('react'));
3737
}
3838

3939
module.exports = {

Diff for: build-system/babel-config/unminified-config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ const {getReplacePlugin} = require('./helpers');
77
/**
88
* Gets the config for babel transforms run during `amp build`.
99
*
10+
* @param {'preact' | 'react'} buildFor
1011
* @return {!Object}
1112
*/
12-
function getUnminifiedConfig() {
13+
function getUnminifiedConfig(buildFor = 'preact') {
1314
const reactJsxPlugin = [
1415
'@babel/plugin-transform-react-jsx',
1516
{
@@ -38,7 +39,7 @@ function getUnminifiedConfig() {
3839
const replacePlugin = getReplacePlugin();
3940
const unminifiedPlugins = [
4041
'./build-system/babel-plugins/babel-plugin-jsx-style-object',
41-
getImportResolverPlugin(),
42+
getImportResolverPlugin(buildFor),
4243
argv.coverage ? 'babel-plugin-istanbul' : null,
4344
replacePlugin,
4445
'./build-system/babel-plugins/babel-plugin-transform-json-import',

Diff for: build-system/tasks/storybook/env/preact/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ module.exports = ({config}) => {
2121
modules,
2222
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
2323
alias: {
24+
...getRelativeAliasMap(rootDir),
2425
'react': 'preact/compat',
2526
'react-dom': 'preact/compat',
26-
...getRelativeAliasMap(rootDir),
2727
},
2828
};
2929
config.module = {

Diff for: build-system/tasks/storybook/env/react/webpack.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ module.exports = ({config}) => {
7272
plugins: [new ReactBuildImportResolver()],
7373
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
7474
alias: {
75-
...getRelativeAliasMap(rootDir),
75+
...getRelativeAliasMap(rootDir, 'react'),
7676
// Alias preact to react
7777
'preact/dom': 'react-dom',
7878
'preact/hooks': 'react',

Diff for: src/react/dom.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {hydrate, render} from '#preact';
2+
3+
export {hydrate, render};
4+
5+
export default {hydrate, render};

Diff for: src/react/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as Preact from '#preact';
2+
import * as PreactCompat from '#preact/compat';
3+
4+
export * from '#preact';
5+
export * from '#preact/compat';
6+
7+
// This file allows us to remap react imports from external libraries to
8+
// our internal preact exports. This file uses a default export in order to
9+
// be compatible with libraries that use react default import syntax.
10+
11+
export default {...Preact, ...PreactCompat};

0 commit comments

Comments
 (0)