nodejs 环境配置 webpack 安装及入门

原文链接: nodejs 环境配置 webpack 安装及入门

上一篇: js 时间库 moment

下一篇: js 基础

node安装vue-cli,配置vue开发环境和web

下载node

https://nodejs.org/zh-cn/

全局安装cnpm和webpack

 npm install -g cnpm --registry=https://registry.npm.taobao.org

cnpm install webpack -g

第一个项目结构

152924_9L8W_2856757.png

index.html

使用构建后的js文件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>test</title>
    </head>
    <body>
        hello webpack
        <script src="main.bundle.js" charset="utf-8"></script>
    </body>
</html>

one.js

导出模块

function one() {
    console.log("one,一")
}

module.exports = one;

在main中 使用

先导入,在使用

import one from "./one"
import two from "./two"
import three from "./three"
one()
two()
three()

构建

webpack main.js main.bundle.js

153123_VasW_2856757.png

浏览器查看效果

153214_Wo6D_2856757.png

使用配置文件,项目结构

154940_FHVN_2856757.png

webpack.config.js 是项目的配置文件,指定入口和出口

const path = require('path');

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

执行webpack即可成功构建

155105_oWjv_2856757.png

配置文件也可以更加复杂,可以进行其他配置

const path = require('path');

const config = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.bundle.js'
  },
  module: {
    rules: [
       
    ]
  }
};

module.exports = config;

配置可以打包css文件

使用css-loader加载css文件

安装

cnpm install --save-dev css-loader -g

配置,设置对所有的css文件使用css-loader 加载

注意,json 数组的末尾没有逗号!!!!!!!!!!!!!!

const path = require('path');

const config = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'main.bundle.js'
  },
  module: {
    rules: [
        { test: /\.css$/, use: 'css-loader' }

    ]
  }
};

module.exports = config;

引用直接引用即可

使用插件混淆代码

 cnpm install webpack --save-dev

在配置文件中使用插件        new webpack.optimize.UglifyJsPlugin() 即可

const webpack = require('webpack'); //访问内置的插件
const path = require('path');

const config = {
    entry: './src/main.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'main.bundle.js'
    },
    module: {
        rules: [{
            test: /\.css$/,
            use: 'css-loader'
        }]
    },
    plugins: [
        new webpack.optimize.UglifyJsPlugin()
    ]
};

module.exports = config;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值