使用 URL 时找不到 Webpack 2 模块

Webpack 2 Module not found when using URL

我刚刚更新了我的 Angular 2 项目以使用运行良好的 Webpack 2。但是,我在使用 resolve: { alias: {...} } 键时遇到了一个新问题。

当我使用 webpack 1 时,这段代码运行良好:

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分量:

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));
}

现在使用 webpack 2 时,我总是会收到无法解析两个文件的错误。有人有想法吗?

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'

到目前为止我的尝试:

webpack.common.js 对于 webpack2 我试过:

    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")
    }

    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")
    ]
},

更改 webpack.common.js 文件中的配置,如下所示。

 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
    ),
]

实际上,我找到了解决方案:我不小心从 package.json.

中删除了 resolve-url-loader 依赖项

我进一步发现我什至不需要在解析对象中包含 aliaspath.resolve