@moonee/nx-next12-plugin
Version:
Nx plugin for Next.js 12 integration
65 lines • 2.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const devkit_1 = require("@nrwl/devkit");
function isNextJsRuleSetRule(rule) {
return typeof rule === 'object' && rule !== null && 'oneOf' in rule;
}
function withNx(nextConfig = {}) {
const userWebpack = nextConfig.webpack || ((x) => x);
return {
...nextConfig,
webpack: (config, options) => {
var _a, _b;
options.defaultLoaders.babel.options.babelrc = true;
options.defaultLoaders.babel.options.rootMode = 'upward';
const includes = [(0, path_1.join)(devkit_1.workspaceRoot, (0, devkit_1.workspaceLayout)().libsDir)];
const nextCssLoaders = (_b = (_a = config.module) === null || _a === void 0 ? void 0 : _a.rules) === null || _b === void 0 ? void 0 : _b.find(isNextJsRuleSetRule);
if (nextCssLoaders === null || nextCssLoaders === void 0 ? void 0 : nextCssLoaders.oneOf) {
nextCssLoaders.oneOf.forEach((rule) => {
if (isNextJsRuleSetRule(rule) &&
rule.sideEffects === false &&
rule.test &&
['css', 'scss', 'sass'].some((ext) => String(rule.test).includes(ext))) {
rule.issuer = rule.issuer || {};
if (typeof rule.issuer === 'object' && rule.issuer !== null) {
rule.issuer.or = rule.issuer.and
? rule.issuer.and.concat(includes)
: includes;
delete rule.issuer.and;
}
}
});
}
addNxEnvVariables(config);
return userWebpack(config, options);
},
};
}
exports.default = withNx;
function getNxEnvironmentVariables() {
return Object.keys(process.env)
.filter((env) => /^NX_/i.test(env))
.reduce((env, key) => {
env[key] = process.env[key];
return env;
}, {});
}
function addNxEnvVariables(config) {
var _a;
const definePlugin = (_a = config.plugins) === null || _a === void 0 ? void 0 : _a.find((plugin) => {
return (typeof plugin === 'object' &&
plugin !== null &&
'definitions' in plugin &&
plugin.definitions &&
'process.env.NODE_ENV' in plugin.definitions);
});
if (definePlugin) {
const env = getNxEnvironmentVariables();
Object.entries(env)
.map(([name, value]) => [`process.env.${name}`, JSON.stringify(value)])
.filter(([name]) => !(name in definePlugin.definitions))
.forEach(([name, value]) => (definePlugin.definitions[name] = value));
}
}
//# sourceMappingURL=with-nx.js.map