/
index.js
192 lines (179 loc) · 6.72 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { isBabel7 } from '@codesandbox/common/lib/utils/is-babel-7';
import Preset from '../';
import stylesTranspiler from '../../transpilers/style';
import babelTranspiler from '../../transpilers/babel';
import jsonTranspiler from '../../transpilers/json';
import rawTranspiler from '../../transpilers/raw';
import svgrTranspiler from '../../transpilers/svgr';
import sassTranspiler from '../../transpilers/sass';
export default function initialize() {
let v2Initialized = false;
const preset = new Preset(
'create-react-app',
['web.js', 'js', 'json', 'web.jsx', 'jsx', 'ts', 'tsx'],
{
// Directly match react-native to react-native-web.
// Attempt to use react-native internals shouldn't work on web.
'react-native$': 'react-native-web',
// Alias core react-native internals to react-native-web equivalents
'react-native/Libraries/EventEmitter/RCTDeviceEventEmitter$':
'react-native-web/dist/vendor/react-native/NativeEventEmitter/RCTDeviceEventEmitter',
'react-native/Libraries/vendor/emitter/EventEmitter$':
'react-native-web/dist/vendor/react-native/emitter/EventEmitter',
'react-native/Libraries/vendor/emitter/EventSubscriptionVendor$':
'react-native-web/dist/vendor/react-native/emitter/EventSubscriptionVendor',
'react-native/Libraries/EventEmitter/NativeEventEmitter$':
'react-native-web/dist/vendor/react-native/NativeEventEmitter',
// Alias core react-native asset management internals to unimodule equivalents.
'react-native/Libraries/Image/AssetSourceResolver$':
'expo-asset/build/AssetSourceResolver',
'react-native/Libraries/Image/assetPathUtils$':
'expo-asset/build/Image/assetPathUtils',
'react-native/Libraries/Image/resolveAssetSource$':
'expo-asset/build/resolveAssetSource',
},
{
hasDotEnv: true,
setup: manager => {
const configurations = manager.configurations;
if (
isBabel7(
configurations &&
configurations.package &&
configurations.package.parsed &&
configurations.package.parsed.dependencies,
configurations &&
configurations.package &&
configurations.package.parsed &&
configurations.package.parsed.devDependencies
) &&
!v2Initialized
) {
const babelOptions = {
isV7: true,
compileNodeModulesWithEnv: true,
config: {
plugins: [
'transform-flow-strip-types',
'transform-destructuring',
'babel-plugin-macros',
['proposal-class-properties', { loose: true }],
['proposal-object-rest-spread', { useBuiltIns: true }],
[
'transform-runtime',
{
corejs: false,
helpers: true,
regenerator: true,
},
],
'syntax-dynamic-import',
],
presets: [
[
'env',
{
// We want Create React App to be IE 9 compatible until React itself
// no longer works with IE 9
targets: {
ie: 9,
},
// Users cannot override this behavior because this Babel
// configuration is highly tuned for ES5 support
ignoreBrowserslistConfig: true,
// If users import all core-js they're probably not concerned with
// bundle size. We shouldn't rely on magic to try and shrink it.
useBuiltIns: false,
// Do not transform modules to CJS
modules: false,
},
],
'react',
'typescript',
],
},
};
preset.registerTranspiler(
module =>
/\.(t|j)sx?$/.test(module.path) && !module.path.endsWith('.d.ts'),
[
{
transpiler: babelTranspiler,
options: babelOptions,
},
],
true
);
preset.registerTranspiler(
module => /\.svg$/.test(module.path),
[
{ transpiler: svgrTranspiler },
{
transpiler: babelTranspiler,
options: babelOptions,
},
],
true
);
preset.registerTranspiler(
module => /\.s[c|a]ss$/.test(module.path),
[{ transpiler: sassTranspiler }, { transpiler: stylesTranspiler }],
true
);
preset.registerTranspiler(
module => /\.module\.s[c|a]ss$/.test(module.path),
[
{ transpiler: sassTranspiler },
{ transpiler: stylesTranspiler, options: { module: true } },
],
true
);
preset.registerTranspiler(
module => /\.module\.css$/.test(module.path),
[{ transpiler: stylesTranspiler, options: { module: true } }],
true
);
v2Initialized = true;
}
},
preEvaluate: manager => {
if (!manager.webpackHMR) {
try {
const children = document.body.children;
// Do unmounting for react
if (
manager.manifest &&
manager.manifest.dependencies.find(n => n.name === 'react-dom')
) {
const reactDOMModule = manager.resolveModule('react-dom', '');
const reactDOM = manager.evaluateModule(reactDOMModule);
reactDOM.unmountComponentAtNode(document.body);
for (let i = 0; i < children.length; i += 1) {
if (children[i].tagName === 'DIV') {
reactDOM.unmountComponentAtNode(children[i]);
}
}
}
} catch (e) {
/* don't do anything with this error */
if (process.env.NODE_ENV === 'development') {
console.error('Problem while cleaning up');
console.error(e);
}
}
}
},
}
);
preset.registerTranspiler(module => /\.css$/.test(module.path), [
{ transpiler: stylesTranspiler },
]);
preset.registerTranspiler(module => /\.jsx?$/.test(module.path), [
{ transpiler: babelTranspiler },
]);
preset.registerTranspiler(module => /\.json$/.test(module.path), [
{ transpiler: jsonTranspiler },
]);
preset.registerTranspiler(() => true, [{ transpiler: rawTranspiler }]);
return preset;
}