Skip to content

Commit 345bb52

Browse files
author
Harrison Ifeanyichukwu
committed
feat: support react 17 automatic runtime flag
1 parent 97e7653 commit 345bb52

File tree

4 files changed

+931
-652
lines changed

4 files changed

+931
-652
lines changed

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,27 @@
6666
}
6767
},
6868
"dependencies": {
69-
"@babel/core": "7.9.6",
69+
"@babel/core": "^7.12.17",
7070
"@babel/plugin-proposal-class-properties": "^7.4.0",
7171
"@babel/plugin-proposal-nullish-coalescing-operator": "7.8.3",
7272
"@babel/plugin-proposal-object-rest-spread": "^7.4.0",
7373
"@babel/plugin-proposal-optional-chaining": "7.9.0",
7474
"@babel/plugin-transform-runtime": "7.9.6",
75-
"@babel/preset-env": "7.9.6",
76-
"@babel/preset-react": "^7.10.4",
77-
"@babel/preset-typescript": "7.9.0",
75+
"@babel/preset-env": "^7.12.17",
76+
"@babel/preset-react": "^7.12.13",
77+
"@babel/preset-typescript": "^7.12.17",
7878
"@babel/runtime": "7.9.6",
79-
"@rollup/plugin-babel": "5.0.4",
80-
"@rollup/plugin-commonjs": "12.0.0",
81-
"@rollup/plugin-json": "4.0.3",
82-
"@rollup/plugin-node-resolve": "8.0.0",
79+
"@rollup/plugin-babel": "^5.3.0",
80+
"@rollup/plugin-commonjs": "^17.1.0",
81+
"@rollup/plugin-json": "^4.1.0",
82+
"@rollup/plugin-node-resolve": "^11.2.0",
8383
"@teclone/node-utils": "1.0.5",
8484
"args": "5.0.1",
8585
"chalk": "4.0.0",
8686
"glob-to-regexp": "0.4.1",
87-
"rollup": "2.10.5",
88-
"rollup-plugin-preserve-shebang": "1.0.1",
89-
"rollup-plugin-terser": "5.3.0"
87+
"rollup": "^2.39.0",
88+
"rollup-plugin-preserve-shebang": "^1.0.1",
89+
"rollup-plugin-terser": "^7.0.2"
9090
},
9191
"peerDependencies": {
9292
"@teclone/utils": "^2.20.1"

src/modules/Bundler.ts

Lines changed: 75 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ class Bundler {
3333

3434
private bundlerOptions: BundlerOptions;
3535

36-
constructor(generalConfig: GeneralConfig = {}, bundlerOptions: BundlerOptions) {
36+
constructor(
37+
generalConfig: GeneralConfig = {},
38+
bundlerOptions: BundlerOptions
39+
) {
3740
this.entryPath = getEntryPath();
3841
this.generalConfig = generalConfig;
3942

40-
this.config = this.resolveConfig(this.entryPath, generalConfig.config ?? {});
43+
this.config = this.resolveConfig(
44+
this.entryPath,
45+
generalConfig.config ?? {}
46+
);
4147

4248
this.bundlerOptions = bundlerOptions;
4349
}
@@ -49,7 +55,11 @@ class Bundler {
4955
*/
5056
private resolveRegex(pattern: string | RegExp) {
5157
if (isString(pattern)) {
52-
return globToRegex(pattern, { extended: true, globstar: true, flags: 'i' });
58+
return globToRegex(pattern, {
59+
extended: true,
60+
globstar: true,
61+
flags: 'i',
62+
});
5363
} else {
5464
return pattern;
5565
}
@@ -58,7 +68,7 @@ class Bundler {
5868
private mergeConfig(
5969
prop: keyof CommonConfig,
6070
config: Config,
61-
target: CJSConfig | ESMConfig | DistConfig,
71+
target: CJSConfig | ESMConfig | DistConfig
6272
) {
6373
const configValue = config[prop];
6474
const targetValue = target[prop];
@@ -131,7 +141,7 @@ class Bundler {
131141
if (err) {
132142
reject(err);
133143
} else {
134-
resolve();
144+
resolve(null);
135145
}
136146
});
137147
});
@@ -152,7 +162,7 @@ class Bundler {
152162
entryFile: string,
153163
moduleName: string,
154164
currentRelativeDir: string,
155-
extensions: string[],
165+
extensions: string[]
156166
): Promise<Module[]> {
157167
return new Promise((resolve, reject) => {
158168
fs.readdir(resolvedPath, (err, files) => {
@@ -171,25 +181,32 @@ class Bundler {
171181
entryFile,
172182
moduleName,
173183
path.join(currentRelativeDir, file),
174-
extensions,
175-
),
184+
extensions
185+
)
176186
);
177187
} else {
178188
const firstDotPos =
179189
file.charAt(0) === '.' ? file.indexOf('.', 1) : file.indexOf('.');
180-
const baseName = firstDotPos > -1 ? file.substring(0, firstDotPos) : file;
190+
const baseName =
191+
firstDotPos > -1 ? file.substring(0, firstDotPos) : file;
181192
const extname = firstDotPos > -1 ? file.substring(firstDotPos) : '';
182193

183194
const oldRelativePath = path.join(currentRelativeDir, file);
184-
const newRelativePath = path.join(currentRelativeDir, baseName + '.js');
195+
const newRelativePath = path.join(
196+
currentRelativeDir,
197+
baseName + '.js'
198+
);
185199

186200
modules.push({
187201
id: modules.length + 1,
188202
ext: extname,
189203
oldRelativePath,
190204
newRelativePath,
191205
filePath,
192-
name: oldRelativePath === entryFile ? moduleName : camelCase(baseName),
206+
name:
207+
oldRelativePath === entryFile
208+
? moduleName
209+
: camelCase(baseName),
193210
isBuildFile: extensions.includes(extname),
194211
});
195212
}
@@ -207,7 +224,7 @@ class Bundler {
207224
private getModulesFiles(
208225
modules: Module[],
209226
config: Config,
210-
buildConfig: CJSConfig | ESMConfig | DistConfig,
227+
buildConfig: CJSConfig | ESMConfig | DistConfig
211228
): ModuleFiles {
212229
const result: ModuleFiles = {
213230
assetFiles: [],
@@ -232,8 +249,10 @@ class Bundler {
232249
result.assetFiles.push(current);
233250
} else if (
234251
isBuildFile &&
235-
(buildConfig.include.length === 0 || buildConfig.include.some(regexMatches)) &&
236-
(buildConfig.exclude.length === 0 || !buildConfig.exclude.some(regexMatches))
252+
(buildConfig.include.length === 0 ||
253+
buildConfig.include.some(regexMatches)) &&
254+
(buildConfig.exclude.length === 0 ||
255+
!buildConfig.exclude.some(regexMatches))
237256
) {
238257
result.buildFiles.push(current);
239258
}
@@ -250,7 +269,7 @@ class Bundler {
250269
runBuild(
251270
modules: Module[],
252271
mainConfig: Config,
253-
config: DistConfig | CJSConfig | ESMConfig,
272+
config: DistConfig | CJSConfig | ESMConfig
254273
) {
255274
const moduleFiles = this.getModulesFiles(modules, mainConfig, config);
256275
const promises: Array<Promise<any>> = [];
@@ -264,58 +283,63 @@ class Bundler {
264283
? config.externals
265284
: allExternal;
266285

267-
// const onWarn = (warning, warn) => {
268-
// console.log(warning.message);
269-
// warn(warning);
270-
// };
271-
272286
const onError = (ex) => {
273287
console.error(ex?.message || ex);
274288
};
275289

276-
buildFiles.forEach(({ filePath, newRelativePath, oldRelativePath, name }) => {
277-
const out = path.resolve(this.entryPath, config.outDir, newRelativePath);
278-
promises.push(
279-
rollup({
280-
input: filePath,
281-
plugins,
282-
external,
283-
onwarn: (warning, warn) => console.log(warning.message, filePath),
284-
})
285-
.then((bundler) =>
286-
bundler.write({
287-
file: out,
288-
format: config.format,
289-
interop: config.interop,
290-
sourcemap: config.sourcemap,
291-
name,
292-
}),
293-
)
294-
.then(() => {
295-
if (this.bundlerOptions.generateOutputLogs) {
296-
log(chalk.green(`${oldRelativePath} ... ${out} \n`));
297-
}
298-
return null;
290+
buildFiles.forEach(
291+
({ filePath, newRelativePath, oldRelativePath, name }) => {
292+
const out = path.resolve(
293+
this.entryPath,
294+
config.outDir,
295+
newRelativePath
296+
);
297+
promises.push(
298+
rollup({
299+
input: filePath,
300+
plugins,
301+
external,
302+
onwarn: (warning, warn) => console.log(warning.message, filePath),
299303
})
300-
.catch(onError),
301-
);
302-
});
304+
.then((bundler) =>
305+
bundler.write({
306+
file: out,
307+
format: config.format,
308+
interop: config.interop,
309+
sourcemap: config.sourcemap,
310+
name,
311+
})
312+
)
313+
.then(() => {
314+
if (this.bundlerOptions.generateOutputLogs) {
315+
log(chalk.green(`${oldRelativePath} ... ${out} \n`));
316+
}
317+
return null;
318+
})
319+
.catch(onError)
320+
);
321+
}
322+
);
303323

304324
assetFiles.forEach((assetFile) => {
305325
promises.push(
306326
this.copyFile(
307327
assetFile.filePath,
308-
path.resolve(this.entryPath, config.outDir, assetFile.oldRelativePath),
309-
),
328+
path.resolve(this.entryPath, config.outDir, assetFile.oldRelativePath)
329+
)
310330
);
311331
});
312332

313333
typeDefinitionFiles.forEach((typeDefinitionFile) => {
314334
promises.push(
315335
this.copyFile(
316336
typeDefinitionFile.filePath,
317-
path.resolve(this.entryPath, config.outDir, typeDefinitionFile.oldRelativePath),
318-
),
337+
path.resolve(
338+
this.entryPath,
339+
config.outDir,
340+
typeDefinitionFile.oldRelativePath
341+
)
342+
)
319343
);
320344
});
321345

@@ -335,7 +359,7 @@ class Bundler {
335359
config.entryFile,
336360
config.moduleName,
337361
'',
338-
config.extensions,
362+
config.extensions
339363
);
340364

341365
//run cjs build

src/modules/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export const getBabelPresets = (
111111
resolveDependency(internalNodeModulesDir, '@babel/preset-react'),
112112
{
113113
development: process.env.BABEL_ENV === 'development',
114+
runtime: 'automatic',
114115
},
115116
],
116117

0 commit comments

Comments
 (0)