使用webpack打包jquery-sparkline插件

6
我在尝试使用webpack的第一次尝试遇到了困难。在浏览器控制台中出现以下错误。
ERROR TypeError: $(...).sparkline is not a function

这是我的webpack.config.vendor.js代码。
const path = require('path');
const webpack = require('webpack');
//const ExtractTextPlugin = require('extract-text-webpack-plugin');
const merge = require('webpack-merge');

const treeShakableModules = [
    '@angular/animations',
    '@angular/common',
    '@angular/compiler',
    '@angular/core',
    '@angular/forms',
    '@angular/http',
    '@angular/platform-browser',
    '@angular/platform-browser-dynamic',
    '@angular/router',
    'zone.js/dist/zone',
];
const nonTreeShakableModules = [
    'jquery',
    'jquery-sparkline',
    '.\\node_modules\\jquery-sparkline\\jquery.sparkline.js',
     '@angular/material',
    'event-source-polyfill',
    '.\\wwwroot\\assets\\styles\\style.scss',
    '.\\node_modules\\chartist\\dist\\chartist.css',
    '.\\node_modules\\quill\\dist\\quill.snow.css',
    '.\\node_modules\\quill\\dist\\quill.bubble.css',
    '.\\node_modules\\angular-calendar\\css\\angular-calendar.css',
    '.\\node_modules\\dragula\\dist\\dragula.css',
    '.\\ClientApp\\styles.css',
];
const allModules = treeShakableModules.concat(nonTreeShakableModules);

