@tarojs/rn-style-transformer 3.3.7 → 3.3.8
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.
- package/__tests__/config.spec.js +1 -1
- package/__tests__/index.spec.js +1 -1
- package/__tests__/platform.spec.js +1 -1
- package/dist/transforms/index.js +71 -44
- package/dist/transforms/index.js.map +1 -1
- package/dist/transforms/less.js +9 -12
- package/dist/transforms/less.js.map +1 -1
- package/dist/transforms/postcss.js +7 -6
- package/dist/transforms/postcss.js.map +1 -1
- package/dist/transforms/sass.js +15 -23
- package/dist/transforms/sass.js.map +1 -1
- package/dist/transforms/stylus.js +14 -11
- package/dist/transforms/stylus.js.map +1 -1
- package/dist/types/index.js +0 -6
- package/dist/types/index.js.map +1 -1
- package/dist/utils/index.js +77 -15
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/lessImport.js +1 -2
- package/dist/utils/lessImport.js.map +1 -1
- package/dist/utils/reporterSkip.js +23 -0
- package/dist/utils/reporterSkip.js.map +1 -0
- package/jest.config.js +3 -2
- package/package.json +9 -8
- package/src/transforms/index.ts +113 -41
- package/src/transforms/less.ts +25 -15
- package/src/transforms/postcss.ts +10 -5
- package/src/transforms/sass.ts +35 -27
- package/src/transforms/stylus.ts +26 -14
- package/src/types/index.ts +10 -1
- package/src/utils/index.ts +89 -10
- package/src/utils/reporterSkip.ts +24 -0
package/src/transforms/less.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import path from 'path'
|
|
2
2
|
import less from 'less'
|
|
3
3
|
import makeLessImport from '../utils/lessImport'
|
|
4
|
+
import { insertBefore, getAdditionalData } from '../utils'
|
|
5
|
+
import { RenderResult, RenderAdditionalResult } from '../types'
|
|
4
6
|
|
|
5
7
|
interface SourceMapOption {
|
|
6
8
|
sourceMapURL?: string;
|
|
@@ -58,7 +60,7 @@ export interface Options {
|
|
|
58
60
|
export interface Config {
|
|
59
61
|
alias?: Record<string, string>
|
|
60
62
|
options: Options
|
|
61
|
-
additionalData?: string | ((string) => string)
|
|
63
|
+
additionalData?: string | ((key: string) => string)
|
|
62
64
|
}
|
|
63
65
|
|
|
64
66
|
function renderToCSS (src, filename, options = {} as any) {
|
|
@@ -73,26 +75,34 @@ function renderToCSS (src, filename, options = {} as any) {
|
|
|
73
75
|
filename,
|
|
74
76
|
plugins: plugins.concat(options.plugins || []),
|
|
75
77
|
paths: paths.concat(options.paths || [])
|
|
76
|
-
}, (err,
|
|
78
|
+
}, (err, result) => {
|
|
77
79
|
if (err) {
|
|
78
80
|
return reject(err.message)
|
|
79
81
|
}
|
|
80
|
-
resolve(
|
|
82
|
+
resolve(result)
|
|
81
83
|
})
|
|
82
84
|
})
|
|
83
85
|
}
|
|
84
86
|
|
|
85
|
-
export default function transform (
|
|
86
|
-
|
|
87
|
+
export default function transform (
|
|
88
|
+
src: string,
|
|
89
|
+
filename: string,
|
|
90
|
+
config: Config
|
|
91
|
+
) {
|
|
92
|
+
const additionalData = getAdditionalData(src, config.additionalData)
|
|
93
|
+
const data = insertBefore(src, additionalData)
|
|
87
94
|
|
|
88
|
-
|
|
89
|
-
data
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
95
|
+
return renderToCSS(
|
|
96
|
+
data,
|
|
97
|
+
filename,
|
|
98
|
+
{
|
|
99
|
+
sourceMap: {
|
|
100
|
+
outputFilename: `${filename}.map`
|
|
101
|
+
},
|
|
102
|
+
alias: config.alias,
|
|
103
|
+
...config.options
|
|
104
|
+
}
|
|
105
|
+
).then((result: RenderResult) => {
|
|
106
|
+
return { ...result, additionalData } as RenderAdditionalResult
|
|
107
|
+
})
|
|
98
108
|
}
|
|
@@ -3,6 +3,7 @@ import pxtransform from 'postcss-pxtransform'
|
|
|
3
3
|
import postcssImport from 'postcss-import'
|
|
4
4
|
import { recursiveMerge } from '@tarojs/helper'
|
|
5
5
|
import { resolveStyle } from '../utils'
|
|
6
|
+
import reporterSkip from '../utils/reporterSkip'
|
|
6
7
|
import stylelintConfig from '../config/rn-stylelint.json'
|
|
7
8
|
|
|
8
9
|
export interface Config {
|
|
@@ -11,7 +12,7 @@ export interface Config {
|
|
|
11
12
|
pxtransform: {
|
|
12
13
|
enable: boolean;
|
|
13
14
|
config: any;
|
|
14
|
-
}
|
|
15
|
+
};
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
const defaultPxtransformOption: {
|
|
@@ -23,11 +24,13 @@ const defaultPxtransformOption: {
|
|
|
23
24
|
}
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
export function
|
|
27
|
+
export function makePostcssPlugins ({
|
|
28
|
+
filename,
|
|
27
29
|
designWidth,
|
|
28
30
|
deviceRatio,
|
|
29
31
|
postcssConfig,
|
|
30
|
-
transformOptions
|
|
32
|
+
transformOptions,
|
|
33
|
+
additionalData
|
|
31
34
|
}) {
|
|
32
35
|
if (designWidth) {
|
|
33
36
|
defaultPxtransformOption.config.designWidth = designWidth
|
|
@@ -59,8 +62,11 @@ export function getPostcssPlugins ({
|
|
|
59
62
|
plugins.push(pxtransform(pxtransformOption.config))
|
|
60
63
|
}
|
|
61
64
|
|
|
65
|
+
const skipRows = additionalData ? additionalData.split('\n').length : 0
|
|
66
|
+
|
|
62
67
|
plugins.push(
|
|
63
68
|
require('stylelint')(stylelintConfig),
|
|
69
|
+
reporterSkip({ skipRows, filename }),
|
|
64
70
|
require('postcss-reporter')({ clearReportedMessages: true })
|
|
65
71
|
)
|
|
66
72
|
|
|
@@ -71,7 +77,6 @@ export default function transform (src: string, filename: string, { options, plu
|
|
|
71
77
|
return postcss(plugins)
|
|
72
78
|
.process(src, { from: filename, ...options })
|
|
73
79
|
.then(result => {
|
|
74
|
-
|
|
75
|
-
return css
|
|
80
|
+
return result
|
|
76
81
|
})
|
|
77
82
|
}
|
package/src/transforms/sass.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import fs from 'fs'
|
|
2
2
|
import path from 'path'
|
|
3
3
|
import sass, { Options } from 'sass'
|
|
4
|
-
import { insertBefore, resolveStyle } from '../utils'
|
|
5
|
-
import { TransformOptions } from '../types'
|
|
4
|
+
import { insertAfter, insertBefore, resolveStyle, getAdditionalData } from '../utils'
|
|
5
|
+
import { TransformOptions, RenderResult, RenderAdditionalResult } from '../types'
|
|
6
6
|
|
|
7
7
|
// https://github.com/sass/node-sass#options
|
|
8
8
|
export interface Config {
|
|
9
|
+
sass?: SassGlobalConfig
|
|
9
10
|
alias?: Record<string, string>
|
|
10
11
|
options?: Options
|
|
11
|
-
additionalData?: string | ((string) => string)
|
|
12
|
+
additionalData?: string | ((key: string) => string)
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
export interface
|
|
15
|
+
export interface SassGlobalConfig {
|
|
15
16
|
resource?: string | string[];
|
|
16
17
|
projectDirectory?: string;
|
|
17
18
|
data?: string;
|
|
@@ -31,13 +32,7 @@ function makeImportStatement (filePath: string, resource: string, rootDir: strin
|
|
|
31
32
|
return `@import '${relativePath}'`
|
|
32
33
|
}
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
* 处理 additional 配置,比如 config.sass 配置
|
|
36
|
-
* @param src
|
|
37
|
-
* @param filename
|
|
38
|
-
* @returns
|
|
39
|
-
*/
|
|
40
|
-
export function processByExternal (src, filename, config: SassExternalConfig) {
|
|
35
|
+
function getGlobalResource (filename: string, config: SassGlobalConfig) {
|
|
41
36
|
let resource = ''
|
|
42
37
|
const projectDirectory = config.projectDirectory || process.cwd()
|
|
43
38
|
const filePath = path.dirname(path.resolve(projectDirectory, filename))
|
|
@@ -48,8 +43,18 @@ export function processByExternal (src, filename, config: SassExternalConfig) {
|
|
|
48
43
|
const resources = config.resource?.map(source => makeImportStatement(filePath, source, projectDirectory)) || []
|
|
49
44
|
resource = resources.join(';\n')
|
|
50
45
|
}
|
|
51
|
-
|
|
52
|
-
return
|
|
46
|
+
// https://taro-docs.jd.com/taro/docs/config-detail/#sassdata, data 覆盖 resurce 配置
|
|
47
|
+
return insertAfter(resource, config?.data)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function combineResource (src: string, filename: string, config: Config) {
|
|
51
|
+
// sass config
|
|
52
|
+
const globalResource = getGlobalResource(filename, config.sass)
|
|
53
|
+
|
|
54
|
+
// sass tranform config
|
|
55
|
+
const additionalData = getAdditionalData(src, config.additionalData)
|
|
56
|
+
|
|
57
|
+
return insertAfter(globalResource, additionalData)
|
|
53
58
|
}
|
|
54
59
|
|
|
55
60
|
function renderToCSS (src, filename, options, transformOptions) {
|
|
@@ -98,8 +103,7 @@ function renderToCSS (src, filename, options, transformOptions) {
|
|
|
98
103
|
if (err) {
|
|
99
104
|
reject(err)
|
|
100
105
|
} else {
|
|
101
|
-
|
|
102
|
-
resolve(css)
|
|
106
|
+
resolve(result)
|
|
103
107
|
}
|
|
104
108
|
})
|
|
105
109
|
})
|
|
@@ -111,21 +115,25 @@ export default function transform (
|
|
|
111
115
|
config: Config,
|
|
112
116
|
transformOptions: TransformOptions
|
|
113
117
|
) {
|
|
114
|
-
|
|
118
|
+
const additionalData = combineResource(src, filename, config)
|
|
119
|
+
let data = insertBefore(src, additionalData)
|
|
115
120
|
|
|
116
121
|
if (!data) {
|
|
117
122
|
data = `\n${data}` // fix empty file error. reference https://github.com/sass/node-sass/blob/91c40a0bf0a3923ab9f91b82dcd479c25486235a/lib/index.js#L430
|
|
118
123
|
}
|
|
119
124
|
|
|
120
|
-
|
|
121
|
-
data
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
125
|
+
return renderToCSS(
|
|
126
|
+
data,
|
|
127
|
+
filename,
|
|
128
|
+
{
|
|
129
|
+
file: filename,
|
|
130
|
+
outFile: `${filename}.map`,
|
|
131
|
+
sourceMap: true, // If no outFile is set, sourceMap parameter is ignored.
|
|
132
|
+
alias: config.alias,
|
|
133
|
+
...config.options
|
|
134
|
+
},
|
|
135
|
+
transformOptions
|
|
136
|
+
).then((result: RenderResult) => {
|
|
137
|
+
return { ...result, additionalData } as RenderAdditionalResult
|
|
138
|
+
})
|
|
131
139
|
}
|
package/src/transforms/stylus.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import stylus from 'stylus'
|
|
2
|
+
import { RenderResult, RenderAdditionalResult } from '../types'
|
|
3
|
+
import { getAdditionalData, insertBefore } from '../utils'
|
|
2
4
|
|
|
3
5
|
class Evaluator { }
|
|
4
6
|
|
|
@@ -99,7 +101,7 @@ interface RenderOptions {
|
|
|
99
101
|
export interface Config {
|
|
100
102
|
alias?: Record<string, string>
|
|
101
103
|
options: RenderOptions
|
|
102
|
-
additionalData?: string | ((string) => string)
|
|
104
|
+
additionalData?: string | ((key: string) => string)
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
export const defaultOptions = {
|
|
@@ -118,6 +120,15 @@ function renderToCSS (src, filename, options = {} as RenderOptions) {
|
|
|
118
120
|
const stylusOptions = { filename, ...options }
|
|
119
121
|
const styl = stylus(src, stylusOptions)
|
|
120
122
|
|
|
123
|
+
styl.set(
|
|
124
|
+
'sourcemap',
|
|
125
|
+
{
|
|
126
|
+
comment: true,
|
|
127
|
+
sourceRoot: '.', // stylusOptions.dest
|
|
128
|
+
basePath: '.'
|
|
129
|
+
}
|
|
130
|
+
)
|
|
131
|
+
|
|
121
132
|
// include regular CSS on @import
|
|
122
133
|
if (stylusOptions.includeCSS) {
|
|
123
134
|
styl.set('include css', true)
|
|
@@ -191,23 +202,24 @@ function renderToCSS (src, filename, options = {} as RenderOptions) {
|
|
|
191
202
|
if (err) {
|
|
192
203
|
reject(err)
|
|
193
204
|
} else {
|
|
194
|
-
resolve(
|
|
205
|
+
resolve({
|
|
206
|
+
css,
|
|
207
|
+
map: styl.sourcemap
|
|
208
|
+
})
|
|
195
209
|
}
|
|
196
210
|
})
|
|
197
211
|
})
|
|
198
212
|
}
|
|
199
213
|
|
|
200
214
|
export default function transform (src: string, filename: string, config: Config) {
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
return css
|
|
212
|
-
})
|
|
215
|
+
const additionalData = getAdditionalData(src, config.additionalData)
|
|
216
|
+
const data = insertBefore(src, additionalData)
|
|
217
|
+
|
|
218
|
+
return renderToCSS(
|
|
219
|
+
data,
|
|
220
|
+
filename,
|
|
221
|
+
config.options
|
|
222
|
+
).then((result: RenderResult) => {
|
|
223
|
+
return { ...result, additionalData } as RenderAdditionalResult
|
|
224
|
+
})
|
|
213
225
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -8,7 +8,7 @@ export interface TransformOptions {
|
|
|
8
8
|
customTransformOptions: any
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export enum LogLevelEnum {
|
|
11
|
+
export const enum LogLevelEnum {
|
|
12
12
|
ERROR = 'error',
|
|
13
13
|
WARNING = 'warning',
|
|
14
14
|
}
|
|
@@ -21,3 +21,12 @@ export interface ResolveStyleOptions {
|
|
|
21
21
|
defaultExt?: string
|
|
22
22
|
alias?: Record<string, string>
|
|
23
23
|
}
|
|
24
|
+
|
|
25
|
+
export interface RenderResult {
|
|
26
|
+
css: string | Buffer
|
|
27
|
+
map?: string | Buffer
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RenderAdditionalResult extends RenderResult {
|
|
31
|
+
additionalData: string
|
|
32
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -3,30 +3,30 @@ import path from 'path'
|
|
|
3
3
|
import { printLog, processTypeEnum } from '@tarojs/helper'
|
|
4
4
|
import { ResolveStyleOptions, LogLevelEnum } from '../types'
|
|
5
5
|
|
|
6
|
-
export function insertBefore (source: string,
|
|
7
|
-
if (!source && !
|
|
6
|
+
export function insertBefore (source: string, additional: string) {
|
|
7
|
+
if (!source && !additional) {
|
|
8
8
|
return ''
|
|
9
9
|
}
|
|
10
10
|
if (!source) {
|
|
11
|
-
return
|
|
11
|
+
return additional
|
|
12
12
|
}
|
|
13
|
-
if (!
|
|
13
|
+
if (!additional) {
|
|
14
14
|
return source
|
|
15
15
|
}
|
|
16
|
-
return
|
|
16
|
+
return additional + ';\n' + source
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export function insertAfter (source: string,
|
|
20
|
-
if (!source && !
|
|
19
|
+
export function insertAfter (source: string, additional: string) {
|
|
20
|
+
if (!source && !additional) {
|
|
21
21
|
return ''
|
|
22
22
|
}
|
|
23
23
|
if (!source) {
|
|
24
|
-
return
|
|
24
|
+
return additional
|
|
25
25
|
}
|
|
26
|
-
if (!
|
|
26
|
+
if (!additional) {
|
|
27
27
|
return source
|
|
28
28
|
}
|
|
29
|
-
return source + ';\n' +
|
|
29
|
+
return source + ';\n' + additional
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
// Iterate through the include paths and extensions to find the file variant
|
|
@@ -99,4 +99,83 @@ export function resolveStyle (id: string, opts: ResolveStyleOptions) {
|
|
|
99
99
|
return file
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
// copy from https://github.com/webpack-contrib/css-loader/blob/master/src/utils.js
|
|
103
|
+
const IS_NATIVE_WIN32_PATH = /^[a-z]:[/\\]|^\\\\/i
|
|
104
|
+
const ABSOLUTE_SCHEME = /^[a-z0-9+\-.]+:/i
|
|
105
|
+
|
|
106
|
+
export function normalizePath (file) {
|
|
107
|
+
return path.sep === '\\' ? file.replace(/\\/g, '/') : file
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function getURLType (source) {
|
|
111
|
+
if (source[0] === '/') {
|
|
112
|
+
if (source[1] === '/') {
|
|
113
|
+
return 'scheme-relative'
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return 'path-absolute'
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (IS_NATIVE_WIN32_PATH.test(source)) {
|
|
120
|
+
return 'path-absolute'
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return ABSOLUTE_SCHEME.test(source) ? 'absolute' : 'path-relative'
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function normalizeSourceMap (map, resourcePath) {
|
|
127
|
+
let newMap = map
|
|
128
|
+
|
|
129
|
+
// Some loader emit source map as string
|
|
130
|
+
// Strip any JSON XSSI avoidance prefix from the string (as documented in the source maps specification), and then parse the string as JSON.
|
|
131
|
+
if (typeof newMap === 'string') {
|
|
132
|
+
newMap = JSON.parse(newMap)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
delete newMap.file
|
|
136
|
+
|
|
137
|
+
const { sourceRoot } = newMap
|
|
138
|
+
|
|
139
|
+
delete newMap.sourceRoot
|
|
140
|
+
|
|
141
|
+
if (newMap.sources) {
|
|
142
|
+
// Source maps should use forward slash because it is URLs (https://github.com/mozilla/source-map/issues/91)
|
|
143
|
+
// We should normalize path because previous loaders like `sass-loader` using backslash when generate source map
|
|
144
|
+
newMap.sources = newMap.sources.map((source) => {
|
|
145
|
+
// Non-standard syntax from `postcss`
|
|
146
|
+
if (source.indexOf('<') === 0) {
|
|
147
|
+
return source
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const sourceType = getURLType(source)
|
|
151
|
+
|
|
152
|
+
// Do no touch `scheme-relative` and `absolute` URLs
|
|
153
|
+
if (sourceType === 'path-relative' || sourceType === 'path-absolute') {
|
|
154
|
+
const absoluteSource =
|
|
155
|
+
sourceType === 'path-relative' && sourceRoot
|
|
156
|
+
? path.resolve(sourceRoot, normalizePath(source))
|
|
157
|
+
: normalizePath(source)
|
|
158
|
+
|
|
159
|
+
return path.relative(path.dirname(resourcePath), absoluteSource)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return source
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return newMap
|
|
167
|
+
}
|
|
168
|
+
// copy end
|
|
169
|
+
|
|
170
|
+
export function getAdditionalData (data: string, config: string | ((key: string) => string)) {
|
|
171
|
+
let additionalData = ''
|
|
172
|
+
if (typeof config !== 'undefined') {
|
|
173
|
+
additionalData =
|
|
174
|
+
typeof config === 'function'
|
|
175
|
+
? `${config(data)}`
|
|
176
|
+
: config
|
|
177
|
+
}
|
|
178
|
+
return additionalData
|
|
179
|
+
}
|
|
180
|
+
|
|
102
181
|
export default {}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
interface Location {
|
|
2
|
+
line: number
|
|
3
|
+
column: number
|
|
4
|
+
file: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default function reporterSkip ({ skipRows, filename }) {
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
return (css, result) => {
|
|
10
|
+
result.messages?.forEach((message: any) => {
|
|
11
|
+
const { line, column, node } = message
|
|
12
|
+
|
|
13
|
+
const messageInput = node?.source?.input
|
|
14
|
+
if (messageInput) {
|
|
15
|
+
const originLocation: Location = messageInput.origin(line, column)
|
|
16
|
+
// 如果是原始引入的样式文件,则对拼接的代码(addionalData)函数做减法
|
|
17
|
+
if (originLocation && originLocation.file.includes(filename)) {
|
|
18
|
+
// force modify line,then source map cannot read origin column, value is 0.
|
|
19
|
+
message.line -= skipRows
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
}
|