Skip to content

Commit

Permalink
Merge pull request #19 from Topthinking/master
Browse files Browse the repository at this point in the history
规范dll规则,仅production时,才会在项目根目录生成.dll文件
  • Loading branch information
Topthinking committed Jan 9, 2020
2 parents a32883c + 676a305 commit 68c9a17
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 34 deletions.
3 changes: 2 additions & 1 deletion examples/basic/package.json
Expand Up @@ -4,7 +4,8 @@
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "award dev"
"dev": "award dev",
"dev_build": "NODE_ENV=test award build"
},
"external": {
"a.js": "./static/jquery.js"
Expand Down
3 changes: 2 additions & 1 deletion packages/award-scripts/src/library/export/index.ts
Expand Up @@ -27,7 +27,8 @@ const start = (options: any, port?: number) => {
process.env.ROUTER = 'hash';
}
process.env.Browser = options.browser ? '1' : '0';
process.env.NODE_ENV = process.env.NODE_ENV || (options.browser ? 'test' : 'production');
process.env.NODE_ENV =
process.env.NODE_ENV || (options.browser || options.local ? 'test' : 'production');
await web_spa(assetPrefixs);
await render(assetPrefixs, port);
resolve();
Expand Down
Expand Up @@ -16,10 +16,16 @@ import * as fs from 'fs-extra';
import { complierInfo } from '../../tool';
import ProdCompiler from '../utils/prod.compiler';
import webpackCompiler from '../utils/webpack.compiler';
import Config from './webpack.config';
import WebpackConfig from './webpack.prod.config';

export default (dir: string, assetPrefixs: string, useRoute: boolean) => {
const dllDir = path.join(dir, '.dll');
// 需要特殊区分生产环境和其他环境
let dllDir: any = null;
if (process.env.NODE_ENV === 'production') {
dllDir = path.join(dir, '.dll');
} else {
dllDir = path.join(dir, 'node_modules', '.cache', 'award', '.dll');
}
const commonDll = path.join(dllDir, 'common.js');
const manifestJson = path.join(dllDir, 'manifest.json');
const dllLockFile = path.join(dllDir, '.lock');
Expand Down Expand Up @@ -107,7 +113,7 @@ export default (dir: string, assetPrefixs: string, useRoute: boolean) => {
}
}

const config: any = Config(entry, dir, assetPrefixs, envs);
const config: any = WebpackConfig(entry, dir, assetPrefixs, envs, dllDir);
if (useRoute) {
console.info(`检测发现当前项目${chalk.green(' 已使用 ')}路由,请确认!!!`);
} else {
Expand Down
Expand Up @@ -9,7 +9,7 @@ import { ProgressBarPlugin } from '../../webpack-plugins';
import { regNodeModules } from '../../help';
import webpackInclude from '../utils/include';

export default (entry: any, dir: string, assetPrefixs: string, envs: any) => {
export default (entry: any, dir: string, assetPrefixs: string, envs: any, dllDir: any) => {
const config = {
entry,
context: dir,
Expand All @@ -19,7 +19,7 @@ export default (entry: any, dir: string, assetPrefixs: string, envs: any) => {
},
mode: process.env.NODE_ENV === 'production' ? 'production' : 'none',
output: {
path: path.join(dir, '.dll'),
path: path.join(dllDir),
filename: 'common.js',
library: '__award_library__',
publicPath: assetPrefixs
Expand Down Expand Up @@ -49,7 +49,7 @@ export default (entry: any, dir: string, assetPrefixs: string, envs: any) => {
'process.env': envs
}),
new webpack.DllPlugin({
path: path.join(dir, '.dll', 'manifest.json'),
path: path.join(dllDir, 'manifest.json'),
name: '__award_library__'
}),
new ProgressBarPlugin({
Expand Down
2 changes: 1 addition & 1 deletion packages/award-scripts/src/tools/build/prod/web.ts
Expand Up @@ -3,7 +3,7 @@ import { clean } from '../../tool';
import { join } from 'path';

import web from '../webpack/web.prod.config';
import dll from '../dll';
import dll from '../dll/prod';

import ProdCompiler from '../utils/prod.compiler';
import webpackCompiler from '../utils/webpack.compiler';
Expand Down
Expand Up @@ -6,11 +6,28 @@ import * as MD5 from 'md5';
import { join } from 'path';

export default function dllSource(dir: any, map: any, publicPath: any) {
let dllDir: any = null;
if (process.env.NODE_ENV === 'production') {
dllDir = join(dir, '.dll');
} else {
dllDir = join(dir, 'node_modules', '.cache', 'award', '.dll');
}
// 确认dll文件里面的静态资源,拷贝到client-dist目录
// 主要拷贝图片和字体资源
const dllImages = join(dir, '.dll', 'images');
const dllFonts = join(dir, '.dll', 'fonts');
const dllStyles = join(dir, '.dll', 'styles', 'module.css');
// dll导出的公共依赖文件
const dllImages = join(dllDir, 'images');
const dllFonts = join(dllDir, 'fonts');
const dllStyles = join(dllDir, 'styles', 'module.css');
const Common_js = join(dllDir, 'common.js');

// 处理common.js文件
if (fs.existsSync(Common_js)) {
map['common.js'] =
process.env.HASHNAME === '1'
? MD5(fs.readFileSync(Common_js, 'utf-8')).substr(0, 9) + '.js'
: 'common.js';
fs.copySync(Common_js, join(dir, publicPath, 'scripts', map['common.js']));
}

// 处理map[0]的main.css
if (fs.existsSync(dllStyles)) {
Expand Down
Expand Up @@ -3,39 +3,20 @@
*/

import * as fs from 'fs-extra';
import * as MD5 from 'md5';
import { join } from 'path';

export default function initMap(dir: any, publicPath: any, exportConfig: any) {
const Common_js = join(dir, '.dll', 'common.js'); // dll导出的公共依赖文件
const publicMapFile = join(dir, publicPath, 'map.json'); // webpack编译导出的map文件
const exportMapFile = join(dir, exportConfig, 'map.json'); // 存储map文件的文件目录

const commonjs_exist = fs.existsSync(Common_js);
const publicMap_exist = fs.existsSync(publicMapFile);

if (publicMap_exist) {
fs.moveSync(publicMapFile, exportMapFile);
}

// 合并map内容,并对文件名称进行md5处理
const map = {
'common.js': commonjs_exist
? process.env.HASHNAME === '1'
? MD5(fs.readFileSync(Common_js, 'utf-8')).substr(0, 9) + '.js'
: 'common.js'
: null,
...(publicMap_exist
? {
...JSON.parse(fs.readFileSync(exportMapFile, 'utf-8'))
}
: {})
};

// 拷贝common.js
if (commonjs_exist) {
fs.copySync(Common_js, join(dir, publicPath, 'scripts', map['common.js']));
}
// 初始化map数据
const map = publicMap_exist ? JSON.parse(fs.readFileSync(exportMapFile, 'utf-8')) : {};

// 移动manifest文件位置
const manifest = join(dir, publicPath, 'manifest.js');
Expand Down
Expand Up @@ -122,10 +122,17 @@ export default function webConfig({
config.output.crossOriginLoading = 'anonymous';
}

let manifestFile: any = null;
if (process.env.NODE_ENV === 'production') {
manifestFile = path.join(dir, '.dll/manifest.json');
} else {
manifestFile = path.join(dir, 'node_modules', '.cache', 'award', '.dll/manifest.json');
}

config.plugins.push(
new webpack.DllReferencePlugin({
context: '.',
manifest: require(path.join(dir, '.dll/manifest.json'))
manifest: require(manifestFile)
})
);

Expand Down

0 comments on commit 68c9a17

Please sign in to comment.