module.exports = (env) => {

    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { "modules": true 
             },
        resolve: {
            extensions: ['.js'],
        },
        module: {
            rules: [
                { test: /\.(png|woff|woff2|eot|ttf|svg)(\?|$)/, use: 'url-loader?limit=100000' }
            ]
        },
        output: {
            publicPath: 'dist/',
            filename: '[name].js',
            library: '[name]_[hash]'
        },
        plugins: [
            new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
            new webpack.ContextReplacementPlugin(/\@angular\b.*\b(bundles|linker)/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/11580
            new webpack.ContextReplacementPlugin(/angular(\\|\/)core(\\|\/)@angular/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/14898
            new webpack.ContextReplacementPlugin(/\@angular(\\|\/)core(\\|\/)esm5/, path.join(__dirname, './ClientApp')), // Workaround for https://github.com/angular/angular/issues/20357
            new webpack.IgnorePlugin(/^vertx$/) // Workaround for https://github.com/stefanpenner/es6-promise/issues/100
        ]
    };

    const clientBundleConfig = merge(sharedConfig, {
        entry: {
            // To keep development builds fast, include all vendor dependencies in the vendor bundle.
            // But for production builds, leave the tree-shakable ones out so the AOT compiler can produce a smaller bundle.
            vendor: isDevBuild ? allModules : nonTreeShakableModules
        },
        output: { path: path.join(__dirname, 'wwwroot', 'dist') },
        module: {
            rules: [
                {
                    test: /\.scss$/, use: ['to-string-loader', 'css-loader', 'sass-loader']
                },
                {
                    test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize']
                },
                //{
                    //test: /\.scss$/, use: ExtractTextPlugin.extract({
                    //    use: ['css-loader', 'sass-loader'],
                    //    // use style-loader in development
                    //    fallback: "style-loader"
                    //})
                //}
            ]
        },
        plugins: [
            new webpack.DllPlugin({
                context: __dirname,
                path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ].concat(isDevBuild ? [] : [
            new webpack.optimize.UglifyJsPlugin()
        ])
    });

    const serverBundleConfig = merge(sharedConfig, {
        target: 'node',
        resolve: { mainFields: ['main'] },
        entry: { vendor: allModules.concat(['aspnet-prerendering']) },
        output: {
            path: path.join(__dirname, 'ClientApp', 'dist'),
            libraryTarget: 'commonjs2',
        },
        module: {

            rules: [
                {
                    test: /\.scss$/, use: ['to-string-loader', 'css-loader', 'sass-loader']
                },
                {
                    test: /\.css$/, use: ['to-string-loader', isDevBuild ? 'css-loader' : 'css-loader?minimize']
                },
                //{
                //test: /\.scss$/, use: ExtractTextPlugin.extract({
                //    use: ['css-loader', 'sass-loader'],
                //    // use style-loader in development
                //    fallback: "style-loader"
                //})
                //}
            ]
        },
        plugins: [
            new webpack.DllPlugin({
                context: __dirname,
                path: path.join(__dirname, 'ClientApp', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ]
    });

    return [clientBundleConfig, serverBundleConfig];
}

这是我的webpack.config.js代码。

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;
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = (env) => {
    // Configuration in common to both client-side and server-side bundles
    const isDevBuild = !(env && env.prod);
    const sharedConfig = {
        stats: { modules: true },
        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: /\.ts$/, use: isDevBuild ? ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-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: ExtractTextPlugin.extract({
                //        use: ['css-loader', 'sass-loader'],
                //        // use style-loader in development
                //        fallback: "style-loader"
                //    })
                //},
                {
                    test: /\.scss$/, use: ['to-string-loader', 'css-loader', 'sass-loader']
                },
                {
                    test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000'
                }
            ]
        },
        plugins: [
            new ExtractTextPlugin({ filename: 'vendor.css', disable: false, allChunks: true }),
            new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' }), // Maps these identifiers to the jQuery package (because Bootstrap expects it to be a global variable)
            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/components/app.browser.module#AppModule'),
                    exclude: ['./**/*.server.ts']
                })
            ])
    });

    // Configuration for server-side (prerendering) bundle suitable for running in Node
    const serverBundleConfig = merge(sharedConfig, {
        resolve: { mainFields: ['main'] },
        entry: { 'main-server': './ClientApp/boot.server.ts' },
        plugins: [
            new webpack.DllReferencePlugin({
                context: __dirname,
                manifest: require('./ClientApp/dist/vendor-manifest.json'),
                sourceType: 'commonjs2',
                name: './vendor'
            })
        ].concat(isDevBuild ? [] : [
            // Plugins that apply in production builds only
            new AngularCompilerPlugin({
                tsConfigPath: './tsconfig.json',
                entryModule: path.join(__dirname, 'ClientApp/app/app.server.module#AppModule'),
                exclude: ['./**/*.browser.ts']
            })
        ]),
        output: {
            libraryTarget: 'commonjs',
            path: path.join(__dirname, './ClientApp/dist')
        },
        target: 'node',
        devtool: 'inline-source-map'
    });

    return [clientBundleConfig, serverBundleConfig];
};

我已经包含了jquery-sparkline,但我仍然遇到这个错误。我可以在vendor.js中看到sparkline代码,但似乎没有任何区别。
我还查看了供应商清单文件,其中包含这个内容,但没有其他关于sparkline的提及。
"./node_modules/jquery-sparkline/jquery.sparkline.js":{
         "id":101,
         "meta":{

         }
      }

我不理解为什么我必须在两个文件中都放置ProvidePlugin。只需要在vendor文件中放置一次就足够了,但当它只存在于vendor文件中时,我的浏览器报错显示无法找到$。
new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' })

任何帮助都将不胜感激!
谢谢。
1个回答

4

看起来您正在使用angular-netcore。

在这种情况下,您需要为sparkline安装types,因为还没有准备好,您需要创建一个。

ClientApp/typings/sparkline/index.d.ts

index.d.ts

// Generated by typings
// Source: ClientApp/typings/sparkline/index.d.ts
interface JQuery {
    sparkline(values?: string | Array<(string | number)>, opts?: sparkline.Settings): any;
}

declare namespace sparkline {
    interface Settings {
        type?: string;
        barColor?: string;
        width?: string | number;
        height?: string;
        lineColor?: string;
        fillColor?: string | number;
        chartRangeMin?: string | number;
        chartRangeMax?: string | number;
        composite?: boolean;
        enableTagOptions?: boolean;
        tagOptionPrefix?: string;
        tagValuesAttribute?: string;
        disableHiddenCheck?: boolean;
    }
}

接下来,您需要声明为全局变量:
typings install --global --save file:./ClientApp/typings/sparkline/index.d.ts  

如果您没有安装typings,可以通过以下方式进行安装:

yarn install typings

最后,在 boot.browser.ts 中添加 jquery-sparkline

boot.browser.ts

import 'reflect-metadata';
import 'zone.js';
import 'bootstrap';
import 'jquery-sparkline';
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.browser.module';
...

因为boot.browser.ts是用于在浏览器中运行的客户端打包配置。

!!!重要提示

不要将import 'jquery-sparkline';放置在应用程序/组件中,因为我们不希望注入到boot.server中(这将导致服务器端预渲染出错)。

此处需要完整的代码示例:dotnet-core-angular-sparkline


太棒了!完美地运行了! - Dan_J

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接