Skip to content

webpack4 support #11

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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ const MpvuePlugin = require('webpack-mpvue-asset-plugin')
};
```

bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。
bug 或者交流建议等请反馈到 [mpvue/issues](https://github.com/Meituan-Dianping/mpvue/issues)。
65 changes: 37 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,47 @@ const getRelativePath = (filePath) => {
return filePath
}

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

entryChunk.files.forEach(filePath => {
const assetFile = compilation.assets[filePath]
const extname = path.extname(filePath)
let content = assetFile.source()

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') {
relativePath = getRelativePath(relativePath)
}

if (/^(\.wxss)|(\.ttss)|(\.acss)|(\.css)$/.test(extname)) {
relativePath = getRelativePath(relativePath)
content = `@import "${relativePath}";\n${content}`
} else if (!(/^\.map$/.test(extname))) {
content = `require("${relativePath}")\n${content}`
}
const chunksHandle = (chunks, compilation) => {
const entryChunk = chunks.pop()

entryChunk.files.forEach(filePath => {
const assetFile = compilation.assets[filePath]
const extname = path.extname(filePath)
let content = assetFile.source()

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') {
relativePath = getRelativePath(relativePath)
}

if (/^(\.wxss)|(\.ttss)|(\.acss)|(\.css)$/.test(extname)) {
relativePath = getRelativePath(relativePath)
content = `@import "${relativePath}";\n${content}`
} else if (!(/^\.map$/.test(extname))) {
content = `require("${relativePath}");\n${content}`
}
})
assetFile.source = () => content
}
})
assetFile.source = () => content
})
})
}

const emitHandle = (compilation, callback) => {
if(compilation.entrypoints instanceof Map) {
compilation.entrypoints.forEach(({chunks}) => chunksHandle(chunks, compilation))
}else {
Object.keys(compilation.entrypoints).forEach(key => {
const { chunks } = compilation.entrypoints[key]
chunksHandle(chunks, compilation)
})
}

callback()
}

Expand Down