在 Next.js 中转译 npm 模块
Transpile npm module in Next.js
我正在尝试从 node_modules 转译模块“react-device-detect”,但还无法实现。以下是我尝试过的:
module.exports = withLess({
webpack: (config, { isServer, defaultLoaders }) => {
// other configs
config.module.rules.push({
test: /\.+(ts|tsx)$/,
include: [path.join(__dirname, 'node_modules/react-device-detect')],
// exclude: /node_modules/,
use: [{ loader: 'ts-loader' }],
});
config.module.rules.push({
test: /\.+(js|jsx)$/,
include: [path.join(__dirname, 'node_modules/react-device-detect')],
// exclude: /node_modules/,
use: [{ loader: 'babel-loader' }],
});
return config;
},
});
我也单独尝试过这些规则,但没有用。
更新 1:
完成 next.config.js 配置@felixmosh
const webpack = require("webpack");
const withLess = require("@zeit/next-less");
const { parsed: localEnv } = require("dotenv").config();
require("dotenv").config({
path: process.env.NODE_ENV === "production" ? ".env.production" : ".env"
});
const Dotenv = require("dotenv-webpack");
module.exports = withLess({
webpack: (config, { isServer }) => {
// Fixes npm packages that depend on `fs` module
config.node = {
fs: "empty"
};
// add env variables on client end
config.plugins.push(new webpack.EnvironmentPlugin(localEnv));
config.plugins.push(new Dotenv());
if (!isServer) {
config.resolve.alias["@sentry/node"] = "@sentry/browser";
}
return config;
}
});
更新 2:
似乎 next-transpile-modules
不能与 next-i18next
一起舒适地工作。我进入了IE浏览器的控制台:
'exports' is undefined
当我 运行 npm run build
:
时出现如下错误
./utils.js
Attempted import error: 'isMobileSafari' is not exported from 'react-device-detect'.
./utils.js
Attempted import error: 'isMobileSafari' is not exported from 'react-device-detect'.
./utils.js
Attempted import error: 'osName' is not exported from 'react-device-detect'.
当我 运行 npm start
:
时我得到了关注
react-i18next:: i18n.languages were undefined or empty undefined
这个 next-transpile-modules
有一个 NPM 模块,允许您指定要转译的模块。
// next.config.js
const withTM = require('next-transpile-modules')(['somemodule', 'and-another']); // pass the modules you would like to see transpiled
module.exports = withTM(withLess({
... // your custom next config
}));
我正在尝试从 node_modules 转译模块“react-device-detect”,但还无法实现。以下是我尝试过的:
module.exports = withLess({
webpack: (config, { isServer, defaultLoaders }) => {
// other configs
config.module.rules.push({
test: /\.+(ts|tsx)$/,
include: [path.join(__dirname, 'node_modules/react-device-detect')],
// exclude: /node_modules/,
use: [{ loader: 'ts-loader' }],
});
config.module.rules.push({
test: /\.+(js|jsx)$/,
include: [path.join(__dirname, 'node_modules/react-device-detect')],
// exclude: /node_modules/,
use: [{ loader: 'babel-loader' }],
});
return config;
},
});
我也单独尝试过这些规则,但没有用。
更新 1: 完成 next.config.js 配置@felixmosh
const webpack = require("webpack");
const withLess = require("@zeit/next-less");
const { parsed: localEnv } = require("dotenv").config();
require("dotenv").config({
path: process.env.NODE_ENV === "production" ? ".env.production" : ".env"
});
const Dotenv = require("dotenv-webpack");
module.exports = withLess({
webpack: (config, { isServer }) => {
// Fixes npm packages that depend on `fs` module
config.node = {
fs: "empty"
};
// add env variables on client end
config.plugins.push(new webpack.EnvironmentPlugin(localEnv));
config.plugins.push(new Dotenv());
if (!isServer) {
config.resolve.alias["@sentry/node"] = "@sentry/browser";
}
return config;
}
});
更新 2:
似乎 next-transpile-modules
不能与 next-i18next
一起舒适地工作。我进入了IE浏览器的控制台:
'exports' is undefined
当我 运行 npm run build
:
./utils.js
Attempted import error: 'isMobileSafari' is not exported from 'react-device-detect'.
./utils.js
Attempted import error: 'isMobileSafari' is not exported from 'react-device-detect'.
./utils.js
Attempted import error: 'osName' is not exported from 'react-device-detect'.
当我 运行 npm start
:
react-i18next:: i18n.languages were undefined or empty undefined
这个 next-transpile-modules
有一个 NPM 模块,允许您指定要转译的模块。
// next.config.js
const withTM = require('next-transpile-modules')(['somemodule', 'and-another']); // pass the modules you would like to see transpiled
module.exports = withTM(withLess({
... // your custom next config
}));