Skip to content

Commit

Permalink
chore: fix cspell-lib rollup test plugin (#4188)
Browse files Browse the repository at this point in the history
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
Jason3S and dependabot[bot] committed Feb 16, 2023
1 parent 024125c commit b720bf5
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 43 deletions.
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test-packages/cspell-lib/test-cspell-lib-rollup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"scripts": {
"clean": "shx rm -rf dist .tsbuildinfo",
"build": "pnpm build:plugin && pnpm build:rollup",
"build:plugin": "tsc -p tsconfig.plugin.json",
"build:plugin": "tsc -p plugin/tsconfig.json",
"build:rollup": "rollup -c rollup.config.mjs",
"clean-build": "pnpm run clean && pnpm run build",
"compile": "tsc -p .",
Expand All @@ -27,7 +27,7 @@
},
"devDependencies": {
"cspell-lib": "workspace:*",
"magic-string": "^0.27.0"
"magic-string": "^0.29.0"
},
"main": "dist/index.cjs",
"module": "dist/index.mjs",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { injectDirname } from './dist/rollup-plugin-inject-dirname.mjs';
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function injectSnippet(
code,
pos,
codeSnippet
): {
code: string;
map: { mappings: string };
};
17 changes: 17 additions & 0 deletions test-packages/cspell-lib/test-cspell-lib-rollup/plugin/inject.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// eslint-disable-next-line node/no-unpublished-import
import MagicString from 'magic-string';

export function injectSnippet(code, pos, codeSnippet) {
const ms = new MagicString(code);

const newCode = ms.appendLeft(pos, codeSnippet);

const map = newCode.generateMap();

return {
code: newCode.toString(),
map: {
mappings: map.mappings,
},
};
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable node/no-extraneous-import */
import type { Plugin } from 'rollup';

import MagicString from './magic-string.mjs';
import { injectSnippet } from '../inject.mjs';

const codeSnippet = `\
import {fileURLToPath as $$inject_fileURLToPath} from 'url';
Expand All @@ -10,7 +10,7 @@ const __filename = $$inject_fileURLToPath(import.meta.url);
const __dirname = $$inject_dirname(__filename);
`;

export default function injectDirname(): Plugin {
export function injectDirname(): Plugin {
return {
name: 'inject-dirname',
renderChunk: {
Expand All @@ -21,8 +21,15 @@ export default function injectDirname(): Plugin {

const ms = inject(code);
if (!ms) return;
const sm = ms.generateMap();
return { code: ms.toString(), map: sm };
return {
code: ms.code,
map: {
...ms.map,
names: [],
sources: [],
version: 1,
},
};
},
},
writeBundle: {
Expand All @@ -36,13 +43,11 @@ export default function injectDirname(): Plugin {
};
}

function inject(code: string): MagicString | undefined {
function inject(code: string) {
const pos = calcPosition(code);
if (pos === undefined) return undefined;

const ms = new MagicString(code);

return ms.appendLeft(pos, codeSnippet);
return injectSnippet(code, pos, codeSnippet);
}

function calcPosition(code: string): number | undefined {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../../../tsconfig.esm.json",
"compilerOptions": {
"outDir": "dist",
"allowJs": true
},
"include": [
"src"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import rollupPluginTypescript from '@rollup/plugin-typescript';
import rollupPluginJson from '@rollup/plugin-json';
import rollupPluginCommonjs from '@rollup/plugin-commonjs';
import { readFileSync } from 'fs';
import injectDirname from './dist/plugin/rollup-plugin-inject-dirname.mjs';
import { injectDirname } from './plugin/index.mjs';

const pkg = JSON.parse(readFileSync('./package.json', 'utf-8'));

Expand Down Expand Up @@ -67,7 +67,7 @@ const configs = [
...common,
output: [
{ ...common.output, file: pkg.main, format: 'cjs' },
{ ...common.output, file: pkg.module, format: 'es', globals: { MyGlobalValueJason: '$$filename_Jason$$' } },
{ ...common.output, file: pkg.module, format: 'es' },
],
plugins: getPlugins(),
},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../../tsconfig.esm.json",
"compilerOptions": {
"outDir": "dist"
"outDir": "dist",
"allowJs": true
},
"include": [
"src"
Expand Down

This file was deleted.

0 comments on commit b720bf5

Please sign in to comment.