-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
172 lines (158 loc) · 5.94 KB
/
gatsby-node.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { testPluginOptionsSchema } from "gatsby-plugin-utils"
import { pluginOptionsSchema } from "../gatsby-node"
describe(`gatsby-plugin-sass`, () => {
jest.mock(`../resolve`, () => module => `/resolved/path/${module}`)
const actions = {
setWebpackConfig: jest.fn(),
}
// loaders "mocks"
const loaders = {
miniCssExtract: () => `miniCssExtract`,
css: args => `css(${JSON.stringify(args)})`,
postcss: args => `postcss(${JSON.stringify(args)})`,
null: () => `null`,
}
const { onCreateWebpackConfig } = require(`../gatsby-node`)
beforeEach(() => {
actions.setWebpackConfig.mockReset()
})
const tests = {
stages: [`develop`, `build-javascript`, `develop-html`, `build-html`],
options: {
"No options": {},
"Sass options": {
sassOptions: {
includePaths: [`absolute/path/a`, `absolute/path/b`],
},
},
"PostCss plugins": {
postCssPlugins: [`test1`],
},
"css-loader options": {
cssLoaderOptions: {
camelCase: false,
modules: {
exportLocalsConvention: `camelCaseOnly`,
},
},
},
"sass rule test options": {
sassRuleTest: /\.global\.s(a|c)ss$/,
},
"sass rule modules test options": {
sassRuleModulesTest: /\.global\.s(a|c)ss$/,
},
},
}
tests.stages.forEach(stage => {
for (let label in tests.options) {
const options = tests.options[label]
it(`Stage: ${stage} / ${label}`, () => {
onCreateWebpackConfig({ actions, loaders, stage }, options)
expect(actions.setWebpackConfig).toMatchSnapshot()
})
}
})
})
describe(`pluginOptionsSchema`, () => {
it(`should provide meaningful errors when fields are invalid`, async () => {
const expectedErrors = [
`"implementation" must be of type object`,
`"cssLoaderOptions" must be of type object`,
`"postCssPlugins" must be an array`,
`"sassRuleTest" must be of type object`,
`"sassRuleModulesTest" must be of type object`,
`"useResolveUrlLoader" must be one of [boolean, object]`,
`"sassOptions.file" must be a string`,
`"sassOptions.data" must be a string`,
`"sassOptions.importer" must be of type function`,
`"sassOptions.functions" must be of type object`,
`"sassOptions.includePaths" must be an array`,
`"sassOptions.indentedSyntax" must be a boolean`,
`"sassOptions.indentType" must be a string`,
`"sassOptions.indentWidth" must be less than or equal to 10`,
`"sassOptions.linefeed" must be one of [cr, crlf, lf, lfcr]`,
`"sassOptions.omitSourceMapUrl" must be a boolean`,
`"sassOptions.outFile" must be a string`,
`"sassOptions.outputStyle" must be one of [nested, expanded, compact, compressed]`,
`"sassOptions.precision" must be a number`,
`"sassOptions.sourceComments" must be a boolean`,
`"sassOptions.sourceMap" must be one of [boolean, string]`,
`"sassOptions.sourceMapContents" must be a boolean`,
`"sassOptions.sourceMapEmbed" must be a boolean`,
`"sassOptions.sourceMapRoot" must be a string`,
]
const { errors } = await testPluginOptionsSchema(pluginOptionsSchema, {
implementation: `This should be a require() thing`,
postCssPlugins: `This should be an array of postCss plugins`,
cssLoaderOptions: `This should be an object of css-loader options`,
sassRuleTest: `This should be a regexp`,
sassRuleModulesTest: `This should be a regexp`,
useResolveUrlLoader: `This should be a boolean`,
sassOptions: {
file: 123, // should be a string
data: 123, // should be a string
importer: `This should be a function`,
functions: `This should be an object of { string: function }`,
includePaths: 123, // should be an array of string
indentedSyntax: `"useResolveUrlLoader" must be a boolean`,
indentType: 123, // this should be a string
indentWidth: 40,
linefeed: `This should be cr, crlf, lf or lfcr`,
omitSourceMapUrl: `This should be a boolean`,
outFile: 123, // This should be a string
outputStyle: `This should be nested, expanded, compact or compressed`,
precision: `This should be a number`,
sourceComments: `This should be a boolean`,
sourceMap: 123, // This should be a string or a boolean
sourceMapContents: `This should be a boolean`,
sourceMapEmbed: `This should be a boolean`,
sourceMapRoot: 123, // This should be a string
},
})
expect(errors).toEqual(expectedErrors)
})
it(`should validate the schema`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
implementation: require(`../gatsby-node.js`),
cssLoaderOptions: { camelCase: false },
postCssPlugins: [require(`autoprefixer`)],
sassRuleTest: /\.global\.s(a|c)ss$/,
sassRuleModulesTest: /\.mod\.s(a|c)ss$/,
useResolveUrlLoader: false,
sassOptions: {
file: `../path-to-file`,
data: `{ some: data }`,
importer: function () {
return { file: `path-to-file`, contents: `data` }
},
functions: {
"headings($from: 0, $to: 6)": function () {
return []
},
},
includePaths: [`some`, `path`],
indentedSyntax: true,
indentType: `tabs`,
indentWidth: 7,
linefeed: `crlf`,
omitSourceMapUrl: true,
outFile: `somewhere-around.css`,
outputStyle: `expanded`,
precision: 12,
sourceComments: true,
sourceMap: true,
sourceMapContents: true,
sourceMapEmbed: true,
sourceMapRoot: `some-source-map-root`,
},
})
expect(isValid).toBe(true)
})
it(`should allow unknown options`, async () => {
const { isValid } = await testPluginOptionsSchema(pluginOptionsSchema, {
webpackImporter: `unknown option`,
})
expect(isValid).toBe(true)
})
})