Skip to content

Commit

Permalink
feat(okam-build): add postcss-plugin-wx2swan
Browse files Browse the repository at this point in the history
  • Loading branch information
xhong0 committed Oct 15, 2018
1 parent c066df5 commit dfd22cc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
25 changes: 25 additions & 0 deletions packages/okam-build/lib/processor/css/postcss-plugin-wx2swan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file postcss plugin px2rpx
* @author [email protected]
*/

'use strict';

const postcss = require('postcss');
const path = require('path');

module.exports = postcss.plugin('postcss-plugin-wx2swan', function (opts = {}) {

let file = opts.file;

return function (css, result) {
css.walkAtRules(rule => {
if (rule.name === 'import') {
// path.resolve(file.fullPath, );
const depRelPath = rule.params.slice(1, -1);
rule.params = rule.params.replace(/\.wxss/, '.css');
file.addDeps(depRelPath);
}
});
};
});
2 changes: 0 additions & 2 deletions packages/okam-build/lib/processor/helper/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ function isNpmModuleFile(modulePath) {

function resolveNpmModuleNewPath(oldPath, rebaseDepDir) {
let newPath = rebaseDepDir + oldPath.substr(DEP_DIR_NAME.length + 1);
// replace all `node_modles` to `npm` to fix weixin cannot find the module
// if the module path exists `node_module` dir name
return nodeModulesToNpm(newPath);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/okam-build/lib/processor/wx2swan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const wx2swanMap = {
rext: 'swan'
},
wxss: {
processor: null,
processor: require('./wxssTransform'),
rext: 'css'
},
js: {
Expand Down
17 changes: 17 additions & 0 deletions packages/okam-build/lib/processor/wx2swan/wxssTransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @file The postcss processor
* @author [email protected]
*/

'use strict';

const postcssProcessor = require('../css/postcss');
const wx2swanPlugin = require('../css/postcss-plugin-wx2swan');

module.exports = function (file, options) {
let config = options.config;
config.plugins = config.plugins || [];
config.plugins.push(wx2swanPlugin.bind(this, {file}));
return postcssProcessor(file, options);
};

0 comments on commit dfd22cc

Please sign in to comment.