Skip to content

Commit

Permalink
添加babel-loader ts-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
zk922 committed Jan 10, 2019
1 parent d78d226 commit 7f60ed4
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 118 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
"serve": "node ./scripts/serve.js"
},
"devDependencies": {
"@babel/core": "^7.2.0",
"@babel/preset-env": "^7.2.0",
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.2.3",
"@types/ejs": "^2.6.1",
"@types/express": "^4.16.0",
"@types/loader-utils": "^1.1.3",
"@types/lodash": "^4.14.119",
"@types/webpack": "^4.4.21",
"babel-loader": "^8.0.4",
"babel-loader": "^8.0.5",
"css-loader": "^2.0.1",
"ejs": "^2.6.1",
"express": "^4.16.4",
Expand All @@ -35,7 +35,7 @@
"rimraf": "^2.6.3",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"ts-loader": "^5.3.2",
"ts-loader": "^5.3.3",
"typescript": "^3.2.2",
"url-loader": "^1.1.2",
"webpack": "^4.27.1",
Expand Down
5 changes: 2 additions & 3 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const fs = require('fs');
const path = require('path');

const webpack = require('webpack');
const set_NODE_ENV = require('./config_scripts/argv');
const generateConfig = require('./config_scripts/generate');
const rimraf = require('rimraf');

//1.根据输入参数,设置process.env.NODE_ENV
const set_NODE_ENV = require('./config_scripts/argv');
set_NODE_ENV();

//2.动态生成配置文件,执行打包脚本
const generateConfig = require('./config_scripts/generate');
generateConfig().then(config => {
//2.1.删除旧dist目录
let distPath = path.resolve(process.cwd(), 'dist');
Expand Down
121 changes: 121 additions & 0 deletions scripts/config_scripts/0.loader-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const {SRC_PATH, PROJECT_PATH} = require('./0.app-path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const isProduction = process.env.NODE_ENV === 'production';
/**
* 各种loader文件的默认配置,需要在设置环境变量的方法set_NODE_ENV之后再引入。
* **/
const fontLoader = {
test: /\.(eot|woff2?|ttf)$/,
use: [
{
loader: "url-loader",
options: {
name: process.env.NODE_ENV === 'production' ? 'fonts/[name].[hash].[ext]' : '[path][name].[ext]',
limit: 5120,
publicPath: "/",
context: SRC_PATH,
outputPath: "/"
}
}
]
};
const imgLoader = {
test: /\.(png|jpe?g|gif|svg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10240,
name: process.env.NODE_ENV === 'production' ? 'images/[hash].[ext]' : '[path][name].[ext]',
publicPath: '/',
context: SRC_PATH, //源码目录,这里更改context,是为了在开发环境下,导出图片和图片源的路径一致
outputPath: '/'
}
}
]
};
const languageLoader = {
ts: {
test: /\.tsx?$/,
use: [
'babel-loader',
'ts-loader'
]
},
js: {
test: /\.js$/,
use: [
'babel-loader'
]
}
};
const styleLoader = {
sass: {
test: /\.(scss|sass)$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
]
},
less: {
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader", // translates CSS into CommonJS
"less-loader" // compiles Less to CSS
]
},
css: {
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader", // translates CSS into CommonJS
]
}
};



const htmlLoaderConfig = {
minimize: isProduction,
removeComments: isProduction,
collapseWhitespace: isProduction,
attrs: [':src', ':href']
};
const templateConfig = {
html: {
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: htmlLoaderConfig
}
]
},
ejs: {
test: /\.ejs$/,
use: [
{
loader: 'html-loader',
options: htmlLoaderConfig
},
{
loader: path.resolve(__dirname, '../loaders/ejs-loader.js'), //自己简单实现的使用ejs2 engine的ejs-loader
options: {
context: PROJECT_PATH
}
}
]
}
};


module.exports = {
fontLoader,
imgLoader,
languageLoader,
styleLoader,
templateConfig
};
18 changes: 2 additions & 16 deletions scripts/config_scripts/4.font-config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
const {SRC_PATH} = require('./0.app-path');
let {fontLoader} = require('./0.loader-config');
/**
* 添加字体文件配置
* **/
function addFontConfig(config) {
config.module.rules.push({
test: /\.(eot|woff2?|ttf)$/,
use: [
{
loader: "url-loader",
options: {
name: process.env.NODE_ENV === 'production' ? 'fonts/[name].[hash].[ext]' : '[path][name].[ext]',
limit: 5120,
publicPath: "/",
context: SRC_PATH,
outputPath: "/"
}
}
]
});
config.module.rules.push(fontLoader);
return config;
}
module.exports = addFontConfig;
18 changes: 2 additions & 16 deletions scripts/config_scripts/5.image-config.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
const {SRC_PATH} = require('./0.app-path');
const {imgLoader} = require('./0.loader-config');
/**
* 添加对图片的loader配置
* **/
function addImageConfig(config) {
config.module.rules.push({
test: /\.(png|jpe?g|gif|svg)$/,
use: [
{
loader: 'url-loader',
options: {
limit: 10240,
name: process.env.NODE_ENV === 'production' ? 'images/[hash].[ext]' : '[path][name].[ext]',
publicPath: '/',
context: SRC_PATH, //源码目录,这里更改context,是为了在开发环境下,导出图片和图片源的路径一致
outputPath: '/'
}
}
]
});
config.module.rules.push(imgLoader);
return config;
}

