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 2 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
58 changes: 34 additions & 24 deletions packages/replace/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,49 @@ export default function replace(options = {}) {
? new RegExp(`${escape(delimiters[0])}(${keys.join('|')})${escape(delimiters[1])}`, 'g')
: new RegExp(`\\b(${keys.join('|')})\\b`, 'g');

return {
name: 'replace',
function executeReplacement(code, id) {
const magicString = new MagicString(code);

transform(code, id) {
if (!keys.length) return null;
if (!filter(id)) return null;
let hasReplacements = false;
let match;
let start;
let end;
let replacement;

const magicString = new MagicString(code);
// eslint-disable-next-line no-cond-assign
while ((match = pattern.exec(code))) {
darthtrevino marked this conversation as resolved.
Show resolved Hide resolved
hasReplacements = true;

let hasReplacements = false;
let match;
let start;
let end;
let replacement;
start = match.index;
end = start + match[0].length;
replacement = String(functionValues[match[1]](id));

// eslint-disable-next-line no-cond-assign
while ((match = pattern.exec(code))) {
hasReplacements = true;
magicString.overwrite(start, end, replacement);
}

start = match.index;
end = start + match[0].length;
replacement = String(functionValues[match[1]](id));
if (!hasReplacements) return null;

magicString.overwrite(start, end, replacement);
}
const result = { code: magicString.toString() };
if (options.sourceMap !== false && options.sourcemap !== false)
result.map = magicString.generateMap({ hires: true });

if (!hasReplacements) return null;
return result;
}

const result = { code: magicString.toString() };
if (options.sourceMap !== false && options.sourcemap !== false)
result.map = magicString.generateMap({ hires: true });
return {
name: 'replace',

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

return result;
transform(code, id) {
if (!keys.length) return null;
if (!filter(id)) return null;
return executeReplacement(code, id);
}
};
}
34 changes: 34 additions & 0 deletions packages/replace/test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,37 @@ test('does not generate sourcemaps if disabled', async (t) => {
await bundle.generate({ format: 'es', sourcemap: true });
t.truthy(warned);
});

test('can be configured with output plugins', async (t) => {
const bundle = await rollup({
input: 'main.js',
plugins: [
{
resolveId(id) {
return id;
},
load(importee) {
if (importee === 'main.js') {
return 'log("environment", process.env.NODE_ENV);';
}
}
}
]
});

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

t.is(code.trim(), 'log("environment", "production");');
t.truthy(map);
});