@embroider/addon-dev 8.5.1 → 8.5.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@embroider/addon-dev",
3
- "version": "8.5.1",
3
+ "version": "8.5.3",
4
4
  "description": "Utilities for addon authors",
5
5
  "repository": {
6
6
  "type": "git",
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.default = keepAssets;
7
+ const pluginutils_1 = require("@rollup/pluginutils");
7
8
  const minimatch_1 = __importDefault(require("minimatch"));
8
9
  const path_1 = require("path");
9
10
  const fs_1 = require("fs");
@@ -13,6 +14,9 @@ const magic_string_1 = __importDefault(require("magic-string"));
13
14
  let counter = 11559;
14
15
  function keepAssets({ from, include, exports, }) {
15
16
  const marker = `__keep_assets_marker_${counter++}__`;
17
+ // ids are absolute, so without `dot` an addon anywhere under a dot directory
18
+ // (say ~/.cache) never matches `**`.
19
+ const isKept = (id) => include.some((pattern) => (0, minimatch_1.default)(id, pattern, { dot: true }));
16
20
  return {
17
21
  name: 'keep-assets',
18
22
  // we implement a load hook for the assets we're keeping so that we can
@@ -23,7 +27,7 @@ function keepAssets({ from, include, exports, }) {
23
27
  // load hooks, in which case this will not run but our transform hook will
24
28
  // still over from there.
25
29
  load(id) {
26
- if (include.some((pattern) => (0, minimatch_1.default)(id, pattern))) {
30
+ if (isKept(id)) {
27
31
  return {
28
32
  code: (0, fs_1.readFileSync)(id).toString('binary'),
29
33
  meta: {
@@ -43,10 +47,10 @@ function keepAssets({ from, include, exports, }) {
43
47
  // string and we can emit the true bytes.
44
48
  output = Buffer.from(code, 'binary');
45
49
  }
46
- if (include.some((pattern) => (0, minimatch_1.default)(id, pattern))) {
50
+ if (isKept(id)) {
47
51
  let ref = this.emitFile({
48
52
  type: 'asset',
49
- fileName: (0, path_1.relative)(from, id),
53
+ fileName: (0, pluginutils_1.normalizePath)((0, path_1.relative)(from, id)),
50
54
  source: output,
51
55
  });
52
56
  let replacement;
@@ -73,7 +77,8 @@ function keepAssets({ from, include, exports, }) {
73
77
  magic.replaceAll(new RegExp(`${marker}\\("([^"]+)"\\)`, 'g'), (_x, ref) => {
74
78
  var _a;
75
79
  let assetFileName = this.getFileName(ref);
76
- let relativeName = './' + (0, path_1.relative)((0, path_1.dirname)(chunk.fileName), assetFileName);
80
+ let relativeName = './' +
81
+ (0, pluginutils_1.normalizePath)((0, path_1.relative)((0, path_1.dirname)(chunk.fileName), assetFileName));
77
82
  return (_a = getName(relativeName)) !== null && _a !== void 0 ? _a : '';
78
83
  });
79
84
  magic.prepend(imports());
@@ -1 +1 @@
1
- {"version":3,"file":"rollup-keep-assets.js","sourceRoot":"","sources":["rollup-keep-assets.ts"],"names":[],"mappings":";;;;;AAUA,6BAwFC;AAjGD,0DAAkC;AAClC,+BAAyC;AACzC,2BAAkC;AAClC,gEAAuC;AAEvC,4EAA4E;AAC5E,gDAAgD;AAChD,IAAI,OAAO,GAAG,KAAK,CAAC;AAEpB,SAAwB,UAAU,CAAC,EACjC,IAAI,EACJ,OAAO,EACP,OAAO,GAKR;IACC,MAAM,MAAM,GAAG,wBAAwB,OAAO,EAAE,IAAI,CAAC;IAErD,OAAO;QACL,IAAI,EAAE,aAAa;QAEnB,uEAAuE;QACvE,uEAAuE;QACvE,sEAAsE;QACtE,EAAE;QACF,0EAA0E;QAC1E,0EAA0E;QAC1E,yBAAyB;QACzB,IAAI,CAAC,EAAU;YACb,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAA,mBAAS,EAAC,EAAE,EAAE,OAAO,CAAC,CAAC,EAAE,CAAC;gBACtD,OAAO;oBACL,IAAI,EAAE,IAAA,iBAAY,EAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;oBACzC,IAAI,EAAE;w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,OAAO,MAAM,CAAC,GAAG,CAAC,SAAS,CAAE,CAAC;QAChC,CAAC;QACD,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;YACjC,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,IAAI,EAAE,CAAC;YACZ,IAAI,SAAS,GAAG,UAAU,OAAO,EAAE,GAAG,CAAC;YACvC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC9B,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;gBACjC,OAAO,SAAS,CAAC;YACnB,CAAC;QACH,CAAC;IACH,CAAC;IAED,SAAS,OAAO;QACd,OAAO,CACL,CAAC,GAAG,MAAM,CAAC;aACR,GAAG,CAAC,CAAC,CAAC,SAAS,EAAE,YAAY,CAAC,EAAE,EAAE;YACjC,IAAI,YAAY,EAAE,CAAC;gBACjB,OAAO,UAAU,YAAY,UAAU,SAAS,GAAG,CAAC;YACtD,CAAC;iBAAM,CAAC;gBACN,OAAO,WAAW,SAAS,GAAG,CAAC;YACjC,CAAC;QACH,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CACrB,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC","sourcesContent":["import type { Plugin } from 'rollup';\nimport minimatch from 'minimatch';\nimport { dirname, relative } from 'path';\nimport { readFileSync } from 'fs';\nimport MagicString from 'magic-string';\n\n// randomly chosen, we're just looking to have high-entropy identifiers that\n// won't collide with anyting else in the source\nlet counter = 11559;\n\nexport default function keepAssets({\n from,\n include,\n exports,\n}: {\n from: string;\n include: string[];\n exports?: undefined | 'default' | '*';\n}): Plugin {\n const marker = `__keep_assets_marker_${counter++}__`;\n\n return {\n name: 'keep-assets',\n\n // we implement a load hook for the assets we're keeping so that we can\n // capture their true binary representations. If we fell through to the\n // default rollup load hook we would get utf8 interpretations of them.\n //\n // Our plugin should be placed after any other plugins that have their own\n // load hooks, in which case this will not run but our transform hook will\n // still over from there.\n load(id: string) {\n if (include.some((pattern) => minimatch(id, pattern))) {\n return {\n code: readFileSync(id).toString('binary'),\n meta: {\n 'keep-assets': {\n binaryLoaded: true,\n },\n },\n };\n }\n },\n\n transform(code: string, id: string) {\n let output: Buffer | string = code;\n let ourMeta = this.getModuleInfo(id)?.meta?.['keep-assets'];\n if (ourMeta?.binaryLoaded) {\n // when the code was produced by our own load hook it is binary-encoded\n // string and we can emit the true bytes.\n output = Buffer.from(code, 'binary');\n }\n if (include.some((pattern) => minimatch(id, pattern))) {\n let ref = this.emitFile({\n type: 'asset',\n fileName: relative(from, id),\n source: output,\n });\n let replacement: string;\n if (exports === '*') {\n replacement = `export * from ${marker}(\"${ref}\")`;\n } else if (exports === 'default') {\n replacement = `export default ${marker}(\"${ref}\")`;\n } else {\n // side-effect only\n replacement = `${marker}(\"${ref}\")`;\n }\n // the asset's content leaves the JS module graph here. An empty map is\n // rollup's convention for \"deliberately no mapping\", which keeps the\n // chunk's source map intact instead of breaking it.\n return { code: replacement, map: { mappings: '' as const } };\n }\n },\n renderChunk(code, chunk) {\n if (code.includes(marker)) {\n const { getName, imports } = nameTracker(code, exports);\n const magic = new MagicString(code);\n\n magic.replaceAll(\n new RegExp(`${marker}\\\\(\"([^\"]+)\"\\\\)`, 'g'),\n (_x: string, ref: string) => {\n let assetFileName = this.getFileName(ref);\n let relativeName =\n './' + relative(dirname(chunk.fileName), assetFileName);\n return getName(relativeName) ?? '';\n }\n );\n magic.prepend(imports());\n\n // returning a map keeps the chunk's source map accurate across the\n // injected imports and marker replacements\n return {\n code: magic.toString(),\n map: magic.generateMap({ hires: 'boundary' }),\n };\n }\n },\n };\n}\n\nfunction nameTracker(code: string, exports: undefined | 'default' | '*') {\n let counter = 0;\n let assets = new Map<string, string | undefined>();\n\n function getName(assetName: string): string | undefined {\n if (assets.has(assetName)) {\n return assets.get(assetName)!;\n }\n if (!exports) {\n assets.set(assetName, undefined);\n return undefined;\n }\n while (true) {\n let candidate = `_asset_${counter++}_`;\n if (!code.includes(candidate)) {\n assets.set(assetName, candidate);\n return candidate;\n }\n }\n }\n\n function imports() {\n return (\n [...assets]\n .map(([assetName, importedName]) => {\n if (importedName) {\n return `import ${importedName} from \"${assetName}\"`;\n } else {\n return `import \"${assetName}\"`;\n }\n })\n .join('\\n') + '\\n'\n );\n }\n\n return { getName, imports };\n}\n"]}
1
+ {"version":3,"file":"rollup-keep-assets.js","sourceRoot":"","sources":["rollup-keep-assets.ts"],"names":[],"mappings":";;;;;AAWA,6BA6FC;AAvGD,qDAAoD;AACpD,0DAAkC;AAClC,+BAAyC;AACzC,2BAAkC;AAClC,gEAAuC;AAEvC,4EAA4E;AAC5E,gDAAgD;AAChD,IAAI,OAAO,GAAG,KAAK,CAAC;AAEpB,SAAwB,UAAU,CAAC,EACjC,IAAI,EACJ,OAAO,EACP,OAAO,GAKR;IACC,MAAM,MAAM,GAAG,wBAAwB,OAAO,EAAE,IAAI,CAAC;IACrD,6EAA6E;IAC7E,qCAAqC;IACrC,MAAM,MAAM,GAAG,CAAC,EAAU,EAAE,EAAE,CAC5B,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAA,mBAAS,EAAC,EAAE,EAAE,OAAO,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;IAEnE,OAAO;QACL,IAAI,EAAE,aAAa;QAEnB,uEAAuE;QACvE,uEAAuE;QACvE,sEAAsE;QACtE,EAAE;QACF,0EAA0E;QAC1E,0EAA0E;QAC1E,yBAAyB;QACzB,IAAI,CAAC,EAAU;YACb,IAAI,MAAM,CAAC,EAAE,CAAC,EAAE,CAAC;gBACf,OAAO;oBACL,IAAI,EAAE,IAAA,iBAAY,EAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC;oBACzC,IA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GAAG,CAAC,SAAS,CAAE,CAAC;QAChC,CAAC;QACD,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;YACjC,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,IAAI,EAAE,CAAC;YACZ,IAAI,SAAS,GAAG,UAAU,OAAO,EAAE,GAAG,CAAC;YACvC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC9B,MAAM,CAAC,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;gBACjC,OAAO,SAAS,CAAC;YACnB,CAAC;QACH,CAAC;IACH,CAAC;IAED,SAAS,OAAO;QACd,OAAO,CACL,CAAC,GAAG,MAAM,CAAC;aACR,GAAG,CAAC,CAAC,CAAC,SAAS,EAAE,YAAY,CAAC,EAAE,EAAE;YACjC,IAAI,YAAY,EAAE,CAAC;gBACjB,OAAO,UAAU,YAAY,UAAU,SAAS,GAAG,CAAC;YACtD,CAAC;iBAAM,CAAC;gBACN,OAAO,WAAW,SAAS,GAAG,CAAC;YACjC,CAAC;QACH,CAAC,CAAC;aACD,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CACrB,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC","sourcesContent":["import type { Plugin } from 'rollup';\nimport { normalizePath } from '@rollup/pluginutils';\nimport minimatch from 'minimatch';\nimport { dirname, relative } from 'path';\nimport { readFileSync } from 'fs';\nimport MagicString from 'magic-string';\n\n// randomly chosen, we're just looking to have high-entropy identifiers that\n// won't collide with anyting else in the source\nlet counter = 11559;\n\nexport default function keepAssets({\n from,\n include,\n exports,\n}: {\n from: string;\n include: string[];\n exports?: undefined | 'default' | '*';\n}): Plugin {\n const marker = `__keep_assets_marker_${counter++}__`;\n // ids are absolute, so without `dot` an addon anywhere under a dot directory\n // (say ~/.cache) never matches `**`.\n const isKept = (id: string) =>\n include.some((pattern) => minimatch(id, pattern, { dot: true }));\n\n return {\n name: 'keep-assets',\n\n // we implement a load hook for the assets we're keeping so that we can\n // capture their true binary representations. If we fell through to the\n // default rollup load hook we would get utf8 interpretations of them.\n //\n // Our plugin should be placed after any other plugins that have their own\n // load hooks, in which case this will not run but our transform hook will\n // still over from there.\n load(id: string) {\n if (isKept(id)) {\n return {\n code: readFileSync(id).toString('binary'),\n meta: {\n 'keep-assets': {\n binaryLoaded: true,\n },\n },\n };\n }\n },\n\n transform(code: string, id: string) {\n let output: Buffer | string = code;\n let ourMeta = this.getModuleInfo(id)?.meta?.['keep-assets'];\n if (ourMeta?.binaryLoaded) {\n // when the code was produced by our own load hook it is binary-encoded\n // string and we can emit the true bytes.\n output = Buffer.from(code, 'binary');\n }\n if (isKept(id)) {\n let ref = this.emitFile({\n type: 'asset',\n fileName: normalizePath(relative(from, id)),\n source: output,\n });\n let replacement: string;\n if (exports === '*') {\n replacement = `export * from ${marker}(\"${ref}\")`;\n } else if (exports === 'default') {\n replacement = `export default ${marker}(\"${ref}\")`;\n } else {\n // side-effect only\n replacement = `${marker}(\"${ref}\")`;\n }\n // the asset's content leaves the JS module graph here. An empty map is\n // rollup's convention for \"deliberately no mapping\", which keeps the\n // chunk's source map intact instead of breaking it.\n return { code: replacement, map: { mappings: '' as const } };\n }\n },\n renderChunk(code, chunk) {\n if (code.includes(marker)) {\n const { getName, imports } = nameTracker(code, exports);\n const magic = new MagicString(code);\n\n magic.replaceAll(\n new RegExp(`${marker}\\\\(\"([^\"]+)\"\\\\)`, 'g'),\n (_x: string, ref: string) => {\n let assetFileName = this.getFileName(ref);\n let relativeName =\n './' +\n normalizePath(relative(dirname(chunk.fileName), assetFileName));\n return getName(relativeName) ?? '';\n }\n );\n magic.prepend(imports());\n\n // returning a map keeps the chunk's source map accurate across the\n // injected imports and marker replacements\n return {\n code: magic.toString(),\n map: magic.generateMap({ hires: 'boundary' }),\n };\n }\n },\n };\n}\n\nfunction nameTracker(code: string, exports: undefined | 'default' | '*') {\n let counter = 0;\n let assets = new Map<string, string | undefined>();\n\n function getName(assetName: string): string | undefined {\n if (assets.has(assetName)) {\n return assets.get(assetName)!;\n }\n if (!exports) {\n assets.set(assetName, undefined);\n return undefined;\n }\n while (true) {\n let candidate = `_asset_${counter++}_`;\n if (!code.includes(candidate)) {\n assets.set(assetName, candidate);\n return candidate;\n }\n }\n }\n\n function imports() {\n return (\n [...assets]\n .map(([assetName, importedName]) => {\n if (importedName) {\n return `import ${importedName} from \"${assetName}\"`;\n } else {\n return `import \"${assetName}\"`;\n }\n })\n .join('\\n') + '\\n'\n );\n }\n\n return { getName, imports };\n}\n"]}