-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add next plugin that deals with webpack workarounds
The canvas package, which is a dependency of jsdom, has a native binding which causes the following error during Next.js build process: "Module did not self-register". We circuvent that by using a null-loader. See vercel/next.js#7894 The ws package, which is also a dependency of jsdom, tries to optionally load some dependencies. This produces a warning in webpack that we want to avoid. We circuvent that by adding them to externals.
- Loading branch information
Showing
6 changed files
with
173 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* eslint-disable prefer-import/prefer-import-over-require */ | ||
|
||
module.exports = require('./lib/plugin'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const castArray = (value) => { | ||
if (Array.isArray(value)) { | ||
return value; | ||
} | ||
|
||
return value != null ? [value] : []; | ||
}; | ||
|
||
const withNextIntl = () => (nextConfig = {}) => ({ | ||
...nextConfig, | ||
webpack: (config, options) => { | ||
const { isServer } = options; | ||
|
||
if (isServer) { | ||
// The canvas package, which is a dependency of jsdom, has a native binding which causes | ||
// the following error during Next.js build process: "Module did not self-register" | ||
// See https://github.com/zeit/next.js/issues/7894 | ||
// We circuvent that by using a null-loader | ||
config.module.rules.unshift({ | ||
test: require.resolve('canvas'), | ||
loader: require.resolve('null-loader'), | ||
}); | ||
|
||
// The ws package, which is also a dependency of jsdom, tries to optionally load some dependencies | ||
// This produces a warning in webpack that we want to avoid | ||
config.externals = [ | ||
'bufferutil', | ||
'utf-8-validate', | ||
...castArray(config.externals), | ||
]; | ||
} | ||
|
||
if (typeof nextConfig.webpack === 'function') { | ||
return nextConfig.webpack(config, options); | ||
} | ||
|
||
return config; | ||
}, | ||
}); | ||
|
||
export default withNextIntl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const nextIntlPlugin = require('./plugin'); | ||
|
||
const webpackOptions = { | ||
isServer: true, | ||
}; | ||
|
||
const createWebpackConfig = () => ({ | ||
module: { | ||
rules: [ | ||
{ | ||
test: 'foo', | ||
loader: 'foo-loader', | ||
}, | ||
], | ||
}, | ||
externals: () => {}, | ||
}); | ||
|
||
it('should add a rule for canvas that uses null-loader', () => { | ||
const config = nextIntlPlugin()().webpack(createWebpackConfig(), webpackOptions); | ||
|
||
const rule = config.module.rules[0]; | ||
|
||
expect(rule.test).toBe(require.resolve('canvas')); | ||
expect(rule.loader).toBe(require.resolve('null-loader')); | ||
}); | ||
|
||
it('should add no canvas rule when not server', () => { | ||
const config = nextIntlPlugin()().webpack(createWebpackConfig(), { ...webpackOptions, isServer: false }); | ||
|
||
expect(config.module.rules[0].test).toBe('foo'); | ||
}); | ||
|
||
it('should add ws\'s optional dependencies to externals', () => { | ||
const config = nextIntlPlugin()().webpack(createWebpackConfig(), webpackOptions); | ||
|
||
expect(config.externals).toHaveLength(3); | ||
expect(config.externals[0]).toBe('bufferutil'); | ||
expect(config.externals[1]).toBe('utf-8-validate'); | ||
expect(typeof config.externals[2]).toBe('function'); | ||
}); | ||
|
||
it('should leave externals untouched when not server', () => { | ||
const config = nextIntlPlugin()().webpack(createWebpackConfig(), { ...webpackOptions, isServer: false }); | ||
|
||
expect(typeof config.externals).toBe('function'); | ||
}); |