From d4b1d9babfb4b9ed4f4b12d56d52dee233e862da Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Tue, 16 Jan 2018 11:29:57 +0100 Subject: [PATCH] Add support for transpiling certain packages in node_modules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks to @giuseppeg’s work in https://github.com/zeit/next.js/pull/3319 --- server/build/webpack/base.config.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/server/build/webpack/base.config.js b/server/build/webpack/base.config.js index c57556dba5108..8bfaa96fa80b1 100644 --- a/server/build/webpack/base.config.js +++ b/server/build/webpack/base.config.js @@ -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, @@ -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 @@ -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',