Skip to content

Commit c1636dd

Browse files
committed
test: add tests for entry level useBabelConfig
1 parent a808b7f commit c1636dd

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

packages/scripts/__tests__/config/CreateWebpackConfig.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import webpack from 'webpack';
12
import webpackMerge from 'webpack-merge';
23
import { CreateWebpackConfig } from '../../src/config/CreateWebpackConfig';
34
import { ProjectConfig } from '../../src/config/project.config.default';
45
import { initConfig, projectConfig, serverConfig } from '../helpers/testConfig';
6+
import { findWpackIoBabelOnTJs } from '../helpers/testUtils';
57

68
jest.mock('webpack-merge');
79
((webpackMerge as unknown) as jest.Mock).mockImplementation(() => ({
@@ -23,6 +25,62 @@ describe('CreateWebpackConfig', () => {
2325
expect(cwc).not.toBeFalsy();
2426
});
2527

28+
test('entry level useBabelConfig overrides project level config', () => {
29+
const newProjectConfig = { ...projectConfig };
30+
newProjectConfig.files = [
31+
{
32+
name: 'config1',
33+
entry: { foo: 'bar.js', biz: ['baz.js'] },
34+
useBabelConfig: false,
35+
},
36+
];
37+
newProjectConfig.useBabelConfig = true;
38+
const config = new CreateWebpackConfig(
39+
newProjectConfig,
40+
serverConfig,
41+
'/foo/bar',
42+
true
43+
).getWebpackConfig() as webpack.Configuration;
44+
const module = config.module;
45+
if (Array.isArray(module.rules)) {
46+
const jsTsRules = findWpackIoBabelOnTJs(module);
47+
expect(jsTsRules).toHaveLength(2);
48+
jsTsRules.forEach(rule => {
49+
if (rule && rule.use && rule.use[0].options) {
50+
expect(rule.use[0].options).toHaveProperty('babelrc', false);
51+
expect(rule.use[0].options).toHaveProperty('configFile', false);
52+
} else {
53+
throw new Error('JavaScript rule is undefined');
54+
}
55+
});
56+
} else {
57+
throw new Error('Invalid module.rules');
58+
}
59+
newProjectConfig.files[0].useBabelConfig = true;
60+
newProjectConfig.useBabelConfig = false;
61+
const config2 = new CreateWebpackConfig(
62+
newProjectConfig,
63+
serverConfig,
64+
'/foo/bar',
65+
true
66+
).getWebpackConfig() as webpack.Configuration;
67+
const module2 = config2.module;
68+
if (Array.isArray(module2.rules)) {
69+
const jsTsRules = findWpackIoBabelOnTJs(module2);
70+
expect(jsTsRules).toHaveLength(2);
71+
jsTsRules.forEach(rule => {
72+
if (rule && rule.use && rule.use[0].options) {
73+
expect(rule.use[0].options).not.toHaveProperty('babelrc', false);
74+
expect(rule.use[0].options).not.toHaveProperty('configFile', false);
75+
} else {
76+
throw new Error('JavaScript rule is undefined');
77+
}
78+
});
79+
} else {
80+
throw new Error('Invalid module.rules');
81+
}
82+
});
83+
2684
describe('getWebpackConfig & isMultiCompiler', () => {
2785
test('works with single-compiler mode', () => {
2886
const multiProjectConfig: ProjectConfig = {

packages/scripts/__tests__/helpers/testConfig.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
serverConfigDefault,
88
} from '../../src/config/server.config.default';
99
// Create separate configuration for easy use within every test
10+
// eslint-disable-next-line import/no-mutable-exports
1011
export let projectConfig: ProjectConfig;
12+
// eslint-disable-next-line import/no-mutable-exports
1113
export let serverConfig: ServerConfig;
1214
export const initConfig = (): void => {
1315
projectConfig = {

site/docs/apis/project-configuration.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ module.exports = {
4646
optimizeForGutenberg: false,
4747
// Extra webpack config to be passed directly
4848
webpackConfig: undefined,
49+
// override project `useBabelConfig` for this entry
50+
useBabelConfig: undefined,
4951
},
5052
// If has more length, then multi-compiler
5153
{
@@ -100,6 +102,13 @@ module.exports = {
100102
...defaults,
101103
plugins: ['react-hot-loader/babel'],
102104
}),
105+
// configure how node_modules is compiled
106+
compileNodeModules: {
107+
// compile node_modules in development
108+
dev: true,
109+
// compile node_modules in production
110+
prod: true,
111+
},
103112
// Files that you want to copy to your ultimate theme/plugin package
104113
// Supports glob matching from minimatch
105114
// @link <https://github.com/isaacs/minimatch#usage>
@@ -147,13 +156,13 @@ A configuration object for banner put above all minified code.
147156

148157
It has the following properties:
149158

150-
- `name` (`string`): Name of application.
151-
- `author` (`string`): Author of application.
152-
- `version` (`string`): Version of application.
153-
- `link` (`string`): Homepage link of application.
154-
- `license` (`string`): License of application.
155-
- `copyrightText` (`string`): Additional copyright text.
156-
- `credit` (`boolean`): Whether to give wpackio a little credit ❤️.
159+
- `name` (`string`): Name of application.
160+
- `author` (`string`): Author of application.
161+
- `version` (`string`): Version of application.
162+
- `link` (`string`): Homepage link of application.
163+
- `license` (`string`): License of application.
164+
- `copyrightText` (`string`): Additional copyright text.
165+
- `credit` (`boolean`): Whether to give wpackio a little credit ❤️.
157166

158167
## `files` (`Array`)
159168

0 commit comments

Comments
 (0)