Skip to content

Commit

Permalink
chore(*): add Adaptor Development
Browse files Browse the repository at this point in the history
  • Loading branch information
myronliu347 committed Dec 9, 2019
1 parent 68f235c commit 2a26e6c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
47 changes: 46 additions & 1 deletion scripts/server/loaders/adaptor/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const path = require('path');
const _ = require('lodash');
const ejs = require('ejs');
const { logger, replaceExt } = require('../../../utils');

const cwd = process.cwd();
const IMPORT_REG = /import {(.+)} from ['"]@alifd\/next['"];?/;
const IMPORT_LIB_REG = /import (.+) from ['"]@alifd\/next\/lib\/(.+)['"];?/;
const IMPORT_LIB_REG_G = /^import .+ from ['"]@alifd\/next\/lib\/(.+)['"];?/gm;

module.exports = function(content) {
return fixImport(content, this.resourcePath);
return fixImport.call(this, content, this.resourcePath);
};

function fixImport(code, resourcePath) {
Expand Down Expand Up @@ -59,5 +61,48 @@ import ${component} from'${newLibPath}'`;
});
}

if (process.env.NODE_ENV === 'development') {
const adaptorTplPath = path.resolve(
__dirname,
'../../tpls/adaptor.ejs'
);
this.addDependency(adaptorTplPath);

const scripts = [
'/common.js',
`/${replaceExt(path.relative(cwd, this.resourcePath), '.js')}`,
];

ejs.renderFile(
adaptorTplPath,
{
scripts,
},
(err, html) => {
if (err) {
logger.error(`Render theme demo failed: ${err}`);
} else {
const htmlPath = replaceExt(
path.relative(
path.join(cwd, 'docs'),
this.resourcePath
),
'.html'
);
this.emitFile(htmlPath, html);
}
}
);

return `
${code.replace('export default', 'const Adaptor = ')};
import AdaptorGenerate from '@alifd/adaptor-generate';
AdaptorGenerate(Adaptor);
if (module.hot) {
module.hot.accept();
}
`;
}

return code;
}
1 change: 1 addition & 0 deletions scripts/server/loaders/theme/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
process.env.NODE_ENV = 'development';
const path = require('path');
const loaderUtils = require('loader-utils');
const ejs = require('ejs');
Expand Down
19 changes: 19 additions & 0 deletions scripts/server/tpls/adaptor.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>主题 Demo</title>
<script src="//shadow.elemecdn.com/npm/[email protected]/dist/polyfill.min.js"></script>
<script src="//shadow.elemecdn.com/npm/[email protected]/umd/react.development.js"></script>
<script src="//shadow.elemecdn.com/npm/[email protected]/umd/react-dom.development.js"></script>
<script src="//shadow.elemecdn.com/npm/[email protected]/min/moment-with-locales.js"></script>
<script src="//unpkg.com/@alifd/[email protected]/umd/adaptor-generate.development.js"></script>
</head>
<body>
<div id="container"></div>
<div id="root"></div>
<% scripts.forEach(function(src) { %>
<script src="<%= src %>"></script>
<% })%>
</body>
</html>
20 changes: 19 additions & 1 deletion scripts/server/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ module.exports = function getWebpackConfig(options) {
);
const demoPaths = glob.sync(path.join(componentPath, 'demo', '*.md'));
const themePaths = glob.sync(path.resolve(componentPath, 'theme/**/*.jsx'));
const entry = getEntry([indexPath, ...demoPaths, ...themePaths]);
const adaptorPaths = glob.sync(
path.resolve(componentPath, 'adaptor/*.jsx')
);
const entry = getEntry([
indexPath,
...demoPaths,
...themePaths,
...adaptorPaths,
]);
config.entry = entry;

config.output = {
Expand Down Expand Up @@ -99,6 +107,16 @@ module.exports = function getWebpackConfig(options) {
}
}

if (adaptorPaths.length) {
const docsPath = path.join(cwd, 'docs');
links.push({
href: path
.relative(docsPath, adaptorPaths[0])
.replace(/\.jsx$/, '.html'),
title: 'Adaptor Demo',
});
}

config.module.rules.push({
test: /docs\/[^/]+\/index\.(en-us\.)?md$/,
use: [
Expand Down

0 comments on commit 2a26e6c

Please sign in to comment.