Skip to content

Feature/ttalipay #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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