javascript - Webpack 导入 LESS 文件时抛出 TypeError : Super expression must either be null or a function, not undefined

标签 javascript webpack css-loader webpack-3 extracttextwebpackplugin

我的文件 initialize.js 中有以下内容:

import App from './components/App';

import './styles/application.less';

document.addEventListener('DOMContentLoaded', () => {
    const app = new App();

    app.start();
});

webpack.config.js中我有:

'use strict';

const path = require('path');

const webpack = require('webpack');
const merge = require('webpack-merge');

const ProvidePlugin = webpack.ProvidePlugin;
const ModuleConcatenationPlugin = webpack.optimize.ModuleConcatenationPlugin;
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const extractLess = new ExtractTextPlugin({
    filename: 'app.css',
});

const webpackCommon = {
    entry: {
        app: ['./app/initialize']
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: [{
                loader: 'babel-loader?presets[]=es2015'
            }]
        }, {
            test: /\.hbs$/,
            use: {
                loader: 'handlebars-loader'
            }
        }, {
            test: /\.less$/,
            exclude: /node_modules/,
            use: extractLess.extract({
                use: [{
                    loader: 'css-loader'
                }, {
                    loader: 'less-loader'
                }],
                // use style-loader in development
                fallback: 'style-loader'
            }),
        }]
    },
    output: {
        filename: 'app.js',
        path: path.join(__dirname, './public'),
        publicPath: '/'
    },
    plugins: [
        extractLess,
        new CopyWebpackPlugin([{
            from: './app/assets/index.html',
            to: './index.html'
        }]),
        new ProvidePlugin({
            $: 'jquery',
            _: 'underscore'
        }),
        new ModuleConcatenationPlugin(),
    ],
};

module.exports = merge(webpackCommon, {
    devtool: '#inline-source-map',
    devServer: {
        contentBase: path.join(__dirname, 'public'),
        compress: true,
        port: 9000
    }
});

我尝试删除插件和 application.less 的内容,但我不断收到此错误:

ERROR in ./node_modules/css-loader!./node_modules/less-loader/dist/cjs.js!./app/styles/application.less

Module build failed: TypeError: Super expression must either be null or a function, not undefined
at ...
@ ./app/styles/application.less 4:14-127
@ ./app/initialize.js

如果我用 CSS 文件替换 LESS 文件并更新配置,它就可以正常工作,所以我猜问题与 less-loader 有关。 .

我正在使用 Webpack 3.4.1、Style Loader 0.18.2、LESS Loader 4.0.5、Extract Text Webpack Plugin 3.0.0 和 CSS 加载器 css-loader

最佳答案

我的错,我没有注意到我使用的是旧的 less 版本。这就是罪魁祸首。只需将其更新到2.7.2,问题就消失了。

关于javascript - Webpack 导入 LESS 文件时抛出 TypeError : Super expression must either be null or a function, not undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45510542/

相关文章:

reactjs - 尝试使用 webpack 和 babel 加载 css 时出现意外标记

javascript - 如何从远处找到latlong?

javascript - JS 代理 HTML5 Canvas 上下文

CSS 背景图像未使用 webpack 加载

javascript - 我可以在 webpack 中构建 sass/less/css 而不需要在我的 JS 中使用它们吗?

sass - 使用 Webpack 将选择器全局添加到我所有的 CSS 选择器(在构建时)

javascript - 两个 Html 按钮用于两种不同的功能

javascript - 如何将按钮事件连接到另一个 React 组件?

node.js - 有没有办法使用 webpack 找到模块所需的缓存键?

webpack - 我在 Webpack 中收到 .css 模块解析失败错误,即使我已经在 module.rules 中有一个 css-loader