Skip to content

Commit

Permalink
Add support for transpiling certain packages in node_modules
Browse files Browse the repository at this point in the history
Thanks to @giuseppeg’s work in #3319
  • Loading branch information
timneutkens committed Jan 16, 2018
1 parent 5434104 commit d4b1d9b
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions server/build/webpack/base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ const interpolateNames = new Map(defaultPages.map((p) => {
return [path.join(nextPagesDir, p), `dist/bundles/pages/${p}`]
}))

function shouldTranspileModule (transpileModules, path) {
return (transpileModules || []).some(pattern => {
if (!(pattern instanceof RegExp)) {
const message = `Incorrect pattern in config.transpileModules: "${pattern}".` +
'It accepts an array of regular expression'
throw new Error(message)
}

return pattern.test(path)
})
}

function excludeModules (config) {
return (str) => {
if (shouldTranspileModule(config.transpileModules, str)) {
return false
}

return /node_modules/.test(str) && str.indexOf(nextPagesDir) !== 0
}
}

function babelConfig (dir, isServer) {
const mainBabelOptions = {
cacheDirectory: true,
Expand Down Expand Up @@ -193,7 +215,7 @@ export default async function baseConfig (dir, {dev = false, isServer = false, b
{
test: /\.+(js|jsx)$/,
include: [dir],
exclude: /node_modules/,
exclude: excludeModules(config),
use: {
loader: 'babel-loader',
options: babelLoaderOptions
Expand All @@ -202,7 +224,7 @@ export default async function baseConfig (dir, {dev = false, isServer = false, b
{
test: /\.+(ts|tsx)$/,
include: [dir],
exclude: /node_modules/,
exclude: excludeModules(config),
use: [
{
loader: 'babel-loader',
Expand Down

0 comments on commit d4b1d9b

Please sign in to comment.