-
Notifications
You must be signed in to change notification settings - Fork 23
/
rollup.plugin.sass.mjs
146 lines (128 loc) · 4.53 KB
/
rollup.plugin.sass.mjs
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
import { fileURLToPath } from 'node:url'
import { minify } from 'terser';
/**
* A rollup plugin for bundling Sass directly into the project
*/
export default function() {
const sassFilePath = fileURLToPath(new URL('node_modules/sass/sass.dart.js', import.meta.url));
return {
/**
* A rollup build hook for resolving the Sass implementation module.
* @param {string} id the importee exactly as it is written in an import statement in the source code
* @returns {string | undefined} the path to the Sass implementation from the root of this project
*/
resolveId(id) {
if (id === 'sass') {
return sassFilePath;
}
},
/**
* Wraps Sass to bundle it into the project
* @param {string} code the code to modify
* @param {string} id module's identifier
* @returns {string} the modified code
*/
async transform(code, id) {
if (id === sassFilePath) {
return await wrapSassImport(code);
}
return code;
},
name: 'sassPlugin'
}
};
/**
* Wraps Sass in an IIFE to make it easier for rollup to find CJS exports and minifies it.
*
* This function generates code for calling Sass' entrypoint function (`load()`) and capturing a reference to its
* `render` function.
*
* @param {string} code the Sass implementation code
* @returns {Promise<string>} the wrapped Sass code
*/
async function wrapSassImport(code) {
code = `
const Sass = {};
(function(exports) {
/** https://www.npmjs.com/package/sass **/
/**
Copyright (c) 2016, Google Inc.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**/
${code};
})(Sass);
Sass.load({});
const render = Sass.render;
export { render };
`;
if (!code.includes('dartNodePreambleSelf.window')) {
// in jest environments, global.window DOES exist
// which messes with sass's file path resolving on node
// remove global.window check to force it to know we're on node
throw new Error('cannot find "dartNodePreambleSelf.window" in sass.dart');
}
code = code.replace(
'dartNodePreambleSelf.window',
'false /** NODE ENVIRONMENT **/'
);
code = removeCliPkgRequire(code, 'chokidar');
code = removeCliPkgRequire(code, 'readline');
code = removeNodeRequire(code, 'immutable');
const minified = await minify(code, { module: true });
code = minified.code;
return code
}
/**
* Node modules are required by node_modules/sass/sass.dart.js via `_cli_pkg_requires`.
*
* This function manually removes unneeded require statements from the source.
*
* @param {string} code the code to modify
* @param {string} moduleId the module identifier found in a require-like statement
* @returns {string} the modified code
*/
function removeCliPkgRequire(code, moduleId) {
// e.g. `self.chokidar = _cli_pkg_requires.chokidar;`
const requireStr = `self.${moduleId} = _cli_pkg_requires.${moduleId};`;
if (!code.includes(requireStr)) {
throw new Error(`cannot find "${requireStr}" in sass.dart`);
}
return code.replace(
requireStr,
'{}'
);
}
/**
* Node modules are required by node_modules/sass/sass.dart.js via `require`.
*
* This function manually removes unneeded require statements from the source.
*
* @param {string} code the code to modify
* @param {string} moduleId the module identifier found in a require-like statement
* @returns {string} the modified code
*/
function removeNodeRequire(code, moduleId) {
const requireStr = `require("${moduleId}")`;
if (!code.includes(requireStr)) {
throw new Error(`cannot find "${requireStr}" in sass.dart`);
}
return code.replace(
requireStr,
'{}'
);
}