简体   繁体   中英

Can't load templates with routeprovider when using webpack

I recentley migrated from grunt to webpack and I can't figure how to require/import my templates. I have looked at several similar issues with no luck.

Here is my webpack.config file:

  var webpack = require('webpack');
    var globby = require('globby');
    var path = require('path');

    var HtmlWebpackPlugin = require('html-webpack-plugin');
    var ExtractTextPlugin = require('extract-text-webpack-plugin');
    var CleanWebpackPlugin = require('clean-webpack-plugin');
    var AssetsPlugin = require('assets-webpack-plugin');


    var ExtractTextPlugin = require('extract-text-webpack-plugin');
    module.exports = {
        entry: {
            app: globby.sync(['./app/app.js','./app/app.run.js', './app/app.config.js', './app/**/*.js']),
            styles: globby.sync(['./content/styles/*.css']),
            images: globby.sync(['./content/images/*.*']),
            vendor: [
           // removed to save space
            ]
        },
        output: {
            filename: './scripts/[name].bundle.js',
            path: path.join(__dirname, "public")
        },
        devServer: {
            port: 1384,
            contentBase: './public/'
        },

        // Enable sourcemaps for debugging webpack's output.
        devtool: 'source-map',

        module: {
            rules: [
                {   test: /\.less$/, 
                    loader: "less-loader",
                    include: path.resolve(__dirname, "content", "styles", "less")
                },
                {
                    test: /\.html$/,
                    use: [
                        {
                            loader: 'ngtemplate-loader',
                            options: {
                                angular: true,
                            },
                        },
                        {
                            loader: 'raw-loader',
                        },
                        {
                            loader: 'html-loader'
                        },
                        {
                            loader: "ngtemplate!html?module=myTemplates&relativeTo=" + 
                            (path.resolve(__dirname, './app/**'))
                        },
                    ],
                    /*
                    exclude: [/node_modules/]
                    */
                },
                {
                    test: /\.css$/,
                    loader: ExtractTextPlugin.extract({ fallback: 'style-loader', use: 'css-loader' }),
                },
                {
                    test: /\.(ico)$/,
                    loader: "url-loader?name=./[name].[ext]",
                    include: path.resolve(__dirname, "content", "images")
                },
                {
                    test: /\.(jpg|jpeg|gif|png|tiff|svg)$/,
                    loader: "file-loader?name=./images/[name].[ext]",
                    include: path.resolve(__dirname, "content", "images")
                },
                {
                    test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                    loader: 'url-loader?minetype=application/font-woff&name=./fonts/[name].[ext]'
                },
                {
                    test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                    loader: 'file-loader?name=./fonts/[name].[ext]'
                },
                {
                    test: require.resolve('adal-angular/lib/adal'),
                    loader: 'expose-loader?AuthenticationContext'
                },
                {
                    test: /\.js$/,
                    enforce: "pre",
                    loader: 'source-map-loader'
                }
            ],
        },

        plugins: [
            new webpack.DefinePlugin({
                ADMIN_API_URL: JSON.stringify('http://localhost:41118/api/'),
                API_URL: JSON.stringify('http://localhost:32605/api/'),
                GLOBAL_ADMIN_URL: JSON.stringify('https://adminapi.tradesolution.no/')
            }),
            new HtmlWebpackPlugin({
                template: './app/layout.ejs',
                filename: 'index.html'
            }),
            new webpack.optimize.CommonsChunkPlugin({ name: 'vendor', filename: './scripts/vendor.bundle.js' }),
            new ExtractTextPlugin({ filename: './styles/[name].bundle.css' }),
            /*new CleanWebpackPlugin(['public'], {
                verbose: false
            }),
            */
            new AssetsPlugin({
                filename: 'webpack.assets.json',
                path: './public/scripts',
                prettyPrint: true
            }),
            new webpack.ProvidePlugin({
                $: 'jquery',
                jQuery: 'jquery',
                'window.jQuery': 'jquery',
                'window.$': 'jquery',
                "window.AuthenticationContext": "AuthenticationContext",
                _: 'underscore'
            }),
        ],
        externals: [
            { xmlhttprequest: '{XMLHttpRequest:XMLHttpRequest}' }
        ],


    }

The public folder builds as it should with .js, styles, images etc. But simple navigation results in this error:

ERROR in ./app/app.config.js
Module not found: Error: Can't resolve 'app/varegruppe/templates/index.html' in 'C:\Users\oystein\work\EPD.SPA\EpdSPA\app'
 @ ./app/app.config.js 50:25-71

Here is an example of how the routes are defined:

  $routeProvider.when('/produkteier', {
            templateUrl: require('app/produkteier/templates/view.html')
        })

Using this plugin I managed to copy all the html files in to the public directory, with the following config:

var CopyWebpackPlugin = require('copy-webpack-plugin');
new CopyWebpackPlugin([
            {from: './app/**/*.html', to: './'}
        ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM