@mpxjs/webpack-plugin 2.6.115 → 2.7.0-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/LICENSE +433 -0
  2. package/README.md +1 -1
  3. package/lib/config.js +14 -0
  4. package/lib/dependencies/AddEntryDependency.js +24 -0
  5. package/lib/dependencies/AppEntryDependency.js +58 -0
  6. package/lib/dependencies/CommonJsAsyncDependency.js +51 -0
  7. package/lib/dependencies/CommonJsVariableDependency.js +81 -0
  8. package/lib/dependencies/DynamicEntryDependency.js +171 -0
  9. package/lib/dependencies/FlagPluginDependency.js +24 -0
  10. package/lib/dependencies/InjectDependency.js +43 -0
  11. package/lib/dependencies/RecordGlobalComponentsDependency.js +50 -0
  12. package/lib/dependencies/RecordIndependentDependency.js +44 -0
  13. package/lib/dependencies/RecordResourceMapDependency.js +62 -0
  14. package/lib/dependencies/RemoveEntryDependency.js +40 -0
  15. package/lib/{dependency → dependencies}/ReplaceDependency.js +19 -2
  16. package/lib/dependencies/ResolveDependency.js +88 -0
  17. package/lib/extractor.js +82 -178
  18. package/lib/file-loader.js +7 -19
  19. package/lib/helpers.js +39 -334
  20. package/lib/independent-loader.js +52 -0
  21. package/lib/index.js +889 -525
  22. package/lib/json-compiler/helper.js +156 -0
  23. package/lib/json-compiler/index.js +245 -451
  24. package/lib/json-compiler/plugin.js +150 -0
  25. package/lib/json-compiler/{theme-loader.js → theme.js} +5 -3
  26. package/lib/loader.js +178 -241
  27. package/lib/native-loader.js +71 -133
  28. package/lib/parser.js +1 -2
  29. package/lib/partial-compile/index.js +35 -0
  30. package/lib/platform/json/wx/index.js +1 -1
  31. package/lib/platform/template/normalize-component-rules.js +2 -3
  32. package/lib/platform/template/wx/component-config/button.js +14 -2
  33. package/lib/platform/template/wx/component-config/image.js +4 -0
  34. package/lib/platform/template/wx/component-config/input.js +4 -0
  35. package/lib/platform/template/wx/component-config/rich-text.js +4 -0
  36. package/lib/platform/template/wx/component-config/scroll-view.js +4 -0
  37. package/lib/platform/template/wx/component-config/switch.js +4 -0
  38. package/lib/platform/template/wx/component-config/text.js +4 -0
  39. package/lib/platform/template/wx/component-config/textarea.js +5 -0
  40. package/lib/platform/template/wx/component-config/view.js +4 -0
  41. package/lib/platform/template/wx/index.js +149 -3
  42. package/lib/record-loader.js +11 -0
  43. package/lib/resolve-loader.js +6 -0
  44. package/lib/resolver/AddEnvPlugin.js +4 -3
  45. package/lib/resolver/AddModePlugin.js +4 -3
  46. package/lib/resolver/FixDescriptionInfoPlugin.js +28 -0
  47. package/lib/resolver/PackageEntryPlugin.js +23 -36
  48. package/lib/runtime/base.styl +5 -0
  49. package/lib/runtime/components/tenon/getInnerListeners.js +317 -0
  50. package/lib/runtime/components/tenon/tenon-button.vue +305 -0
  51. package/lib/runtime/components/tenon/tenon-image.vue +61 -0
  52. package/lib/runtime/components/tenon/tenon-input.vue +99 -0
  53. package/lib/runtime/components/tenon/tenon-rich-text.vue +21 -0
  54. package/lib/runtime/components/tenon/tenon-scroll-view.vue +124 -0
  55. package/lib/runtime/components/tenon/tenon-switch.vue +91 -0
  56. package/lib/runtime/components/tenon/tenon-text-area.vue +64 -0
  57. package/lib/runtime/components/tenon/tenon-text.vue +64 -0
  58. package/lib/runtime/components/tenon/tenon-view.vue +93 -0
  59. package/lib/runtime/components/tenon/util.js +44 -0
  60. package/lib/runtime/components/web/getInnerListeners.js +1 -3
  61. package/lib/runtime/components/web/mpx-image.vue +20 -5
  62. package/lib/runtime/components/web/mpx-movable-view.vue +6 -2
  63. package/lib/runtime/components/web/mpx-swiper.vue +18 -3
  64. package/lib/runtime/i18n.wxs +31 -11
  65. package/lib/runtime/optionProcessor.js +48 -3
  66. package/lib/runtime/optionProcessor.tenon.js +386 -0
  67. package/lib/selector.js +29 -10
  68. package/lib/style-compiler/index.js +16 -24
  69. package/lib/style-compiler/load-postcss-config.js +3 -1
  70. package/lib/style-compiler/plugins/conditional-strip.js +68 -65
  71. package/lib/style-compiler/plugins/hm.js +20 -0
  72. package/lib/style-compiler/plugins/rpx.js +43 -37
  73. package/lib/style-compiler/plugins/scope-id.js +79 -72
  74. package/lib/style-compiler/plugins/trans-special.js +25 -18
  75. package/lib/style-compiler/plugins/trim.js +13 -7
  76. package/lib/style-compiler/plugins/vw.js +22 -16
  77. package/lib/template-compiler/compiler.js +106 -199
  78. package/lib/template-compiler/index.js +52 -139
  79. package/lib/template-compiler/trans-dynamic-class-expr.js +18 -13
  80. package/lib/tenon/index.js +105 -0
  81. package/lib/tenon/processJSON.js +356 -0
  82. package/lib/tenon/processScript.js +261 -0
  83. package/lib/tenon/processStyles.js +21 -0
  84. package/lib/tenon/processTemplate.js +133 -0
  85. package/lib/url-loader.js +11 -29
  86. package/lib/utils/add-query.js +1 -1
  87. package/lib/utils/const.js +10 -0
  88. package/lib/utils/emit-file.js +10 -0
  89. package/lib/utils/eval-json-js.js +31 -0
  90. package/lib/utils/get-entry-name.js +13 -0
  91. package/lib/utils/get-json-content.js +42 -0
  92. package/lib/utils/get-relative-path.js +25 -0
  93. package/lib/utils/is-url-request.js +10 -1
  94. package/lib/utils/match-condition.js +4 -1
  95. package/lib/utils/normalize.js +4 -15
  96. package/lib/utils/parse-request.js +3 -3
  97. package/lib/utils/resolve.js +13 -0
  98. package/lib/utils/set.js +47 -0
  99. package/lib/utils/stringify-loaders-resource.js +25 -0
  100. package/lib/utils/stringify-query.js +4 -0
  101. package/lib/web/processJSON.js +113 -144
  102. package/lib/web/processScript.js +47 -34
  103. package/lib/web/processTemplate.js +57 -40
  104. package/lib/wxml/{wxml-loader.js → loader.js} +21 -62
  105. package/lib/wxs/WxsModuleIdsPlugin.js +29 -0
  106. package/lib/wxs/WxsParserPlugin.js +2 -2
  107. package/lib/wxs/WxsPlugin.js +4 -8
  108. package/lib/wxs/WxsTemplatePlugin.js +46 -92
  109. package/lib/wxs/{wxs-i18n-loader.js → i18n-loader.js} +5 -4
  110. package/lib/wxs/loader.js +142 -0
  111. package/lib/wxs/{wxs-pre-loader.js → pre-loader.js} +20 -5
  112. package/lib/wxss/loader.js +31 -43
  113. package/lib/wxss/localsLoader.js +1 -5
  114. package/lib/wxss/processCss.js +107 -103
  115. package/package.json +21 -18
  116. package/lib/built-in-loader.js +0 -49
  117. package/lib/content-loader.js +0 -13
  118. package/lib/dependency/ChildCompileDependency.js +0 -24
  119. package/lib/dependency/InjectDependency.js +0 -26
  120. package/lib/dependency/RemovedModuleDependency.js +0 -23
  121. package/lib/dependency/ResolveDependency.js +0 -49
  122. package/lib/path-loader.js +0 -3
  123. package/lib/plugin-loader.js +0 -287
  124. package/lib/staticConfig.js +0 -4
  125. package/lib/utils/get-main-compilation.js +0 -6
  126. package/lib/utils/read-json-for-src.js +0 -34
  127. package/lib/utils/try-require.js +0 -16
  128. package/lib/wxs/wxs-loader.js +0 -117
  129. package/lib/wxss/getImportPrefix.js +0 -30
@@ -0,0 +1,142 @@
1
+ const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin')
2
+ const EntryPlugin = require('webpack/lib/EntryPlugin')
3
+ const LazySet = require('webpack/lib/util/LazySet')
4
+ const LimitChunkCountPlugin = require('webpack/lib/optimize/LimitChunkCountPlugin')
5
+ const FlagEntryExportAsUsedPlugin = require('webpack/lib/FlagEntryExportAsUsedPlugin')
6
+ const path = require('path')
7
+ const WxsPlugin = require('./WxsPlugin')
8
+ const RecordResourceMapDependency = require('../dependencies/RecordResourceMapDependency')
9
+ const parseRequest = require('../utils/parse-request')
10
+ const toPosix = require('../utils/to-posix')
11
+ const fixRelative = require('../utils/fix-relative')
12
+ const addQuery = require('../utils/add-query')
13
+ const config = require('../config')
14
+
15
+ module.exports = content => content
16
+
17
+ module.exports.pitch = function (remainingRequest) {
18
+ const nativeCallback = this.async()
19
+ const moduleGraph = this._compilation.moduleGraph
20
+ const mpx = this.getMpx()
21
+ const mode = mpx.mode
22
+ const getOutputPath = mpx.getOutputPath
23
+ let { resourcePath, queryObj } = parseRequest(this.resource)
24
+ const issuer = moduleGraph.getIssuer(this._module)
25
+ const { resourcePath: issuerResourcePath, queryObj: issuerQueryObj } = parseRequest(queryObj.issuerResource || issuer.resource)
26
+ const issuerPackageName = issuerQueryObj.packageRoot || 'main'
27
+ const pagesMap = mpx.pagesMap
28
+ const componentsMap = mpx.componentsMap[issuerPackageName]
29
+ const staticResourcesMap = mpx.staticResourcesMap[issuerPackageName]
30
+ const issuerName = pagesMap[issuerResourcePath] || componentsMap[issuerResourcePath] || staticResourcesMap[issuerResourcePath]
31
+ const issuerDir = path.dirname(issuerName)
32
+
33
+ const getName = (raw) => {
34
+ const match = /^(.*?)(\.[^.]*)?$/.exec(raw)
35
+ return match[1]
36
+ }
37
+
38
+ const wxsModule = queryObj.wxsModule
39
+ if (wxsModule) {
40
+ resourcePath = `${resourcePath}~${wxsModule}`
41
+ }
42
+ const packageRoot = queryObj.packageRoot || ''
43
+ const ext = config[mode].wxs.ext
44
+ const filename = toPosix(path.join(packageRoot, getOutputPath(resourcePath, ext.slice(1), { ext })))
45
+ this._module.addPresentationalDependency(new RecordResourceMapDependency(resourcePath, 'staticResource', filename, packageRoot))
46
+
47
+ const callback = (err) => {
48
+ if (err) return nativeCallback(err)
49
+ let relativePath = toPosix(path.relative(issuerDir, filename))
50
+ relativePath = fixRelative(relativePath, mode)
51
+ nativeCallback(null, `module.exports = ${JSON.stringify(relativePath)};`)
52
+ }
53
+
54
+ // 清空issuerResource query避免文件内容输出报错并进行子编译缓存优化
55
+ const request = '!!' + addQuery(remainingRequest, {}, false, ['issuerResource'])
56
+
57
+ // request中已经包含全量构成filename的信息,故可以直接使用request作为key来进行缓存
58
+ if (!mpx.wxsAssetsCache.has(request)) {
59
+ mpx.wxsAssetsCache.set(request, new Promise((resolve, reject) => {
60
+ const outputOptions = {
61
+ filename,
62
+ // 避免输出的wxs中包含es语法
63
+ environment: {
64
+ // The environment supports arrow functions ('() => { ... }').
65
+ arrowFunction: false,
66
+ // The environment supports BigInt as literal (123n).
67
+ bigIntLiteral: false,
68
+ // The environment supports const and let for variable declarations.
69
+ const: false,
70
+ // The environment supports destructuring ('{ a, b } = obj').
71
+ destructuring: false,
72
+ // The environment supports an async import() function to import EcmaScript modules.
73
+ dynamicImport: false,
74
+ // The environment supports 'for of' iteration ('for (const x of array) { ... }').
75
+ forOf: false,
76
+ // The environment supports ECMAScript Module syntax to import ECMAScript modules (import ... from '...').
77
+ module: false
78
+ }
79
+ }
80
+
81
+ const plugins = [
82
+ new WxsPlugin({ mode }),
83
+ new NodeTargetPlugin(),
84
+ new EntryPlugin(this.context, request, { name: getName(filename) }),
85
+ new LimitChunkCountPlugin({ maxChunks: 1 }),
86
+ new FlagEntryExportAsUsedPlugin(true, 'entry')
87
+ ]
88
+
89
+ const childCompiler = this._compilation.createChildCompiler(resourcePath, outputOptions, plugins)
90
+
91
+ let assets = []
92
+
93
+ childCompiler.hooks.afterCompile.tap('MpxWebpackPlugin', (compilation) => {
94
+ // 持久化缓存,使用module.buildInfo.assets来输出文件
95
+ assets = compilation.getAssets()
96
+ compilation.clearAssets()
97
+ })
98
+
99
+ childCompiler.runAsChild((err, entries, compilation) => {
100
+ if (err) return reject(err)
101
+ const fileDependencies = new LazySet()
102
+ const contextDependencies = new LazySet()
103
+ const missingDependencies = new LazySet()
104
+ const buildDependencies = new LazySet()
105
+ compilation.modules.forEach((module) => {
106
+ module.addCacheDependencies(
107
+ fileDependencies,
108
+ contextDependencies,
109
+ missingDependencies,
110
+ buildDependencies
111
+ )
112
+ })
113
+ resolve({
114
+ assets,
115
+ fileDependencies,
116
+ contextDependencies,
117
+ missingDependencies,
118
+ buildDependencies
119
+ })
120
+ })
121
+ }))
122
+ }
123
+
124
+ mpx.wxsAssetsCache.get(request).then(({ assets, fileDependencies, contextDependencies, missingDependencies, buildDependencies }) => {
125
+ assets.forEach(({ name, source, info }) => {
126
+ this.emitFile(name, source.source(), undefined, info)
127
+ })
128
+ fileDependencies.forEach((dep) => {
129
+ this.addDependency(dep)
130
+ })
131
+ contextDependencies.forEach((dep) => {
132
+ this.addContextDependency(dep)
133
+ })
134
+ missingDependencies.forEach((dep) => {
135
+ this.addMissingDependency(dep)
136
+ })
137
+ buildDependencies.forEach((dep) => {
138
+ this.addBuildDependency(dep)
139
+ })
140
+ callback()
141
+ }).catch(callback)
142
+ }
@@ -2,21 +2,20 @@ const babylon = require('@babel/parser')
2
2
  const traverse = require('@babel/traverse').default
3
3
  const t = require('@babel/types')
4
4
  const generate = require('@babel/generator').default
5
- const getMainCompilation = require('../utils/get-main-compilation')
6
5
  const parseRequest = require('../utils/parse-request')
7
6
  const isEmptyObject = require('../utils/is-empty-object')
8
7
  const parseQuery = require('loader-utils').parseQuery
9
8
 
10
9
  module.exports = function (content) {
11
10
  this.cacheable()
12
- const mainCompilation = getMainCompilation(this._compilation)
11
+ const mpx = this.getMpx()
13
12
  const module = this._module
14
- const mode = mainCompilation.__mpx__.mode
13
+ const mode = mpx.mode
15
14
  const wxsModule = parseQuery(this.resourceQuery || '?').wxsModule
16
15
 
17
16
  // 处理内联wxs
18
17
  if (wxsModule) {
19
- const wxsContentMap = mainCompilation.__mpx__.wxsContentMap
18
+ const wxsContentMap = mpx.wxsContentMap
20
19
  const resourcePath = parseRequest(this.resource).resourcePath
21
20
  content = wxsContentMap[`${resourcePath}~${wxsModule}`] || content
22
21
  }
@@ -45,6 +44,15 @@ module.exports = function (content) {
45
44
  }
46
45
  }
47
46
  },
47
+ CallExpression (path) {
48
+ const callee = path.node.callee
49
+ if (t.isIdentifier(callee) && callee.name === 'getRegExp') {
50
+ const argPath = path.get('arguments')[0]
51
+ if (argPath.isStringLiteral()) {
52
+ argPath.replaceWith(t.stringLiteral(argPath.node.extra.raw.slice(1, -1)))
53
+ }
54
+ }
55
+ },
48
56
  ForStatement (path) {
49
57
  if (path.shouldStopTraverse) {
50
58
  path.stop()
@@ -93,12 +101,19 @@ module.exports = function (content) {
93
101
  },
94
102
  CallExpression (path) {
95
103
  const callee = path.node.callee
104
+ const args = path.node.arguments
96
105
  const transMap = {
97
106
  getDate: 'Date',
98
107
  getRegExp: 'RegExp'
99
108
  }
100
109
  if (t.isIdentifier(callee) && transMap[callee.name]) {
101
- path.replaceWith(t.newExpression(t.identifier(transMap[callee.name]), path.node.arguments))
110
+ if (callee.name === 'getRegExp') {
111
+ const arg = args[0]
112
+ if (t.isStringLiteral(arg)) {
113
+ args[0] = t.stringLiteral(arg.extra.raw.slice(1, -1))
114
+ }
115
+ }
116
+ path.replaceWith(t.newExpression(t.identifier(transMap[callee.name]), args))
102
117
  }
103
118
  }
104
119
  })
