配置webpack2 设置modules: false 找不到模块

发布于 2022-09-04 11:59:43 字数 2766 浏览 6 评论 0

今天配置的webpack2,想用一下对es6的import支持,在我{ modules: false }设置上时,然后编译酒会出现找不到模块的错误信息,明显是webpack2不能解析import语法,请问这是什么问题呢?

var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");  //CSS 模块加载
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: [
        'react-hot-loader/patch',
        'webpack-dev-server/client?http://127.0.0.1:3001',
        'webpack/hot/dev-server',
        "./js/src/Unique_main.jsx"
    ],
    output: {
        path: path.join(__dirname, 'js/dist'),
        // path: __dirname,
        filename: "bundle.js",
        publicPath: '/assets/',
        chunkFilename: '[name].[chunkhash:10].chunk.js',
    },
   plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new ExtractTextPlugin({
                filename: "Respectively.css",
                disable: false,
                allChunks: true
        }),
       new HtmlWebpackPlugin({ title: 'Tree-shaking' })
    ],
    module: {
        // loaders:[
        rules: [
            { 
                test: /\.jsx$/,
                exclude: /node_modules/,
                // loader: 'babel-loader',
                use:[
                    {
                        loader:"babel-loader",
                        options: {
                            presets: ['es2015',{ modules: false },'stage-2'],
                            //按需加载模块,antd...
                            plugins:[["import",[
                                {
                                    "libraryName": "antd",
                                    "libraryDirectory": "lib",
                                    "style": true
                                },
                                {
                                    "libraryName": "antd-mobile",
                                    "libraryDirectory": "component",
                                },
                            ]],
                                "transform-decorators-legacy",
                                "transform-class-properties"
                            ]
                        }
                    },
                ]


            {
                test:/\.css$/,
                exclude:/node_modules/,
                use:ExtractTextPlugin.extract({
                     fallback: "style-loader",
                     use:["css-loader"]
                     // publicPath: "/dist"
                })
            },
            {
                test:/\.(png|jpg|svg)$/,
                use:['file-loader'],
            },
            {
                test:/\.less$/,
                use:['style-loader','css-loader','less-loader']
            },
        ]
    },
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾家有女初长成 2022-09-11 11:59:43

我查阅文档,并试了下,不论是 {resolve:{modules:false}} 还是 {module:false} ,全是提示参数错误的。要补贴下webpack.config.js?

我试了下,没问题,支持import/export

webpack.config.js

const path = require('path');

module.exports = {
    entry: {
        main: './src/main.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].js'
    }
}

./src/main.js

import other from './other.js';
console.log('abcd...');
other();

./src/other.js

export default () => {
    console.log('qwerty...');
}
小ぇ时光︴ 2022-09-11 11:59:43

应该是babel的配置问题吧,module:false就不会对es6的语法(诸如:import)进行转换了,这里有说明:http://babeljs.io/docs/plugin...

clipboard.png

鹊巢 2022-09-11 11:59:43

看着像是没有配置 resolve 字段
resolve: {

extensions: ['.ts', '.tsx', '.js']

},

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文