Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix the non-working of latest raw-loader version in webpack config?

In my angular project if we use the app is compiling and working fine if we use [email protected]. Whereas if we use version 2.0.0, application is not working. What would be the difference between version 1.0.0 & 2.0.0?

webpack.config.ts

const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/main.ts',
    resolve: {
        extensions: ['.ts', '.js']
    },
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: ['ts-loader', 'angular2-template-loader'],
                exclude: /node_modules/
            },
            {
                test: /\.(html|css)$/,
                use: 'raw-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/index.html',
            filename: 'index.html',
            inject: 'body'
        }),
        new webpack.DefinePlugin({
            config: JSON.stringify({
                apiUrl: 'http://localhost:9999'
            })
        })
    ],
    devServer: {
        historyApiFallback: true
    }
};
like image 500
Alexpandiyan Chokkan Avatar asked Jun 05 '19 03:06

Alexpandiyan Chokkan


People also ask

What is raw loader webpack?

GitHub - webpack-contrib/raw-loader: A loader for webpack that allows importing files as a String.

How do I update my webpack to 5?

Now let's upgrade webpack to version 5: npm: npm install webpack@latest. Yarn: yarn add webpack@latest.


1 Answers

According to their release page 2.0.0 had the breaking change "use ES Module export instead of CommonJS" (https://github.com/webpack-contrib/raw-loader/releases), so depending on your project setup (using commonJS modules not ES modules) this could be causing your issue.

Using CommonJS modules looks like this:

const myModule = require('../path/to/module/my-module.js');

And using the new ES Module standard looks something like:

import {MyModule} from '../path/to/module/my-module.js';

If your code is using imports from the first example then the 2.0.0 version of raw-loader won't work for you (which looks the case here as your example uses CommonJS module syntax). The imports that are causing the issue may be in your application code, other config files such as your webpack config or possibly in another dependency your using in the project.

It may be difficult to debug because all of those areas (app code, configs, dependencies) would need to be updated to using ES modules, which is not always an easy thing to do. Depending on your project your best option may be to stay with version 1.0.0 of raw-loader or even start a new project using the Angular CLI tool and then copy in all of your app code so that way everything is up to date, and future updates can be handled fairly easily using the CLI's update command

like image 168
tim545 Avatar answered Oct 09 '22 01:10

tim545