0

I have a folder (ClientApp/static/assets/i18n) in my Angular2/ASP.NET Core App which contains different json files where translations are stored. I want to copy them to the public dist folder so that the files are available for the browser under http://localhost:54135/assets/i18n/en.json for instance.

How can I achieve that using webpack with the staticBundleConfig. I've tried, but I don't quite get the meaning of the entry point in this case. It doesn't work either. Any help in achieving that?

    var isDevBuild = process.argv.indexOf('--env.prod') < 0;
var path = require('path');
var webpack = require('webpack');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var nodeExternals = require('webpack-node-externals');
var merge = require('webpack-merge');
var allFilenamesExceptJavaScript = /\.(?!js(\?|$))([^.]+(\?|$))/;

// Configuration in common to both client-side and server-side bundles
var sharedConfig = {
    resolve: { extensions: [ '', '.js', '.ts' ] },
    output: {
        filename: '[name].js',
        publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
    },
    module: {
        loaders: [
            { test: /\.ts$/, include: /ClientApp/, loader: 'ts', query: { silent: true } },
            { test: /\.html$/, loader: 'raw' },
            { test: /\.json$/, include: /ClientApp/, loader: 'json'},
            { test: /\.css$/, loader: 'to-string!css' },
            { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url', query: { limit: 25000 } }
        ]
    }
};

// Configuration for client-side bundle suitable for running in browsers
var clientBundleConfig = merge(sharedConfig, {
    entry: { 'main-client': './ClientApp/boot-client.ts' },
    output: { path: path.join(__dirname, './wwwroot/dist') },
    devtool: isDevBuild ? 'inline-source-map' : null,
    plugins: [
        new webpack.DllReferencePlugin({
            context: __dirname,
            manifest: require('./wwwroot/dist/vendor-manifest.json')
        })
    ].concat(isDevBuild ? [] : [
        // Plugins that apply in production builds only
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.optimize.UglifyJsPlugin()
    ])
});

var staticBundleConfig = merge(sharedConfig, {

    //entry: { 'static': './ClientApp/boot.ts' },
    output: { path: path.join(__dirname, './wwwroot/dist') },
    plugins: [
        new CopyWebpackPlugin([
            { from: './ClientApp/static', to: path.join(__dirname, './wwwroot/dist') }
        ])
    ]
});

// Configuration for server-side (prerendering) bundle suitable for running in Node
var serverBundleConfig = merge(sharedConfig, {
    entry: { 'main-server': './ClientApp/boot-server.ts' },
    output: {
        libraryTarget: 'commonjs',
        path: path.join(__dirname, './ClientApp/dist')
    },
    target: 'node',
    devtool: 'inline-source-map',
    externals: [nodeExternals({ whitelist: [allFilenamesExceptJavaScript] })] // Don't bundle .js files from node_modules
});

module.exports = [clientBundleConfig, serverBundleConfig, staticBundleConfig];
Stefan
  • 1,590
  • 3
  • 18
  • 33
  • http://stackoverflow.com/questions/27639005/how-to-copy-static-files-to-build-directory-with-webpack try this – Vinay Pandya Nov 29 '16 at 10:08
  • I've tried that and that works fine if I have single files that I want to bundle in my js-file that is delivered. What I want to do though, is deliver the whole folder with all json files in it. – Stefan Nov 29 '16 at 10:30
  • Is there any way to load third party library from node_modules.? – Karthick Feb 27 '17 at 11:27

1 Answers1

2

Use copy-webpack-plugin for that

var CopyWebpackPlugin = require('copy-webpack-plugin');
    plugins: [
            new CopyWebpackPlugin([               

                // Copy directory contents to {output}/to/directory/ 
                { from: 'from/directory', to: 'to/directory' }
                ])               

            ],

follow this link for more info click-here

Sriram
  • 739
  • 7
  • 18
  • I don't quite get it done this way. I've added my code above. Can you maybe re-check. – Stefan Nov 29 '16 at 10:47
  • you just want to copy all the static assets to root folder or else you want to create a folder named static or something else and move the static files into that folder? – Sriram Nov 29 '16 at 10:50
  • I just want to copy all the static assets to the root folder, so the browser can find them using requests, such as: http://localhost:54135/assets/i18n/en.json – Stefan Nov 29 '16 at 10:59
  • then you don't need to use "to" just give from directory webpack will copy the static files to root folder and tell me what issue are you facing. – Sriram Nov 29 '16 at 11:03
  • 1
    It is shown in the dist folder in my IDE (Visual Studio) then, but it is not delivered to the browser somehow. In the browser are just three files: main-client.js, vendor.js, vendor.css – Stefan Nov 29 '16 at 11:04
  • try moving it inside a folder named asset and change the output to output: { publicPath: '/assets/' } – Sriram Nov 29 '16 at 11:18
  • Any answer for above question ? – gauti Jan 28 '18 at 07:05