0

I have a html template with a bunch of css and js files that want to import to angular 5 project using webpack. I am lost in all the training in the internet and didn't find anything like my problem.

i've read somewhere to import css to my style.css like this

   @import './assets/css/icons.css';

and works fine for me. But i didn't find anything for my js files. i appreciate someone to point me to right path. this is my webpack.config.json

  const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin;

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: ['.js', '.ts'] },
        output: {
            filename: '[name].js',
            publicPath: 'dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                { test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader'] : '@ngtools/webpack' },
                { test: /\.html$/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize'] },
                { test: /\.scss$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize', 'sass-loader'] },
                { test: /\.(png|jpg|jpeg|gif|svg|ttf|eot|woff|woff2)$/, use: 'url-loader?limit=25000' }
            ]
        },
        plugins: [new CheckerPlugin()]
    };

    // Configuration for client-side bundle suitable for running in browsers
    const clientBundleOutputDir = './wwwroot/dist';
    const clientBundleConfig = merge(sharedConfig, {
        entry: { 'main-client': './ClientApp/boot.browser.ts' },
        output: { path: path.join(__dirname, clientBundleOutputDir) },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./wwwroot/dist/vendor-manifest.json')
            })
        ].concat(isDevBuild ? [
            // Plugins that apply in development builds only
            new webpack.SourceMapDevToolPlugin({
                filename: '[file].map', // Remove this line if you prefer inline source maps
                moduleFilenameTemplate: path.relative(clientBundleOutputDir, '[resourcePath]') // Point sourcemap entries to the original file locations on disk
            })
        ] : [
                // Plugins that apply in production builds only
                new webpack.optimize.UglifyJsPlugin(),
                new AngularCompilerPlugin({
                    tsConfigPath: './tsconfig.json',
                    entryModule: path.join(__dirname, 'ClientApp/app/app.module#AppModule')
                })
            ])
    });

    return [clientBundleConfig];
};
6
  • are you using angular-cli in your project? Commented Feb 25, 2018 at 9:02
  • You could just import them in your main.ts with import LIBRARY from ./path/to/lib.js, include the script in your angular-cli.json in the scripts array, or import them in your index.html in a script tag Commented Feb 25, 2018 at 9:06
  • Yes i'm using angular-cli in my project. Commented Feb 25, 2018 at 9:20
  • @darron614 i inserted all js files in script array of angular-cli.json like this "assets/js/waves.js", "assets/js/wow.min.js", "assets/js/jquery.nicescroll.js", "assets/js/jquery.scrollTo.min.js", "assets/js/jquery.core.js", "assets/js/jquery.app.js" but no luck. should i do something more? i want to know the right way to do this actually. i can insert them in head of my html but i think that not the right way Commented Feb 25, 2018 at 9:34
  • Are your sure the paths are correct? Also you have to rerun "ng serve", so webpack bundles your newly included js files. Commented Feb 25, 2018 at 10:09

1 Answer 1

2

You can add them in entry object like this:

 entry: {
  // other things,
  scripts: [
    "script-loader!./node_modules/jquery/dist/jquery.min.js",
    "script-loader!./src/plugins/bower_components/bootstrap/dist/js/bootstrap.min.js",
    "script-loader!./node_modules/bootstrap/js/dropdown.js",

  ],
  styles: [
    "./src/styles.scss"
  ]
},

Do not forget to add script-loader! before js paths.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.