Skip to content

Commit

Permalink
fix: fixing webpack loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
jpapini committed Jun 17, 2024
1 parent 966018c commit c21a1da
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-wombats-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@jpapini/webpack-config': patch
---

Fixing require.resolve for loaders.
14 changes: 1 addition & 13 deletions packages/webpack-config/src/configs/common.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import type { IWebpackEnv } from '~/types';
import { loadDotenv } from '~/utils/dotenv.util';

const require = createRequire(import.meta.url);
const NODE_MODULES_PATH = require
.resolve('swc-loader')
.replace(/\/node_modules\/swc-loader\/.*$/, '/node_modules/');

export function createCommonWebpackConfig(rootDir: string) {
return function (env: IWebpackEnv): Configuration {
Expand All @@ -32,10 +29,6 @@ export function createCommonWebpackConfig(rootDir: string) {
consola.info('Root:', colors.blue.bold(rootDir));
consola.info('Cache:', colors.blue.bold(cacheDirectory));
consola.info('TSConfig:', colors.blue.bold(tsconfigPath));
consola.info(
'Node modules for loaders:',
colors.blue.bold(NODE_MODULES_PATH ?? 'not found'),
);

loadDotenv(rootDir);

Expand All @@ -47,9 +40,6 @@ export function createCommonWebpackConfig(rootDir: string) {
extensions: ['.js', '.ts'],
plugins: [new TsconfigPathsPlugin({ configFile: tsconfigPath })],
},
resolveLoader: {
modules: ['node_modules'],
},
optimization: {
minimize: false,
nodeEnv: isProduction ? 'production' : 'development',
Expand All @@ -64,7 +54,7 @@ export function createCommonWebpackConfig(rootDir: string) {
exclude: [/dist\//, /node_modules\//],
use: [
{
loader: 'swc-loader',
loader: require.resolve('swc-loader'),
options: {
minify: false,
module: {
Expand All @@ -88,8 +78,6 @@ export function createCommonWebpackConfig(rootDir: string) {
},
};

if (NODE_MODULES_PATH) commonConfig.resolveLoader?.modules?.push(NODE_MODULES_PATH);

if (!isProduction) {
commonConfig.cache = {
type: 'filesystem',
Expand Down
5 changes: 4 additions & 1 deletion packages/webpack-config/src/configs/nest-app.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { createRequire } from 'node:module';
import path from 'node:path';

import type { Config } from '@swc/types';
Expand All @@ -15,6 +16,8 @@ import { createCommonWebpackConfig } from './common.config';

const { HotModuleReplacementPlugin } = webpack;

const require = createRequire(import.meta.url);

export type ICreateNestAppWebpackConfigOptions = {
entryFile?: string;
outputDir?: string;
Expand Down Expand Up @@ -59,7 +62,7 @@ export function createNestAppWebpackConfig(
test: /\.tsx?$/i,
use: [
{
loader: 'swc-loader',
loader: require.resolve('swc-loader'),
options: {
jsc: {
target: 'es2022',
Expand Down
11 changes: 8 additions & 3 deletions packages/webpack-config/src/configs/react-app.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'webpack-dev-server';

import { createRequire } from 'node:module';
import path from 'node:path';

import ReactRefreshWebpackPlugin from '@pmmmwh/react-refresh-webpack-plugin';
Expand All @@ -15,6 +16,8 @@ import { mergeConfig } from '~/utils/merge-config.util';

import { createCommonWebpackConfig } from './common.config';

const require = createRequire(import.meta.url);

export type ICreateReactAppWebpackConfigOptions = {
entryFile?: string;
outputDir?: string;
Expand Down Expand Up @@ -72,7 +75,7 @@ export function createReactAppWebpackConfig(
test: /\.tsx?$/i,
use: [
{
loader: 'swc-loader',
loader: require.resolve('swc-loader'),
options: {
jsc: {
target: 'es5',
Expand All @@ -95,9 +98,11 @@ export function createReactAppWebpackConfig(
test: /\.css$/i,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: 'postcss-loader',
loader: require.resolve('css-loader'),
},
{
loader: require.resolve('postcss-loader'),
options: {
implementation: require.resolve('postcss'),
postcssOptions: {
Expand Down

0 comments on commit c21a1da

Please sign in to comment.