Skip to content

Commit e55528d

Browse files
committed
fix: module issue with plugin-transform-runtime
It makes babel assume every file is module. So set source to unambiguous. See #479
1 parent 16feb15 commit e55528d

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

packages/babel-preset-base/@types/babel-helper-plugin-utils.d.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@ declare module '@babel/helper-plugin-utils' {
33
// I have written all these under `__mocks__/@babel/helper-plugin-utils.ts`
44
// Is there any way to reuse it?
55
namespace helper {
6-
type assertversion = (v:number) => boolean;
6+
type assertversion = (v: number) => boolean;
77
interface Api {
8-
assertVersion:assertversion;
8+
assertVersion: assertversion;
99
}
10-
type Options = { [x: string]: string | Options[] | Options };
11-
type declareHandler = (api: Api, opts: Options, dirname?: string) => any;
12-
function declare(builder:declareHandler) : (api:Api, options:Options, dirname?:string) => any;
10+
interface Options {
11+
[x: string]: string | Options[] | Options;
12+
}
13+
type declareHandler = (
14+
api: Api,
15+
opts: Options,
16+
dirname?: string
17+
) => any;
18+
function declare(
19+
builder: declareHandler
20+
): (api: Api, options: Options, dirname?: string) => any;
1321
}
1422
export = helper;
1523
}

packages/babel-preset-base/src/preset.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ export type babelPresetConfiguration = {
2929
export type babelPreset = [string] | [string, babelPresetConfiguration];
3030

3131
export const preset = (opts: PresetOptions | null = {}) => {
32+
// from CRA
33+
// This is similar to how `env` works in Babel:
34+
// https://babeljs.io/docs/usage/babelrc/#env-option
35+
// We are not using `env` because it’s ignored in versions > [email protected]:
36+
// https://github.com/babel/babel/issues/4539
37+
// https://github.com/facebook/create-react-app/issues/720
38+
// It’s also nice that we can enforce `NODE_ENV` being specified.
39+
const env = process.env.BABEL_ENV || process.env.NODE_ENV;
3240
// Extract this preset specific options and pass the rest to @babel/preset-env
3341
const {
3442
presetEnv = {},
@@ -55,7 +63,7 @@ export const preset = (opts: PresetOptions | null = {}) => {
5563
// Put development based on BABEL_ENV
5664
// Adds component stack to warning messages
5765
// Adds __self attribute to JSX which React will use for some warnings
58-
development: process.env.BABEL_ENV !== 'production',
66+
development: env !== 'production',
5967
// Will use the native built-in instead of trying to polyfill
6068
// behavior for any plugins that require one.
6169
useBuiltIns: true,
@@ -81,7 +89,8 @@ export const preset = (opts: PresetOptions | null = {}) => {
8189
corejs: false,
8290
helpers: true,
8391
regenerator: true,
84-
useESModules: true,
92+
// We might wanna turn it on once node LTS has ESModules support
93+
useESModules: env !== 'test',
8594
},
8695
],
8796
};

packages/scripts/__tests__/config/__snapshots__/WebpackConfigHelper.spec.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Object {
4444
],
4545
],
4646
"sourceMaps": false,
47+
"sourceType": "unambiguous",
4748
}
4849
`;
4950

@@ -137,6 +138,7 @@ Object {
137138
],
138139
],
139140
"sourceMaps": false,
141+
"sourceType": "unambiguous",
140142
},
141143
},
142144
],

packages/scripts/src/config/WebpackConfigHelper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,11 @@ ${bannerConfig.copyrightText}${bannerConfig.credit ? creditNote : ''}`,
494494
options: {
495495
// cache
496496
...babelLoaderCacheOptions,
497+
// Babel assumes ES Modules, which isn't safe until CommonJS
498+
// dies. This changes the behavior to assume CommonJS unless
499+
// an `import` or `export` is present in the file.
500+
// https://github.com/webpack/webpack/issues/4039#issuecomment-419284940
501+
sourceType: 'unambiguous',
497502
// preset from our own package
498503
presets: getBabelPresets({ hasReact: false }),
499504
// If an error happens in a package, it's possible to be

0 commit comments

Comments
 (0)