Skip to content

Commit

Permalink
Merge pull request #8 from mpvue/feature/ttalipay
Browse files Browse the repository at this point in the history
Feature/ttalipay
  • Loading branch information
hucq authored Jan 15, 2019
2 parents 7a22896 + cfad04a commit f7b9e8d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
64 changes: 32 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
const path = require('path');
const upath = require('upath');
const relative = require('relative');
const path = require('path')
const upath = require('upath')
const relative = require('relative')

function MpvuePlugin() {}
const emitHandle = (compilation, callback) => {
Object.keys(compilation.entrypoints).forEach(key => {
const { chunks } = compilation.entrypoints[key]
const entryChunk = chunks.pop()

MpvuePlugin.prototype.apply = function(compiler) {
compiler.plugin('emit', function(compilation, callback) {
Object.keys(compilation.entrypoints).forEach(key => {
const entry = compilation.entrypoints[key];
const { chunks } = entry;
const entryChunk = chunks.pop();
entryChunk.files.forEach(filePath => {
const extname = path.extname(filePath);
let content = compilation.assets[filePath].source();
chunks.reverse().forEach(chunk => {
chunk.files.forEach(childFile => {
if (path.extname(childFile) === extname && compilation.assets[filePath]) {
let relativePath = upath.normalize(relative(filePath, childFile))
entryChunk.files.forEach(filePath => {
const assetFile = compilation.assets[filePath]
const extname = path.extname(filePath)
let content = assetFile.source()

// 百度小程序js引用不支持绝对路径,改为相对路径
if (extname === '.js' && !/^\.(\.)?\//.test(relativePath)) {
relativePath = `./${relativePath}`;
}
chunks.reverse().forEach(chunk => {
chunk.files.forEach(subFile => {
if (path.extname(subFile) === extname && assetFile) {
let relativePath = upath.normalize(relative(filePath, subFile))
// 百度小程序 js 引用不支持绝对路径,改为相对路径
if (extname === '.js' && !/^\.(\.)?\//.test(relativePath)) {
relativePath = `./${relativePath}`
}

if (/^(\.wxss)|(\.css)$/.test(extname)) {
content = `@import "${relativePath}";\n${content}`;
} else {
content = `require("${relativePath}");\n${content}`;
}
if (/^(\.wxss)|(\.ttss)|(\.acss)|(\.css)$/.test(extname)) {
content = `@import "${relativePath}"\n${content}`
} else {
content = `require("${relativePath}")\n${content}`
}
})
compilation.assets[filePath].source = () => content;
}
})
assetFile.source = () => content
})
})
callback();
});
};
})
callback()
}

function MpvuePlugin() {}
MpvuePlugin.prototype.apply = compiler => compiler.plugin('emit', emitHandle)

module.exports = MpvuePlugin;
module.exports = MpvuePlugin
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-mpvue-asset-plugin",
"version": "0.1.2",
"version": "0.1.3",
"main": "index.js",
"directories": {
"lib": "lib"
Expand Down

0 comments on commit f7b9e8d

Please sign in to comment.