@@ -5,25 +5,22 @@
5
5
  */
6
6
  const loaderUtils = require('loader-utils')
7
7
  const processCss = require('./processCss')
8
- const getImportPrefix = require('./getImportPrefix')
9
8
  const compileExports = require('./compile-exports')
10
9
  const createResolver = require('./createResolver')
11
10
  const isUrlRequest = require('../utils/is-url-request')
12
- const getMainCompilation = require('../utils/get-main-compilation')
13
- const addQuery = require('../utils/add-query')
11
+ const createHelpers = require('../helpers')
14
12
 
15
13
  module.exports = function (content, map) {
16
14
  if (this.cacheable) this.cacheable()
17
-
18
15
  const callback = this.async()
19
16
  const query = loaderUtils.getOptions(this) || {}
20
- const root = query.root
21
17
  const moduleMode = query.modules || query.module
22
18
  const camelCaseKeys = query.camelCase || query.camelcase
23
- const sourceMap = this.sourceMap || false
24
19
  const resolve = createResolver(query.alias)
25
- const mpx = getMainCompilation(this._compilation).__mpx__
20
+ const mpx = this.getMpx()
26
21
  const externals = mpx.externals
22
+ const root = mpx.projectRoot
23
+ const sourceMap = mpx.cssSourceMap || false
27
24
 
28
25
  if (sourceMap) {
29
26
  if (map) {
@@ -43,25 +40,24 @@ module.exports = function (content, map) {
43
40
  map = null
44
41
  }
45
42
 
43
+ const { getRequestString } = createHelpers(this)
44
+
46
45
  processCss(content, map, {
47
46
  mode: moduleMode ? 'local' : 'global',
48
47
  from: loaderUtils.getRemainingRequest(this).split('!').pop(),
49
48
  to: loaderUtils.getCurrentRequest(this).split('!').pop(),
50
- query: query,
51
- resolve: resolve,
49
+ query,
50
+ resolve,
52
51
  minimize: this.minimize,
53
52
  loaderContext: this,
54
- sourceMap: sourceMap
55
- }, function (err, result) {
53
+ sourceMap
54
+ }, (err, result) => {
56
55
  if (err) return callback(err)
57
56
 
58
57
  let cssAsString = JSON.stringify(result.source)
59
58
 
60
- // for importing CSS
61
- const importUrlPrefix = getImportPrefix(this)
62
-
63
59
  const alreadyImported = {}
64
- const importJs = result.importItems.filter(function (imp) {
60
+ const importJs = result.importItems.filter((imp) => {
65
61
  if (!imp.mediaQuery) {
66
62
  if (alreadyImported[imp.url]) {
67
63
  return false
@@ -69,43 +65,35 @@ module.exports = function (content, map) {
69
65
  alreadyImported[imp.url] = true
70
66
  }
71
67
  return true
72
- }).map(function (imp) {
73
- if (!isUrlRequest(imp.url, root) || externals.some((external) => {
74
- if (typeof external === 'string') {
75
- return external === imp.url
76
- } else if (external instanceof RegExp) {
77
- return external.test(imp.url)
78
- }
79
- return false
80
- })) {
68
+ }).map((imp, i) => {
69
+ if (!isUrlRequest(imp.url, root, externals)) {
81
70
  return 'exports.push([module.id, ' +
82
71
  JSON.stringify('@import url(' + imp.url + ');') + ', ' +
83
72
  JSON.stringify(imp.mediaQuery) + ']);'
84
73
  } else {
85
- if (query.extract) {
86
- const importUrlPrefix = getImportPrefix(this, true)
87
- const importUrl = importUrlPrefix + addQuery(imp.url, { isStatic: true, issuerResource: this.resource })
88
- return 'exports.push([module.id, ' +
89
- JSON.stringify('@import "') +
90
- '+ require(' + loaderUtils.stringifyRequest(this, importUrl) + ') +' +
91
- JSON.stringify('";') + ', ' +
92
- JSON.stringify(imp.mediaQuery) + ']);'
93
- }
94
- const importUrl = importUrlPrefix + imp.url
95
- return 'exports.i(require(' + loaderUtils.stringifyRequest(this, importUrl) + '), ' + JSON.stringify(imp.mediaQuery) + ');'
74
+ const requestString = getRequestString('styles', { src: imp.url }, {
75
+ isStatic: true,
76
+ issuerResource: this.resource,
77
+ fromImport: true
78
+ }, i)
79
+ return 'exports.push([module.id, ' +
80
+ JSON.stringify('@import "') +
81
+ '+ require(' + requestString + ') +' +
82
+ JSON.stringify('";') + ', ' +
83
+ JSON.stringify(imp.mediaQuery) + ']);'
96
84
  }
97
- }, this).join('\n')
85
+ }).join('\n')
98
86
 
99
- function importItemMatcher (item) {
87
+ const importItemMatcher = (item) => {
100
88
  const match = result.importItemRegExp.exec(item)
101
89
  const idx = +match[1]
102
90
  const importItem = result.importItems[idx]
103
- const importUrl = importUrlPrefix + importItem.url
91
+ const importUrl = importItem.url
104
92
  return '" + require(' + loaderUtils.stringifyRequest(this, importUrl) + ').locals' +
105
93
  '[' + JSON.stringify(importItem.export) + '] + "'
106
94
  }
107
95
 
108
- cssAsString = cssAsString.replace(result.importItemRegExpG, importItemMatcher.bind(this))
96
+ cssAsString = cssAsString.replace(result.importItemRegExpG, importItemMatcher)
109
97
 
110
98
  // helper for ensuring valid CSS strings from requires
111
99
  let urlEscapeHelper = ''
@@ -113,7 +101,7 @@ module.exports = function (content, map) {
113
101
  if (query.url !== false && result.urlItems.length > 0) {
114
102
  urlEscapeHelper = 'var escape = require(' + loaderUtils.stringifyRequest(this, '!!' + require.resolve('./url/escape.js')) + ');\n'
115
103
 
116
- cssAsString = cssAsString.replace(result.urlItemRegExpG, function (item) {
104
+ cssAsString = cssAsString.replace(result.urlItemRegExpG, (item) => {
117
105
  const match = result.urlItemRegExp.exec(item)
118
106
  let idx = +match[1]
119
107
  const urlItem = result.urlItems[idx]
@@ -129,10 +117,10 @@ module.exports = function (content, map) {
129
117
  }
130
118
  urlRequest = url
131
119
  return '" + escape(require(' + loaderUtils.stringifyRequest(this, urlRequest) + ')) + "'
132
- }.bind(this))
120
+ })
133
121
  }
134
122
 
135
- let exportJs = compileExports(result, importItemMatcher.bind(this), camelCaseKeys)
123
+ let exportJs = compileExports(result, importItemMatcher, camelCaseKeys)
136
124
  if (exportJs) {
137
125
  exportJs = 'exports.locals = ' + exportJs + ';'
138
126
  }
@@ -165,5 +153,5 @@ module.exports = function (content, map) {
165
153
  moduleJs + '\n\n' +
166
154
  '// exports\n' +
167
155
  exportJs)
168
- }.bind(this))
156
+ })
169
157
  }
@@ -5,7 +5,6 @@
5
5
  */
6
6
  var loaderUtils = require('loader-utils')
7
7
  var processCss = require('./processCss')
8
- var getImportPrefix = require('./getImportPrefix')
9
8
  var compileExports = require('./compile-exports')
10
9
  var createResolver = require('./createResolver')
11
10
 
@@ -26,14 +25,11 @@ module.exports = function (content) {
26
25
  }, function (err, result) {
27
26
  if (err) return callback(err)
28
27
 
29
- // for importing CSS
30
- var importUrlPrefix = getImportPrefix(this, query)
31
-
32
28
  function importItemMatcher (item) {
33
29
  var match = result.importItemRegExp.exec(item)
34
30
  var idx = +match[1]
35
31
  var importItem = result.importItems[idx]
36
- var importUrl = importUrlPrefix + importItem.url
32
+ var importUrl = importItem.url
37
33
  return '" + require(' + loaderUtils.stringifyRequest(this, importUrl) + ')' +
38
34
  '[' + JSON.stringify(importItem.export) + '] + "'
39
35
  }
@@ -17,129 +17,133 @@ const modulesScope = require('postcss-modules-scope')
17
17
  const modulesValues = require('postcss-modules-values')
18
18
  const valueParser = require('postcss-value-parser')
19
19
  const isUrlRequest = require('../utils/is-url-request')
20
+ // css-loader-parser
20
21
 
21
- const parserPlugin = postcss.plugin('css-loader-parser', function (options) {
22
- return function (css) {
23
- const imports = {}
24
- let exports = {}
25
- const importItems = []
26
- const urlItems = []
22
+ const parserPlugin = function (options) {
23
+ return {
24
+ postcssPlugin: 'css-loader-parser',
25
+ Once (css) {
26
+ const imports = {}
27
+ let exports = {}
28
+ const importItems = []
29
+ const urlItems = []
30
+
31
+ function replaceImportsInString (str) {
32
+ if (options.import) {
33
+ const tokens = valueParser(str)
34
+ tokens.walk(function (node) {
35
+ if (node.type !== 'word') {
36
+ return
37
+ }
38
+ const token = node.value
39
+ const importIndex = imports['$' + token]
40
+ if (typeof importIndex === 'number') {
41
+ node.value = '___CSS_LOADER_IMPORT___' + importIndex + '___'
42
+ }
43
+ })
44
+ return tokens.toString()
45
+ }
46
+ return str
47
+ }
27
48
 
28
- function replaceImportsInString (str) {
29
49
  if (options.import) {
30
- const tokens = valueParser(str)
31
- tokens.walk(function (node) {
32
- if (node.type !== 'word') {
50
+ css.walkAtRules(/^import$/i, function (rule) {
51
+ const values = Tokenizer.parseValues(rule.params)
52
+ let url = values.nodes[0].nodes[0]
53
+ if (url && url.type === 'url') {
54
+ url = url.url
55
+ } else if (url && url.type === 'string') {
56
+ url = url.value
57
+ } else throw rule.error('Unexpected format ' + rule.params)
58
+ if (!url.replace(/\s/g, '').length) {
33
59
  return
34
60
  }
35
- const token = node.value
36
- const importIndex = imports['$' + token]
37
- if (typeof importIndex === 'number') {
38
- node.value = '___CSS_LOADER_IMPORT___' + importIndex + '___'
61
+ values.nodes[0].nodes.shift()
62
+ const mediaQuery = Tokenizer.stringifyValues(values)
63
+
64
+ if (isUrlRequest(url, options.root)) {
65
+ url = loaderUtils.urlToRequest(url, options.root)
39
66
  }
67
+
68
+ importItems.push({
69
+ url: url,
70
+ mediaQuery: mediaQuery
71
+ })
72
+ rule.remove()
40
73
  })
41
- return tokens.toString()
42
74
  }
43
- return str
44
- }
45
-
46
- if (options.import) {
47
- css.walkAtRules(/^import$/i, function (rule) {
48
- const values = Tokenizer.parseValues(rule.params)
49
- let url = values.nodes[0].nodes[0]
50
- if (url && url.type === 'url') {
51
- url = url.url
52
- } else if (url && url.type === 'string') {
53
- url = url.value
54
- } else throw rule.error('Unexpected format ' + rule.params)
55
- if (!url.replace(/\s/g, '').length) {
56
- return
57
- }
58
- values.nodes[0].nodes.shift()
59
- const mediaQuery = Tokenizer.stringifyValues(values)
60
75
 
61
- if (isUrlRequest(url, options.root)) {
62
- url = loaderUtils.urlToRequest(url, options.root)
63
- }
64
-
65
- importItems.push({
66
- url: url,
67
- mediaQuery: mediaQuery
76
+ const icss = icssUtils.extractICSS(css)
77
+ exports = icss.icssExports
78
+ Object.keys(icss.icssImports).forEach(function (key) {
79
+ const url = loaderUtils.parseString(key)
80
+ Object.keys(icss.icssImports[key]).forEach(function (prop) {
81
+ imports['$' + prop] = importItems.length
82
+ importItems.push({
83
+ url: url,
84
+ export: icss.icssImports[key][prop]
85
+ })
68
86
  })
69
- rule.remove()
70
87
  })
71
- }
72
88
 
73
- const icss = icssUtils.extractICSS(css)
74
- exports = icss.icssExports
75
- Object.keys(icss.icssImports).forEach(function (key) {
76
- const url = loaderUtils.parseString(key)
77
- Object.keys(icss.icssImports[key]).forEach(function (prop) {
78
- imports['$' + prop] = importItems.length
79
- importItems.push({
80
- url: url,
81
- export: icss.icssImports[key][prop]
82
- })
89
+ Object.keys(exports).forEach(function (exportName) {
90
+ exports[exportName] = replaceImportsInString(exports[exportName])
83
91
  })
84
- })
85
-
86
- Object.keys(exports).forEach(function (exportName) {
87
- exports[exportName] = replaceImportsInString(exports[exportName])
88
- })
89
92
 
90
- function isAlias (url) {
91
- // Handle alias starting by / and root disabled
92
- return url !== options.resolve(url)
93
- }
93
+ function isAlias (url) {
94
+ // Handle alias starting by / and root disabled
95
+ return url !== options.resolve(url)
96
+ }
94
97
 
95
- function processNode (item) {
96
- switch (item.type) {
97
- case 'value':
98
- item.nodes.forEach(processNode)
99
- break
100
- case 'nested-item':
101
- item.nodes.forEach(processNode)
102
- break
103
- case 'item':
104
- const importIndex = imports['$' + item.name]
105
- if (typeof importIndex === 'number') {
106
- item.name = '___CSS_LOADER_IMPORT___' + importIndex + '___'
107
- }
108
- break
109
- case 'url':
110
- if (options.url && item.url.replace(/\s/g, '').length && !/^#/.test(item.url) && (isAlias(item.url) || isUrlRequest(item.url, options.root))) {
111
- // Strip quotes, they will be re-added if the module needs them
112
- item.stringType = ''
113
- delete item.innerSpacingBefore
114
- delete item.innerSpacingAfter
115
- const url = item.url
116
- item.url = '___CSS_LOADER_URL___' + urlItems.length + '___'
117
- urlItems.push({
118
- url: url
119
- })
120
- }
121
- break
98
+ function processNode (item) {
99
+ switch (item.type) {
100
+ case 'value':
101
+ item.nodes.forEach(processNode)
102
+ break
103
+ case 'nested-item':
104
+ item.nodes.forEach(processNode)
105
+ break
106
+ case 'item':
107
+ const importIndex = imports['$' + item.name]
108
+ if (typeof importIndex === 'number') {
109
+ item.name = '___CSS_LOADER_IMPORT___' + importIndex + '___'
110
+ }
111
+ break
112
+ case 'url':
113
+ if (options.url && item.url.replace(/\s/g, '').length && !/^#/.test(item.url) && (isAlias(item.url) || isUrlRequest(item.url, options.root))) {
114
+ // Strip quotes, they will be re-added if the module needs them
115
+ item.stringType = ''
116
+ delete item.innerSpacingBefore
117
+ delete item.innerSpacingAfter
118
+ const url = item.url
119
+ item.url = '___CSS_LOADER_URL___' + urlItems.length + '___'
120
+ urlItems.push({
121
+ url: url
122
+ })
123
+ }
124
+ break
125
+ }
122
126
  }
123
- }
124
127
 
125
- css.walkDecls(function (decl) {
126
- const values = Tokenizer.parseValues(decl.value)
127
- values.nodes.forEach(function (value) {
128
- value.nodes.forEach(processNode)
128
+ css.walkDecls(function (decl) {
129
+ const values = Tokenizer.parseValues(decl.value)
130
+ values.nodes.forEach(function (value) {
131
+ value.nodes.forEach(processNode)
132
+ })
133
+ decl.value = Tokenizer.stringifyValues(values)
134
+ })
135
+ css.walkAtRules(function (atrule) {
136
+ if (typeof atrule.params === 'string') {
137
+ atrule.params = replaceImportsInString(atrule.params)
138
+ }
129
139
  })
130
- decl.value = Tokenizer.stringifyValues(values)
131
- })
132
- css.walkAtRules(function (atrule) {
133
- if (typeof atrule.params === 'string') {
134
- atrule.params = replaceImportsInString(atrule.params)
135
- }
136
- })
137
140
 
138
- options.importItems = importItems
139
- options.urlItems = urlItems
140
- options.exports = exports
141
+ options.importItems = importItems
142
+ options.urlItems = urlItems
143
+ options.exports = exports
144
+ }
141
145
  }
142
- })
146
+ }
143
147
 
144
148
  module.exports = function processCss (inputSource, inputMap, options, callback) {
145
149
  const query = options.query