@rollup-extras/plugin-script-loader 0.1.0 → 0.1.2
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/README.md +2 -0
- package/index.js +73 -21
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
1
3
|
# Plugin Script Loader
|
|
2
4
|
|
|
3
5
|
Rollup plugin to mimic Webpack's `script-loader` inline behavior. Inlines raw scripts into the bundle in import order, or emits them as a separate asset file for non-module, non-strict mode execution.
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import { basename } from 'node:path';
|
|
2
|
+
import { basename, dirname, join, relative, resolve } from 'node:path';
|
|
3
3
|
|
|
4
|
+
import remapping from '@ampproject/remapping';
|
|
4
5
|
import MagicString, { Bundle } from 'magic-string';
|
|
5
6
|
|
|
6
7
|
import { LogLevel } from '@niceties/logger';
|
|
@@ -61,7 +62,7 @@ export default function (options = {}) {
|
|
|
61
62
|
const externalMatch = code.match(/\/\/[#@]\s*sourceMappingURL=(.+?)\s*$/);
|
|
62
63
|
if (externalMatch && !externalMatch[1].startsWith('data:')) {
|
|
63
64
|
try {
|
|
64
|
-
const mapPath =
|
|
65
|
+
const mapPath = join(dirname(filePath), externalMatch[1]);
|
|
65
66
|
const mapContent = await readFile(mapPath, 'utf8');
|
|
66
67
|
return JSON.parse(mapContent);
|
|
67
68
|
} catch {
|
|
@@ -187,7 +188,9 @@ export default function (options = {}) {
|
|
|
187
188
|
})
|
|
188
189
|
.map(([, entry]) => entry);
|
|
189
190
|
|
|
190
|
-
const
|
|
191
|
+
const outputDir = _outputOptions.dir ?? (_outputOptions.file ? dirname(_outputOptions.file) : process.cwd());
|
|
192
|
+
|
|
193
|
+
const magicBundle = new Bundle({ separator: '\n;\n' });
|
|
191
194
|
|
|
192
195
|
for (const entry of sortedEntries) {
|
|
193
196
|
const ms = new MagicString(entry.code, {
|
|
@@ -205,7 +208,7 @@ export default function (options = {}) {
|
|
|
205
208
|
let generatedMap;
|
|
206
209
|
|
|
207
210
|
if (sourcemap) {
|
|
208
|
-
|
|
211
|
+
const magicMap = (
|
|
209
212
|
magicBundle.generateMap({
|
|
210
213
|
file: name,
|
|
211
214
|
source: name,
|
|
@@ -213,6 +216,26 @@ export default function (options = {}) {
|
|
|
213
216
|
hires: true,
|
|
214
217
|
})
|
|
215
218
|
);
|
|
219
|
+
|
|
220
|
+
const originalMaps = new Map(sortedEntries.filter(e => e.originalMap).map(e => [e.filePath, e.originalMap]));
|
|
221
|
+
|
|
222
|
+
if (originalMaps.size > 0) {
|
|
223
|
+
|
|
224
|
+
const returned = new Set();
|
|
225
|
+
generatedMap = (
|
|
226
|
+
remapping(magicMap, file => {
|
|
227
|
+
if (returned.has(file)) return null;
|
|
228
|
+
const orig = originalMaps.get(file);
|
|
229
|
+
if (orig) {
|
|
230
|
+
returned.add(file);
|
|
231
|
+
return orig;
|
|
232
|
+
}
|
|
233
|
+
return null;
|
|
234
|
+
})
|
|
235
|
+
);
|
|
236
|
+
} else {
|
|
237
|
+
generatedMap = magicMap;
|
|
238
|
+
}
|
|
216
239
|
}
|
|
217
240
|
|
|
218
241
|
if (minify) {
|
|
@@ -227,29 +250,58 @@ export default function (options = {}) {
|
|
|
227
250
|
}
|
|
228
251
|
}
|
|
229
252
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
253
|
+
if (exactFileName) {
|
|
254
|
+
|
|
255
|
+
if (sourcemap && generatedMap) {
|
|
256
|
+
const assetDir = resolve(outputDir, dirname(name));
|
|
257
|
+
generatedMap.sources = generatedMap.sources.map(( s) => relative(assetDir, s));
|
|
258
|
+
|
|
259
|
+
const mapFileName = `${name}.map`;
|
|
260
|
+
generatedMap.file = basename(name);
|
|
261
|
+
concatenatedCode += `\n//# sourceMappingURL=${basename(mapFileName)}`;
|
|
262
|
+
|
|
263
|
+
this.emitFile({
|
|
264
|
+
type: 'asset',
|
|
265
|
+
fileName: name,
|
|
266
|
+
source: concatenatedCode,
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
this.emitFile({
|
|
270
|
+
type: 'asset',
|
|
271
|
+
fileName: mapFileName,
|
|
272
|
+
source: JSON.stringify(generatedMap),
|
|
273
|
+
});
|
|
274
|
+
} else {
|
|
275
|
+
this.emitFile({
|
|
276
|
+
type: 'asset',
|
|
277
|
+
fileName: name,
|
|
278
|
+
source: concatenatedCode,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
} else {
|
|
235
282
|
|
|
236
|
-
|
|
283
|
+
const assetRefId = this.emitFile({
|
|
284
|
+
type: 'asset',
|
|
285
|
+
name,
|
|
286
|
+
source: concatenatedCode,
|
|
287
|
+
});
|
|
237
288
|
|
|
238
|
-
|
|
289
|
+
const finalFileName = this.getFileName(assetRefId);
|
|
239
290
|
|
|
240
|
-
|
|
291
|
+
if (sourcemap && generatedMap) {
|
|
292
|
+
const assetDir = resolve(outputDir, dirname(finalFileName));
|
|
293
|
+
generatedMap.sources = generatedMap.sources.map(( s) => relative(assetDir, s));
|
|
241
294
|
|
|
242
|
-
|
|
295
|
+
const mapFileName = `${finalFileName}.map`;
|
|
296
|
+
generatedMap.file = basename(finalFileName);
|
|
243
297
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
298
|
+
this.emitFile({
|
|
299
|
+
type: 'asset',
|
|
300
|
+
fileName: mapFileName,
|
|
301
|
+
source: JSON.stringify(generatedMap),
|
|
302
|
+
});
|
|
249
303
|
|
|
250
|
-
|
|
251
|
-
if (bundleEntry && bundleEntry.type === 'asset' && typeof bundleEntry.source === 'string') {
|
|
252
|
-
bundleEntry.source += `\n//# sourceMappingURL=${basename(mapFileName)}`;
|
|
304
|
+
(bundle[finalFileName]).source += `\n//# sourceMappingURL=${basename(mapFileName)}`;
|
|
253
305
|
}
|
|
254
306
|
}
|
|
255
307
|
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.1.
|
|
2
|
+
"version": "0.1.2",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"name": "@rollup-extras/plugin-script-loader",
|
|
5
5
|
"author": "Konstantin Shutkin",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"concatenation"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
+
"@ampproject/remapping": "^2.3.0",
|
|
37
38
|
"@niceties/logger": "^2.1.0",
|
|
38
39
|
"magic-string": "^0.30.21",
|
|
39
40
|
"@rollup-extras/utils": "^2.0.0"
|