Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Replacement plugin to operate as an output plugin #55

Merged
merged 7 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions packages/replace/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,57 @@ export default function replace(options = {}) {
const keys = Object.keys(functionValues)
.sort(longest)
.map(escape);

const pattern = delimiters
? new RegExp(`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}`, 'g')
: new RegExp(`\\b(${keys.join('|')})\\b`, 'g');

return {
name: 'replace',

transform(code, id) {
renderChunk(code, chunk) {
const id = chunk.fileName;
if (!keys.length) return null;
if (!filter(id)) return null;
return executeReplacement(code, id);
},

const magicString = new MagicString(code);

let hasReplacements = false;
let match;
let start;
let end;
let replacement;

// eslint-disable-next-line no-cond-assign
while ((match = pattern.exec(code))) {
hasReplacements = true;
transform(code, id) {
if (!keys.length) return null;
if (!filter(id)) return null;
return executeReplacement(code, id);
}
};

start = match.index;
end = start + match[0].length;
replacement = String(functionValues[match[1]](id));
function executeReplacement(code, id) {
const magicString = new MagicString(code);
if (!codeHasReplacements(code, id, magicString)) {
return null;
}

magicString.overwrite(start, end, replacement);
}
const result = { code: magicString.toString() };
if (isSourceMapEnabled()) {
result.map = magicString.generateMap({ hires: true });
}
return result;
}

if (!hasReplacements) return null;
function codeHasReplacements(code, id, magicString) {
let result = false;
let match;

const result = { code: magicString.toString() };
if (options.sourceMap !== false && options.sourcemap !== false)
result.map = magicString.generateMap({ hires: true });
// eslint-disable-next-line no-cond-assign
while ((match = pattern.exec(code))) {
result = true;

return result;
const start = match.index;
const end = start + match[0].length;
const replacement = String(functionValues[match[1]](id));
magicString.overwrite(start, end, replacement);
}
};
return result;
}

function isSourceMapEnabled() {
return options.sourceMap !== false && options.sourcemap !== false;
}
}
72 changes: 14 additions & 58 deletions packages/replace/test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

const test = require('ava');
const { rollup } = require('rollup');
const { SourceMapConsumer } = require('source-map');
const { getLocator } = require('locate-character');

const replace = require('../dist/rollup-plugin-replace.cjs.js');

Expand Down Expand Up @@ -33,78 +31,36 @@ test('does not mutate the values map properties', async (t) => {
t.deepEqual(valuesMap, { ANSWER: '42' });
});

test('generates sourcemaps', async (t) => {
test('can be configured with output plugins', async (t) => {
const bundle = await rollup({
input: 'main.js',
onwarn(warning) {
throw new Error(warning.message);
},
plugins: [
replace({ values: { ANSWER: '42' } }),
{
resolveId(id) {
return id;
},
load(importee) {
if (importee === 'main.js') {
return 'import value from "other.js";\nlog(value);';
}
if (importee === 'other.js') {
return 'export default ANSWER;';
return 'log("environment", process.env.NODE_ENV);';
}
}
}
]
});

const { code, map } = getOutputFromGenerated(
await bundle.generate({ format: 'es', sourcemap: true })
await bundle.generate({
format: 'es',
sourcemap: true,
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
delimiters: ['', '']
})
]
})
);

await SourceMapConsumer.with(map, null, async (smc) => {
const locator = getLocator(code, { offsetLine: 1 });

let generatedLoc = locator('42');
let loc = smc.originalPositionFor(generatedLoc);
t.snapshot(generatedLoc);
t.snapshot(loc);

generatedLoc = locator('log');
loc = smc.originalPositionFor(generatedLoc);
t.snapshot(generatedLoc);
t.snapshot(loc);
});
});

test('does not generate sourcemaps if disabled', async (t) => {
let warned = false;

const bundle = await rollup({
input: 'main.js',
onwarn(warning) {
t.snapshot(warning.message);
warned = true;
},
plugins: [
replace({ values: { ANSWER: '42' }, sourcemap: false }),
{
resolveId(id) {
return id;
},

load(importee) {
if (importee === 'main.js') {
return 'import value from "other.js";\nlog(value);';
}
if (importee === 'other.js') {
return 'export default ANSWER;';
}
}
}
]
});

t.truthy(!warned);
await bundle.generate({ format: 'es', sourcemap: true });
t.truthy(warned);
t.is(code.trim(), 'log("environment", "production");');
t.truthy(map);
});
185 changes: 185 additions & 0 deletions packages/replace/test/snapshots/sourcemaps.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# Snapshot report for `test/sourcemaps.js`

The actual snapshot is saved in `sourcemaps.js.snap`.

Generated by [AVA](https://ava.li).

## generates sourcemaps when sourcemap is used as an output argument

> Snapshot 1

{
character: 12,
column: 12,
line: 1,
}

> Snapshot 2

{
column: 15,
line: 1,
name: null,
source: 'other.js',
}

> Snapshot 3

{
character: 17,
column: 0,
line: 3,
}

> Snapshot 4

{
column: 0,
line: 2,
name: null,
source: 'main.js',
}

## generates sourcemaps if enabled in plugin

> Snapshot 1

{
character: 12,
column: 12,
line: 1,
}

> Snapshot 2

{
column: 15,
line: 1,
name: null,
source: 'other.js',
}

> Snapshot 3

{
character: 17,
column: 0,
line: 3,
}

> Snapshot 4

{
column: 0,
line: 2,
name: null,
source: 'main.js',
}

## generates sourcemaps if enabled in plugin (camelcase)

> Snapshot 1

{
character: 12,
column: 12,
line: 1,
}

> Snapshot 2

{
column: 15,
line: 1,
name: null,
source: 'other.js',
}

> Snapshot 3

{
character: 17,
column: 0,
line: 3,
}

> Snapshot 4

{
column: 0,
line: 2,
name: null,
source: 'main.js',
}

## generates sourcemaps by dfault

> Snapshot 1

{
character: 12,
column: 12,
line: 1,
}

> Snapshot 2

{
column: 15,
line: 1,
name: null,
source: 'other.js',
}

> Snapshot 3

{
character: 17,
column: 0,
line: 3,
}

> Snapshot 4

{
column: 0,
line: 2,
name: null,
source: 'main.js',
}

## generates sourcemaps by default

> Snapshot 1

{
character: 12,
column: 12,
line: 1,
}

> Snapshot 2

{
column: 15,
line: 1,
name: null,
source: 'other.js',
}

> Snapshot 3

{
character: 17,
column: 0,
line: 3,
}

> Snapshot 4

{
column: 0,
line: 2,
name: null,
source: 'main.js',
}
Binary file added packages/replace/test/snapshots/sourcemaps.js.snap
Binary file not shown.
Loading