Skip to content

Commit

Permalink
Replace merge.smart with a simple merge.
Browse files Browse the repository at this point in the history
It's not a direct replacement, however this may cover all the needs. If
needed, we can work on the merge strategy.

Jest has a file system cache that is not cleared, not even using
`jest.resetModules()`. As a workaround, we create the webpack config
files with a unique suffix.

Ref:
jestjs/jest#11426 (comment)
  • Loading branch information
bpinto committed Aug 18, 2021
1 parent 20d4350 commit 3437b26
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 21 deletions.
4 changes: 2 additions & 2 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var fs = require('fs');
var path = require('path');
var conf = require('./config');
var webpack = require('webpack');
var merge = require('webpack-merge');
const { merge } = require('webpack-merge');
const findUp = require('find-up');

const readdir = util.promisify(fs.readdir);
Expand Down Expand Up @@ -198,7 +198,7 @@ async function webpackConfig(
if (userWebpackConfig) {
var webpackAdditional = require(path.join(cwd, userWebpackConfig));

return merge.smart(webpackConfig, webpackAdditional);
return merge(webpackConfig, webpackAdditional);
}

return webpackConfig;
Expand Down
68 changes: 49 additions & 19 deletions lib/build.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,58 @@ describe('build', () => {
).rejects.toThrow('Cannot find module');
});

it('should merge webpack custom config', async () => {
const script = `module.exports = () => console.log("hello world")`;
await writeFileInFunctions(script, 'index.js');
describe('webpack custom config merge', () => {
it('should merge plugins', async () => {
const script = `module.exports = () => console.log("hello world")`;
await writeFileInFunctions(script, 'index.js');

const webpackConfig = `
const webpack = require('webpack');
module.exports = { plugins: [new webpack.EnvironmentPlugin(['NODE_ENV'])] };
`;

const suffix = expect.getState().currentTestName.replace(/\s+/g, '_');
const userWebpackConfig = await writeFileInBuild(
webpackConfig,
`webpack/webpack_${suffix}.js`,
);

const stats = await build.run(functions, {
userWebpackConfig,
});
expect(stats.compilation.errors).toHaveLength(0);
expect(stats.compilation.options.plugins).toHaveLength(3);
expect(
stats.compilation.options.plugins.map(
(plugin) => plugin.constructor.name,
),
).toEqual(['IgnorePlugin', 'DefinePlugin', 'EnvironmentPlugin']);
});

const webpackConfig = `module.exports = { resolve: { extensions: ['.custom'] } }`;
const userWebpackConfig = await writeFileInBuild(
webpackConfig,
'webpack/webpack.js',
);
it('should merge resolve extensions', async () => {
const script = `module.exports = () => console.log("hello world")`;
await writeFileInFunctions(script, 'index.js');

const stats = await build.run(functions, {
userWebpackConfig,
const webpackConfig = `module.exports = { resolve: { extensions: ['.custom'] } }`;
const suffix = expect.getState().currentTestName.replace(/\s+/g, '_');
const userWebpackConfig = await writeFileInBuild(
webpackConfig,
`webpack/webpack_${suffix}.js`,
);

const stats = await build.run(functions, {
userWebpackConfig,
});
expect(stats.compilation.errors).toHaveLength(0);
expect(stats.compilation.options.resolve.extensions).toEqual([
'.wasm',
'.mjs',
'.js',
'.json',
'.ts',
'.custom',
]);
});
expect(stats.compilation.errors).toHaveLength(0);
expect(stats.compilation.options.resolve.extensions).toEqual([
'.wasm',
'.mjs',
'.js',
'.json',
'.ts',
'.custom',
]);
});

describe('babel config file resolution', () => {
Expand Down

0 comments on commit 3437b26

Please sign in to comment.