zhouna 5 years ago
parent
commit
a788c4da40
8 changed files with 249 additions and 0 deletions
  1. 32 0
      .gitignore
  2. 43 0
      package.json
  3. 16 0
      server.js
  4. 0 0
      src/css/common.css
  5. 15 0
      src/html/index.html
  6. 1 0
      src/js/es5-shim.js
  7. 2 0
      src/js/index.js
  8. 140 0
      webpack.config.js

+ 32 - 0
.gitignore

@@ -0,0 +1,32 @@
+# Logs
+logs
+*.log
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+icssfront.iml
+package-lock.json
+yarn.lock
+*-bak.*
+
+# Dependency directories
+node_modules/
+package-lock.json
+# Optional eslint cache
+.eslintcache
+/.DS_Store
+dist/
+dist
+.idea
+
+.vscode
+/.idea
+/dist
+
+
+# node
+app/
+app/package-lock.json
+app/yarn.lock
+app/logs
+app/node_modules

+ 43 - 0
package.json

@@ -0,0 +1,43 @@
+{
+  "name": "zn",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "dependencies": {
+    "bluebird": "^3.5.5",
+    "es3ify-loader": "^0.2.0",
+    "jquery": "^1.12.4",
+    "webpack-cli": "^3.3.1"
+  },
+  "devDependencies": {
+    "@babel/core": "^7.4.4",
+    "@babel/preset-env": "^7.4.4",
+    "babel-loader": "^8.0.5",
+    "clean-webpack-plugin": "^2.0.1",
+    "css-loader": "^2.1.1",
+    "express": "^4.16.4",
+    "file-loader": "^3.0.1",
+    "html-webpack-plugin": "^3.2.0",
+    "less": "^3.9.0",
+    "less-loader": "^5.0.0",
+    "mini-css-extract-plugin": "^0.6.0",
+    "style-loader": "^0.23.1",
+    "uglifyjs-webpack-plugin": "^2.1.2",
+    "webpack": "^4.31.0",
+    "webpack-dev-middleware": "^3.6.2",
+    "webpack-dev-server": "^3.3.1"
+  },
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "build": "webpack",
+    "watch": "webpack --watch",
+    "dev": "webpack-dev-server --open",
+    "server": "node server.js"
+  },
+  "repository": {
+    "type": "git",
+    "url": "http://192.168.2.236:10080/zhouna/icssDService.git"
+  },
+  "author": "zn",
+  "license": "ISC"
+}

+ 16 - 0
server.js

@@ -0,0 +1,16 @@
+const express = require('express');
+const webpack = require('webpack');
+const webpackDevMiddleware = require('webpack-dev-middleware');
+
+const app = express();
+const config = require('./webpack.config');
+const compiler = webpack(config);
+
+app.use(webpackDevMiddleware(compiler,{
+  publicPath: config.output.publicPath
+}));
+
+app.listen(3000,function(){
+  console.log('app listening on port 3000\n');
+});
+

+ 0 - 0
src/css/common.css


+ 15 - 0
src/html/index.html

@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <meta http-equiv="X-UA-Compatible" content="ie=edge">
+  <title><%= htmlWebpackPlugin.options.title %></title>
+</head>
+
+<body>
+  aaaa111
+</body>
+
+</html>
+

File diff suppressed because it is too large
+ 1 - 0
src/js/es5-shim.js


+ 2 - 0
src/js/index.js

@@ -0,0 +1,2 @@
+const $ = require("jquery");
+document.write("aaaa");

+ 140 - 0
webpack.config.js

@@ -0,0 +1,140 @@
+const path = require('path');
+const HtmlWebpackPlugin = require('html-webpack-plugin');
+const CleanWebpackPlugin = require('clean-webpack-plugin') // 清空打包目录的插件
+const MiniCssExtractPlugin = require('mini-css-extract-plugin');
+const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
+const webpack = require('webpack');
+const proxyHost = "http://192.168.2.236:5050";
+module.exports = {
+  entry: {
+    index: path.resolve(__dirname, 'src/js', 'index.js'),
+    vendor: 'lodash'// 多个页面所需的公共库文件,防止重复打包带入
+  },
+  output: {
+    publicPath: '/', //这里要放的是静态资源CDN的地址
+    path: path.resolve(__dirname, 'dist'),
+    filename: 'js/[name].js'
+  },
+  resolve: {
+    extensions: [".js", ".css", ".json"],
+    alias: {} //配置别名可以加快webpack查找模块的速度
+  },
+  plugins: [// 多入口的html文件用chunks这个参数来区分
+    new HtmlWebpackPlugin({
+      title: 'index',
+      template: path.resolve(__dirname, 'src/html', 'index.html'),
+      filename: 'index.html',
+      chunks: ['index', 'vendor', 'common'],
+      inject: true,
+      hash: true, //防止缓存
+      minify: {
+        removeAttributeQuotes: true, //压缩 去掉引号
+        removeComments: true, //移除HTML中的注释
+        collapseWhitespace: true //删除空白符与换行符
+      }
+    }),
+    new MiniCssExtractPlugin({
+      filename: 'css/[name].css',
+      chunkFilename: '[id].css'
+    }),
+    new webpack.HotModuleReplacementPlugin(),
+    new CleanWebpackPlugin()
+  ],
+  optimization: { //webpack4.x的最新优化配置项,用于提取公共代码
+    minimizer: [
+      new UglifyJsPlugin({
+        uglifyOptions: {
+          ie8: true,
+          compress: {
+            properties: false,
+            warnings: false
+          },
+          mangle: {
+            screw_ie8: false,
+            except: ['e']
+          },
+          output: {
+              beautify: true
+          },
+          sourceMap: false
+        }
+      })
+    ],
+    splitChunks: {
+      cacheGroups: {
+        commons: {
+          chunks: "initial",
+          name: "common",
+          minChunks: 2,
+          maxInitialRequests: 5, // The default limit is too small to showcase the effect
+          minSize: 0, // This is example is too small to create commons chunks
+          reuseExistingChunk: true // 可设置是否重用该chunk
+        }
+      }
+    }
+  },
+      
+  module: {
+    rules: [
+      {
+          test: /.js$/,
+          enforce: 'post', 
+          loader: 'es3ify-loader'
+      },
+      {
+        test: /\.m?js$/,
+        exclude: /(node_modules|bower_components)/,
+        use: {
+          loader: 'babel-loader',
+          options: {
+            presets:['@babel/preset-env']
+          }
+        }
+      },
+      {
+        test: /\.css$/,
+        use: [{
+            loader: MiniCssExtractPlugin.loader
+          },
+          'css-loader'
+        ]
+      },
+      {
+        test: /\.less$/,
+        use: [
+          {
+            loader: 'style-loader', // creates style nodes from JS strings
+          },
+          {
+            loader: 'css-loader', // translates CSS into CommonJS
+          },
+          {
+            loader: 'less-loader', // compiles Less to CSS
+          },
+        ],
+      },
+      { 
+        test: /\.(png|jpg|jpeg|gif|svg)$/,
+        use: {
+          loader: 'file-loader',
+          options: {
+            outputPath: 'images/', // 图片输出的路径和存储路径保持一致
+            limit: 10000,
+            name: '[name].[ext]'
+          }
+        }
+      }
+    ]
+  },
+  // devtool: 'cheap-module-eval-source-map', //开发环境cheap-module-eval-source-map   //生产环境cheap-module-source-map
+  mode: 'development',
+  devServer: {
+    contentBase: "./dist", //静态文件根目录
+    proxy: {
+      '/api': proxyHost
+    },
+    hot: true,
+    openPage:'index.html'
+    
+  }
+}