-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luoxue
committed
Nov 18, 2019
1 parent
d7835fb
commit 9a1d29c
Showing
8 changed files
with
285 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,99 @@ | ||
# learn_webpack | ||
webpack 系列学习 | ||
|
||
## 打包 src 下的 index.js index.css 到 dist/bundle.js | ||
|
||
### 配置 | ||
|
||
package.json | ||
|
||
```json | ||
{ | ||
"scripts": { | ||
"dev": "cross-env NODE_ENV=development webpack", // 开发环境 | ||
"build": "cross-env NODE_ENV=production webpack" // 生产环境 | ||
}, | ||
"dependencies": { | ||
"cross-env": "^6.0.3", | ||
"css-loader": "^3.2.0", | ||
"rimraf": "^3.0.0", | ||
"webpack": "^4.41.2" | ||
}, | ||
"devDependencies": { | ||
"webpack-cli": "^3.3.10" | ||
} | ||
} | ||
``` | ||
|
||
webpack.config.js | ||
|
||
```js | ||
const path = require('path'); | ||
const rimraf = require('rimraf'); | ||
|
||
// 删除 dist 目录 | ||
rimraf.sync('dist'); | ||
|
||
// webpack 配置 | ||
module.exports = { | ||
entry: './src/index', | ||
mode: process.env.NODE_ENV, | ||
output: { | ||
filename: 'bundle.js', | ||
path: path.resolve(__dirname, 'dist') | ||
} | ||
}; | ||
``` | ||
|
||
src/index.js | ||
|
||
```js | ||
const css = require('css-loader!./index.css'); | ||
const a = 100; | ||
console.log(a, css); | ||
``` | ||
|
||
src/index.css | ||
|
||
```css | ||
body { | ||
width: 100%; | ||
height: 100vh; | ||
background-color: orange; | ||
} | ||
``` | ||
|
||
使用 webpack-chain 修改配置 | ||
|
||
```js | ||
const path = require('path'); | ||
const rimraf = require('rimraf'); | ||
const Config = require('webpack-chain'); | ||
const config = new Config(); | ||
const resolve = (src) => { | ||
return path.join(process.cwd(), src) | ||
} | ||
|
||
// 删除 dist 目录 | ||
rimraf.sync('dist') | ||
|
||
config | ||
// 入口 | ||
.entry('src/index') | ||
.add(resolve('src/index.js')) | ||
.end() | ||
// 模式 | ||
// .mode(process.env.NODE_ENV) 等价下面 | ||
.set('mode', process.env.NODE_ENV) | ||
// 出口 | ||
.output | ||
.path(resolve('dist')) | ||
.filename('[name].bundle.js'); | ||
|
||
config.module | ||
.rule('css') | ||
.test(/\.css$/) | ||
.use('css') | ||
.loader('css-loader') | ||
|
||
module.exports = config.toConfig(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
/******/ | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/******/ | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) { | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ } | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
/******/ | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
/******/ | ||
/******/ // Flag the module as loaded | ||
/******/ module.l = true; | ||
/******/ | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
/******/ | ||
/******/ | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
/******/ | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ | ||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); | ||
/******/ } | ||
/******/ }; | ||
/******/ | ||
/******/ // define __esModule on exports | ||
/******/ __webpack_require__.r = function(exports) { | ||
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { | ||
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); | ||
/******/ } | ||
/******/ Object.defineProperty(exports, '__esModule', { value: true }); | ||
/******/ }; | ||
/******/ | ||
/******/ // create a fake namespace object | ||
/******/ // mode & 1: value is a module id, require it | ||
/******/ // mode & 2: merge all properties of value into the ns | ||
/******/ // mode & 4: return value when already ns object | ||
/******/ // mode & 8|1: behave like require | ||
/******/ __webpack_require__.t = function(value, mode) { | ||
/******/ if(mode & 1) value = __webpack_require__(value); | ||
/******/ if(mode & 8) return value; | ||
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; | ||
/******/ var ns = Object.create(null); | ||
/******/ __webpack_require__.r(ns); | ||
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); | ||
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); | ||
/******/ return ns; | ||
/******/ }; | ||
/******/ | ||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
/******/ | ||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
/******/ | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ | ||
/******/ | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ({ | ||
|
||
/***/ "./node_modules/css-loader/dist/runtime/api.js": | ||
/*!*****************************************************!*\ | ||
!*** ./node_modules/css-loader/dist/runtime/api.js ***! | ||
\*****************************************************/ | ||
/*! no static exports found */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
|
||
"use strict"; | ||
eval("\n\n/*\n MIT License http://www.opensource.org/licenses/mit-license.php\n Author Tobias Koppers @sokra\n*/\n// css base code, injected by the css-loader\n// eslint-disable-next-line func-names\nmodule.exports = function (useSourceMap) {\n var list = []; // return the list of modules as css string\n\n list.toString = function toString() {\n return this.map(function (item) {\n var content = cssWithMappingToString(item, useSourceMap);\n\n if (item[2]) {\n return \"@media \".concat(item[2], \"{\").concat(content, \"}\");\n }\n\n return content;\n }).join('');\n }; // import a list of modules into the list\n // eslint-disable-next-line func-names\n\n\n list.i = function (modules, mediaQuery) {\n if (typeof modules === 'string') {\n // eslint-disable-next-line no-param-reassign\n modules = [[null, modules, '']];\n }\n\n var alreadyImportedModules = {};\n\n for (var i = 0; i < this.length; i++) {\n // eslint-disable-next-line prefer-destructuring\n var id = this[i][0];\n\n if (id != null) {\n alreadyImportedModules[id] = true;\n }\n }\n\n for (var _i = 0; _i < modules.length; _i++) {\n var item = modules[_i]; // skip already imported module\n // this implementation is not 100% perfect for weird media query combinations\n // when a module is imported multiple times with different media queries.\n // I hope this will never occur (Hey this way we have smaller bundles)\n\n if (item[0] == null || !alreadyImportedModules[item[0]]) {\n if (mediaQuery && !item[2]) {\n item[2] = mediaQuery;\n } else if (mediaQuery) {\n item[2] = \"(\".concat(item[2], \") and (\").concat(mediaQuery, \")\");\n }\n\n list.push(item);\n }\n }\n };\n\n return list;\n};\n\nfunction cssWithMappingToString(item, useSourceMap) {\n var content = item[1] || ''; // eslint-disable-next-line prefer-destructuring\n\n var cssMapping = item[3];\n\n if (!cssMapping) {\n return content;\n }\n\n if (useSourceMap && typeof btoa === 'function') {\n var sourceMapping = toComment(cssMapping);\n var sourceURLs = cssMapping.sources.map(function (source) {\n return \"/*# sourceURL=\".concat(cssMapping.sourceRoot).concat(source, \" */\");\n });\n return [content].concat(sourceURLs).concat([sourceMapping]).join('\\n');\n }\n\n return [content].join('\\n');\n} // Adapted from convert-source-map (MIT)\n\n\nfunction toComment(sourceMap) {\n // eslint-disable-next-line no-undef\n var base64 = btoa(unescape(encodeURIComponent(JSON.stringify(sourceMap))));\n var data = \"sourceMappingURL=data:application/json;charset=utf-8;base64,\".concat(base64);\n return \"/*# \".concat(data, \" */\");\n}\n\n//# sourceURL=webpack:///./node_modules/css-loader/dist/runtime/api.js?"); | ||
|
||
/***/ }), | ||
|
||
/***/ "./src/index.js": | ||
/*!**********************!*\ | ||
!*** ./src/index.js ***! | ||
\**********************/ | ||
/*! no static exports found */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
|
||
eval("const css = __webpack_require__(/*! ./style/index.css */ \"./src/style/index.css\")\nconst a = 100;\nconsole.log(a, css)\n\n//# sourceURL=webpack:///./src/index.js?"); | ||
|
||
/***/ }), | ||
|
||
/***/ "./src/style/index.css": | ||
/*!*****************************!*\ | ||
!*** ./src/style/index.css ***! | ||
\*****************************/ | ||
/*! no static exports found */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
|
||
eval("exports = module.exports = __webpack_require__(/*! ../../node_modules/css-loader/dist/runtime/api.js */ \"./node_modules/css-loader/dist/runtime/api.js\")(false);\n// Module\nexports.push([module.i, \"body {\\n width: 100%;\\n height: 100vh;\\n background-color: orange;\\n}\", \"\"]);\n\n\n//# sourceURL=webpack:///./src/style/index.css?"); | ||
|
||
/***/ }), | ||
|
||
/***/ 0: | ||
/*!****************************!*\ | ||
!*** multi ./src/index.js ***! | ||
\****************************/ | ||
/*! no static exports found */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
|
||
eval("module.exports = __webpack_require__(/*! /Users/luoxue/Desktop/learn/learn_webpack/src/index.js */\"./src/index.js\");\n\n\n//# sourceURL=webpack:///multi_./src/index.js?"); | ||
|
||
/***/ }) | ||
|
||
/******/ }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
var a = 100; | ||
|
||
console.log(a) | ||
const css = require('./style/index.css') | ||
const a = 100; | ||
console.log(a, css) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
body { | ||
width: 100%; | ||
height: 100vh; | ||
background-color: orange; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const path = require('path'); | ||
const rimraf = require('rimraf'); | ||
const Config = require('webpack-chain'); | ||
const config = new Config(); | ||
const resolve = (src) => { | ||
return path.join(process.cwd(), src) | ||
} | ||
|
||
// 删除 dist 目录 | ||
rimraf.sync('dist') | ||
|
||
config | ||
// 入口 | ||
.entry('src/index') | ||
.add(resolve('src/index.js')) | ||
.end() | ||
// 模式 | ||
// .mode(process.env.NODE_ENV) 等价下面 | ||
.set('mode', process.env.NODE_ENV) | ||
// 出口 | ||
.output | ||
.path(resolve('dist')) | ||
.filename('[name].bundle.js'); | ||
|
||
config.module | ||
.rule('css') | ||
.test(/\.css$/) | ||
.use('css') | ||
.loader('css-loader') | ||
|
||
module.exports = config.toConfig(); |