123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- const fs = require('fs');
- const path = require('path');
- const webpack = require('webpack');
- const webpackDevServer = require('webpack-dev-server');
- const webpackBaseConfig = require('./webpack.base.config');
- const webpackDllConfig = require('./webpack.dll.config');
- const getEntry = require('./getEntry');
- const config = require('../new-config.json');
- const dependjsConf = require('../dependjs.json');
- Object.assign(config, dependjsConf);
- // 在编译之后打印文字的webpack插件
- const LogAfterCompilePlugin = require('./LogAfterCompilePlugin');
- const CreateHtmlPlugin = require('./create-html-webpack-plugin/CreateHtmlPlugin');
- // 工具集
- const h = require('./hammer');
- // 入口对象集
- let entryTmp = getEntry();
- let libEntryTmp = getEntry('lib');
- // 取出js入口路径
- let entries = entryTmp.entry;
- let libEntries = libEntryTmp.entry;
- // 取出模版路径
- let entryTmpMap = entryTmp.template;
- let libEntryTmpMap = libEntryTmp.template;
- // 目标路径
- let targetPath = webpackBaseConfig.output.path
- // 定义项目路径
- let projectPath = path.join(__dirname, '../');
- // 根据模版路径文件,取出模版
- let templateMap = getTemplate(entryTmpMap);
- module.exports = (m) => {
- copyDir();
- if (config['dll-entry'] && config['dll-entry'].length > 0) {
- runDllCompile()
- .then(() => {
- let manifestPath = getManifestPath();
- webpackBaseConfig.plugins.unshift(
- new webpack.DllReferencePlugin({
- manifest: require(manifestPath)
- })
- )
- runCompile(m);
- });
- } else {
- runCompile(m);
- }
- }
- // 获取模板内容并且拼装模板
- function getTemplate(templateMap) {
- let templateContentMap = {};
- templateContentMap['default'] = path.join(projectPath, config['default-template'])
- Object.keys(templateMap).map((item) => {
- let templatePath = templateMap[item];
- templateContentMap[item] = path.join(projectPath, templatePath)
- });
- return templateContentMap
- }
- // 获取dll生成得manifest文件
- function getManifestPath() {
- let outputPath = webpackDllConfig.output.path;
- let fileList = fs.readdirSync(outputPath);
- let manifestPath = '../dist/hrpub/dll/vendor-manifest.json';
- for (let filename of fileList) {
- if (path.extname(filename) === '.json') {
- manifestPath = path.join(outputPath, filename);
- break;
- }
- }
- return manifestPath;
- }
- // 运行dll编译
- function runDllCompile() {
- webpackDllConfig.plugins.push(new LogAfterCompilePlugin({
- word: 'note: dll has compiled'
- }));
- return new Promise((resolve, reject) => {
- webpack(webpackDllConfig, (err) => {
- if (err) {
- console.log(err);
- }
- resolve();
- });
- });
- }
- // 执行编译
- function runCompile(m = []) {
- let pageCompiler = null;
- let nEntries = {};
- let reg = new RegExp(m.join('|'));
- let externals = {};
- /*let configjs = ''; //额外配置的js文件
- let configcss = ''; //额外配置的css文件*/
- Object.keys(entries).map((item) => {
- if (reg.test(item) || m.length === 0) {
- nEntries[item] = entries[item];
- }
- });
- // dependModuleName: 依赖的模块名
- // dependjs: 依赖的js文件配置
- /*if (Array.isArray(config.dependjs)) {
- configjs += config.dependjs.map(src => {
- let moduleName = /(?:\.\.\/)*([^\.]*)\.js/.exec(src);
- if (moduleName && moduleName[1]) {
- externals[moduleName[1]] = moduleName[1];
- }
- return `<script src="${src}?v=${Date.now()}"></script>`;
- }).join('');
- }*/
- // dependModuleName: 依赖的模块名
- if (Array.isArray(config.dependModuleName)) {
- // 打包时排除
- config.dependModuleName.forEach(item => (externals[`${item}`] = `${item}/index`));
- }
- // dependcss: 依赖的css文件配置
- /*if (Array.isArray(config.dependcss)) {
- configcss += config.dependcss
- .map(item => `<link rel="stylesheet" href=${item}?v=${Date.now()}>`)
- .join('');
- }*/
- entries = nEntries;
- let pageWBC = {
- ...webpackBaseConfig,
- plugins: [...webpackBaseConfig.plugins],
- entry: entries,
- externals: {
- ...webpackBaseConfig.externals,
- ...externals
- }
- };
- pageWBC.plugins.push(new CreateHtmlPlugin({
- templateMap: templateMap,
- beforeAppendCss: ($, filePath = "") => {
- // 为了去掉平台对于console的影响
- /*$('head').prepend(`
- <script>
- window.__console = {
- ...window.console
- };
- </script>
- `);*/
- let configcss = getConfigCss(filePath.substr(0, filePath.length - 5));
- $('head').prepend(configcss)
- },
- beforeAppendJs: ($, filePath = "") => {
- if (config['dll-entry'] && config['dll-entry'].length > 0) {
- $('body').append(`<script src="/hrpub/dll/vendor.js"></script>`);
- }
- /*$('body').append(`
- <script>
- window.console = window.__console
- </script>
- `);*/
- let configjs = getConfigJs(filePath.substr(0, filePath.length - 5));
- $('body').append(configjs);
- }
- }));
- /*Object.keys(entries).map((key) => {
- let htmlWebpackOption = {
- filename: `${key}.html`,
- template: path.join(projectPath, config['default-template']),
- chunks: [key],
- inject: true,
- templateParameters: {
- buildInfo: '',
- configjs: configjs, //为模板添加js
- configcss: configcss //为模板添加css
- }
- };
- if (entryTmpMap[key]) {
- htmlWebpackOption.template = path.join(projectPath, entryTmpMap[key]);
- }
- pageWBC.plugins.push(new HtmlWebpackPlugin(htmlWebpackOption));
- });*/
- pageWBC.plugins.push(new LogAfterCompilePlugin());
- pageCompiler = webpack(pageWBC);
- let libCompiler = webpack({
- ...webpackBaseConfig,
- entry: libEntries
- });
- libCompiler.run(function (err, stat) {
- if (err) {
- console.log(err);
- }
- let server = new webpackDevServer(pageCompiler, {
- contentBase: path.join(__dirname, '../dist'),
- proxy: config.proxy,
- stats: 'errors-only',
- inline: config['dev-server']['refresh-immediately']
- });
- server.listen(3006, '', () => {
- console.log('open localhost:3006');
- });
- });
- }
- // 执行文件复制
- function copyDir() {
- if (!h.isFileExist(targetPath)) {
- fs.mkdirSync(targetPath);
- }
- config.copy.map((obj) => {
- let absFrom = path.join(projectPath, obj.from);
- let absTo = path.join(targetPath, obj.to);
- if (!h.isFileExist(absTo)) {
- h.copyDirSync(targetPath, absFrom, absTo);
- }
- });
- }
- /*
- * 引用js优化,如果需要只在特定的html中引入uap的js,可以放开这里的代码
- * */
- function getConfigJs(filePath) {
- // dependjs: 依赖的js文件配置
- let configjs = '';
- if (config.dependjs && Array.isArray(config.dependjs[filePath])) {
- config.dependjs[filePath].forEach(file => {
- configjs += `<script src="${file}?v=${Date.now()}"></script>`;
- })
- }
- if (Array.isArray(config.report) && config.report.includes(filePath)) {
- configjs += `<script src="../../../../lappreportrt/nc-report/public/vendor.js"></script>`;
- configjs += `<script src="../../../../lappreportrt/nc-report/index.js"></script>`;
- }
- if (Array.isArray(config.wpsconfig) && config.wpsconfig.includes(filePath)) {
- configjs += `<script src="../../../../hrpub/public/wpsconfig/wps_sdk.js"></script>`;
- }
- return configjs;
- }
- function getConfigCss(filePath) {
- // dependcss: 依赖的css文件配置
- let configcss = '';
- if (Array.isArray(config.dependcss)) {
- configcss += config.dependcss
- .map(item => `<link rel="stylesheet" href=${item}?v=${Date.now()}>`)
- .join('');
- }
- if (Array.isArray(config.report) && config.report.includes(filePath)) {
- configcss += `<link rel="stylesheet" href="../../../../lappreportrt/nc-report/public/vendor.css" />`;
- configcss += `<link rel="stylesheet" href="../../../../lappreportrt/nc-report/index.css" />`;
- }
- return configcss;
- }
|