Skip to content

Commit

Permalink
feat: compatible @easy-team mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sky committed Aug 3, 2019
1 parent 15e3efa commit c34806b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ node_js:
- '6'
- '8'
- '10'
before_install:
- npm i npminstall -g
install:
- npm i npminstall && npminstall
- npminstall
script:
- npm run ci
after_script:
Expand Down
38 changes: 29 additions & 9 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';
const path = require('path');
const fs = require('fs');
const imgRegex = /\.(png|jpe?g|gif|svg)(\?.*)?$/;
const IMG_REGEX = /\.(png|jpe?g|gif|svg)(\?.*)?$/;
const PKG_PREFIX = '@easy-team';
exports.readWebpackMemoryFile = (compiler, filePath) => {
const compilerList = Array.isArray(compiler) ? compiler : [compiler];
for (let i = 0; i < compilerList.length; i++) {
Expand All @@ -11,7 +12,7 @@ exports.readWebpackMemoryFile = (compiler, filePath) => {
});
if (fileCompiler && fileCompiler.length) {
const ext = path.extname(filePath).toLocaleLowerCase();
if (imgRegex.test(ext)) {
if (IMG_REGEX.test(ext)) {
const base64 = fileCompiler[0].outputFileSystem.readFileSync(filePath).toString('base64');
return `data:image/${ext.replace(/^\./, '')};base64,${base64}`;
}
Expand All @@ -21,12 +22,26 @@ exports.readWebpackMemoryFile = (compiler, filePath) => {
return '';
};

exports.requireModule = (module, baseDir) => {
exports.requireModule = (moduleName, baseDir) => {
try {
const modulepath = path.join(baseDir, 'node_modules', module);
const modulepath = path.join(baseDir, 'node_modules', moduleName);
return require(modulepath);
} catch (e) {
return require(module);
return require(moduleName);
}
};


exports.requireEasyModule = (moduleName, baseDir) => {
const name = `${PKG_PREFIX}/${moduleName}`;
return exports.requireModule(name, baseDir);
};

exports.requireEasyCompatibleModule = (moduleName, baseDir) => {
try {
return exports.requireModule(moduleName, baseDir);
} catch (e) {
return exports.requireEasyModule(moduleName, baseDir);
}
};

Expand Down Expand Up @@ -65,14 +80,19 @@ exports.isUseMultProcess = (baseDir, pluginConfig) => {
};

exports.getFramework = (root, option = {}) => {
const vuePkgName = 'easywebpack-vue';
const reactPkgName = 'easywebpack-vue';

if (option.framework) {
return option.framework;
}
const pkgFile = path.join(root, 'package.json');
const pkg = require(pkgFile);
if (pkg.dependencies['egg-view-vue-ssr'] || pkg.dependencies['easywebpack-vue'] || pkg.devDependencies['easywebpack-vue']) {
if (pkg.dependencies['egg-view-vue-ssr'] || pkg.dependencies[vuePkgName] || pkg.devDependencies[vuePkgName]
|| pkg.dependencies[`${PKG_PREFIX}/${vuePkgName}`] || pkg.devDependencies[`${PKG_PREFIX}/${vuePkgName}`]) {
return 'vue';
} else if (pkg.dependencies['egg-view-react-ssr'] || pkg.dependencies['easywebpack-react'] || pkg.devDependencies['easywebpack-react']) {
} else if (pkg.dependencies['egg-view-react-ssr'] || pkg.dependencies[reactPkgName] || pkg.devDependencies[reactPkgName]
|| pkg.dependencies[`${PKG_PREFIX}/${reactPkgName}`] || pkg.devDependencies[`${PKG_PREFIX}/${reactPkgName}`]) {
return 'react';
}
return null;
Expand All @@ -84,7 +104,7 @@ exports.getWebpackConfig = (eggWebpackConfig, option) => {
option.framework = framework;
const module = framework ? `easywebpack-${framework}` : 'easywebpack';
const webpackConfigFile = eggWebpackConfig.webpackConfigFile;
const easywebpack = exports.requireModule(module, baseDir);
const easywebpack = exports.requireEasyCompatibleModule(module, baseDir);
return easywebpack.getWebpackConfig(webpackConfigFile, option);
};

Expand Down Expand Up @@ -112,4 +132,4 @@ exports.normalizeProxyUrlFile = (app, url) => {

exports.getPort = port => {
return Number(process.env.EASY_ENV_DEV_PORT || port);
};
};

0 comments on commit c34806b

Please sign in to comment.