Lukas
Lukas

Reputation: 168

Webpack 2 Module not found when using URL

I just updated my Angular 2 project to work with Webpack 2 which is working just fine. However, i'm facing a new problem when using the resolve: { alias: {...} } key.

When i used webpack 1, this code just worked fine:

webpack.config.js

    resolve: {
    // http://webpack.github.io/docs/configuration.html#resolve-extensions
    extensions: ['.ts', '.js', '.json', '.css', '.html'],
    alias: {
        "orion/editor/edit": "http://www.eclipse.org/orion/editor/releases/current/built-editor.min.js"
    }
},

angular component:

ngOnInit() {
    Promise.all([
        require('http://www.eclipse.org/orion/editor/releases/current/built-editor.min.js'),
        require('http://eclipse.org/orion/editor/releases/current/built-editor.css')
    ]).then(function () {
        requirejs(['orion/editor/edit'], function (edit: any) {
            this.orionEditor = edit({className: 'editor', parent: 'xml'})[0];
            this.receiveXmlData();
        }.bind(this));
    }.bind(this));
}

Now when using webpack 2, i always get an error that both files cannot be resolved. Does anyone have an idea?

Module not found: Error: Can't resolve 'http://eclipse.org/orion/editor/releases/current/built-editor.css' in [Angular-component file]

Module not found: Error: Can't resolve 'http://www.eclipse.org/orion/editor/releases/current/built-editor.min.js' in 'C:\Applications\winery\enpro-winery\org.eclipse.winery.ui\src\app\instance\editXML'

My try up till now:

in the webpack.common.js for webpack2 i tried:

    const path = require('path');

    resolve: {
        extensions: ['.ts', '.js', '.json', '.css', '.html'],
        alias: {
            "orion/editor/edit": path.resolve("http://www.eclipse.org/orion/editor/releases/current/built-editor.min.js")
    }

and

    resolve: {
        extensions: ['.ts', '.js', '.json', '.css', '.html'],
        modules: [
            path.join(__dirname, "src"),
            "node_modules",
            path.resolve("http://www.eclipse.org/orion/editor/releases/current/built-editor.min.js"),
            path.resolve("http://eclipse.org/orion/editor/releases/current/built-editor.css")
    ]
},

Upvotes: 2

Views: 4077

Answers (2)

Lukas
Lukas

Reputation: 168

Actually, I found the solution: I accidentally removed the resolve-url-loader dependency from my package.json.

I further found that I don't even need to include the alias or path.resolve in the resolve object.

Upvotes: 1

Venkateswaran R
Venkateswaran R

Reputation: 478

Change the configuration in webpack.common.js file which are given below.

 module: {
        rules: [
            {
                test: /\.ts$/,
                loaders: ['awesome-typescript-loader', 'angular2-template-loader']
            },
            {
                test: /\.html$/,
                loader: 'html-loader'
            },
            {
                test: /\.json$/,
                exclude: /node_modules/,
                loader: 'file-loader?name=[name].json'
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                loader: 'file-loader?name=assets/[name].[ext]'
            },
            {
                test: /\.css$/,
                exclude: helpers.root('src', 'app'),
                loader: ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: 'css-loader?sourceMap' })
            },
            {
                test: /\.css$/,
                include: helpers.root('src', 'app'),
                loader: 'raw-loader'
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                loaders: ['raw-loader', 'sass-loader']
            },

        ]
    },


 plugins: [
    // Workaround for angular/angular#11580
    new webpack.ContextReplacementPlugin(
      // The (\\|\/) piece accounts for path separators in *nix and Windows
      /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
      helpers.root('./src'), // location of your src
      {} // a map of your routes
    ),
]

Upvotes: 0

Related Questions