mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const path = require('path')
 
const webpack = require('webpack')
 
const {
  normalizePath,
  pathToRegexp,
  isInHBuilderX
} = require('@dcloudio/uni-cli-shared/lib/util')
 
let sourceRoot = false
 
function getSourceRoot () {
  if (!sourceRoot) {
    if (isInHBuilderX) {
      sourceRoot = normalizePath(process.env.UNI_INPUT_DIR)
    } else {
      sourceRoot = normalizePath(process.env.UNI_CLI_CONTEXT)
    }
  }
  return sourceRoot
}
 
function moduleFilenameTemplate (info) {
  if (
    info.resourcePath &&
    (
      !info.allLoaders ||
      info.query.includes('type=script&lang=ts') ||
      info.resourcePath.endsWith('.ts')
    )
  ) {
    const filepath = normalizePath(path.relative(getSourceRoot(), info.absoluteResourcePath))
    if (filepath.indexOf('../') === 0) {
      return
    }
    return `uni-app:///${filepath}`
  }
}
const exclude = [/pages\.json/, /node_modules/, /vue&type=template/, /vue&type=style/]
 
module.exports = {
  createSourceMapDevToolPlugin (filename = false, args) {
    const options = {
      test: [/\.js$/],
      exclude,
      moduleFilenameTemplate,
      ...args
    }
    if (filename) {
      options.filename = '../.sourcemap/' + process.env.UNI_PLATFORM + '/[file].map'
    }
    return new webpack.SourceMapDevToolPlugin(options)
  },
  createEvalSourceMapDevToolPlugin () {
    return new webpack.EvalSourceMapDevToolPlugin({
      test: pathToRegexp(process.env.UNI_INPUT_DIR, { start: true }),
      exclude,
      moduleFilenameTemplate
    })
  }
}