Expand Down
30 changes: 30 additions & 0 deletions scripts/config_scripts/6.language-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
const appConfig = require('../../app.config');
const {languageLoader} = require('./0.loader-config');
let {getType} = require('./utils');


/**
* 对代码配置babel
* **/
module.exports = function (config) {


let language = appConfig.language || 'js';
let resolve;
if(getType(language) === 'string'){
resolve = [language];
}
else if(getType(language) === 'array'){
resolve = language;
}
else {
throw new Error(`app.config language 配置错误`);
}
config.resolve.extensions = resolve;

resolve.forEach(v => {
if(!languageLoader[v]){
throw new Error(`未找到${v}文件对应的loader默认配置`);
}
config.module.rules.push(languageLoader[v]);
});

return config;
};
46 changes: 10 additions & 36 deletions scripts/config_scripts/7.style-config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const appConfig = require('../../app.config');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const {styleLoader} = require('./0.loader-config');
const {getType} = require('./utils');
/**
* 添加样式表的配置
Expand All @@ -9,47 +10,20 @@ module.exports = function addStyleConfig(config) {
let isProduction = process.env.NODE_ENV === 'production';
let ext = appConfig.style || 'css';

let sassConfig = {
test: /\.(scss|sass)$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
]
};
let lessConfig = {
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader", // translates CSS into CommonJS
"less-loader" // compiles Less to CSS
]
};
let cssConfig = {
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader", // translates CSS into CommonJS
]
};

let pluginConfig = new MiniCssExtractPlugin({ //分离css为单独文件的插件
filename: isProduction ? "[name]/style/[name].[hash].css" : "[name]/style/[name].css",
chunkFilename: isProduction ? "[name].[hash].css" : "[name].css"
});

function addConfig(ext) {
if(ext === 'scss' || 'sass'){
config.module.rules.push(sassConfig);
}
else if(ext === 'less'){
config.module.rules.push(lessConfig);
}
else if(ext === 'css'){
config.module.rules.push(cssConfig);
let added = false;
function addConfig(ext){
if((ext === 'scss' || ext === 'sass') && !added){
config.module.rules.push(styleLoader.sass);
added = true;
}
else {
throw Error('没有找到对应的样式表配置');
else{
if(!styleLoader[ext]) throw Error('没有找到对应的样式表配置');
config.module.rules.push(styleLoader[ext]);
}
}

Expand All @@ -64,7 +38,7 @@ module.exports = function addStyleConfig(config) {
})
}
else {
throw Error('app.config style 配置错误');
throw Error('app.config style 配置错误。配置应为字符串或者数组');
}
config.plugins.push(pluginConfig);

Expand Down
40 changes: 3 additions & 37 deletions scripts/config_scripts/8.template-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,14 @@ const fs = require('fs');
const path = require('path');

const HtmlWebpackPlugin = require('html-webpack-plugin');
const {PAGE_PATH, PROJECT_PATH} = require('./0.app-path');
const {PAGE_PATH} = require('./0.app-path');
const appConfig = require('../../app.config');
const {getType} = require('./utils');
const {templateConfig} = require('./0.loader-config');

module.exports = function (config){
//1.获取环境信息
const env = process.env.NODE_ENV;
const isProduction = env === 'production';

//2.模板的loader配置
const htmlLoaderConfig = {
minimize: isProduction,
removeComments: isProduction,
collapseWhitespace: isProduction,
attrs: [':src', ':href']
};
const templateConfig = {
html: {
test: /\.html$/,
use: [
{
loader: 'html-loader',
options: htmlLoaderConfig
}
]
},
ejs: {
test: /\.ejs$/,
use: [
{
loader: 'html-loader',
options: htmlLoaderConfig
},
{
loader: path.resolve(__dirname, '../loaders/ejs-loader.js'), //自己简单实现的使用ejs2 engine的ejs-loader
options: {
context: PROJECT_PATH
}
}
]
}
};
//3.获取当前的模板选择,默认html
let ext = appConfig.template || 'html';

Expand Down Expand Up @@ -84,7 +50,7 @@ module.exports = function (config){
/** 添加指定loader **/
switchExtType(function (ext){
if(templateConfig[ext]){
config.module.rules.unshift(templateConfig[ext]);
config.module.rules.push(templateConfig[ext]);
}
else {
throw Error(`未找到${ext}文件对应的loader配置`);
Expand Down
Loading

0 comments on commit 7f60ed4

Please sign in to comment.