简体   繁体   English

如何使用 webpack 捆绑 javascript 文件/Jquery 文件?

[英]How to bundle javascript files/Jquery Files using webpack?

I was able to succeffully bundles my sass files using webpack but i am having trouble packing my javascript files.我能够使用 webpack 成功捆绑我的 sass 文件,但我无法打包我的 javascript 文件。 The way i do it on css is that the entry point is./public/javascript/index.js which contains import '../sass/main.scss';我在 css 上这样做的方式是入口点是 ./public/javascript/index.js ,其中包含 import '../sass/main.scss'; and then main.scss contains like @import "layout/header";然后 main.scss 包含类似 @import "layout/header"; and a lot more.还有更多。 The question is that how would i pack javascript files?问题是我将如何打包 javascript 文件?

for example i have my javascript files on \public\js\ -- > js files here like test.js and etc.例如,我的 javascript 文件位于 \public\js\ --> js 文件中,例如 test.js 等。

I have provided my webpack config below.我在下面提供了我的 webpack 配置。

My webpack config我的 webpack 配置

const javascript = {
    test: /\.(js)$/,
    use: {
        loader: 'babel-loader',
        options: {
            presets: ['@babel/env']
        }
    }
}

const images = {
    test: /\.(jpg|jpeg|png|gif)$/,
    use: {
        loader: 'file-loader',
        options: {
            outputPath: '../images/'
        }
    }
}

const postcss = {
    loader: 'postcss-loader',
    options: {
        plugins() { return [ autoprefixer({ browsers: 'last 5 versions' }) ]; }
    }
}

const styles = {
    test: /\.scss$/,
    use: [
        'style-loader',
        MiniCssExtractPlugin.loader,
        'css-loader',
        postcss,
        'sass-loader'
    ]
}

const config = {
    watch: true,
    watchOptions: {
        poll: true,
        ignored: /node_modules/
    },
    entry: './public/javascript/index.js',
    output: {
        path: path.resolve(__dirname, 'public', 'dist'),
        filename: 'main.js'
    },
    module: {
        rules: [
            javascript,
            images,
            styles
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: 'style.bundle.css'
        }),
        new OptimizeCssAssetsPlugin({
            assetNameRegExp: /\.css$/g,
            cssProcessor: require('cssnano'),
            cssProcessorOptions: { preset: 'default', discardComments: { removeAll: true } },
            canPrint: true
        })
    ]
}

Webpack is, by simplest explanation, a javascript bundler. Webpack 通过最简单的解释是 javascript 捆绑器。 There are plugins to support other assets like css, but the webpack hello world will bundle your javascript for you.有插件支持 css 等其他资产,但 webpack hello world 将为您捆绑您的 javascript。

By defining an entry point, you're asking webpack to traverse the module dependency tree you've created with import/require statements, and bundle the scripts it finds along the way into a single output file.通过定义入口点,您要求 webpack 遍历您使用 import/require 语句创建的模块依赖关系树,并将沿途找到的脚本捆绑到单个 output 文件中。

If you were to create a file in./public/javascript named "foo.js" and add an import "./foo";如果您要在./public/javascript 中创建一个名为“foo.js”的文件并添加一个import "./foo"; in your index.js file, you'd see that after running your webpack build, any code you add to foo.js will appear in your main.js在您的 index.js 文件中,您会看到在运行 webpack 构建后,您添加到 foo.js 的任何代码都将出现在您的 main.js 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM